Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accounting for NoneType coordinates in attrs/encoding #359

Merged
merged 3 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 13 additions & 6 deletions cf_xarray/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,14 @@ 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(" "))
coordinates = obj.encoding.get("coordinates", None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
coordinates = obj.encoding.get("coordinates", None)
attrs_or_encoding = ChainMap(self._obj[name].attrs, self._obj[name].encoding)
coordinates = attrs_or_encoding.get("coordinates", None)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not sure if it was intentional that here a set of attrs["coordinates"] and encoding["coordinates"] is being constructed and not the ChainMap operator is being used (which respects then only the first mention of "coordinates" in attrs and encoding).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice catch. I think we should treat coordinates in both attrs and encoding as a bug perhaps.

# 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(" "))
coordinates = obj.attrs.get("coordinates", None)
if coordinates:
search_in.update(coordinates.split(" "))
dcherian marked this conversation as resolved.
Show resolved Hide resolved
if not search_in:
search_in = set(obj.coords)

Expand Down Expand Up @@ -1596,8 +1600,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