Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harmonize FillValue and missing_value during encoding and decoding steps #3502

Merged
merged 7 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 9 additions & 2 deletions xarray/coding/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from ..core import dtypes, duck_array_ops, indexing
from ..core.pycompat import dask_array_type
from ..core.utils import equivalent
from ..core.variable import Variable


Expand Down Expand Up @@ -152,18 +151,26 @@ def encode(self, variable, name=None):
fv = encoding.get("_FillValue")
mv = encoding.get("missing_value")

if fv is not None and mv is not None and not equivalent(fv, mv):
if (
fv is not None
and mv is not None
and not duck_array_ops.allclose_or_equiv(fv, mv)
):
raise ValueError(
"Variable {!r} has multiple fill values {}. "
andersy005 marked this conversation as resolved.
Show resolved Hide resolved
"Cannot encode data. ".format(name, [fv, mv])
)

if fv is not None:
# Ensure _FillValue is cast to same dtype as data's
encoding["_FillValue"] = data.dtype.type(encoding["_FillValue"])
andersy005 marked this conversation as resolved.
Show resolved Hide resolved
fill_value = pop_to(encoding, attrs, "_FillValue", name=name)
if not pd.isnull(fill_value):
data = duck_array_ops.fillna(data, fill_value)

if mv is not None:
# Ensure missing_value is cast to same dtype as data's
encoding["missing_value"] = data.dtype.type(encoding["missing_value"])
andersy005 marked this conversation as resolved.
Show resolved Hide resolved
fill_value = pop_to(encoding, attrs, "missing_value", name=name)
if not pd.isnull(fill_value) and fv is None:
data = duck_array_ops.fillna(data, fill_value)
Expand Down
16 changes: 16 additions & 0 deletions xarray/tests/test_coding.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ def test_CFMaskCoder_decode():
assert_identical(expected, encoded)


def test_CFMaskCoder_encode_missing_fill_values_conflict():
original = xr.Variable(
("x",),
[0.0, -1.0, 1.0],
encoding={"_FillValue": np.float32(1e20), "missing_value": np.float64(1e20)},
)
coder = variables.CFMaskCoder()
encoded = coder.encode(original)

assert encoded.dtype == encoded.attrs["missing_value"].dtype
assert encoded.dtype == encoded.attrs["_FillValue"].dtype

roundtripped = coder.decode(coder.encode(original))
assert_identical(roundtripped, original)


def test_CFMaskCoder_missing_value():
expected = xr.DataArray(
np.array([[26915, 27755, -9999, 27705], [25595, -9999, 28315, -9999]]),
Expand Down