-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathindex.js
360 lines (298 loc) · 10.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
const parseKLV = require('./code/parseKLV');
const groupDevices = require('./code/groupDevices');
const deviceList = require('./code/deviceList');
const streamList = require('./code/streamList');
const keys = require('./code/data/keys');
const timeKLV = require('./code/timeKLV');
const interpretKLV = require('./code/interpretKLV');
const mergeStream = require('./code/mergeStream');
const groupTimes = require('./code/groupTimes');
const smoothSamples = require('./code/smoothSamples');
const decimalPlaces = require('./code/decimalPlaces')
const processGPS = require('./code/processGPS');
const filterWrongSpeed = require('./code/filterWrongSpeed');
const presetsOpts = require('./code/data/presetsOptions');
const toGpx = require('./code/presets/toGpx');
const toVirb = require('./code/presets/toVirb');
const toKml = require('./code/presets/toKml');
const toGeojson = require('./code/presets/toGeojson');
const toCsv = require('./code/presets/toCsv');
const toMgjson = require('./code/presets/toMgjson');
const mergeInterpretedSources = require('./code/mergeInterpretedSources');
const breathe = require('./code/utils/breathe');
const getOffset = require('./code/utils/getOffset');
const findFirstTimes = require('./code/utils/findFirstTimes');
async function parseOne({ rawData, parsedData }, opts, gpsTimeSrc) {
if (parsedData) return parsedData;
await breathe();
//Parse input
const parsed = await parseKLV(rawData, opts, { gpsTimeSrc });
if (!parsed.DEVC) {
const error = new Error(
'Invalid GPMF data. Root object must contain DEVC key'
);
if (opts.tolerant) {
await breathe();
console.error(error);
return parsed;
} else throw error;
}
return parsed;
}
async function interpretOne({ timing, parsed, opts, timeMeta, gpsTimeSrc }) {
//Group it by device
const grouped = await groupDevices(parsed);
await breathe();
//Correct GPS height and filter out bad GPS data
if (
!opts.ellipsoid ||
opts.geoidHeight ||
opts.GPSPrecision != null ||
opts.GPSFix != null
) {
for (const key in grouped)
grouped[key] = await processGPS(grouped[key], opts, gpsTimeSrc);
}
let interpreted = {};
//Apply scale and matrix transformations
for (const key in grouped) {
await breathe();
interpreted[key] = await interpretKLV(grouped[key], opts);
}
let timed = {};
//Apply timing (gps and mp4) to every sample
for (const key in interpreted) {
await breathe();
timed[key] = await timeKLV(interpreted[key], {
timing,
opts,
timeMeta,
gpsTimeSrc
});
}
//Merge samples in sensor entries
let merged = {};
for (const key in timed) {
await breathe();
merged[key] = await mergeStream(timed[key], opts);
}
if (opts.WrongSpeed != null) {
for (const key in merged) {
if (merged[key].streams.GPS5) {
merged[key].streams.GPS5.samples = filterWrongSpeed(
merged[key].streams.GPS5.samples,
opts.WrongSpeed
);
}
if (merged[key].streams.GPS9) {
merged[key].streams.GPS9.samples = filterWrongSpeed(
merged[key].streams.GPS9.samples,
opts.WrongSpeed
);
}
}
}
return merged;
}
function progress(options, amount) {
if (options.progress) options.progress(amount);
}
async function process(input, opts) {
await breathe();
//Prepare presets
if (presetsOpts[opts.preset]) {
opts = {
...opts,
...presetsOpts.general.mandatory,
...presetsOpts[opts.preset].mandatory
};
//Only pick the non mandatory options when the user did not specify them
for (const key in presetsOpts.general.preferred)
if (opts[key] == null) opts[key] = presetsOpts.general.preferred[key];
for (const key in presetsOpts[opts.preset].preferred)
if (opts[key] == null)
opts[key] = presetsOpts[opts.preset].preferred[key];
}
//Create filter arrays if user didn't
if (opts.device && !Array.isArray(opts.device)) opts.device = [opts.device];
if (opts.stream && !Array.isArray(opts.stream)) opts.stream = [opts.stream];
if (opts.GPSFix == null && opts.GPS5Fix != null) opts.GPSFix = opts.GPS5Fix;
if (opts.GPSPrecision == null && opts.GPS5Precision != null) {
opts.GPSPrecision = opts.GPS5Precision;
}
// Find available GPS type and store first times for potential sorting
const userGPSChoices = ['GPS9', 'GPS5'].filter(k =>
(opts.stream || []).includes(k)
);
const forceGPSSrc = userGPSChoices.length === 1 ? userGPSChoices[0] : null;
if (!Array.isArray(input)) input = [input];
const firstTimes = input.map(i => findFirstTimes(i.rawData, forceGPSSrc));
let bestGPSTimeSrc;
if (firstTimes.every(t => t.GPS9Time)) bestGPSTimeSrc = 'GPS9';
else if (firstTimes.every(t => t.GPSU)) bestGPSTimeSrc = 'GPS5';
else if (firstTimes.some(t => t.GPS9Time)) bestGPSTimeSrc = 'GPS9';
else {
if (opts.timeIn === 'GPS') delete opts.timeIn;
bestGPSTimeSrc = 'GPS5';
}
if ((opts.stream || []).includes('GPS')) {
opts.stream = opts.stream.map(s => (s === 'GPS' ? bestGPSTimeSrc : s));
}
let interpreted;
let timing;
// Provide approximate progress updates
progress(opts, 0.01);
// Treat single or multiple inputs differently
if (input.length === 1) input = input[0];
if (!Array.isArray(input)) {
if (input.timing) {
timing = JSON.parse(JSON.stringify(input.timing));
timing.start = new Date(timing.start);
}
await breathe();
const gpsTimeSrc = bestGPSTimeSrc;
const parsed = await parseOne(input, opts, gpsTimeSrc);
progress(opts, 0.2);
await breathe();
//Return list of devices/streams only
if (opts.deviceList) return deviceList(parsed);
if (opts.streamList) return streamList(parsed);
//Return now if raw wanted
if (opts.raw) return parsed;
await breathe();
interpreted = await interpretOne({ timing, parsed, opts, gpsTimeSrc });
progress(opts, 0.4);
} else {
if (input.some(i => !i.timing))
throw new Error(
'per-source timing is necessary in order to merge sources'
);
if (
input.every(
i => i.timing.start.getTime() === input[0].timing.start.getTime()
)
) {
// Some firmwares produce consecutive files with the same creation date.
// Try to use GPS time or timestamps to solve this
input.sort((a, b) => {
const foundA = firstTimes[input.indexOf(a)];
const foundB = firstTimes[input.indexOf(b)];
if (foundA.GPS9Time && foundB.GPS9Time) {
return foundA.GPS9Time - foundB.GPS9Time;
}
if (foundA.GPSU && foundB.GPSU) return foundA.GPSU - foundB.GPSU;
if (foundA.STMP != null && foundB.STMP != null) {
return foundA.STMP - foundB.STMP;
}
return 0;
});
}
timing = input.map(i => JSON.parse(JSON.stringify(i.timing)));
timing = timing.map(t => ({ ...t, start: new Date(t.start) }));
const getGPSTimeSrc = i =>
firstTimes[i][bestGPSTimeSrc]
? bestGPSTimeSrc
: firstTimes[i].GPS9Time
? 'GPS9'
: 'GPS5';
//Loop parse all files, with offsets
const parsed = [];
for (let i = 0; i < input.length; i++) {
const oneParsed = await parseOne(input[i], opts, getGPSTimeSrc(i));
parsed.push(oneParsed);
}
progress(opts, 0.2);
await breathe();
//Return list of devices/streams only
if (opts.deviceList) return parsed.map(p => deviceList(p));
if (opts.streamList) return parsed.map(p => streamList(p));
//Return now if raw wanted
if (opts.raw) return parsed;
//Interpret all
const interpretedArr = [];
let gpsDate, mp4Date;
for (let i = 0; i < parsed.length; i++) {
const p = parsed[i];
await breathe();
let interpreted;
let offset = 0;
if (i > 0) {
offset = getOffset({ interpretedArr, i, opts, timing });
}
const timeMeta = { gpsDate, mp4Date, offset };
interpreted = await interpretOne({
timing: timing[i],
parsed: p,
opts,
timeMeta,
gpsTimeSrc: getGPSTimeSrc(i)
});
if (!gpsDate && timeMeta.gpsDate) {
gpsDate = timeMeta.gpsDate;
}
if (!mp4Date && timeMeta.mp4Date) {
mp4Date = timeMeta.mp4Date;
}
interpretedArr.push(interpreted);
}
progress(opts, 0.3);
await breathe();
//Merge samples in interpreted obj
interpreted = await mergeInterpretedSources(interpretedArr);
progress(opts, 0.4);
//Set single timing for rest of outer function
timing = timing[0];
}
await breathe();
//Clean unused streams (namely GPS5 & GPS9 used for timing if cached raw data)
if (opts.stream && opts.stream.length) {
for (const dev in interpreted) {
for (const stream in interpreted[dev].streams) {
if (
!opts.stream.includes(stream) &&
!keys.computedStreams.includes(stream)
) {
delete interpreted[dev].streams[stream];
}
}
}
}
//Read framerate to convert groupTimes to number if needed
if (opts.groupTimes === 'frames') {
if (timing && timing.frameDuration) {
opts.groupTimes = timing.frameDuration * 1000;
} else throw new Error('Frame rate is needed for your current options');
}
await breathe();
//Group samples by time if necessary
if (opts.smooth) interpreted = await smoothSamples(interpreted, opts);
progress(opts, 0.6);
await breathe();
// Apply decimal places
if(opts.decimalPlaces) interpreted = await decimalPlaces(interpreted, opts)
//Group samples by time if necessary
if (opts.groupTimes) interpreted = await groupTimes(interpreted, opts);
//Add framerate to top level
if (timing && timing.frameDuration != null)
interpreted['frames/second'] = 1 / timing.frameDuration;
progress(opts, 0.9);
await breathe();
//Process presets
if (opts.preset === 'gpx') return await toGpx(interpreted, opts);
if (opts.preset === 'virb') return await toVirb(interpreted, opts);
if (opts.preset === 'kml') return await toKml(interpreted, opts);
if (opts.preset === 'geojson') return await toGeojson(interpreted, opts);
if (opts.preset === 'csv') return await toCsv(interpreted);
if (opts.preset === 'mgjson') return await toMgjson(interpreted, opts);
progress(opts, 1);
return interpreted;
}
async function GoProTelemetry(input, options = {}, callback) {
const result = await process(input, options);
if (!callback) return result;
callback(result);
}
module.exports = GoProTelemetry;
exports = module.exports;
exports.GoProTelemetry = GoProTelemetry;
exports.goProTelemetry = GoProTelemetry;