Replies: 2 comments 2 replies
-
You may want to review this section about units and Xarray with MetPy: Essentially your Xarray object has an See if that helps! |
Beta Was this translation helpful? Give feedback.
-
Yes thanks for the explanation and how to use it. So we are in essence chaining decorators. Might I ask then if it's possible to mention in the function comments of check_units in units.py that the decorator preprocess_and_wrap from xarray.py must be "chained" to this decorator in order to use it in the proper way ? One could argue that it's not always necessary to chain preprocess_and_wrap and check_units together and they need to be chained if and only if the arguments being passed to check_units are not an instance of Pint Quantity. |
Beta Was this translation helpful? Give feedback.
-
I have this toy example
`
from metpy.units import check_units
import os
import xarray as xr
import boto3
from botocore import UNSIGNED
from botocore.config import Config
@check_units(umask='[speed]')
def test_decorator(umask,b):
u = umask
def main():
if (not os.path.isfile('gfs.t12z.pgrb2.0p25.f000')):
main()`
When I run this I get this error -
Traceback (most recent call last): File "/home/winash12/testpython/testmetpydec.py", line 24, in <module> main() File "/home/winash12/testpython/testmetpydec.py", line 23, in main test_decorator(umask,b) File "/usr/local/lib/python3.9/dist-packages/metpy/units.py", line 332, in wrapper _check_units_inner_helper(func, sig, defaults, dims, *args, **kwargs) File "/usr/local/lib/python3.9/dist-packages/metpy/units.py", line 319, in _check_units_inner_helper raise ValueError(msg) ValueError:
test_decoratorgiven arguments with incorrect units:
umaskrequires "[speed]" but given "none" A xarray DataArray or numpy array
x` can be assigned a unit as follows:from metpy.units import units
x = x * units("m/s")
For more information see the Units Tutorial: https://unidata.github.io/MetPy/latest/tutorials/unit_tutorial.html
But the "umask` does have the units "m/s" as shown here .
`<xarray.DataArray 'u' (latitude: 721, longitude: 1440)> Size: 4MB
[1038240 values with dtype=float32]
Coordinates:
time datetime64[ns] 8B ...
step timedelta64[ns] 8B ...
isobaricInhPa float64 8B ...
valid_time datetime64[ns] 8B ...
Attributes: (12/29)
GRIB_paramId: 131
GRIB_dataType: fc
GRIB_numberOfPoints: 1038240
GRIB_typeOfLevel: isobaricInhPa
GRIB_stepUnits: 1
GRIB_stepType: instant
... ...
GRIB_name: U component of wind
GRIB_shortName: u
GRIB_units: m s**-1
long_name: U component of wind
units: m s**-1
standard_name: eastward_wind
Traceback (most recent call last):
File "/home/winash12/testpython/testmetpydec.py", line 26, in
main()
File "/home/winash12/testpython/testmetpydec.py", line 23, in main`
Beta Was this translation helpful? Give feedback.
All reactions