Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions cf_xarray/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2303,7 +2303,7 @@ def get_associated_variable_names(
# Handles case where the coordinates attribute is None
# This is used to tell xarray to not write a coordinates attribute
if coordinates := attrs_or_encoding.get("coordinates", None):
coords["coordinates"] = coordinates.split(" ")
coords["coordinates"] = coordinates.split()

if "cell_measures" in attrs_or_encoding:
try:
Expand All @@ -2324,7 +2324,7 @@ def get_associated_variable_names(
if isinstance(self._obj, Dataset) and (
anc := attrs_or_encoding.get("ancillary_variables", None)
):
coords["ancillary_variables"] = anc.split(" ")
coords["ancillary_variables"] = anc.split()

if not skip_bounds:
if bounds := attrs_or_encoding.get("bounds", None):
Expand Down
25 changes: 25 additions & 0 deletions cf_xarray/tests/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,31 @@ def test_getitem_errors(obj):
obj2.cf["X"]


@pytest.mark.parametrize("attribute", ["coordinates", "ancillary_variables"])
@pytest.mark.parametrize("location", ["attrs", "encoding"])
@pytest.mark.parametrize(
"names", ["aux1 aux2", "aux1 aux2", " aux1 aux2", "aux1 aux2 ", " aux1 aux2 "]
)
def test_associated_variables_blank_separated(attribute, location, names):
# CF blank-separated lists allow repeated, leading and trailing spaces.
ds = xr.Dataset(
{"data": ("x", [1, 2])},
coords={"x": [0, 1], "aux1": ("x", [3, 4]), "aux2": ("x", [5, 6])},
)
getattr(ds["data"], location)[attribute] = names
original = ds.copy(deep=True)

with warnings.catch_warnings():
warnings.simplefilter("error")
associated = ds.cf.get_associated_variable_names("data")
actual = ds.cf["data"]

assert associated[attribute] == ["aux1", "aux2"]
assert_identical(actual, ds["data"])
assert_identical(ds, original)
assert getattr(ds["data"], location)[attribute] == names


def test_bad_cell_measures_attribute() -> None:
air2 = airds.copy(deep=False)
air2.air.attrs["cell_measures"] = "--OPT"
Expand Down