Skip to content
Snippets Groups Projects
Commit ba081cc4 authored by Shavers, Nicholas H's avatar Shavers, Nicholas H
Browse files

accept and use file path is provided via --input-file argument, else try...

accept and use file path is provided via --input-file argument, else try urlTemplates path. Network as a custom global attribute
parent b221ec1d
No related branches found
No related tags found
1 merge request!368Imagcdf factory mvp
...@@ -103,6 +103,7 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -103,6 +103,7 @@ class ImagCDFFactory(TimeseriesFactory):
interval: DataInterval = "minute", interval: DataInterval = "minute",
urlTemplate="file://etc/imagcdf/{obs}_{dt}_{t}.cdf", urlTemplate="file://etc/imagcdf/{obs}_{dt}_{t}.cdf",
urlInterval: int = -1, urlInterval: int = -1,
inputFile: Optional[str] = None,
): ):
""" """
Initialize the ImagCDFFactory with default parameters. Initialize the ImagCDFFactory with default parameters.
...@@ -114,6 +115,7 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -114,6 +115,7 @@ class ImagCDFFactory(TimeseriesFactory):
- interval: Data interval (e.g., 'minute', 'second'). - interval: Data interval (e.g., 'minute', 'second').
- urlTemplate: Template for generating file URLs or paths. - urlTemplate: Template for generating file URLs or paths.
- urlInterval: Interval size for splitting data into multiple files. - 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__( super().__init__(
observatory=observatory, observatory=observatory,
...@@ -123,6 +125,7 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -123,6 +125,7 @@ class ImagCDFFactory(TimeseriesFactory):
urlTemplate=urlTemplate, urlTemplate=urlTemplate,
urlInterval=urlInterval, urlInterval=urlInterval,
) )
self.inputFile = inputFile
def write_file(self, fh, timeseries: Stream, channels: List[str]): def write_file(self, fh, timeseries: Stream, channels: List[str]):
# Create a temporary file to write the CDF data # Create a temporary file to write the CDF data
...@@ -333,24 +336,26 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -333,24 +336,26 @@ class ImagCDFFactory(TimeseriesFactory):
) )
for urlInterval in urlIntervals: for urlInterval in urlIntervals:
url = self._get_url( if self.inputFile is None:
observatory=observatory, url = self._get_url(
date=urlInterval["start"], observatory=observatory,
type=type, date=urlInterval["start"],
interval=interval, type=type,
channels=channels, 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"
) )
url_file = Util.get_file_from_url(url, createParentDirectory=False) if url == "stdout":
if not os.path.isfile(url_file): continue # stdout is not a valid input source
# If file doesn't exist, skip if not url.startswith("file://"):
continue 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: try:
# Read CDF data and merge # Read CDF data and merge
cdf = CDFReader(url_file) cdf = CDFReader(url_file)
...@@ -406,6 +411,7 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -406,6 +411,7 @@ class ImagCDFFactory(TimeseriesFactory):
getattr(stats, "station_name", None) or self.observatory or "" getattr(stats, "station_name", None) or self.observatory or ""
) )
station = getattr(stats, "station", None) or "" station = getattr(stats, "station", None) or ""
network = getattr(stats, "network", None) or ""
institution = getattr(stats, "agency_name", None) or "" institution = getattr(stats, "agency_name", None) or ""
latitude = getattr(stats, "geodetic_latitude", None) or 0.0 latitude = getattr(stats, "geodetic_latitude", None) or 0.0
longitude = getattr(stats, "geodetic_longitude", None) or 0.0 longitude = getattr(stats, "geodetic_longitude", None) or 0.0
...@@ -450,10 +456,12 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -450,10 +456,12 @@ class ImagCDFFactory(TimeseriesFactory):
# 'UniqueIdentifier': {0: ''}, # 'UniqueIdentifier': {0: ''},
# 'ParentIdentifiers': {0: ''}, # 'ParentIdentifiers': {0: ''},
# 'ReferenceLinks': {0: ''}, #links to /ws, plots, USGS.gov # 'ReferenceLinks': {0: ''}, #links to /ws, plots, USGS.gov
"SensorSamplingRate": {0: sensor_sampling_rate}, # Optional # Custom Attributes Below
"DataType": {0: data_type}, # Optional "SensorSamplingRate": {0: sensor_sampling_rate},
"Comments": {0: comments}, # Optional "DataType": {0: data_type},
"DeclinationBase": {0: declination_base}, # Optional "Comments": {0: comments},
"DeclinationBase": {0: declination_base},
"Network": {0: network},
} }
return global_attrs return global_attrs
...@@ -639,7 +647,8 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -639,7 +647,8 @@ class ImagCDFFactory(TimeseriesFactory):
"DEPEND_0": depend_0, "DEPEND_0": depend_0,
"DISPLAY_TYPE": "time_series", "DISPLAY_TYPE": "time_series",
"LABLAXIS": channel, "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 return var_attrs
...@@ -745,9 +754,10 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -745,9 +754,10 @@ class ImagCDFFactory(TimeseriesFactory):
sensor_orientation = global_attrs.get("VectorSensOrient", [""])[0] sensor_orientation = global_attrs.get("VectorSensOrient", [""])[0]
data_type = global_attrs.get("DataType", ["variation"])[0] data_type = global_attrs.get("DataType", ["variation"])[0]
publication_level = global_attrs.get("PublicationLevel", ["1"])[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] terms_of_use = global_attrs.get("TermsOfUse", [""])[0]
declination_base = global_attrs.get("DeclinationBase", [0.0])[0] declination_base = global_attrs.get("DeclinationBase", [0.0])[0]
network = global_attrs.get("Network", [""])[0]
# Identify all time variables # Identify all time variables
time_vars = {} time_vars = {}
...@@ -862,10 +872,12 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -862,10 +872,12 @@ class ImagCDFFactory(TimeseriesFactory):
"station_name": station_name, "station_name": station_name,
"agency_name": institution, "agency_name": institution,
"conditions_of_use": terms_of_use, "conditions_of_use": terms_of_use,
# data points not in a traditional ImagCDF
"sensor_sampling_rate": sensor_sampling_rate, "sensor_sampling_rate": sensor_sampling_rate,
"data_interval_type": data_interval, "data_interval_type": data_interval,
"declination_base": declination_base, "declination_base": declination_base,
"filter_comments": comments, "filter_comments": comments,
"network": network
}, },
) )
stream += trace stream += trace
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment