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

Can parse_cf routine skip nondimensional variable instead of giving an error? #1092

Closed
hdsingh opened this issue Jul 14, 2019 · 1 comment · Fixed by #1116
Closed

Can parse_cf routine skip nondimensional variable instead of giving an error? #1092

hdsingh opened this issue Jul 14, 2019 · 1 comment · Fixed by #1116
Labels
Area: Units Pertains to unit information Area: Xarray Pertains to xarray integration Type: Enhancement Enhancement to existing functionality
Milestone

Comments

@hdsingh
Copy link

hdsingh commented Jul 14, 2019

metpy.parse_cf gives error when nondimensional variable is encountered. For example:

import metpy.calc as mpcalc
url = 'http://opendap.co-ops.nos.noaa.gov/thredds/dodsC/NOAA/LOOFS/MODELS/201903/glofs.loofs.fields.forecast.20190320.t06z.nc'
data = xr.open_dataset(url)
data = data.set_coords(['lat', 'lon','mask'])

parsed_var = data.metpy.parse_cf('temp')
x, y = parsed_var.metpy.coordinates('x', 'y')
[coord.name for coord in (x, y)]

This gives error UndefinedUnitError: 'nondimensional' is not defined in the unit registry .

This happens when mask is encountered for this example. However on adding data['mask'].attrs['units'] = 1 before parsing solves the problem. Also it runs fine if mask is not present as coordinate.

Expected output:
If a user picks a variable with an unknown CF unit (like nondimensional), shouldn't the cf_parse routine just skip that variable instead of throwing an error?

  • Python 3.7.3
  • metpy version: 0.10.0
@hdsingh hdsingh changed the title Can cf_parse routine skip nondimensional variable instead of giving an error? Can parse_cf routine skip nondimensional variable instead of giving an error? Jul 14, 2019
@jthielen
Copy link
Collaborator

This is a good catch! Right now, check_axis is not resilient to units that pint does not understand, so the following should get wrapped in a try/except block that just passes when a UndefinedUnitError is raised:

MetPy/metpy/xarray.py

Lines 429 to 440 in b1e68bc

# Check for units, either by dimensionality or name
if (axis in coordinate_criteria['units'] and (
(
coordinate_criteria['units'][axis]['match'] == 'dimensionality'
and (units.get_dimensionality(var.attrs.get('units'))
== units.get_dimensionality(
coordinate_criteria['units'][axis]['units']))
) or (
coordinate_criteria['units'][axis]['match'] == 'name'
and var.attrs.get('units') in coordinate_criteria['units'][axis]['units']
))):
return True

(Note that on the master branch and in the upcoming 0.11 release, the coordinate identification no longer requires the parse_cf call; the implementation was refactored in #1058).

@dopplershift dopplershift added Area: Units Pertains to unit information Area: Xarray Pertains to xarray integration Type: Enhancement Enhancement to existing functionality labels Jul 17, 2019
@dopplershift dopplershift added this to the 0.11 milestone Jul 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Units Pertains to unit information Area: Xarray Pertains to xarray integration Type: Enhancement Enhancement to existing functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants