|
1 | 1 | import dataclasses |
| 2 | +import datetime |
2 | 3 | import io |
3 | 4 | import itertools |
4 | 5 | import pathlib |
5 | 6 | import typing as T |
6 | | -import datetime |
7 | | - |
8 | 7 |
|
9 | 8 | import construct as C |
10 | 9 |
|
11 | 10 | from .. import imu |
12 | 11 | from ..mp4.mp4_sample_parser import MovieBoxParser, Sample, TrackBoxParser |
13 | | - |
14 | 12 | from ..telemetry import GPSFix, GPSPoint |
15 | 13 |
|
16 | 14 | """ |
@@ -209,7 +207,11 @@ def gps5_from_stream( |
209 | 207 |
|
210 | 208 | gpsu = indexed.get(b"GPSU") |
211 | 209 | if gpsu is not None: |
212 | | - epoch_time = _gps5_timestamp_to_epoch_time(gpsu[0][0].decode("utf-8")) |
| 210 | + try: |
| 211 | + yymmdd = gpsu[0][0].decode("utf-8") |
| 212 | + epoch_time = _gps5_timestamp_to_epoch_time(yymmdd) |
| 213 | + except Exception: |
| 214 | + epoch_time = None |
213 | 215 | else: |
214 | 216 | epoch_time = None |
215 | 217 |
|
@@ -478,6 +480,28 @@ def _extract_dvnm_from_samples( |
478 | 480 | return dvnm_by_dvid |
479 | 481 |
|
480 | 482 |
|
| 483 | +def _backfill_gps_timestamps(gps_points: T.Iterable[GPSPoint]) -> None: |
| 484 | + it = iter(gps_points) |
| 485 | + |
| 486 | + # find the first point with epoch time |
| 487 | + last = None |
| 488 | + for point in it: |
| 489 | + if point.epoch_time is not None: |
| 490 | + last = point |
| 491 | + break |
| 492 | + |
| 493 | + # if no point with epoch time found, return |
| 494 | + if last is None: |
| 495 | + return |
| 496 | + |
| 497 | + # backfill points without epoch time |
| 498 | + for point in it: |
| 499 | + assert last.epoch_time is not None |
| 500 | + if point.epoch_time is None: |
| 501 | + point.epoch_time = last.epoch_time + (point.time - last.time) |
| 502 | + last = point |
| 503 | + |
| 504 | + |
481 | 505 | def _extract_points_from_samples( |
482 | 506 | fp: T.BinaryIO, samples: T.Iterable[Sample] |
483 | 507 | ) -> TelemetryData: |
@@ -549,8 +573,16 @@ def _extract_points_from_samples( |
549 | 573 | for idx, (z, x, y, *_) in enumerate(sample_magns) |
550 | 574 | ) |
551 | 575 |
|
| 576 | + gps_points = list(points_by_dvid.values())[0] if points_by_dvid else [] |
| 577 | + |
| 578 | + # backfill forward from the first point with epoch time |
| 579 | + _backfill_gps_timestamps(gps_points) |
| 580 | + |
| 581 | + # backfill backward from the first point with epoch time in reversed order |
| 582 | + _backfill_gps_timestamps(reversed(gps_points)) |
| 583 | + |
552 | 584 | return TelemetryData( |
553 | | - gps=list(points_by_dvid.values())[0] if points_by_dvid else [], |
| 585 | + gps=gps_points, |
554 | 586 | accl=list(accls_by_dvid.values())[0] if accls_by_dvid else [], |
555 | 587 | gyro=list(gyros_by_dvid.values())[0] if gyros_by_dvid else [], |
556 | 588 | magn=list(magns_by_dvid.values())[0] if magns_by_dvid else [], |
|
0 commit comments