From ee606ebb6445d443f368c12d3b931893ecbc7d16 Mon Sep 17 00:00:00 2001 From: Nicholas Shavers <nshavers@contractor.usgs.gov> Date: Mon, 9 Dec 2024 13:10:09 -0800 Subject: [PATCH] general clean up --- geomagio/ImagCDFFactory.py | 56 ++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/geomagio/ImagCDFFactory.py b/geomagio/ImagCDFFactory.py index 6ba44f3e..3d8ba136 100644 --- a/geomagio/ImagCDFFactory.py +++ b/geomagio/ImagCDFFactory.py @@ -31,6 +31,8 @@ from . import TimeseriesUtility from . import Util import cdflib +from cdflib.cdfwrite import CDF as CDFWriter +from cdflib.cdfread import CDF as CDFReader import tempfile @@ -48,7 +50,7 @@ class IMCDFPublicationLevel: 4: Definitive data with no further changes expected. Reference: - - ImagCDF Documentation Section 4.2: Attributes that Uniquely Identify the Data + - ImagCDF Technical Documentation: Attributes that Uniquely Identify the Data """ class PublicationLevel: @@ -137,13 +139,13 @@ class ImagCDFFactory(TimeseriesFactory): # Initialize the CDF writer cdf_spec = { 'Compressed': 9, # Enable compression (0-9) - # 'Majority': 'row_major', # Data layout - gets set automatically - # 'Encoding': 'ibmpc', # Corrupts CDF - gets set automatically + 'Majority': CDFWriter.ROW_MAJOR, # Data layout - gets set automatically + 'Encoding': CDFWriter.IBMPC_ENCODING, # gets set automatically 'Checksum': True, # Disable checksum for faster writes (optional) 'rDim_sizes': [], # Applicable only if using rVariables - CDF protocol recommends only using zVariables. } - cdf_writer = cdflib.cdfwrite.CDF(path=tmp_file_path, cdf_spec=cdf_spec, delete=True) + cdf_writer = CDFWriter(path=tmp_file_path, cdf_spec=cdf_spec, delete=True) # Write global attributes global_attrs = self._create_global_attributes(timeseries, channels) @@ -155,13 +157,13 @@ class ImagCDFFactory(TimeseriesFactory): # Define time variable specification var_spec = { 'Variable': ts_name, - 'Data_Type': 33, # CDF_TIME_TT2000 + 'Data_Type': CDFWriter.CDF_TIME_TT2000, # CDF_TIME_TT2000 'Num_Elements': 1, 'Rec_Vary': True, 'Var_Type': 'zVariable', 'Dim_Sizes': [], 'Sparse': 'no_sparse', - 'Compress': 6, + 'Compress': 9, 'Pad': None, } # Define time variable attributes @@ -182,9 +184,7 @@ class ImagCDFFactory(TimeseriesFactory): var_name = f"GeomagneticField{channel}" data_type = self._get_cdf_data_type(trace) num_elements = 1 - CDF_CHAR = 51 - CDF_UCHAR = 52 - if data_type in [CDF_CHAR, CDF_UCHAR]: # Handle string types + if data_type in [CDFWriter.CDF_CHAR, CDFWriter.CDF_UCHAR]: # Handle string types num_elements = len(trace.data[0]) if len(trace.data) > 0 else 1 var_spec = { @@ -195,7 +195,7 @@ class ImagCDFFactory(TimeseriesFactory): 'Var_Type': 'zVariable', 'Dim_Sizes': [], 'Sparse': 'no_sparse', - 'Compress': 6, + 'Compress': 9, 'Pad': None, } var_attrs = self._create_var_attrs(trace, temperature_index, self.isUniqueTimes) @@ -294,7 +294,7 @@ class ImagCDFFactory(TimeseriesFactory): if os.path.isfile(url_file): try: # Read existing data to merge with new data - existing_cdf = cdflib.cdfread.CDF(url_file) + existing_cdf = CDFReader(url_file) existing_stream = self._read_cdf(existing_cdf) # existing_cdf.close() #no close method? existing_data = existing_stream @@ -371,7 +371,7 @@ class ImagCDFFactory(TimeseriesFactory): try: # Read CDF data and merge - cdf = cdflib.cdfread.CDF(url_file) + cdf = CDFReader(url_file) file_stream = self._read_cdf(cdf) # Attempt to select only requested channels selected = Stream() @@ -418,7 +418,7 @@ class ImagCDFFactory(TimeseriesFactory): descriptions. References: - - ImagCDF Documentation Section 4: ImagCDF Global Attributes + - ImagCDF Technical Documentation: ImagCDF Global Attributes """ stats = timeseries[0].stats if len(timeseries) > 0 else None @@ -458,10 +458,10 @@ class ImagCDFFactory(TimeseriesFactory): # 'UniqueIdentifier': {0: ''}, # 'ParentIdentifiers': {0: ''}, # 'ReferenceLinks': {0: ''}, #links to /ws, plots, USGS.gov - 'SensorSamplingRate': {0: sensor_sampling_rate}, - 'DataType': {0: data_type}, - 'Comments': {0: comments}, - 'DeclinationBase': {0: declination_base}, + 'SensorSamplingRate': {0: sensor_sampling_rate}, #Optional + 'DataType': {0: data_type},#Optional + 'Comments': {0: comments}, #Optional + 'DeclinationBase': {0: declination_base}, #Optional } return global_attrs @@ -470,7 +470,6 @@ class ImagCDFFactory(TimeseriesFactory): scalar_times = None temperature_times = {} temperature_index = 1 - print(timeseries[0].stats) for trace in timeseries: channel = trace.stats.channel @@ -615,9 +614,9 @@ class ImagCDFFactory(TimeseriesFactory): Create a dictionary of time variable attributes. These attributes provide metadata for time variables. - Note: None of these attributes are required for the time stamp variables GeomagneticVectorTimes and GeomagneticScalarTimes. + Note: None of these attributes are required for the time stamp variables. Reference: - - ImagCDF Documentation Section 3: ImagCDF Data + - ImagCDF Technical Documentation: ImagCDF Data """ # var_attrs = { # 'UNITS': 'TT2000', @@ -639,21 +638,18 @@ class ImagCDFFactory(TimeseriesFactory): - CDF_INT4 (41) for integer data. Reference: - - CDF Data Types: http://cdf.gsfc.nasa.gov/html/cdfdatatypes.html + - See CDF for more data types """ - # CDF data type constants - CDF_DOUBLE = 45 # CDF_DOUBLE corresponds to 64-bit float - CDF_INT4 = 41 # CDF_INT4 corresponds to 32-bit int if trace.data.dtype in [np.float32, np.float64]: - return CDF_DOUBLE + return CDFWriter.CDF_DOUBLE elif trace.data.dtype in [np.int32, np.int64]: - return CDF_INT4 + return CDFWriter.CDF_INT4 else: # Default to double precision float - return CDF_DOUBLE + return CDFWriter.CDF_DOUBLE - def _read_cdf(self, cdf: cdflib.cdfread.CDF) -> Stream: + def _read_cdf(self, cdf: CDFReader) -> Stream: """ Read CDF data into an ObsPy Stream. @@ -677,7 +673,7 @@ class ImagCDFFactory(TimeseriesFactory): institution = global_attrs.get('Institution', [''])[0] latitude = global_attrs.get('Latitude', [0.0])[0] longitude = global_attrs.get('Longitude', [0.0])[0] - elevation = global_attrs.get('Elevation', [99_999.0])[0] + elevation = global_attrs.get('Elevation', [99_999.0])[0] #default to 99_999 per technical documents. sensor_sampling_rate = global_attrs.get('SensorSamplingRate', [0.0])[0] sensor_orientation = global_attrs.get('VectorSensOrient', [''])[0] data_type = global_attrs.get('DataType', ['variation'])[0] @@ -816,7 +812,7 @@ class ImagCDFFactory(TimeseriesFactory): - The formatted file URL or path. Reference: - - ImagCDF Documentation Section 5: ImagCDF File Names + - ImagCDF Technical Documentation: ImagCDF File Names """ # Get the publication level for the type publication_level = IMCDFPublicationLevel(data_type=type).to_string() -- GitLab