Reputation: 35
I'm attempting to follow this training example to calculate QG omega on NCEP/NCAR data but I'm getting hung up on mpcalc.absolute_vorticity.
import xarray as xr
import metpy.calc as mc
import metpy.constants as mpconstants
from metpy.units import units
import numpy as np
path='./'
uf = 'uwnd.2018.nc'
vf = 'vwnd.2018.nc'
af = 'air.2018.nc'
ads = xr.open_dataset(path+af).metpy.parse_cf()
uds = xr.open_dataset(path+uf).metpy.parse_cf()
vds = xr.open_dataset(path+vf).metpy.parse_cf()
a700 = ads['air'].metpy.sel(
level=700 * units.hPa,
time='2018-01-04T12')
u700 = uds['uwnd'].metpy.sel(
level=700 * units.hPa,
time='2018-01-04T12')
v700 = vds['vwnd'].metpy.sel(
level=700 * units.hPa,
time='2018-01-04T12')
lats = ads['lat'].metpy.unit_array
lons = ads['lon'].metpy.unit_array
X, Y = np.meshgrid(lons,lats)
dx, dy = mc.lat_lon_grid_deltas(lons,lats)
avort = mc.absolute_vorticity(u700,v700,dx,dy,lats)
I get the error, "ValueError: operands could not be broadcast together with shapes (73,144) (73,) ".
Full error traceback below:
Traceback (most recent call last):
File "metpy.decomp.py", line 43, in <module>
avort = mc.absolute_vorticity(u700,v700,dx,dy,lats)
File "/work1/jsa/anconda3/envs/earth/lib/python3.7/site-packages/metpy/xarray.py", line 570, in wrapper
return func(*args, **kwargs)
File "/work1/jsa/anconda3/envs/earth/lib/python3.7/site-packages/metpy/units.py", line 312, in wrapper
return func(*args, **kwargs)
File "/work1/jsa/anconda3/envs/earth/lib/python3.7/site-packages/metpy/calc/kinematics.py", line 639, in absolute_vorticity
return relative_vorticity + f
File "/work1/jsa/anconda3/envs/earth/lib/python3.7/site-packages/pint/quantity.py", line 754, in __add__
return self._add_sub(other, operator.add)
File "/work1/jsa/anconda3/envs/earth/lib/python3.7/site-packages/pint/quantity.py", line 75, in wrapped
result = f(self, *args, **kwargs)
File "/work1/jsa/anconda3/envs/earth/lib/python3.7/site-packages/pint/quantity.py", line 686, in _add_sub
magnitude = op(self._magnitude, other._magnitude)
ValueError: operands could not be broadcast together with shapes (73,144) (73,)
Upvotes: 2
Views: 298
Reputation: 5843
Without access to the data or the full error, my guess is the problem is that lats doesn't have the right shape to obey NumPy's broadcasting rules. Try changing to:
avort = mc.absolute_vorticity(u700, v700, dx, dy, lats[:, None])
Essentially, the trailing dimensions need to line up. Before, NumPy was trying to line up 73 (lats) with the lons dimension (144). Adding the [:, None]
creates a size-1 dimension as the last dimension for lats, and then the alignment is trying to match shape (73, 1) with (73, 144), which works.
Upvotes: 1