Skip to content

Commit

Permalink
Merge pull request #97 from pacificclimate/fix-fill-value
Browse files Browse the repository at this point in the history
Fix fill value
  • Loading branch information
eyvorchuk authored Aug 30, 2023
2 parents 23878b4 + b0edd75 commit f033504
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 7 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/python-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: [3.6, 3.7, 3.8]
python-version:
- "3.8"
- "3.9"
- "3.10"
- "3.11"

steps:
- uses: actions/checkout@v2
Expand Down
8 changes: 5 additions & 3 deletions nchelpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class CFDataset(Dataset):
Some of this class replaces the functionality of helper functions defined
in pacificclimate/modelmeta. The following list maps those functions to
\properties/methods of this class.
properties/methods of this class.
get_file_metadata -> metadata.<global attribute>
- <global attribute> is the unified name for actual CF standard global
Expand Down Expand Up @@ -1047,7 +1047,7 @@ def dependent_varnames(self, dim_names=set()):
"""
if isinstance(dim_names, six.string_types):
dim_names = {dim_names}
elif isinstance(dim_names, collections.Iterable):
elif isinstance(dim_names, collections.abc.Iterable):
dim_names = {d for d in dim_names if isinstance(d, six.string_types)}
else:
raise ValueError(
Expand Down Expand Up @@ -1110,14 +1110,16 @@ def var_range(self, var_name, chunksize=2 ** 20):
:param var_name: (str) name of variable
:return (tuple) (min, max) minimum and maximum values
"""
# TODO: What about fill values?
# Temporarily mask fill values
self.set_auto_mask(True)
variable = self.variables[var_name]
range_min = float("inf")
range_max = float("-inf")
chunk_shape = opt_chunk_shape(variable.shape, chunksize)
for chunk in chunks(variable, chunk_shape):
range_min = min(range_min, np.nanmin(chunk))
range_max = max(range_max, np.nanmax(chunk))
self.set_auto_mask(False)
return range_min, range_max

###########################################################################
Expand Down
Binary file added nchelpers/data/tiny_gridded_obs_missing_data.nc
Binary file not shown.
2 changes: 1 addition & 1 deletion nchelpers/date_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def to_datetime(value):
"""
# TODO: Convert time values in case of 360_day calendar?
# See https://github.com/pacificclimate/modelmeta/blob/master/db/index_netcdf.r#L468-L479
if isinstance(value, collections.Iterable):
if isinstance(value, collections.abc.Iterable):
return (to_datetime(v) for v in value)

if isinstance(value, (datetime, date)):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_CFDataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,15 @@ def test_get_var_bounds_and_values(tiny_dataset, var_name):
def test_variable_range(tiny_dataset, var_name, expected):
assert tiny_dataset.var_range(var_name, chunksize=2) == approx(expected)

@mark.slow
@mark.parametrize('dataset, var_name, fill_value, expected', [
('prism_pr_small', 'pr', -9999., (45.703, 7346.324)),
('tiny_gridded_obs_missing_data', 'tasmax', -32768., (-28.9, -18.18)),
], indirect=['dataset'])
def test_variable_range_with_missing_data(dataset, var_name, fill_value, expected):
assert dataset.variables[var_name]._FillValue == fill_value
assert fill_value in dataset.variables[var_name][:]
assert dataset.var_range(var_name, chunksize=2) == approx(expected)

@mark.parametrize('tiny_dataset, expected', [
('gcm', {'time', 'lon', 'lat', 'nb2'}),
Expand Down
4 changes: 2 additions & 2 deletions tests/test_date_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_jday_360_to_remapped_date(jday_360, month, day):
(num2date(60, 'days since 1999-12-30', '360_day'), datetime(2000, 3, 1)),
])
def test_to_datetime(arg, expected):
if isinstance(arg, collections.Iterable):
if isinstance(arg, collections.abc.Iterable):
assert list(to_datetime(arg)) == list(expected)
else:
assert to_datetime(arg) == expected
Expand Down Expand Up @@ -95,4 +95,4 @@ def test_to_datetime_360(jday_360, month, day):
(datetime(2012, 5, 1), "seasonal", datetime(2012, 3, 1))
])
def test_truncate_to_resolution(date, resolution, expected):
assert(truncate_to_resolution(date, resolution)) == expected
assert(truncate_to_resolution(date, resolution)) == expected

0 comments on commit f033504

Please sign in to comment.