Skip to content

Commit ce1a732

Browse files
committed
Search for related variables in both attrs and encoding Mappings.
1 parent 11000bd commit ce1a732

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

cf_xarray/accessor.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -741,20 +741,25 @@ def __getitem__(self, key: Union[str, List[str]]):
741741
# 2. set measures variables as coordinates
742742
# 3. set ancillary variables as coordinates
743743
for name in varnames:
744-
attrs = self._obj[name].attrs
745-
if "coordinates" in attrs:
746-
coords.extend(attrs.get("coordinates").split(" "))
744+
attrs_or_encoding = ChainMap(
745+
self._obj[name].attrs, self._obj[name].encoding
746+
)
747+
if "coordinates" in attrs_or_encoding:
748+
coords.extend(attrs_or_encoding["coordinates"].split(" "))
747749

748-
if "cell_measures" in attrs:
750+
if "cell_measures" in attrs_or_encoding:
749751
measures = [
750752
_get_measure(self._obj[name], measure)
751753
for measure in _CELL_MEASURES
752-
if measure in attrs["cell_measures"]
754+
if measure in attrs_or_encoding["cell_measures"]
753755
]
754756
coords.extend(_strip_none_list(*measures))
755757

756-
if isinstance(self._obj, xr.Dataset) and "ancillary_variables" in attrs:
757-
anames = attrs["ancillary_variables"].split(" ")
758+
if (
759+
isinstance(self._obj, xr.Dataset)
760+
and "ancillary_variables" in attrs_or_encoding
761+
):
762+
anames = attrs_or_encoding["ancillary_variables"].split(" ")
758763
coords.extend(anames)
759764

760765
if isinstance(self._obj, xr.DataArray):

0 commit comments

Comments
 (0)