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

general clean up

parent a0403695
No related branches found
No related tags found
1 merge request!368Imagcdf factory mvp
...@@ -31,6 +31,8 @@ from . import TimeseriesUtility ...@@ -31,6 +31,8 @@ from . import TimeseriesUtility
from . import Util from . import Util
import cdflib import cdflib
from cdflib.cdfwrite import CDF as CDFWriter
from cdflib.cdfread import CDF as CDFReader
import tempfile import tempfile
...@@ -48,7 +50,7 @@ class IMCDFPublicationLevel: ...@@ -48,7 +50,7 @@ class IMCDFPublicationLevel:
4: Definitive data with no further changes expected. 4: Definitive data with no further changes expected.
Reference: Reference:
- ImagCDF Documentation Section 4.2: Attributes that Uniquely Identify the Data - ImagCDF Technical Documentation: Attributes that Uniquely Identify the Data
""" """
class PublicationLevel: class PublicationLevel:
...@@ -137,13 +139,13 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -137,13 +139,13 @@ class ImagCDFFactory(TimeseriesFactory):
# Initialize the CDF writer # Initialize the CDF writer
cdf_spec = { cdf_spec = {
'Compressed': 9, # Enable compression (0-9) 'Compressed': 9, # Enable compression (0-9)
# 'Majority': 'row_major', # Data layout - gets set automatically 'Majority': CDFWriter.ROW_MAJOR, # Data layout - gets set automatically
# 'Encoding': 'ibmpc', # Corrupts CDF - gets set automatically 'Encoding': CDFWriter.IBMPC_ENCODING, # gets set automatically
'Checksum': True, # Disable checksum for faster writes (optional) 'Checksum': True, # Disable checksum for faster writes (optional)
'rDim_sizes': [], # Applicable only if using rVariables - CDF protocol recommends only using zVariables. '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 # Write global attributes
global_attrs = self._create_global_attributes(timeseries, channels) global_attrs = self._create_global_attributes(timeseries, channels)
...@@ -155,13 +157,13 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -155,13 +157,13 @@ class ImagCDFFactory(TimeseriesFactory):
# Define time variable specification # Define time variable specification
var_spec = { var_spec = {
'Variable': ts_name, 'Variable': ts_name,
'Data_Type': 33, # CDF_TIME_TT2000 'Data_Type': CDFWriter.CDF_TIME_TT2000, # CDF_TIME_TT2000
'Num_Elements': 1, 'Num_Elements': 1,
'Rec_Vary': True, 'Rec_Vary': True,
'Var_Type': 'zVariable', 'Var_Type': 'zVariable',
'Dim_Sizes': [], 'Dim_Sizes': [],
'Sparse': 'no_sparse', 'Sparse': 'no_sparse',
'Compress': 6, 'Compress': 9,
'Pad': None, 'Pad': None,
} }
# Define time variable attributes # Define time variable attributes
...@@ -182,9 +184,7 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -182,9 +184,7 @@ class ImagCDFFactory(TimeseriesFactory):
var_name = f"GeomagneticField{channel}" var_name = f"GeomagneticField{channel}"
data_type = self._get_cdf_data_type(trace) data_type = self._get_cdf_data_type(trace)
num_elements = 1 num_elements = 1
CDF_CHAR = 51 if data_type in [CDFWriter.CDF_CHAR, CDFWriter.CDF_UCHAR]: # Handle string types
CDF_UCHAR = 52
if data_type in [CDF_CHAR, CDF_UCHAR]: # Handle string types
num_elements = len(trace.data[0]) if len(trace.data) > 0 else 1 num_elements = len(trace.data[0]) if len(trace.data) > 0 else 1
var_spec = { var_spec = {
...@@ -195,7 +195,7 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -195,7 +195,7 @@ class ImagCDFFactory(TimeseriesFactory):
'Var_Type': 'zVariable', 'Var_Type': 'zVariable',
'Dim_Sizes': [], 'Dim_Sizes': [],
'Sparse': 'no_sparse', 'Sparse': 'no_sparse',
'Compress': 6, 'Compress': 9,
'Pad': None, 'Pad': None,
} }
var_attrs = self._create_var_attrs(trace, temperature_index, self.isUniqueTimes) var_attrs = self._create_var_attrs(trace, temperature_index, self.isUniqueTimes)
...@@ -294,7 +294,7 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -294,7 +294,7 @@ class ImagCDFFactory(TimeseriesFactory):
if os.path.isfile(url_file): if os.path.isfile(url_file):
try: try:
# Read existing data to merge with new data # 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_stream = self._read_cdf(existing_cdf)
# existing_cdf.close() #no close method? # existing_cdf.close() #no close method?
existing_data = existing_stream existing_data = existing_stream
...@@ -371,7 +371,7 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -371,7 +371,7 @@ class ImagCDFFactory(TimeseriesFactory):
try: try:
# Read CDF data and merge # Read CDF data and merge
cdf = cdflib.cdfread.CDF(url_file) cdf = CDFReader(url_file)
file_stream = self._read_cdf(cdf) file_stream = self._read_cdf(cdf)
# Attempt to select only requested channels # Attempt to select only requested channels
selected = Stream() selected = Stream()
...@@ -418,7 +418,7 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -418,7 +418,7 @@ class ImagCDFFactory(TimeseriesFactory):
descriptions. descriptions.
References: References:
- ImagCDF Documentation Section 4: ImagCDF Global Attributes - ImagCDF Technical Documentation: ImagCDF Global Attributes
""" """
stats = timeseries[0].stats if len(timeseries) > 0 else None stats = timeseries[0].stats if len(timeseries) > 0 else None
...@@ -458,10 +458,10 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -458,10 +458,10 @@ 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}, 'SensorSamplingRate': {0: sensor_sampling_rate}, #Optional
'DataType': {0: data_type}, 'DataType': {0: data_type},#Optional
'Comments': {0: comments}, 'Comments': {0: comments}, #Optional
'DeclinationBase': {0: declination_base}, 'DeclinationBase': {0: declination_base}, #Optional
} }
return global_attrs return global_attrs
...@@ -470,7 +470,6 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -470,7 +470,6 @@ class ImagCDFFactory(TimeseriesFactory):
scalar_times = None scalar_times = None
temperature_times = {} temperature_times = {}
temperature_index = 1 temperature_index = 1
print(timeseries[0].stats)
for trace in timeseries: for trace in timeseries:
channel = trace.stats.channel channel = trace.stats.channel
...@@ -615,9 +614,9 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -615,9 +614,9 @@ class ImagCDFFactory(TimeseriesFactory):
Create a dictionary of time variable attributes. Create a dictionary of time variable attributes.
These attributes provide metadata for time variables. 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: Reference:
- ImagCDF Documentation Section 3: ImagCDF Data - ImagCDF Technical Documentation: ImagCDF Data
""" """
# var_attrs = { # var_attrs = {
# 'UNITS': 'TT2000', # 'UNITS': 'TT2000',
...@@ -639,21 +638,18 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -639,21 +638,18 @@ class ImagCDFFactory(TimeseriesFactory):
- CDF_INT4 (41) for integer data. - CDF_INT4 (41) for integer data.
Reference: 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]: 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]: elif trace.data.dtype in [np.int32, np.int64]:
return CDF_INT4 return CDFWriter.CDF_INT4
else: else:
# Default to double precision float # 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. Read CDF data into an ObsPy Stream.
...@@ -677,7 +673,7 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -677,7 +673,7 @@ class ImagCDFFactory(TimeseriesFactory):
institution = global_attrs.get('Institution', [''])[0] institution = global_attrs.get('Institution', [''])[0]
latitude = global_attrs.get('Latitude', [0.0])[0] latitude = global_attrs.get('Latitude', [0.0])[0]
longitude = global_attrs.get('Longitude', [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_sampling_rate = global_attrs.get('SensorSamplingRate', [0.0])[0]
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]
...@@ -816,7 +812,7 @@ class ImagCDFFactory(TimeseriesFactory): ...@@ -816,7 +812,7 @@ class ImagCDFFactory(TimeseriesFactory):
- The formatted file URL or path. - The formatted file URL or path.
Reference: Reference:
- ImagCDF Documentation Section 5: ImagCDF File Names - ImagCDF Technical Documentation: ImagCDF File Names
""" """
# Get the publication level for the type # Get the publication level for the type
publication_level = IMCDFPublicationLevel(data_type=type).to_string() publication_level = IMCDFPublicationLevel(data_type=type).to_string()
......
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