Skip to content

Commit

Permalink
Bug fix to enable quality flags to be identified as level data. Inclu…
Browse files Browse the repository at this point in the history
…de tests in testing module for Temperature_IQUODflags.
  • Loading branch information
BecCowley committed Sep 1, 2023
1 parent df12c77 commit 12c6a4f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
30 changes: 29 additions & 1 deletion tests/netcdf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
'''
Expand Down Expand Up @@ -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]
Expand Down
18 changes: 15 additions & 3 deletions wodpy/wodnc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 12c6a4f

Please sign in to comment.