diff --git a/geomagio/imagcdf/ImagCDFFactory.py b/geomagio/imagcdf/ImagCDFFactory.py index 89c1f6a6ccf5f40f832af22174b1fd650edf761f..c285504510cc2a9fd77bb47026145b4ef0c50453 100644 --- a/geomagio/imagcdf/ImagCDFFactory.py +++ b/geomagio/imagcdf/ImagCDFFactory.py @@ -103,6 +103,7 @@ class ImagCDFFactory(TimeseriesFactory): interval: DataInterval = "minute", urlTemplate="file://etc/imagcdf/{obs}_{dt}_{t}.cdf", urlInterval: int = -1, + inputFile: Optional[str] = None, ): """ Initialize the ImagCDFFactory with default parameters. @@ -114,6 +115,7 @@ class ImagCDFFactory(TimeseriesFactory): - interval: Data interval (e.g., 'minute', 'second'). - urlTemplate: Template for generating file URLs or paths. - urlInterval: Interval size for splitting data into multiple files. + - inputFile: An ImagCDF file to read data from. If not provided urlTemplate is assumed path for reads. """ super().__init__( observatory=observatory, @@ -123,6 +125,7 @@ class ImagCDFFactory(TimeseriesFactory): urlTemplate=urlTemplate, urlInterval=urlInterval, ) + self.inputFile = inputFile def write_file(self, fh, timeseries: Stream, channels: List[str]): # Create a temporary file to write the CDF data @@ -333,24 +336,26 @@ class ImagCDFFactory(TimeseriesFactory): ) for urlInterval in urlIntervals: - url = self._get_url( - observatory=observatory, - date=urlInterval["start"], - type=type, - interval=interval, - channels=channels, - ) - if url == "stdout": - continue # stdout is not a valid input source - if not url.startswith("file://"): - raise TimeseriesFactoryException( - "Only file urls are supported for reading ImagCDF" + if self.inputFile is None: + url = self._get_url( + observatory=observatory, + date=urlInterval["start"], + type=type, + interval=interval, + channels=channels, ) - url_file = Util.get_file_from_url(url, createParentDirectory=False) - if not os.path.isfile(url_file): - # If file doesn't exist, skip - continue - + if url == "stdout": + continue # stdout is not a valid input source + if not url.startswith("file://"): + raise TimeseriesFactoryException( + "Only file urls are supported for reading ImagCDF" + ) + url_file = Util.get_file_from_url(url, createParentDirectory=False) + if not os.path.isfile(url_file): + # If file doesn't exist, skip + continue + else: + url_file = self.inputFile try: # Read CDF data and merge cdf = CDFReader(url_file) @@ -406,6 +411,7 @@ class ImagCDFFactory(TimeseriesFactory): getattr(stats, "station_name", None) or self.observatory or "" ) station = getattr(stats, "station", None) or "" + network = getattr(stats, "network", None) or "" institution = getattr(stats, "agency_name", None) or "" latitude = getattr(stats, "geodetic_latitude", None) or 0.0 longitude = getattr(stats, "geodetic_longitude", None) or 0.0 @@ -450,10 +456,12 @@ class ImagCDFFactory(TimeseriesFactory): # 'UniqueIdentifier': {0: ''}, # 'ParentIdentifiers': {0: ''}, # 'ReferenceLinks': {0: ''}, #links to /ws, plots, USGS.gov - "SensorSamplingRate": {0: sensor_sampling_rate}, # Optional - "DataType": {0: data_type}, # Optional - "Comments": {0: comments}, # Optional - "DeclinationBase": {0: declination_base}, # Optional + # Custom Attributes Below + "SensorSamplingRate": {0: sensor_sampling_rate}, + "DataType": {0: data_type}, + "Comments": {0: comments}, + "DeclinationBase": {0: declination_base}, + "Network": {0: network}, } return global_attrs @@ -639,7 +647,8 @@ class ImagCDFFactory(TimeseriesFactory): "DEPEND_0": depend_0, "DISPLAY_TYPE": "time_series", "LABLAXIS": channel, - "DATA_INTERVAL_TYPE": trace.stats.data_interval_type, # optional + # custom ImagCDF variable attributes below + "DATA_INTERVAL_TYPE": trace.stats.data_interval_type, } return var_attrs @@ -745,9 +754,10 @@ class ImagCDFFactory(TimeseriesFactory): sensor_orientation = global_attrs.get("VectorSensOrient", [""])[0] data_type = global_attrs.get("DataType", ["variation"])[0] publication_level = global_attrs.get("PublicationLevel", ["1"])[0] - comments = global_attrs.get("Comments", [""]) + comments = global_attrs.get("Comments", [""]) #keep comments as an array terms_of_use = global_attrs.get("TermsOfUse", [""])[0] declination_base = global_attrs.get("DeclinationBase", [0.0])[0] + network = global_attrs.get("Network", [""])[0] # Identify all time variables time_vars = {} @@ -862,10 +872,12 @@ class ImagCDFFactory(TimeseriesFactory): "station_name": station_name, "agency_name": institution, "conditions_of_use": terms_of_use, + # data points not in a traditional ImagCDF "sensor_sampling_rate": sensor_sampling_rate, "data_interval_type": data_interval, "declination_base": declination_base, "filter_comments": comments, + "network": network }, ) stream += trace