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

KeyError: 'lon_bnds' raised occasionally when opening (mostly NetCDF) datasets #930

Closed
forman opened this issue Feb 27, 2024 · 0 comments · Fixed by #929
Closed

KeyError: 'lon_bnds' raised occasionally when opening (mostly NetCDF) datasets #930

forman opened this issue Feb 27, 2024 · 0 comments · Fixed by #929
Assignees
Labels
bug Something isn't working

Comments

@forman
Copy link
Member

forman commented Feb 27, 2024

Describe the bug

We sometimes see KeyError: 'lon_bnds' raised when opening datasets, mostly NetCDF. Cause of the problem is that an (internal) xcube function get_dataset_bounds() expects bounds coordinates, e.g. lon_bnds, to always be in xarray.Dataset.coords. However, xarray often puts such coordinates in xarray.Dataset.data_vars. The name of bounds coordinates are usually given by the value of a coordinate attribute named bounds - according to CF conventions.

Expected behavior

  1. check if given bounds coordinate is contained in dataset
  2. get bounds coordinate from xarray.Dataset.variables, not xarray.Dataset.coords

Traceback

...
File ~/projects/xcube/xcube/core/store/descriptor.py:432, in _determine_bbox(data)
    429 def _determine_bbox(data: xr.Dataset) \
    430         -> Optional[Tuple[float, float, float, float]]:
    431     try:
--> 432         return get_dataset_bounds(data)
    433     except ValueError:
    434         if 'geospatial_lon_min' in data.attrs and \
    435                 'geospatial_lat_min' in data.attrs and \
    436                 'geospatial_lon_max' in data.attrs and \
    437                 'geospatial_lat_max' in data.attrs:

File ~/projects/xcube/xcube/core/geom.py:726, in get_dataset_bounds(dataset, xy_var_names)
    724 x_bnds_name = get_dataset_bounds_var_name(dataset, x_name)
    725 if x_bnds_name:
--> 726     x_bnds_var = dataset.coords[x_bnds_name]
    727     x1 = x_bnds_var[0, 0]
    728     x2 = x_bnds_var[0, 1]

File ~/mamba/envs/xcube/Lib/site-packages/xarray/core/coordinates.py:581, in DatasetCoordinates.__getitem__(self, key)
    579 def __getitem__(self, key: Hashable) -> DataArray:
    580     if key in self._data.data_vars:
--> 581         raise KeyError(key)
    582     return self._data[key]

KeyError: 'lon_bnds'
@forman forman added the bug Something isn't working label Feb 27, 2024
@forman forman self-assigned this Feb 27, 2024
forman added a commit that referenced this issue Feb 27, 2024
@forman forman mentioned this issue Feb 27, 2024
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant