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

[Feature]: Improve error messages when attempting retrieve coords/coord bounds for an axis #260

Closed
tomvothecoder opened this issue Jun 21, 2022 · 1 comment · Fixed by #262
Assignees
Labels
type: enhancement New enhancement request

Comments

@tomvothecoder
Copy link
Collaborator

tomvothecoder commented Jun 21, 2022

Is your feature request related to a problem?

In PR #246, a new method called _get_coord_var() was added, which checks if the supplied axis key can be interpreted using the axis attribute:

xcdat/xcdat/axis.py

Lines 327 to 355 in 350ac8b

def _get_coord_var(dataset: xr.Dataset, axis: GenericAxis):
"""Gets a coordinate variable using its "axis" attribute.
Parameters
----------
dataset : xr.Dataset
The dataset.
axis : GenericAxis
The axis.
Returns
-------
xr.DataArray
The coordinate variable.
Raises
------
KeyError
If the coordinate variable was not found in the dataset.
"""
try:
coord_var = dataset.cf[axis]
except KeyError:
raise KeyError(
f"A '{axis}' coordinate variable was found in the dataset. Make sure "
"the coordinate variable exists in the Dataset, and its 'axis' attribute "
f"is set to '{axis}'."
)
return coord_var

However, when attempting to retrieve coord bounds, we raise an ambiguous error message if they aren't found:

xcdat/xcdat/bounds.py

Lines 179 to 183 in 350ac8b

try:
axis = GENERIC_AXIS_MAP[axis]
bounds = self._dataset.cf.get_bounds(axis)
except KeyError:
raise KeyError(f"{axis} bounds were not found, they must be added.")

Describe the solution you'd like

There are several reasons for why coord bounds can't be retrieved (#164 (comment)):

I plan on opening a separate PR to improve error messages when attempting to fetch coordinate variables or bounds data variables using cf_xarray and CF-compliant "axis" and/or "standard_name" attributes.

This KeyError is possibly being raised because:

  1. There is no Z coordinate variable
  2. There is a Z coordinate variable, but the "axis" attribute is not set to "Z"
  3. There is a Z coordinate variable and the "axis" attribute is set to "Z", but the "bounds" attribute is not set to the name of the bounds data variable
  4. The bounds data var doesn't exist and need to be added.

We need a clearer error message based on which case is true.

Describe alternatives you've considered

No response

Additional context

No response

@tomvothecoder tomvothecoder added the type: enhancement New enhancement request label Jun 21, 2022
@tomvothecoder tomvothecoder self-assigned this Jun 21, 2022
@durack1
Copy link
Collaborator

durack1 commented Jun 21, 2022

@tomvothecoder FYI, the upcoming Python 3.11 release (October 2022) plans to include "Fine-Grained Error Locations in Tracebacks" which looks like a great improvement over 3.10 and earlier. The Exception Groups also looks like a great optimization.

Might be useful to ponder as you're working on this feature

See https://www.python.org/downloads/release/python-3110b3/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New enhancement request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants