@rangefoundry/importers 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +20 -1
- package/dist/cells.d.ts +31 -0
- package/dist/cells.js +22 -0
- package/dist/cfb.d.ts +2 -0
- package/dist/cfb.js +196 -0
- package/dist/csrload.d.ts +12 -0
- package/dist/csrload.js +153 -0
- package/dist/diagnostics.d.ts +42 -0
- package/dist/diagnostics.js +29 -0
- package/dist/generic.d.ts +44 -0
- package/dist/generic.js +272 -0
- package/dist/grtload.d.ts +32 -0
- package/dist/grtload.js +264 -0
- package/dist/index.d.ts +85 -0
- package/dist/index.js +126 -0
- package/dist/limits.d.ts +7 -0
- package/dist/limits.js +12 -0
- package/dist/pressure-history.d.ts +61 -0
- package/dist/pressure-history.js +158 -0
- package/dist/rangefoundry-json.d.ts +17 -0
- package/dist/rangefoundry-json.js +176 -0
- package/dist/record-fields.d.ts +35 -0
- package/dist/record-fields.js +190 -0
- package/dist/record-ids.d.ts +2 -0
- package/dist/record-ids.js +28 -0
- package/dist/records.d.ts +91 -0
- package/dist/records.js +433 -0
- package/dist/result.d.ts +65 -0
- package/dist/result.js +90 -0
- package/dist/shot-rows.d.ts +11 -0
- package/dist/shot-rows.js +39 -0
- package/dist/shotview.d.ts +16 -0
- package/dist/shotview.js +131 -0
- package/dist/statistics.d.ts +12 -0
- package/dist/statistics.js +48 -0
- package/dist/tables.d.ts +41 -0
- package/dist/tables.js +277 -0
- package/dist/template.d.ts +13 -0
- package/dist/template.js +37 -0
- package/dist/text.d.ts +28 -0
- package/dist/text.js +150 -0
- package/dist/units.d.ts +12 -0
- package/dist/units.js +62 -0
- package/dist/workbook.d.ts +12 -0
- package/dist/workbook.js +264 -0
- package/dist/worksheet-strings.d.ts +36 -0
- package/dist/worksheet-strings.js +50 -0
- package/dist/xls.d.ts +3 -0
- package/dist/xls.js +486 -0
- package/dist/xml.d.ts +26 -0
- package/dist/xml.js +261 -0
- package/dist/zip.d.ts +27 -0
- package/dist/zip.js +183 -0
- package/package.json +58 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { refuse } from './diagnostics.js';
|
|
2
|
+
import { parseDelimited } from './generic.js';
|
|
3
|
+
import { isRangeFoundryJson, parseRangeFoundryJson } from './rangefoundry-json.js';
|
|
4
|
+
import { isShotViewHeader, parseShotView, parseShotViewWorkbook, shotViewHeaderIndex, } from './shotview.js';
|
|
5
|
+
import { isRangeFoundryCsv, parseRangeFoundryCsv } from './template.js';
|
|
6
|
+
import { splitLines } from './text.js';
|
|
7
|
+
export { readCsrLoad } from './csrload.js';
|
|
8
|
+
export { ReaderError } from './diagnostics.js';
|
|
9
|
+
export { parseDelimited } from './generic.js';
|
|
10
|
+
export { readGrtLoad } from './grtload.js';
|
|
11
|
+
export { parsePressureHistory } from './pressure-history.js';
|
|
12
|
+
export { isRangeFoundryJson, parseRangeFoundryJson, sessionJsonFormat, stringifyRangeFoundryJson, } from './rangefoundry-json.js';
|
|
13
|
+
export { fieldSpecs, tableKinds } from './record-fields.js';
|
|
14
|
+
export { idOfKey } from './record-ids.js';
|
|
15
|
+
export { autoMap, buildRecords, suggestKind, tierFloors } from './records.js';
|
|
16
|
+
export { parseShotView, parseShotViewWorkbook } from './shotview.js';
|
|
17
|
+
export { readTable, tableOfRows } from './tables.js';
|
|
18
|
+
export { parseRangeFoundryCsv } from './template.js';
|
|
19
|
+
export { decodeText, formatMinutes } from './text.js';
|
|
20
|
+
export { parseVelocityUnit } from './units.js';
|
|
21
|
+
export { isWorkbook, readWorkbook } from './workbook.js';
|
|
22
|
+
/** Options of the format parsers that read none of `date`, `device` and the column mapping. */
|
|
23
|
+
function withoutFormatOptions(options) {
|
|
24
|
+
const { date, device, columns, delimiter, skipRows, ...rest } = options;
|
|
25
|
+
return rest;
|
|
26
|
+
}
|
|
27
|
+
/** Workbook detection by its cells alone (never by file or worksheet names). */
|
|
28
|
+
const anyRow = (book, test) => book.worksheets.some((s) => s.rows.some(test));
|
|
29
|
+
// the literal list, with each id and what it reads in the type: `detectFormat` and `detectWorkbookFormat` fail to compile on an id that `ChronoFormatId` or `WorkbookFormatId` lacks, formats.test.ts on one they have and the list does not
|
|
30
|
+
const formats = [
|
|
31
|
+
{
|
|
32
|
+
id: 'rangefoundry-json',
|
|
33
|
+
name: 'RangeFoundry JSON',
|
|
34
|
+
reads: 'text',
|
|
35
|
+
/** True when the text is JSON with `format` `rangefoundry-session`. */
|
|
36
|
+
detect: isRangeFoundryJson,
|
|
37
|
+
/** Parses a RangeFoundry session JSON text; velocities are m/s. */
|
|
38
|
+
parse: (text, options) => parseRangeFoundryJson(text, withoutFormatOptions(options)),
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
id: 'shotview-csv',
|
|
42
|
+
name: 'Garmin Xero ShotView CSV',
|
|
43
|
+
reads: 'text',
|
|
44
|
+
/** True when a line is a ShotView header row. */
|
|
45
|
+
detect: (text) => shotViewHeaderIndex(splitLines(text)) >= 0,
|
|
46
|
+
/** Reads a ShotView CSV text into one string; velocity unit from the header brackets, date from the footer. */
|
|
47
|
+
parse: (text, options) => parseShotView(text, withoutFormatOptions(options)),
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
id: 'shotview-xlsx',
|
|
51
|
+
name: 'Garmin Xero ShotView workbook',
|
|
52
|
+
reads: 'workbook',
|
|
53
|
+
/** True when a worksheet row is a ShotView header row. */
|
|
54
|
+
detect: (book) => anyRow(book, isShotViewHeader),
|
|
55
|
+
/** Reads a ShotView workbook into one string per worksheet; `date` (`YYYY-MM-DD`) keeps one day. */
|
|
56
|
+
parse: (book, options) => parseShotViewWorkbook(book, { ...withoutFormatOptions(options), date: options.date }),
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
id: 'rangefoundry-csv',
|
|
60
|
+
name: 'RangeFoundry CSV template',
|
|
61
|
+
reads: 'text',
|
|
62
|
+
/** True when the first row holds known column names with `velocity` among them. */
|
|
63
|
+
detect: isRangeFoundryCsv,
|
|
64
|
+
/** Parses a RangeFoundry CSV template; `device` sets the device. */
|
|
65
|
+
parse: (text, options) => parseRangeFoundryCsv(text, { ...withoutFormatOptions(options), device: options.device }),
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
id: 'delimited',
|
|
69
|
+
name: 'delimited text with a column mapping',
|
|
70
|
+
reads: 'text',
|
|
71
|
+
/** Parses delimited text with the column mapping in `columns`; throws without it. */
|
|
72
|
+
parse: (text, options) => {
|
|
73
|
+
const { columns, delimiter, skipRows, device } = options;
|
|
74
|
+
if (!columns)
|
|
75
|
+
refuse('needsColumnMapping');
|
|
76
|
+
return parseDelimited(text, {
|
|
77
|
+
...withoutFormatOptions(options),
|
|
78
|
+
columns,
|
|
79
|
+
delimiter,
|
|
80
|
+
skipRows,
|
|
81
|
+
device,
|
|
82
|
+
});
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
];
|
|
86
|
+
/**
|
|
87
|
+
* Every chronograph format, listed to users in this order by `name`; detection walks the list.
|
|
88
|
+
* `delimited` has no `detect`: it needs a column mapping.
|
|
89
|
+
*/
|
|
90
|
+
export const chronoFormats = formats;
|
|
91
|
+
/**
|
|
92
|
+
* The format of a chronograph export, told by its shape, or null when none matches.
|
|
93
|
+
* `delimited` is never returned: it needs a column mapping.
|
|
94
|
+
*/
|
|
95
|
+
export function detectFormat(text) {
|
|
96
|
+
for (const f of formats)
|
|
97
|
+
if (f.reads === 'text' && 'detect' in f && f.detect(text))
|
|
98
|
+
return f.id;
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The format of a workbook read by `readWorkbook`, told by its cells alone (never by file or
|
|
103
|
+
* worksheet names), or null when none matches.
|
|
104
|
+
*/
|
|
105
|
+
export function detectWorkbookFormat(book) {
|
|
106
|
+
for (const f of formats)
|
|
107
|
+
if (f.reads === 'workbook' && f.detect(book))
|
|
108
|
+
return f.id;
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
export function parseChrono(input, options = {}, format) {
|
|
112
|
+
const text = typeof input === 'string';
|
|
113
|
+
const id = format ?? (text ? detectFormat(input) : detectWorkbookFormat(input));
|
|
114
|
+
const f = chronoFormats.find((c) => c.id === id);
|
|
115
|
+
if (!f)
|
|
116
|
+
return null;
|
|
117
|
+
if (f.reads === 'text') {
|
|
118
|
+
if (!text)
|
|
119
|
+
refuse('readsText', { format: f.id });
|
|
120
|
+
return f.parse(input, options);
|
|
121
|
+
}
|
|
122
|
+
if (text)
|
|
123
|
+
refuse('readsWorkbooks', { format: f.id });
|
|
124
|
+
return f.parse(input, options);
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=index.js.map
|
package/dist/limits.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const maxColumn = 256;
|
|
2
|
+
export declare const maxCells = 1000000;
|
|
3
|
+
export declare const maxCellCharacters: number;
|
|
4
|
+
export declare const maxElements: number;
|
|
5
|
+
export declare const maxXmlCharacters: number;
|
|
6
|
+
export declare const maxPartSize: number;
|
|
7
|
+
export declare const maxTotalSize: number;
|
package/dist/limits.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Size bounds of the workbook readers (columns and cells also of delimited text and JSON lists), all assumed, not cited.
|
|
2
|
+
// the exports use 9 columns, and a 10000-shot worksheet holds 90000 cells of about 10 characters
|
|
3
|
+
export const maxColumn = 256;
|
|
4
|
+
export const maxCells = 1_000_000;
|
|
5
|
+
export const maxCellCharacters = 32 * 1024 * 1024;
|
|
6
|
+
// headroom for the XML of those cells: 4 elements per cell, twice the `c` and `v` of a value cell, and twice the cell text in characters of text and attribute markup
|
|
7
|
+
export const maxElements = 4 * maxCells;
|
|
8
|
+
export const maxXmlCharacters = 2 * maxCellCharacters;
|
|
9
|
+
// the largest needed part of the real exports is about 20 KB; the per-part limit also bounds the Workbook stream of a legacy XLS file
|
|
10
|
+
export const maxPartSize = 8 * 1024 * 1024;
|
|
11
|
+
export const maxTotalSize = 32 * 1024 * 1024;
|
|
12
|
+
//# sourceMappingURL=limits.js.map
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type PressureDiagnostic } from './diagnostics.ts';
|
|
2
|
+
/** A pressure unit the trace importer recognizes (any case in the file). */
|
|
3
|
+
export type PressureUnit = 'bar' | 'mbar' | 'MPa' | 'kPa' | 'Pa' | 'psi' | 'kpsi';
|
|
4
|
+
/** A time unit the trace importer recognizes (`µs` reads as `us`). */
|
|
5
|
+
export type TimeUnit = 's' | 'ms' | 'us';
|
|
6
|
+
/** Where the sensor measured: at the breech, the space mean in the chamber, or at the projectile base. */
|
|
7
|
+
export type SensorPosition = 'breech' | 'mean' | 'base';
|
|
8
|
+
/** Options of `parsePressureHistory`. */
|
|
9
|
+
export interface PressureHistoryOptions {
|
|
10
|
+
/** Sensor position, always asked from the user: the file cannot tell it. */
|
|
11
|
+
sensor: SensorPosition;
|
|
12
|
+
/** Pressure unit when the file names none in its header. */
|
|
13
|
+
pressureUnit?: PressureUnit | undefined;
|
|
14
|
+
/** Time unit when the file names none in its header. */
|
|
15
|
+
timeUnit?: TimeUnit | undefined;
|
|
16
|
+
/** Column of the times and of the pressures, counted from 1; default 1 and 2. */
|
|
17
|
+
columns?: {
|
|
18
|
+
time: number;
|
|
19
|
+
pressure: number;
|
|
20
|
+
} | undefined;
|
|
21
|
+
/** Cell separator; detected when absent. */
|
|
22
|
+
delimiter?: ',' | ';' | '\t' | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* One-pole low-pass of the pressure, run forwards and backwards (no delay): -3 dB at about 0.64
|
|
25
|
+
* times `cutoff`, Hz. It smooths the peak when the cutoff is not well above the trace's content.
|
|
26
|
+
*/
|
|
27
|
+
lowPass?: {
|
|
28
|
+
cutoff: number;
|
|
29
|
+
} | undefined;
|
|
30
|
+
}
|
|
31
|
+
/** A trace as the importer returns it: times in s, pressures in bar (the stored unit), the sensor. */
|
|
32
|
+
export interface PressureHistoryImport {
|
|
33
|
+
sensor: SensorPosition;
|
|
34
|
+
/** Sample times, s, strictly increasing. */
|
|
35
|
+
time: number[];
|
|
36
|
+
/** Pressures, bar (the fit of core takes Pa: multiply by 1e5). */
|
|
37
|
+
pressureBar: number[];
|
|
38
|
+
/** The units read and where each came from: the file's header or the caller. */
|
|
39
|
+
units: {
|
|
40
|
+
time: {
|
|
41
|
+
unit: TimeUnit;
|
|
42
|
+
source: 'file' | 'caller';
|
|
43
|
+
};
|
|
44
|
+
pressure: {
|
|
45
|
+
unit: PressureUnit;
|
|
46
|
+
source: 'file' | 'caller';
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
/** The low-pass applied, or absent. */
|
|
50
|
+
lowPass?: {
|
|
51
|
+
cutoff: number;
|
|
52
|
+
};
|
|
53
|
+
warnings: PressureDiagnostic[];
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Reads a pressure-time trace from delimited text (the caller turns bytes into text): one sample per
|
|
57
|
+
* row, a header row optional. Units come from the header (`time (ms)`, `pressure [MPa]`) or the options; a unit that is neither throws with a message naming the column, and one
|
|
58
|
+
* the file names differently from the option throws. Times must increase. Pressures are returned
|
|
59
|
+
* in bar and times in s; the sensor position is the caller's. A simulation input, never load data.
|
|
60
|
+
*/
|
|
61
|
+
export declare function parsePressureHistory(text: string, options: PressureHistoryOptions): PressureHistoryImport;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// A pressure-time trace of the user's own measurement: time and pressure columns, the units and the sensor position named by the user or the file, an optional zero-phase low-pass.
|
|
2
|
+
import { refuseTrace } from './diagnostics.js';
|
|
3
|
+
import { detectDelimiter, splitCells } from './generic.js';
|
|
4
|
+
import { psi } from './record-fields.js';
|
|
5
|
+
import { bracketed, readDecimal, splitLines } from './text.js';
|
|
6
|
+
/** Factor of each pressure unit to the stored unit, bar. */
|
|
7
|
+
const pressureUnits = {
|
|
8
|
+
bar: 1,
|
|
9
|
+
mbar: 1e-3,
|
|
10
|
+
MPa: 10,
|
|
11
|
+
kPa: 0.01,
|
|
12
|
+
Pa: 1e-5,
|
|
13
|
+
psi,
|
|
14
|
+
kpsi: psi * 1000,
|
|
15
|
+
};
|
|
16
|
+
/** Factor of each time unit to s. */
|
|
17
|
+
const timeUnits = { s: 1, ms: 1e-3, us: 1e-6 };
|
|
18
|
+
const sensors = ['breech', 'mean', 'base'];
|
|
19
|
+
/** Fewest samples of a trace. */
|
|
20
|
+
const minSamples = 3;
|
|
21
|
+
/** The text in `(...)` or `[...]` of a header cell, else undefined. */
|
|
22
|
+
function bracketedUnit(cell) {
|
|
23
|
+
return bracketed(cell) ?? squareBracketed(cell)?.trim();
|
|
24
|
+
}
|
|
25
|
+
/** The text between the first `[` and the next `]` that encloses some, else undefined; linear time. */
|
|
26
|
+
function squareBracketed(cell) {
|
|
27
|
+
for (let a = cell.indexOf('['); a >= 0; a = cell.indexOf('[', a + 1)) {
|
|
28
|
+
const b = cell.indexOf(']', a + 1);
|
|
29
|
+
if (b < 0)
|
|
30
|
+
return undefined;
|
|
31
|
+
if (b > a + 1)
|
|
32
|
+
return cell.slice(a + 1, b);
|
|
33
|
+
}
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
/** The unit text of a header cell: in brackets, else the last word when it is a unit, else none. */
|
|
37
|
+
function namedUnit(table, cell) {
|
|
38
|
+
const word = cell.trim().split(/\s+/).at(-1) ?? '';
|
|
39
|
+
return (bracketedUnit(cell) ?? (lookup(table, word) ? word : '')).trim();
|
|
40
|
+
}
|
|
41
|
+
/** The exact spelling, else any case when the text is all lower or all upper case (`mPa` and `Mbar` name no unit of the table, they are not `MPa`, `mbar`). */
|
|
42
|
+
function lookup(table, text) {
|
|
43
|
+
const key = text.replace('µ', 'u').replace('μ', 'u');
|
|
44
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the keys of the table are its units
|
|
45
|
+
const units = Object.keys(table);
|
|
46
|
+
const exact = units.find((k) => k === key);
|
|
47
|
+
const loose = key === key.toLowerCase() || key === key.toUpperCase();
|
|
48
|
+
return exact ?? (loose ? units.find((k) => k.toLowerCase() === key.toLowerCase()) : undefined);
|
|
49
|
+
}
|
|
50
|
+
/** One pass of the one-pole low-pass over the samples, in their order. */
|
|
51
|
+
function pass(samples, rc) {
|
|
52
|
+
const out = [];
|
|
53
|
+
for (const s of samples) {
|
|
54
|
+
const before = out.at(-1);
|
|
55
|
+
if (!before) {
|
|
56
|
+
out.push(s);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const dt = s.time - before.time;
|
|
60
|
+
out.push({ time: s.time, value: before.value + (dt / (rc + dt)) * (s.value - before.value) });
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
/** Zero-phase one-pole low-pass: forwards, then backwards on the reversed series. */
|
|
65
|
+
function zeroPhase(samples, cutoff) {
|
|
66
|
+
const rc = 1 / (2 * Math.PI * cutoff);
|
|
67
|
+
const forward = pass(samples, rc).map((s) => ({ time: -s.time, value: s.value }));
|
|
68
|
+
return pass(forward.toReversed(), rc)
|
|
69
|
+
.toReversed()
|
|
70
|
+
.map((s) => s.value);
|
|
71
|
+
}
|
|
72
|
+
/** The time and pressure column numbers and the low-pass cutoff of the options, checked. */
|
|
73
|
+
function checkedOptions(options) {
|
|
74
|
+
if (!sensors.includes(options.sensor))
|
|
75
|
+
refuseTrace('sensorMissing', { sensors });
|
|
76
|
+
const { time: tc = 1, pressure: pc = 2 } = options.columns ?? {};
|
|
77
|
+
if (![tc, pc].every((c) => Number.isInteger(c) && c >= 1) || tc === pc)
|
|
78
|
+
refuseTrace('columnsNotDistinct');
|
|
79
|
+
return { tc, pc, cutoff: checkedCutoff(options.lowPass?.cutoff) };
|
|
80
|
+
}
|
|
81
|
+
/** The low-pass cutoff, refused unless it is a frequency above 0 Hz. */
|
|
82
|
+
function checkedCutoff(cutoff) {
|
|
83
|
+
if (cutoff !== undefined && !(Number.isFinite(cutoff) && cutoff > 0))
|
|
84
|
+
refuseTrace('cutoffNotPositive');
|
|
85
|
+
return cutoff;
|
|
86
|
+
}
|
|
87
|
+
/** Removes and returns the first row when its time cell is not a number: the header row. */
|
|
88
|
+
function takeHeader(rows, tc) {
|
|
89
|
+
const [top] = rows;
|
|
90
|
+
return top && Number.isNaN(readDecimal(top[tc - 1] ?? '')) ? rows.shift() : undefined;
|
|
91
|
+
}
|
|
92
|
+
/** The one its header cell names, else the caller's; refused when the two differ or neither names one, a warning when the header names an unknown unit. */
|
|
93
|
+
function columnUnit(column, table, caller, warnings) {
|
|
94
|
+
const named = namedUnit(table, column.header);
|
|
95
|
+
const file = lookup(table, named);
|
|
96
|
+
if (file && caller && file !== caller)
|
|
97
|
+
refuseTrace('columnUnitDiffersFromCaller', { column: column.name, text: named ?? '', caller });
|
|
98
|
+
const unit = file ?? caller;
|
|
99
|
+
if (!unit)
|
|
100
|
+
refuseTrace('columnUnitMissing', { column: column.name });
|
|
101
|
+
if (!file && named) {
|
|
102
|
+
warnings.push({
|
|
103
|
+
code: 'columnUnitNamedByCaller',
|
|
104
|
+
params: { column: column.name, text: named, unit },
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return { unit, source: file ? 'file' : 'caller' };
|
|
108
|
+
}
|
|
109
|
+
/** The samples of the rows in s and bar, with the factors of the time and pressure units; refused for a cell that is not a finite number and for times that do not increase; `first` is the line number of the first row. */
|
|
110
|
+
function readSamples(rows, { tc, pc, first }, { tf, pf }) {
|
|
111
|
+
const samples = [];
|
|
112
|
+
rows.forEach((cells, i) => {
|
|
113
|
+
const [t, p] = [readDecimal(cells[tc - 1] ?? ''), readDecimal(cells[pc - 1] ?? '')];
|
|
114
|
+
const line = i + first;
|
|
115
|
+
if (Number.isNaN(t) || Number.isNaN(p))
|
|
116
|
+
refuseTrace('readingNotNumber', { line });
|
|
117
|
+
const [seconds, bar] = [t * tf, p * pf];
|
|
118
|
+
if (!Number.isFinite(seconds) || !Number.isFinite(bar))
|
|
119
|
+
refuseTrace('readingNotFinite', { line });
|
|
120
|
+
const before = samples.at(-1);
|
|
121
|
+
if (before && seconds <= before.time)
|
|
122
|
+
refuseTrace('timesNotIncreasing', { line });
|
|
123
|
+
samples.push({ time: seconds, value: bar });
|
|
124
|
+
});
|
|
125
|
+
return samples;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Reads a pressure-time trace from delimited text (the caller turns bytes into text): one sample per
|
|
129
|
+
* row, a header row optional. Units come from the header (`time (ms)`, `pressure [MPa]`) or the options; a unit that is neither throws with a message naming the column, and one
|
|
130
|
+
* the file names differently from the option throws. Times must increase. Pressures are returned
|
|
131
|
+
* in bar and times in s; the sensor position is the caller's. A simulation input, never load data.
|
|
132
|
+
*/
|
|
133
|
+
export function parsePressureHistory(text, options) {
|
|
134
|
+
const { tc, pc, cutoff } = checkedOptions(options);
|
|
135
|
+
const lines = splitLines(text).filter((l) => l.trim());
|
|
136
|
+
const delimiter = options.delimiter ?? detectDelimiter(lines);
|
|
137
|
+
const rows = lines.map((l) => splitCells(l, delimiter));
|
|
138
|
+
const header = takeHeader(rows, tc);
|
|
139
|
+
const headerCell = (c) => header?.[c - 1] ?? '';
|
|
140
|
+
const warnings = [];
|
|
141
|
+
const timeUnit = columnUnit({ name: 'time', header: headerCell(tc) }, timeUnits, options.timeUnit, warnings);
|
|
142
|
+
const pressureUnit = columnUnit({ name: 'pressure', header: headerCell(pc) }, pressureUnits, options.pressureUnit, warnings);
|
|
143
|
+
const factors = { tf: timeUnits[timeUnit.unit], pf: pressureUnits[pressureUnit.unit] };
|
|
144
|
+
const samples = readSamples(rows, { tc, pc, first: header ? 2 : 1 }, factors);
|
|
145
|
+
if (samples.length < minSamples)
|
|
146
|
+
refuseTrace('tooFewSamples', { min: minSamples });
|
|
147
|
+
const time = samples.map((s) => s.time);
|
|
148
|
+
const pressure = samples.map((s) => s.value);
|
|
149
|
+
return {
|
|
150
|
+
sensor: options.sensor,
|
|
151
|
+
time,
|
|
152
|
+
pressureBar: cutoff === undefined ? pressure : zeroPhase(samples, cutoff),
|
|
153
|
+
units: { time: timeUnit, pressure: pressureUnit },
|
|
154
|
+
...(cutoff === undefined ? {} : { lowPass: { cutoff } }),
|
|
155
|
+
warnings,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=pressure-history.js.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type ChronoImport, type ChronoParserOptions } from './result.ts';
|
|
2
|
+
/** Value of the `format` member: `rangefoundry-session`. */
|
|
3
|
+
export declare const sessionJsonFormat = "rangefoundry-session";
|
|
4
|
+
/** True when the text is JSON whose `format` is `rangefoundry-session`. */
|
|
5
|
+
export declare function isRangeFoundryJson(text: string): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* A RangeFoundry session JSON file, the format the session summary exports. Velocities are m/s.
|
|
8
|
+
* The muzzle distance is m in the file and mm (the stored unit) in the session.
|
|
9
|
+
*
|
|
10
|
+
* ```
|
|
11
|
+
* { "format": "rangefoundry-session", "version": 1, "date"?, "device"?: { "kind", "manufacturerName"?, "model"? },
|
|
12
|
+
* "muzzleDistance"? (m), "note"?, "strings": [{ "label"?, "shots": [{ "velocity" (m/s), "coldBore"?, "excluded"? }] }] }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseRangeFoundryJson(text: string, options?: ChronoParserOptions): ChronoImport;
|
|
16
|
+
/** The session of an import as RangeFoundry session JSON, with the string labels inside the strings. */
|
|
17
|
+
export declare function stringifyRangeFoundryJson(data: ChronoImport): string;
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { refuse } from './diagnostics.js';
|
|
2
|
+
import { buildChronoImport, } from './result.js';
|
|
3
|
+
import { yearFirstDate } from './text.js';
|
|
4
|
+
/** Value of the `format` member: `rangefoundry-session`. */
|
|
5
|
+
export const sessionJsonFormat = 'rangefoundry-session';
|
|
6
|
+
// every kind of the session schema once: a kind added or dropped there fails the type check here
|
|
7
|
+
const deviceKindSet = {
|
|
8
|
+
'doppler-radar': true,
|
|
9
|
+
optical: true,
|
|
10
|
+
magnetic: true,
|
|
11
|
+
other: true,
|
|
12
|
+
};
|
|
13
|
+
const deviceKinds = Object.keys(deviceKindSet);
|
|
14
|
+
const isObj = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
15
|
+
function parse(text) {
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse(text.replace(/^\uFEFF/, ''));
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** True when the text is JSON whose `format` is `rangefoundry-session`. */
|
|
24
|
+
export function isRangeFoundryJson(text) {
|
|
25
|
+
if (!text
|
|
26
|
+
.replace(/^\uFEFF/, '')
|
|
27
|
+
.trimStart()
|
|
28
|
+
.startsWith('{'))
|
|
29
|
+
return false;
|
|
30
|
+
const root = parse(text);
|
|
31
|
+
return isObj(root) && root['format'] === sessionJsonFormat;
|
|
32
|
+
}
|
|
33
|
+
/** Refuses the member at `path` with `code`. */
|
|
34
|
+
function fail(path, code) {
|
|
35
|
+
refuse(code, { path });
|
|
36
|
+
}
|
|
37
|
+
/** A finite positive number of the object member `key`, or undefined when absent. */
|
|
38
|
+
function positive(o, key, path) {
|
|
39
|
+
const v = o[key];
|
|
40
|
+
if (v === undefined)
|
|
41
|
+
return undefined;
|
|
42
|
+
if (typeof v !== 'number' || !Number.isFinite(v) || v <= 0)
|
|
43
|
+
fail(`${path}.${key}`, 'memberNotPositive');
|
|
44
|
+
return v;
|
|
45
|
+
}
|
|
46
|
+
/** The object member `key` as non-empty text, or undefined when absent; `path` names it in a refusal. */
|
|
47
|
+
function optionalText(o, key, path) {
|
|
48
|
+
const v = o[key];
|
|
49
|
+
if (v === undefined)
|
|
50
|
+
return undefined;
|
|
51
|
+
if (typeof v !== 'string' || v === '')
|
|
52
|
+
fail(path, 'memberNotText');
|
|
53
|
+
return v;
|
|
54
|
+
}
|
|
55
|
+
const isDeviceKind = (x) => typeof x === 'string' && Object.hasOwn(deviceKindSet, x);
|
|
56
|
+
/** The `device` member: kind (default `other`), manufacturer name and model as non-empty text when present. */
|
|
57
|
+
function readDevice(root) {
|
|
58
|
+
const dev = root['device'] === undefined ? {} : root['device'];
|
|
59
|
+
if (!isObj(dev))
|
|
60
|
+
fail('device', 'memberNotObject');
|
|
61
|
+
const kind = dev['kind'] === undefined ? 'other' : dev['kind'];
|
|
62
|
+
if (!isDeviceKind(kind))
|
|
63
|
+
refuse('memberNotDeviceKind', { path: 'device.kind', kinds: deviceKinds });
|
|
64
|
+
return {
|
|
65
|
+
kind,
|
|
66
|
+
manufacturerName: optionalText(dev, 'manufacturerName', 'device.manufacturerName'),
|
|
67
|
+
model: optionalText(dev, 'model', 'device.model'),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/** One shot at the member path `p`: velocity in m/s required, `coldBore` and `excluded` optional. */
|
|
71
|
+
function readShot(shot, p) {
|
|
72
|
+
if (!isObj(shot))
|
|
73
|
+
fail(p, 'memberNotObject');
|
|
74
|
+
const v = positive(shot, 'velocity', p);
|
|
75
|
+
if (v === undefined)
|
|
76
|
+
fail(`${p}.velocity`, 'velocityRequired');
|
|
77
|
+
const out = { velocity: { value: v, raw: `${v} m/s` } };
|
|
78
|
+
if (shot['coldBore'] !== undefined) {
|
|
79
|
+
if (typeof shot['coldBore'] !== 'boolean')
|
|
80
|
+
fail(`${p}.coldBore`, 'memberNotBoolean');
|
|
81
|
+
if (shot['coldBore'])
|
|
82
|
+
out.coldBore = true;
|
|
83
|
+
}
|
|
84
|
+
if (shot['excluded'] !== undefined) {
|
|
85
|
+
const excluded = shot['excluded'];
|
|
86
|
+
if (excluded === true || (typeof excluded === 'string' && excluded !== ''))
|
|
87
|
+
out.excluded = excluded;
|
|
88
|
+
else
|
|
89
|
+
fail(`${p}.excluded`, 'excludedNotText');
|
|
90
|
+
}
|
|
91
|
+
return out;
|
|
92
|
+
}
|
|
93
|
+
/** The `strings` member, at least one string of at least one shot each, and a label per string. */
|
|
94
|
+
function readStrings(root) {
|
|
95
|
+
if (!Array.isArray(root['strings']) || root['strings'].length === 0)
|
|
96
|
+
fail('strings', 'noStrings');
|
|
97
|
+
const labels = [];
|
|
98
|
+
const strings = root['strings'].map((s, i) => {
|
|
99
|
+
const at = `strings[${i}]`;
|
|
100
|
+
if (!isObj(s))
|
|
101
|
+
fail(at, 'memberNotObject');
|
|
102
|
+
if (s['label'] !== undefined && typeof s['label'] !== 'string')
|
|
103
|
+
fail(`${at}.label`, 'memberNotText');
|
|
104
|
+
labels.push(s['label'] || undefined);
|
|
105
|
+
if (!Array.isArray(s['shots']) || s['shots'].length === 0)
|
|
106
|
+
fail(`${at}.shots`, 'noStringShots');
|
|
107
|
+
const shots = s['shots'].map((raw, j) => readShot(raw, `${at}.shots[${j}]`));
|
|
108
|
+
return { shots };
|
|
109
|
+
});
|
|
110
|
+
return { strings, labels };
|
|
111
|
+
}
|
|
112
|
+
/** The `date` member as `YYYY-MM-DD`, or undefined when absent. */
|
|
113
|
+
function readDate(root) {
|
|
114
|
+
if (root['date'] === undefined)
|
|
115
|
+
return undefined;
|
|
116
|
+
const date = typeof root['date'] === 'string' ? yearFirstDate(root['date']) : undefined;
|
|
117
|
+
if (!date)
|
|
118
|
+
fail('date', 'memberNotDate');
|
|
119
|
+
return date;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* A RangeFoundry session JSON file, the format the session summary exports. Velocities are m/s.
|
|
123
|
+
* The muzzle distance is m in the file and mm (the stored unit) in the session.
|
|
124
|
+
*
|
|
125
|
+
* ```
|
|
126
|
+
* { "format": "rangefoundry-session", "version": 1, "date"?, "device"?: { "kind", "manufacturerName"?, "model"? },
|
|
127
|
+
* "muzzleDistance"? (m), "note"?, "strings": [{ "label"?, "shots": [{ "velocity" (m/s), "coldBore"?, "excluded"? }] }] }
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
export function parseRangeFoundryJson(text, options = {}) {
|
|
131
|
+
const root = parse(text);
|
|
132
|
+
if (!isObj(root) || root['format'] !== sessionJsonFormat)
|
|
133
|
+
refuse('notSessionJson', { format: sessionJsonFormat });
|
|
134
|
+
if (root['version'] !== 1)
|
|
135
|
+
refuse('versionUnsupported', { version: String(root['version']) });
|
|
136
|
+
const warnings = [];
|
|
137
|
+
const date = readDate(root);
|
|
138
|
+
const device = readDevice(root);
|
|
139
|
+
const distance = positive(root, 'muzzleDistance', '');
|
|
140
|
+
const note = optionalText(root, 'note', 'note');
|
|
141
|
+
const { strings, labels } = readStrings(root);
|
|
142
|
+
return buildChronoImport('rangefoundry-json', {
|
|
143
|
+
date,
|
|
144
|
+
device,
|
|
145
|
+
...(distance === undefined
|
|
146
|
+
? {}
|
|
147
|
+
: { muzzleDistance: { value: distance * 1000, raw: `${distance} m` } }),
|
|
148
|
+
strings,
|
|
149
|
+
...(note === undefined ? {} : { note }),
|
|
150
|
+
}, { labels, warnings }, options);
|
|
151
|
+
}
|
|
152
|
+
/** The session of an import as RangeFoundry session JSON, with the string labels inside the strings. */
|
|
153
|
+
export function stringifyRangeFoundryJson(data) {
|
|
154
|
+
const { date, device, muzzleDistance, strings, note } = data.session;
|
|
155
|
+
return `${JSON.stringify({
|
|
156
|
+
format: sessionJsonFormat,
|
|
157
|
+
version: 1,
|
|
158
|
+
...(date ? { date } : {}),
|
|
159
|
+
device: {
|
|
160
|
+
kind: device.kind,
|
|
161
|
+
...(device.manufacturerName ? { manufacturerName: device.manufacturerName } : {}),
|
|
162
|
+
...(device.model ? { model: device.model } : {}),
|
|
163
|
+
},
|
|
164
|
+
...(muzzleDistance ? { muzzleDistance: muzzleDistance.value / 1000 } : {}),
|
|
165
|
+
...(note ? { note } : {}),
|
|
166
|
+
strings: strings.map((s, i) => ({
|
|
167
|
+
...(data.stringLabels[i] ? { label: data.stringLabels[i] } : {}),
|
|
168
|
+
shots: s.shots.map((shot) => ({
|
|
169
|
+
velocity: shot.velocity.value,
|
|
170
|
+
...(shot.coldBore ? { coldBore: true } : {}),
|
|
171
|
+
...(shot.excluded ? { excluded: shot.excluded } : {}),
|
|
172
|
+
})),
|
|
173
|
+
})),
|
|
174
|
+
}, null, 2)}\n`;
|
|
175
|
+
}
|
|
176
|
+
//# sourceMappingURL=rangefoundry-json.js.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** A kind of user record a table is read as. */
|
|
2
|
+
export type TableKind = 'bullet' | 'powder' | 'cartridge';
|
|
3
|
+
/** The kinds a table is scored for. */
|
|
4
|
+
export declare const tableKinds: readonly TableKind[];
|
|
5
|
+
/** The plural name of each kind, as the table import page titles its sections. */
|
|
6
|
+
export declare const kindNames: Record<TableKind, string>;
|
|
7
|
+
/** A unit a column can be in: its symbol and the conversion into the stored unit of the field. */
|
|
8
|
+
export interface ColumnUnit {
|
|
9
|
+
/** The symbol, also the id a column map names. */
|
|
10
|
+
symbol: string;
|
|
11
|
+
/** A number in this unit converted into the stored unit. */
|
|
12
|
+
toStored: (x: number) => number;
|
|
13
|
+
/** Read only when the header names it, never guessed from the values. */
|
|
14
|
+
named?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export declare const psi: number;
|
|
17
|
+
/** One mappable field of a kind. `range` is the usual span in the stored unit: outside it is a warning. */
|
|
18
|
+
export interface FieldSpec {
|
|
19
|
+
key: string;
|
|
20
|
+
label: string;
|
|
21
|
+
/** Words a header may use, lower case without accents. */
|
|
22
|
+
aliases: string[];
|
|
23
|
+
/** Share of the kind score: the core fields of a kind add up to 1, the others are a bonus on top. */
|
|
24
|
+
weight: number;
|
|
25
|
+
required?: boolean;
|
|
26
|
+
/** A number field: its units (the first is stored) and usual range; a text field has none. */
|
|
27
|
+
units?: ColumnUnit[];
|
|
28
|
+
range?: [number, number];
|
|
29
|
+
/** The span most values fall in, in the stored unit: it tells a unit apart when several fit the range. */
|
|
30
|
+
typical?: [number, number];
|
|
31
|
+
/** An enumerated text field: spelling to stored value. */
|
|
32
|
+
choices?: Record<string, string>;
|
|
33
|
+
}
|
|
34
|
+
/** The fields of each kind with their header aliases, units, usual ranges and score weights. */
|
|
35
|
+
export declare const fieldSpecs: Record<TableKind, FieldSpec[]>;
|