diff --git a/tests/netcdf_test.py b/tests/netcdf_test.py index d651c30..969819d 100644 --- a/tests/netcdf_test.py +++ b/tests/netcdf_test.py @@ -23,7 +23,7 @@ def test_longitude(classic1): ''' longitude = classic1.longitude() - assert round(longitude, 2) == -172.270, 'longitude should have been approx -172.270, instead read %f' % latitude + assert round(longitude, 2) == -172.270, 'longitude should have been approx -172.270, instead read %f' % longitude def test_uid(classic1): ''' @@ -110,6 +110,34 @@ def test_temperature(classic1): t = classic1.t() assert numpy.array_equal(t, truth), 'temperatures should have been [8.96, 8.95, 0.9, -1.23], instead read %s' % t.__str__() +def test_temperature_WODflags(classic1): + ''' + check WOD flags are not returned = [--, --, --, --] + ''' + + qc = classic1.t_level_qc('WOD') + assert numpy.alltrue(qc.mask) , \ + 'temperature_WODflags should not be in file, instead read %s' % qc.__str__() + + +def test_temperature_IQUODflags(classic1): + ''' + check IQUOD flags are returned = [0, 0, 0, 0] + ''' + + errors = [] + truth = [0, 0, 0, 0] + qc = classic1.t_level_qc('IQUOD') + # replace assertions by conditions + if numpy.alltrue(qc.mask): + errors.append("Temperature_IQUODflags not found in file") + if not numpy.array_equal(qc, truth): + errors.append('Temperature_IQUODflags should have been [0, 0, 0, 0], instead read %s' % qc.__str__()) + + # assert no error message has been registered, else print messages + assert not errors, "errors occured:\n{}".format("\n".join(errors)) + + def test_salinity(classic1): ''' check salinities == [30.900, 30.900, 31.910, 32.410] diff --git a/wodpy/wodnc.py b/wodpy/wodnc.py index 0a1b3ca..31c935b 100644 --- a/wodpy/wodnc.py +++ b/wodpy/wodnc.py @@ -36,7 +36,12 @@ def __init__(self, filename): ''' filename: name of netcdf file containing wod profiles ''' - self.rootgrp = Dataset(filename, "r", format="NETCDF4") + if isinstance(filename, str): + self.rootgrp = Dataset(filename, "r", format="NETCDF4") + else: + # if this is a file object from s3 bucket + nc_bytes = filename.read() + self.rootgrp = Dataset(f'inmemory.nc', 'r', format='NETCDF4', memory=nc_bytes) def ncasts(self): return self.rootgrp.dimensions['casts'].size @@ -97,6 +102,8 @@ def locate_in_ragged(self, v): ''' returns (offset, nentries) for variable v to extract it for this profile from the raggedarray. ''' + # trim variable v to ust include the variable name left of any underscore: + v = v.split('_')[0] offset = self.determine_offset(v+'_row_size') nentries = self.metadata(v+'_row_size') return offset, nentries @@ -127,11 +134,16 @@ def is_level_data(self, data_key): returns true if data_key looks like per level data ''' - if data_key + '_obs' in self.r.dimensions(): + if data_key not in self.r.rootgrp.variables.keys(): + return False + if len(self.r.rootgrp.variables[data_key].dimensions) == 0: + logging.warning(data_key + ' not level data.') + return False + if '_obs' in self.r.rootgrp.variables[data_key].dimensions[0]: # per level data should have a *_obs dimension return True else: - logging.warning(data_key + ' not found in this dataset.') + logging.warning(data_key + ' not level data.') return False def show_profile_metadata(self):