Skip to content

Commit

Permalink
Allow _FillValue and missing_value to differ (Fixes pydata#1749)
Browse files Browse the repository at this point in the history
The CF standard permits both values, and them to have different values,
so we should not be treating this as an error--just mask out all of
them.
  • Loading branch information
dopplershift committed Mar 26, 2018
1 parent 7c2c43c commit b31b3a8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
28 changes: 12 additions & 16 deletions xarray/coding/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,26 +152,22 @@ def encode(self, variable, name=None):
def decode(self, variable, name=None):
dims, data, attrs, encoding = unpack_for_decoding(variable)

raw_fill_values = []
if 'missing_value' in attrs:
# missing_value is deprecated, but we still want to support it as
# an alias for _FillValue.
if ('_FillValue' in attrs and
not utils.equivalent(attrs['_FillValue'],
attrs['missing_value'])):
raise ValueError("Conflicting _FillValue and missing_value "
"attrs on a variable {!r}: {} vs. {}\n\n"
"Consider opening the offending dataset "
"using decode_cf=False, correcting the "
"attrs and decoding explicitly using "
"xarray.decode_cf()."
.format(name, attrs['_FillValue'],
attrs['missing_value']))
attrs['_FillValue'] = attrs.pop('missing_value')
missing = pop_to(attrs, encoding, 'missing_value', name=name)
# Need to handle single and multiple values in 'missing_value'
try:
raw_fill_values.extend(missing)
except TypeError:
raw_fill_values.append(missing)

if '_FillValue' in attrs:
raw_fill_value = pop_to(attrs, encoding, '_FillValue', name=name)
raw_fill_values.append(pop_to(attrs, encoding, '_FillValue',
name=name))

if raw_fill_values:
encoded_fill_values = [
fv for fv in np.ravel(raw_fill_value) if not pd.isnull(fv)]
fv for fv in np.ravel(raw_fill_values) if not pd.isnull(fv)]

if len(encoded_fill_values) > 1:
warnings.warn("variable {!r} has multiple fill values {}, "
Expand Down
10 changes: 7 additions & 3 deletions xarray/tests/test_conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,16 @@ def test(self):


def test_decode_cf_with_conflicting_fill_missing_value():
var = Variable(['t'], np.arange(10),
expected = Variable(['t'], [np.nan, np.nan, 2], {'units': 'foobar'})
expected[[0, 1]] = np.nan
var = Variable(['t'], np.arange(3),
{'units': 'foobar',
'missing_value': 0,
'_FillValue': 1})
with raises_regex(ValueError, "_FillValue and missing_value"):
conventions.decode_cf_variable('t', var)
with warnings.catch_warnings(record=True) as w:
actual = conventions.decode_cf_variable('t', var)
assert_identical(actual, expected)
assert 'has multiple fill' in str(w[0].message)

expected = Variable(['t'], np.arange(10), {'units': 'foobar'})

Expand Down

0 comments on commit b31b3a8

Please sign in to comment.