xarray-contrib/cf-xarray

grid mappings not propagated causes accessor failures

Closed this issue · 1 comments

I am not sure if this is a bug or if it should work. For example, this works (propagates the grid mapping variable correctly):

import xarray as xr
from cf_xarray.datasets import rotds

rotds.cf['temp'].cf['X']

while this does not:

rotds.temp.cf['X']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File /srv/conda/envs/notebook/lib/python3.10/site-packages/xarray/core/dataarray.py:807, in DataArray._getitem_coord(self, key)
    806 try:
--> 807     var = self._coords[key]
    808 except KeyError:

KeyError: 'rotated_pole'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
Cell In[11], line 4
      1 import xarray as xr
      2 from cf_xarray.datasets import rotds
----> 4 rotds.temp.cf['X']

File /srv/conda/envs/notebook/lib/python3.10/site-packages/cf_xarray/accessor.py:2754, in CFDataArrayAccessor.__getitem__(self, key)
   2749 if not isinstance(key, Hashable):
   2750     raise KeyError(
   2751         f"Cannot use an Iterable of keys with DataArrays. Expected a single string. Received {key!r} instead."
   2752     )
-> 2754 return _getitem(self, key)

File /srv/conda/envs/notebook/lib/python3.10/site-packages/cf_xarray/accessor.py:784, in _getitem(accessor, key, skip)
    782 else:
    783     try:
--> 784         grid_mapping_names = [accessor.grid_mapping_name]
    785     except ValueError:
    786         grid_mapping_names = []

File /srv/conda/envs/notebook/lib/python3.10/site-packages/cf_xarray/accessor.py:2712, in CFDataArrayAccessor.grid_mapping_name(self)
   2709 if not grid_mapping:
   2710     raise ValueError("No 'grid_mapping' attribute present.")
-> 2712 grid_mapping_var = da[grid_mapping]
   2713 return grid_mapping_var.attrs["grid_mapping_name"]

File /srv/conda/envs/notebook/lib/python3.10/site-packages/xarray/core/dataarray.py:816, in DataArray.__getitem__(self, key)
    814 def __getitem__(self: T_DataArray, key: Any) -> T_DataArray:
    815     if isinstance(key, str):
--> 816         return self._getitem_coord(key)
    817     else:
    818         # xarray-style array indexing
    819         return self.isel(indexers=self._item_key_to_dict(key))

File /srv/conda/envs/notebook/lib/python3.10/site-packages/xarray/core/dataarray.py:810, in DataArray._getitem_coord(self, key)
    808 except KeyError:
    809     dim_sizes = dict(zip(self.dims, self.shape))
--> 810     _, key, var = _get_virtual_variable(self._coords, key, dim_sizes)
    812 return self._replace_maybe_drop_dims(var, name=key)

File /srv/conda/envs/notebook/lib/python3.10/site-packages/xarray/core/dataset.py:178, in _get_virtual_variable(variables, key, dim_sizes)
    176 split_key = key.split(".", 1)
    177 if len(split_key) != 2:
--> 178     raise KeyError(key)
    180 ref_name, var_name = split_key
    181 ref_var = variables[ref_name]

KeyError: 'rotated_pole'

Could be related to #357 ? I think it should work since not propagating the grid mapping should not break accessing other coordinates...

I'll try to have a closer look...

grid_mapping_var = da[grid_mapping]

Let's change this to (something like this)

if grid_mapping not in da._coords:
	raise ValueError("Grid Mapping variable {grid_mapping} not present.")