From 68798d807806a6d681af5b93d770fd4e23e9ce92 Mon Sep 17 00:00:00 2001 From: Nicholas Shavers <nshavers@contractor.usgs.gov> Date: Thu, 5 Dec 2024 15:51:48 -0800 Subject: [PATCH] with compression at the file level --- geomagio/ImagCDFFactory.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/geomagio/ImagCDFFactory.py b/geomagio/ImagCDFFactory.py index 157ad0cf..0acf6ece 100644 --- a/geomagio/ImagCDFFactory.py +++ b/geomagio/ImagCDFFactory.py @@ -129,12 +129,20 @@ class ImagCDFFactory(TimeseriesFactory): def write_file(self, fh, timeseries: Stream, channels: List[str]): # Create a temporary file to write the CDF data - with tempfile.NamedTemporaryFile(delete=False) as tmp_file: - tmp_file_path = tmp_file.name + ".cdf" + with tempfile.NamedTemporaryFile(delete=False, suffix='.cdf') as tmp_file: + tmp_file_path = tmp_file.name try: # Initialize the CDF writer - cdf_writer = cdflib.cdfwrite.CDF(tmp_file_path) + cdf_spec = { + 'Compressed': 9, # Enable compression (0-9) + # 'Majority': 'row_major', # Data layout - gets set automatically + # 'Encoding': 'ibmpc', # Corrupts CDF - 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) # Write global attributes global_attrs = self._create_global_attributes(timeseries, channels) @@ -206,7 +214,6 @@ class ImagCDFFactory(TimeseriesFactory): # Cleanup the temporary file os.remove(tmp_file_path) - def put_timeseries( self, timeseries: Stream, @@ -288,7 +295,7 @@ class ImagCDFFactory(TimeseriesFactory): # Read existing data to merge with new data existing_cdf = cdflib.cdfread.CDF(url_file) existing_stream = self._read_cdf(existing_cdf) - existing_cdf.close() + # existing_cdf.close() #no close method? existing_data = existing_stream # Merge existing data with new data @@ -480,7 +487,7 @@ class ImagCDFFactory(TimeseriesFactory): elif channel in TEMPERATURE_ELEMENTS_ID: units = 'Celsius' fieldnam = f"Temperature {temperature_index} {trace.stats.location}" - validmin = -273.15 + validmin = -273.15 #absolute zero validmax = 79_999 elif channel in ['F','S']: units = 'nT' -- GitLab