Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion activestorage/active.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def hfix(x):
missing_value = ds.attrs.get('missing_value')
# see https://github.com/NCAS-CMS/PyActiveStorage/pull/303
if isinstance(missing_value, np.ndarray):
missing_value = missing_value[0]
if missing_value.size == 1:
missing_value = missing_value[0]
valid_min = hfix(ds.attrs.get('valid_min'))
valid_max = hfix(ds.attrs.get('valid_max'))
valid_range = hfix(ds.attrs.get('valid_range'))
Expand Down
10 changes: 8 additions & 2 deletions activestorage/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,16 @@ def mask_missing(data, missing):
fill_value, missing_value, valid_min, valid_max = missing

if fill_value is not None:
data = np.ma.masked_equal(data, fill_value)
if isinstance(fill_value, np.ndarray) or isinstance(fill_value, list):
data = np.ma.masked_where(data == fill_value, data)
else:
data = np.ma.masked_equal(data, fill_value)

if missing_value is not None:
data = np.ma.masked_equal(data, missing_value)
if isinstance(missing_value, np.ndarray) or isinstance(missing_value, list):
data = np.ma.masked_where(data == missing_value, data)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely this only works when missing_value broadcasts to data, which in general it wont.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well I'm running out of any valid numpy function that can mask with an array then 😁

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I got distracted by the news, how do you even apply a missing_value mask array if it's not broadcastable? I am really not familiar with anything missing value that's dim > 1 (or not scalar) - please suggest code, buds 🍺

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidhassell sorry - only just now that I refactored - given this is for storage.py, and that's just an emulator of Reductionist, I decided to pop a ValueError instead of going through with your albeit solid suggestion to go through the individual missing values - you reckon that'll do it? 🍺

else:
data = np.ma.masked_equal(data, missing_value)

if valid_max is not None:
data = np.ma.masked_greater(data, valid_max)
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@
import activestorage.storage as st


def test_mask_missing():
"""Test mask missing."""
missing_1 = ([-900.], np.array([-900.]), None, None)
missing_2 = ([-900., 33.], np.array([-900., 33.]), None, None)
data_1 = np.ma.array(
[[[-900., 33.], [33., -900], [33., 44.]]],
mask=False,
fill_value=-900.0,
dtype=float
)
data_2 = np.ma.array(
[[[-900., 33.], [33., -900], [33., 44.]]],
mask=False,
fill_value=[-900.0, 33.],
dtype=float
)
res_1 = st.mask_missing(data_1, missing_1)
print(res_1)
res_2 = st.mask_missing(data_2, missing_2)
print(res_2)


def test_reduce_chunk():
"""Test reduce chunk entirely."""
rfile = "tests/test_data/cesm2_native.nc"
Expand Down
Loading