From 0829e53b0c450cb2e4301a2d46ae583cde8142d0 Mon Sep 17 00:00:00 2001 From: glopesdev Date: Fri, 3 May 2024 13:53:35 +0100 Subject: [PATCH] Allow referencing stream harp times to UBX time --- pluma/export/ogcapi/features.py | 12 ++---- pluma/schema/__init__.py | 65 ++++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 31 deletions(-) diff --git a/pluma/export/ogcapi/features.py b/pluma/export/ogcapi/features.py index 15ee567..fb45d8d 100644 --- a/pluma/export/ogcapi/features.py +++ b/pluma/export/ogcapi/features.py @@ -13,8 +13,7 @@ def convert_dataset_to_geoframe( dataset, - sampling_dt: datetime.timedelta = datetime.timedelta(seconds=1), - rereference_to_ubx_time: bool = False + sampling_dt: datetime.timedelta = datetime.timedelta(seconds=1) ): streams_to_export = {} @@ -41,10 +40,6 @@ def convert_dataset_to_geoframe( out = out.join(out_columns) out.index.name = 'time' - if rereference_to_ubx_time: - offset = dataset.streams.UBX.positiondata['Time_UTC'][0] - out.index[0] - out.index += offset - geometry = gpd.points_from_xy( x=out['Longitude'], y=out['Latitude'], @@ -56,11 +51,10 @@ def convert_dataset_to_geoframe( def export_dataset_to_geojson( dataset, filename, - sampling_dt: datetime.timedelta = datetime.timedelta(seconds=1), - rereference_to_ubx_time: bool = False + sampling_dt: datetime.timedelta = datetime.timedelta(seconds=1) ): - out = convert_dataset_to_geoframe(dataset, sampling_dt, rereference_to_ubx_time) + out = convert_dataset_to_geoframe(dataset, sampling_dt) export_geoframe_to_geojson(out, filename) def export_geoframe_to_geojson( diff --git a/pluma/schema/__init__.py b/pluma/schema/__init__.py index f0d0082..242dc6e 100644 --- a/pluma/schema/__init__.py +++ b/pluma/schema/__init__.py @@ -83,9 +83,19 @@ def add_ubx_georeference(self, self.georeference.clockreference.referenceid =\ ubxstream.clockreference.referenceid - def reload_streams(self, - schema: Union[DotMap, Stream, None] = None, - force_load: bool = False) -> None: + @staticmethod + def _iter_schema_streams(schema: Union[DotMap, Stream, None] = None): + if isinstance(schema, Stream): + yield schema + elif isinstance(schema, DotMap): + for _stream in schema.values(): + for _nested in Dataset._iter_schema_streams(_stream): + yield _nested + else: + raise TypeError(f"Invalid type was found. Must be of \ + {Union[DotMap, Stream]}") + + def reload_streams(self, force_load: bool = False) -> None: """Recursively loads, from disk , all available streams in the streams' schema Args: @@ -97,21 +107,12 @@ def reload_streams(self, TypeError: An error is raised if a not allowed type is passed. """ - if schema is None: - schema = self.streams - - if isinstance(schema, Stream): + for stream in self._iter_schema_streams(self.streams): if force_load is True: - schema.load() + stream.load() else: - if schema.autoload is True: - schema.load() - elif isinstance(schema, DotMap): - for _stream in schema.values(): - self.reload_streams(_stream, force_load=force_load) - else: - raise TypeError(f"Invalid type was found. Must be of \ - {Union[DotMap, Stream]}") + if stream.autoload is True: + stream.load() @staticmethod def import_dataset(filename: Union[str, ComplexPath]) -> Dataset: @@ -240,13 +241,33 @@ def add_georeference_and_calibrate(self, plot_diagnosis=True): else: raise AssertionError('Dataset is already been automatically calibrated.') + @staticmethod + def _offset_data_index(data, offset): + if isinstance(data, DotMap): + for frame in data.values(): + Dataset._offset_data_index(frame, offset) + else: + data.index += offset - data.index[0] + + def reference_harp_to_ubx_time(self): + if self.has_calibration is False: + raise AssertionError('Dataset is not calibrated to UBX time.') + + utc_offset = self.streams.UBX.positiondata['Time_UTC'][0] + Dataset._offset_data_index(self.georeference.spacetime, utc_offset) + Dataset._offset_data_index(self.georeference.time, utc_offset) + for stream in self._iter_schema_streams(self.streams): + if len(stream.data) == 0: + continue + if stream.clockreference.referenceid == ClockRefId.HARP: + Dataset._offset_data_index(stream.data, utc_offset) + stream.clockreference.referenceid = ClockRefId.GNSS + def to_geoframe(self, - sampling_dt: datetime.timedelta = datetime.timedelta(seconds=1), - rereference_to_ubx_time: bool = False): - return convert_dataset_to_geoframe(self, sampling_dt, rereference_to_ubx_time) + sampling_dt: datetime.timedelta = datetime.timedelta(seconds=1)): + return convert_dataset_to_geoframe(self, sampling_dt) def to_geojson(self, filename, - sampling_dt: datetime.timedelta = datetime.timedelta(seconds=1), - rereference_to_ubx_time: bool = False): - export_dataset_to_geojson(self, filename, sampling_dt, rereference_to_ubx_time) + sampling_dt: datetime.timedelta = datetime.timedelta(seconds=1)): + export_dataset_to_geojson(self, filename, sampling_dt)