Skip to content

Commit

Permalink
Check compatibility of coordinate variable dimensions before assignme…
Browse files Browse the repository at this point in the history
…nt. (#484)

Closes #428
  • Loading branch information
dcherian authored Dec 5, 2023
1 parent a70fed0 commit 2a1e4e1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
11 changes: 7 additions & 4 deletions cf_xarray/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,14 @@ def check_results(names, key):

if scalar_key:
if len(allnames) == 1:
da: DataArray = ds.reset_coords()[allnames[0]] # type: ignore
if allnames[0] in coords:
coords.remove(allnames[0])
(name,) = allnames
da: DataArray = ds.reset_coords()[name] # type: ignore
if name in coords:
coords.remove(name)
for k1 in coords:
da.coords[k1] = ds.variables[k1]
var = ds.variables[k1]
if set(var.dims) <= set(da.dims):
da.coords[k1] = ds.variables[k1]
return da
else:
raise KeyError(
Expand Down
13 changes: 13 additions & 0 deletions cf_xarray/tests/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2036,3 +2036,16 @@ def test_sgrid():
"Y": {"jface", "jnode"},
"Z": {"kface", "knode"},
}


def test_ancillary_variables_extra_dim():
ds = xr.Dataset(
{
"x": (
"x",
range(10),
{"axis": "X", "ancillary_variables": "position_flag"},
),
}
)
assert_identical(ds.cf["X"], ds["x"])

0 comments on commit 2a1e4e1

Please sign in to comment.