Skip to content

Commit

Permalink
Merge 5a4cd51 into 110a190
Browse files Browse the repository at this point in the history
  • Loading branch information
aburrell committed Dec 9, 2021
2 parents 110a190 + 5a4cd51 commit 34957d4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
26 changes: 17 additions & 9 deletions pysatMadrigal/instruments/methods/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,23 @@ def load(fnames, tag='', inst_id='', xarray_coords=None):
else:
notes = "No catalog text"

for item in file_data.data_vars.keys():
name_string = item
unit_string = file_data[item].attrs['units']
desc_string = file_data[item].attrs['description']
meta[name_string.lower()] = {meta.labels.name: name_string,
meta.labels.notes: notes,
meta.labels.units: unit_string,
meta.labels.desc: desc_string,
meta.labels.fill_val: np.nan}
# Get the coordinate and data variable names
meta_items = [dkey for dkey in file_data.data_vars.keys()]
meta_items.extend([dkey for dkey in file_data.coords.keys()])

for item in meta_items:
# Set the meta values for the expected labels
meta_dict = {meta.labels.name: item, meta.labels.fill_val: np.nan,
meta.labels.notes: notes}

for key, label in [('units', meta.labels.units),
('description', meta.labels.desc)]:
if key in file_data[item].attrs.keys():
meta_dict[label] = file_data[item].attrs[key]
else:
meta_dict[label] = ''

meta[item.lower()] = meta_dict

# Remove any metadata from xarray
file_data[item].attrs = {}
Expand Down
19 changes: 12 additions & 7 deletions pysatMadrigal/tests/test_instruments.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#!/usr/bin/env python
# Full license can be found in License.md
# Full author list can be found in .zenodo.json file
# DOI:10.5281/zenodo.3824979
# ----------------------------------------------------------------------------
"""Unit tests for the Instruments."""

import tempfile
import pytest

Expand Down Expand Up @@ -50,22 +57,20 @@

class TestInstruments(InstTestClass):
def setup_class(self):
"""Runs once before the tests to initialize the testing setup
"""
"""Initialize the testing setup."""
# Make sure to use a temporary directory so that the user's setup is
# not altered
self.tempdir = tempfile.TemporaryDirectory()
self.saved_path = pysat.params['data_dirs']
pysat.params.data['data_dirs'] = [self.tempdir.name]

# Developers for instrument libraries should update the following line
# to point to their own subpackage location, e.g.,
# self.inst_loc = mypackage.instruments
# Point to the Instrument subpackage location
self.inst_loc = pysatMadrigal.instruments
return

def teardown_class(self):
"""Runs after every method to clean up previous testing
"""
"""Clean up previous testing."""
pysat.params.data['data_dirs'] = self.saved_path
self.tempdir.cleanup()
del self.inst_loc, self.saved_path, self.tempdir
return
23 changes: 20 additions & 3 deletions pysatMadrigal/tests/test_methods_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,9 @@ def write_temp_files(self, nfiles=0):
size=(10, 10))

# Write the default Madrigal meta data
dat_dict['value'].units = 'test'
dat_dict['value'].description = 'test data set'
for dat_name in ['lat', 'lon', 'value']:
dat_dict[dat_name].units = 'deg'
dat_dict[dat_name].description = 'test data set'

self.temp_files.append(tfile)

Expand All @@ -446,11 +447,27 @@ def write_temp_files(self, nfiles=0):
def eval_dataset_meta_output(self):
"""Evaluate the dataset and meta output for the temp files."""

# Evaluate the Instrument data variables and coordinates
pysat.utils.testing.assert_lists_equal(
[ckey for ckey in self.data.coords.keys()], ['time', 'lat', 'lon'])
assert "value" in self.data.data_vars
assert self.data['time'].shape[0] == len(self.temp_files)
assert "value" in self.meta.keys()

# Evaluate the meta data
meta_keys = ['timestamps', 'lat', 'lon', 'value']
pysat.utils.testing.assert_lists_equal(
[ckey for ckey in self.meta.keys()], meta_keys)

for mkey in meta_keys:
uval = '' if mkey == 'timestamps' else 'deg'
dval = '' if mkey == 'timestamps' else 'test data set'
assert self.meta[mkey, self.meta.labels.units] == uval, \
"unexpected unit metadata for {:}: {:}".format(
repr(mkey), repr(self.meta[mkey, self.meta.labels.units]))
assert self.meta[mkey, self.meta.labels.desc] == dval, \
"unexpected unit metadata for {:}: {:}".format(
repr(mkey), repr(self.meta[mkey, self.meta.labels.desc]))

return

@pytest.mark.parametrize("nfiles", [1, 2, 3])
Expand Down

0 comments on commit 34957d4

Please sign in to comment.