Skip to content
Merged
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
17 changes: 11 additions & 6 deletions cf_xarray/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,12 @@ def _get_axis_coord(obj: DataArray | Dataset, key: str) -> list[str]:
)

search_in = set()
if "coordinates" in obj.encoding:
search_in.update(obj.encoding["coordinates"].split(" "))
if "coordinates" in obj.attrs:
search_in.update(obj.attrs["coordinates"].split(" "))
attrs_or_encoding = ChainMap(obj.attrs, obj.encoding)
coordinates = attrs_or_encoding.get("coordinates", None)
# Handles case where the coordinates attribute is None
# This is used to tell xarray to not write a coordinates attribute
if coordinates:
search_in.update(coordinates.split(" "))
if not search_in:
search_in = set(obj.coords)

Expand Down Expand Up @@ -1596,8 +1598,11 @@ def get_associated_variable_names(
coords: dict[str, list[str]] = {k: [] for k in keys}
attrs_or_encoding = ChainMap(self._obj[name].attrs, self._obj[name].encoding)

if "coordinates" in attrs_or_encoding:
coords["coordinates"] = attrs_or_encoding["coordinates"].split(" ")
coordinates = attrs_or_encoding.get("coordinates", None)
# Handles case where the coordinates attribute is None
# This is used to tell xarray to not write a coordinates attribute
if coordinates:
coords["coordinates"] = coordinates.split(" ")

if "cell_measures" in attrs_or_encoding:
try:
Expand Down
8 changes: 8 additions & 0 deletions cf_xarray/tests/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,14 @@ def test_accessor_getattr_and_describe():
assert str(ds_verta.cf) == str(ds_vertb.cf)


def test_accessor_getattr_coordinate_Nonetype():
ds_vert = vert
ds_vert["o3"].encoding["coordinates"] = None
assert ds_vert.o3.cf["latitude"].name == "lat"
ds_vert["lat"].encoding["coordinates"] = None
assert ds_vert.cf["latitude"].name == "lat"


def test_getitem_standard_name():
actual = airds.cf["air_temperature"]
expected = airds["air"]
Expand Down