Skip to content

Commit c1f3355

Browse files
authored
Fix bug when coordinates is in both attrs and encoding (#95)
* Fix bug when coordinates is in both attrs and encoding * Cleaner code * Fixes
1 parent aa89d9c commit c1f3355

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

cf_xarray/accessor.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,19 @@ def _get_axis_coord(var: Union[DataArray, Dataset], key: str) -> List[str]:
297297
f"cf_xarray did not understand key {key!r}. Expected one of {valid_keys!r}"
298298
)
299299

300+
search_in = set()
300301
if "coordinates" in var.encoding:
301-
search_in = var.encoding["coordinates"].split(" ")
302-
elif "coordinates" in var.attrs:
303-
search_in = var.attrs["coordinates"].split(" ")
304-
else:
305-
search_in = list(var.coords)
302+
search_in.update(var.encoding["coordinates"].split(" "))
303+
if "coordinates" in var.attrs:
304+
search_in.update(var.attrs["coordinates"].split(" "))
305+
if not search_in:
306+
search_in = set(var.coords)
307+
308+
# maybe only do this for key in _AXIS_NAMES?
309+
search_in.update(var.indexes)
306310

307311
results: Set = set()
308-
for coord in itertools.chain(search_in, var.indexes):
312+
for coord in search_in:
309313
for criterion, valid_values in coordinate_criteria.items():
310314
if key in valid_values:
311315
expected = valid_values[key]

cf_xarray/tests/datasets.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,4 @@
9191
)
9292
romsds.coords["z_rho"] = ("s_rho", np.linspace(-100, 0, 10), {"positive": "up"})
9393
romsds["temp"] = ("s_rho", np.linspace(20, 30, 10), {"coordinates": "z_rho"})
94+
romsds["temp"].encoding["coordinates"] = "s_rho"

cf_xarray/tests/test_accessor.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import cf_xarray # noqa
1010

1111
from . import raise_if_dask_computes
12-
from .datasets import airds, anc, ds_no_attrs, multiple, popds, romsds
12+
from .datasets import airds, anc, ds_no_attrs, multiple, popds
1313

1414
mpl.use("Agg")
1515

@@ -463,5 +463,22 @@ def test_missing_variable_in_coordinates():
463463

464464

465465
def test_Z_vs_vertical_ROMS():
466+
from .datasets import romsds
467+
468+
assert_identical(romsds.s_rho.reset_coords(drop=True), romsds.temp.cf["Z"])
469+
assert_identical(romsds.z_rho.reset_coords(drop=True), romsds.temp.cf["vertical"])
470+
471+
romsds = romsds.copy(deep=True)
472+
473+
romsds.temp.attrs.clear()
474+
# look in encoding
475+
assert_identical(romsds.s_rho.reset_coords(drop=True), romsds.temp.cf["Z"])
476+
with pytest.raises(KeyError):
477+
# z_rho is not in .encoding["coordinates"]
478+
# so this won't work
479+
romsds.temp.cf["vertical"]
480+
481+
# use .coords if coordinates attribute is not available
482+
romsds.temp.encoding.clear()
466483
assert_identical(romsds.s_rho.reset_coords(drop=True), romsds.temp.cf["Z"])
467484
assert_identical(romsds.z_rho.reset_coords(drop=True), romsds.temp.cf["vertical"])

0 commit comments

Comments
 (0)