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

grid mappings not propagated causes accessor failures #448

Closed
larsbuntemeyer opened this issue Jun 5, 2023 · 1 comment · Fixed by #458
Closed

grid mappings not propagated causes accessor failures #448

larsbuntemeyer opened this issue Jun 5, 2023 · 1 comment · Fixed by #458
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@larsbuntemeyer
Copy link
Contributor

larsbuntemeyer commented Jun 5, 2023

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...

@dcherian
Copy link
Contributor

dcherian commented Jun 5, 2023

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.")

@dcherian dcherian added the bug Something isn't working label Jun 5, 2023
@dcherian dcherian added the help wanted Extra attention is needed label Jul 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants