Unit/Pint Troubles #1641
-
Hello! I have been trying to plot frontogenesis using GFS data pulled from NOMADS: ds = xr.open_dataset(url)
init_hr = dt.datetime(int(year),int(month),int(day),int(init_hour))
times = ds['tmp2m'].metpy.time
init_time = ds['time'][0]
lats = np.arange(15,70,0.25)
lons = np.arange(220,310,0.25)
forecast_hour = times[0].values
data = ds.metpy.parse_cf()
data = data.isel(time=6)
#Rename variables to useful things
data = data.rename({
'absvprs':'avort',
'hgtprs':'gph',
'rhprs':'rh',
'tmpprs':'temp',
'ugrdprs':'u',
'vgrdprs': 'v',
})
vertical, = data['temp'].metpy.coordinates('vertical')
time = data['temp'].metpy.time
zH5_crs = data['temp'].metpy.cartopy_crs
### Pull dataarrays for each parameter of interest
t7 = data['temp'].sel(lev=700.0,lat=slice(15, 70),lon=slice(220, 310))
u7 = data['u'].sel(lev=700.0,lat=slice(15, 70),lon=slice(220, 310)).squeeze()*1.94384449
v7 = data['v'].sel(lev=700.0,lat=slice(15, 70),lon=slice(220, 310)).squeeze()*1.94384449
h7 = data['gph'].sel(lev=700.0,lat=slice(15, 70),lon=slice(220, 310)).squeeze()
x, y = t7.metpy.coordinates('x', 'y')
dx, dy = mpcalc.grid_deltas_from_dataarray(t7)
lat, lon = xr.broadcast(y, x)
wind_slice = slice(5,-5,5)
t7 = t7 *units.K
u7 = u7*units.knots
v7 = v7*units.knots
h7 = h7*units.m
h7_fgen = mpcalc.frontogenesis(t7,u7,v7,dx,dy) despite explicitly tagging each array with units, I am still getting the following error: I am currently running: I tried printing out one of these arrays to see if the unit attribution actually worked, and it seemed to look good: Any ideas what might be causing the trouble and how I might get metpy to see the units? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 11 replies
-
In this most recent example, it looks like there are issues with using xarray DataArrays with Quantity arrays inside them (what is created when multiplying by DataArrays by units). While this a well-supported data structure in MetPy v1.0 and later, it can cause issues with earlier MetPy versions. Instead, for MetPy v0.12.1, we would need units to be on the xarray 'units' attribute. And so, I'd recommend seeing if this adaptation resolves your issue: ds = xr.open_dataset(url)
init_hr = dt.datetime(int(year),int(month),int(day),int(init_hour))
times = ds['tmp2m'].metpy.time
init_time = ds['time'][0]
lats = np.arange(15,70,0.25)
lons = np.arange(220,310,0.25)
forecast_hour = times[0].values
data = ds.metpy.parse_cf()
data = data.isel(time=6)
#Rename variables to useful things
data = data.rename({
'absvprs':'avort',
'hgtprs':'gph',
'rhprs':'rh',
'tmpprs':'temp',
'ugrdprs':'u',
'vgrdprs': 'v',
})
vertical, = data['temp'].metpy.coordinates('vertical')
time = data['temp'].metpy.time
zH5_crs = data['temp'].metpy.cartopy_crs
### Pull dataarrays for each parameter of interest
t7 = data['temp'].sel(lev=700.0,lat=slice(15, 70),lon=slice(220, 310))
u7 = data['u'].sel(lev=700.0,lat=slice(15, 70),lon=slice(220, 310)).squeeze()*1.94384449
v7 = data['v'].sel(lev=700.0,lat=slice(15, 70),lon=slice(220, 310)).squeeze()*1.94384449
h7 = data['gph'].sel(lev=700.0,lat=slice(15, 70),lon=slice(220, 310)).squeeze()
x, y = t7.metpy.coordinates('x', 'y')
dx, dy = mpcalc.grid_deltas_from_dataarray(t7)
lat, lon = xr.broadcast(y, x)
wind_slice = slice(5,-5,5)
t7.attrs['units'] = 'K'
u7.attrs['units'] = v7.attrs['units'] = 'knots'
h7.attrs['units'] = 'm'
h7_fgen = mpcalc.frontogenesis(t7,u7,v7,dx,dy) |
Beta Was this translation helpful? Give feedback.
-
Since you're using 0.12.1, try this:
I'm not sure 0.12.1 can handle a pint quantity wrapped by a DataArray but I think 1.0 can. By using |
Beta Was this translation helpful? Give feedback.
Since you're using 0.12.1, try this:
h7_fgen = mpcalc.frontogenesis(t7.values,u7.values,v7.values,dx,dy)
I'm not sure 0.12.1 can handle a pint quantity wrapped by a DataArray but I think 1.0 can. By using
values
, you'll pull out just the pint quantity object from the DataArray object forfrontogenesis()
to work with.