Skip to content

Commit 04ebf64

Browse files
authored
Ignore bad cell_measures attributes (#294)
* Ignore bad cell_measures attributes CMIP6 likes the "--OPT" value. Closes #216 * Consolidate tests * fixes
1 parent f7c98f0 commit 04ebf64

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

cf_xarray/accessor.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,12 @@ def __repr__(self):
12301230

12311231
def make_text_section(subtitle, attr, valid_values, default_keys=None):
12321232

1233-
vardict = getattr(self, attr, {})
1233+
with warnings.catch_warnings():
1234+
warnings.simplefilter("ignore")
1235+
try:
1236+
vardict = getattr(self, attr, {})
1237+
except ValueError:
1238+
vardict = {}
12341239

12351240
star = " * "
12361241
tab = len(star) * " "
@@ -1398,8 +1403,15 @@ def cell_measures(self) -> dict[str, list[str]]:
13981403
]
13991404

14001405
keys = {}
1401-
for attr in all_attrs:
1402-
keys.update(parse_cell_methods_attr(attr))
1406+
for attr in set(all_attrs):
1407+
try:
1408+
keys.update(parse_cell_methods_attr(attr))
1409+
except ValueError:
1410+
warnings.warn(
1411+
f"Ignoring bad cell_measures attribute: {attr}.",
1412+
UserWarning,
1413+
stacklevel=2,
1414+
)
14031415
measures = {
14041416
key: self._drop_missing_variables(_get_all(self._obj, key)) for key in keys
14051417
}

cf_xarray/tests/test_accessor.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -608,21 +608,23 @@ def test_getitem_errors(obj):
608608
obj2.cf["X"]
609609

610610

611-
def test_getitem_ignores_bad_measure_attribute():
611+
def test_bad_cell_measures_attribute():
612612
air2 = airds.copy(deep=True)
613-
air2.air.attrs["cell_measures"] = "asd"
613+
air2.air.attrs["cell_measures"] = "--OPT"
614614
with pytest.warns(UserWarning):
615615
assert_identical(air2.air.drop_vars("cell_area"), air2.cf["air"])
616-
617-
with pytest.raises(ValueError):
618-
air2.cf.cell_measures
619-
with pytest.raises(ValueError):
620-
air2.air.cf.cell_measures
616+
with pytest.warns(UserWarning):
617+
assert air2.cf.cell_measures == {}
618+
with pytest.warns(UserWarning):
619+
assert air2.air.cf.cell_measures == {}
621620
with pytest.raises(ValueError):
622621
air2.cf.get_associated_variable_names("air", error=True)
623622
with pytest.warns(UserWarning):
624623
air2.cf.get_associated_variable_names("air", error=False)
625624

625+
# GH216
626+
repr(air2.cf)
627+
626628

627629
def test_getitem_clash_standard_name():
628630
ds = xr.Dataset()
@@ -1496,18 +1498,18 @@ def test_missing_variables():
14961498

14971499
# Bounds
14981500
ds = mollwds.copy(deep=True)
1499-
ds = ds.drop("lon_bounds")
1501+
ds = ds.drop_vars("lon_bounds")
15001502
assert ds.cf.bounds == {"lat": ["lat_bounds"], "latitude": ["lat_bounds"]}
15011503

15021504
with pytest.raises(KeyError, match=r"No results found for 'longitude'."):
15031505
ds.cf.get_bounds("longitude")
15041506

15051507
# Cell measures
15061508
ds = airds.copy(deep=True)
1507-
ds = ds.drop("cell_area")
1509+
ds = ds.drop_vars("cell_area")
15081510
assert ds.cf.cell_measures == {}
15091511

15101512
# Formula terms
15111513
ds = vert.copy(deep=True)
1512-
ds = ds.drop("ap")
1514+
ds = ds.drop_vars("ap")
15131515
assert ds.cf.formula_terms == {"lev": {"b": "b", "ps": "ps"}}

0 commit comments

Comments
 (0)