From ed4000e085b33b7936e577583023b366a8a0f366 Mon Sep 17 00:00:00 2001 From: sol1105 Date: Mon, 15 Aug 2022 17:41:04 +0200 Subject: [PATCH 1/3] Accounting for NoneType coordinates in attrs/encoding --- cf_xarray/accessor.py | 6 +++--- cf_xarray/tests/test_accessor.py | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cf_xarray/accessor.py b/cf_xarray/accessor.py index d4f17048..ad968208 100644 --- a/cf_xarray/accessor.py +++ b/cf_xarray/accessor.py @@ -254,9 +254,9 @@ def _get_axis_coord(obj: DataArray | Dataset, key: str) -> list[str]: ) search_in = set() - if "coordinates" in obj.encoding: + if "coordinates" in obj.encoding and obj.encoding["coordinates"]: search_in.update(obj.encoding["coordinates"].split(" ")) - if "coordinates" in obj.attrs: + if "coordinates" in obj.attrs and obj.attrs["coordinates"]: search_in.update(obj.attrs["coordinates"].split(" ")) if not search_in: search_in = set(obj.coords) @@ -1596,7 +1596,7 @@ 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: + if "coordinates" in attrs_or_encoding and attrs_or_encoding["coordinates"]: coords["coordinates"] = attrs_or_encoding["coordinates"].split(" ") if "cell_measures" in attrs_or_encoding: diff --git a/cf_xarray/tests/test_accessor.py b/cf_xarray/tests/test_accessor.py index 459ccbf0..ec3cda00 100644 --- a/cf_xarray/tests/test_accessor.py +++ b/cf_xarray/tests/test_accessor.py @@ -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"] From ec7b470b3aaf06eede838a47f0f5a7c5d3531ccf Mon Sep 17 00:00:00 2001 From: sol1105 Date: Mon, 15 Aug 2022 18:36:23 +0200 Subject: [PATCH 2/3] Added comment about NoneType coordinates attribute --- cf_xarray/accessor.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/cf_xarray/accessor.py b/cf_xarray/accessor.py index ad968208..b9c4f551 100644 --- a/cf_xarray/accessor.py +++ b/cf_xarray/accessor.py @@ -254,10 +254,14 @@ def _get_axis_coord(obj: DataArray | Dataset, key: str) -> list[str]: ) search_in = set() - if "coordinates" in obj.encoding and obj.encoding["coordinates"]: - search_in.update(obj.encoding["coordinates"].split(" ")) - if "coordinates" in obj.attrs and obj.attrs["coordinates"]: - search_in.update(obj.attrs["coordinates"].split(" ")) + coordinates = obj.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(" ")) + coordinates = obj.attrs.get("coordinates", None) + if coordinates: + search_in.update(coordinates.split(" ")) if not search_in: search_in = set(obj.coords) @@ -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 and attrs_or_encoding["coordinates"]: - 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: From 7bbb603912f59262d836f6bbafeb59c64fbe3f58 Mon Sep 17 00:00:00 2001 From: sol1105 Date: Mon, 15 Aug 2022 19:25:29 +0200 Subject: [PATCH 3/3] Replaced set by ChainMap in accessor._get_axis_coord --- cf_xarray/accessor.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cf_xarray/accessor.py b/cf_xarray/accessor.py index b9c4f551..a82cc512 100644 --- a/cf_xarray/accessor.py +++ b/cf_xarray/accessor.py @@ -254,12 +254,10 @@ def _get_axis_coord(obj: DataArray | Dataset, key: str) -> list[str]: ) search_in = set() - coordinates = obj.encoding.get("coordinates", None) + 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(" ")) - coordinates = obj.attrs.get("coordinates", None) if coordinates: search_in.update(coordinates.split(" ")) if not search_in: