How deal with the UndefinedUnitError´╝č

Tags: , ,



I downloaded data from noaa and i wanted to calculate vertical velocity using the function vertical_velocity=metpy.calcmpcalc.vertical_velocity(omega,pressure,temperature). But something wrong when i dealing with the units of varibles.

import xarray as xr
import metpy.calc as mpcalc
omega=xr.open_dataset('D:\data_english\jwk\omega.mon.mean.nc')

temperature=xr.open_dataset('D:\data_english\jwk\air.mon.mean.nc')

height=xr.open_dataset('D:\data_english\jwk\hgt.mon.mean.nc')

pressure=mpcalc.height_to_pressure_std(height['hgt'])

verticalwind=mpcalc.vertical_velocity(omega['omega'], pressure, temperature['air'])


Traceback (most recent call last):
  File "<ipython-input-194-da22b63a1943>", line 1, in <module>
    verticalwind=mpcalc.vertical_velocity(omega['omega'], pressure, temperature['air'])
  File "D:anacondalibsite-packagesmetpyxarray.py", line 1199, in wrapper
    _mutate_arguments(bound_args, xr.DataArray, lambda arg, _: arg.metpy.unit_array)
  File "D:anacondalibsite-packagesmetpyxarray.py", line 1233, in _mutate_arguments
    bound_args.arguments[arg_name] = mutate_arg(arg_val, arg_name)
  File "D:anacondalibsite-packagesmetpyxarray.py", line 1199, in <lambda>
    _mutate_arguments(bound_args, xr.DataArray, lambda arg, _: arg.metpy.unit_array)
  File "D:anacondalibsite-packagesmetpyxarray.py", line 157, in unit_array
    return units.Quantity(self._data_array.data, self.units)
  File "D:anacondalibsite-packagesmetpyxarray.py", line 134, in units
    return units.parse_units(self._data_array.attrs.get('units', 'dimensionless'))
  File "D:anacondalibsite-packagespintregistry.py", line 1084, in parse_units
    units = self._parse_units(input_string, as_delta)
  File "D:anacondalibsite-packagespintregistry.py", line 1298, in _parse_units
    return super()._parse_units(input_string, as_delta)
  File "D:anacondalibsite-packagespintregistry.py", line 1112, in _parse_units
    cname = self.get_name(name)
  File "D:anacondalibsite-packagespintregistry.py", line 636, in get_name
    raise UndefinedUnitError(name_or_alias)
UndefinedUnitError: 'Pascal' is not defined in the unit registry

**The units of omega, height and temperature are ‘Pascal/s’, ‘m’ and ‘degC’, repectively. The varible pressure was calculate through the function mpcalc.height_to_pressure_std, and this function didn’t give the unit of pressure. But the values of pressure range from 1000 to 0, so i think its unit is ‘hpa’.

The error reported that “‘Pascal’ is not defined in the unit registry”. Maybe ‘Pascal/s’ is not the default unit of omega? But how can i know which units are defined in the unit registry ? Can anyone help me? Thanks!**

Answer

This is a problem where the unit library MetPy uses (Pint) does not have the same rules about capitalization/case sensitivity as the UDUnits format used by the netCDF Climate and Forecasting Conventions for metadata. Fixing this is on MetPy’s todo list, but some roadblocks have been encountered.

The work-around right now is to change your units to something that Pint understands, like:

omega['omega'].attrs['units'] = 'pascal / s'


Source: stackoverflow