2

I'm trying to plot a 500 hPa vorticity and keep running into a valueerror and haven't been able to figure it out. Have tried several ideas, including ones from my instructor, and still hasn't worked. My code isn't finished, trying to figure out the error before I finish the plotting. Does anyone have any ideas? Thank you for any help!!!

This is what I'm getting:

ValueError                                Traceback (most recent call last)
<ipython-input-7-7aee4011a39a> in <module>
      4 f = mpcalc.coriolis_parameter(np.deg2rad(lats)).to(units('1/sec'))
      5 
----> 6 avor = mpcalc.absolute_vorticity(uwnd_500, vwnd_500, dx=dx, dy=dy, x_dim=- 1, y_dim=- 2)
      7 
      8 avor = ndimage.gaussian_filter(avor, sigma=3, order=0) * units('1/s')

/opt/tljh/user/lib/python3.7/site-packages/metpy/xarray.py in wrapper(*args, **kwargs)
   1488                 )
   1489             else:
-> 1490                 raise ValueError('Must provide latitude argument or input DataArray with '
   1491                                  'latitude/longitude coordinates.')
   1492 

ValueError: Must provide latitude argument or input DataArray with latitude/longitude coordinates.

Here is my code so far:

ds = xr.open_dataset('https://thredds.ucar.edu/thredds/dodsC/casestudies/'
                     'python-gallery/GFS_20101026_1200.nc').metpy.parse_cf()

# Set subset slice for the geographic extent of data to limit download
lon_slice = slice(200, 350)
lat_slice = slice(85, 10)

# Grab lat/lon values (GFS will be 1D)
lats = ds.lat.sel(lat=lat_slice).values
lons = ds.lon.sel(lon=lon_slice).values

level = 500 * units.hPa
hght_500 = ds.Geopotential_height_isobaric.metpy.sel(
    vertical=level, lat=lat_slice, lon=lon_slice).metpy.unit_array.squeeze()
tmpk_500 = ds.Temperature_isobaric.metpy.sel(
    vertical=level, lat=lat_slice, lon=lon_slice).metpy.unit_array.squeeze()
uwnd_500 = ds['u-component_of_wind_isobaric'].metpy.sel(
    vertical=level, lat=lat_slice, lon=lon_slice).metpy.unit_array.squeeze()
vwnd_500 = ds['v-component_of_wind_isobaric'].metpy.sel(
    vertical=level, lat=lat_slice, lon=lon_slice).metpy.unit_array.squeeze()

# Get a sensible datetime format
vtime = ds.time.data[0].astype('datetime64[ms]').astype('O')
dx, dy = mpcalc.lat_lon_grid_deltas(lons, lats)

#Data Calculations
f = mpcalc.coriolis_parameter(np.deg2rad(lats)).to(units('1/sec'))

avor = mpcalc.absolute_vorticity(uwnd_500, vwnd_500, dx=dx, dy=dy, latitude=None, x_dim=- 1, y_dim=- 2)

avor = ndimage.gaussian_filter(avor, sigma=3, order=0) * units('1/s')

vort_adv = mpcalc.advection(avor, [uwnd_500, vwnd_500], (dx, dy)) * 1e9

# Set map projection
mapcrs = ccrs.LambertConformal(central_longitude=-100, central_latitude=35,
                               standard_parallels=(30, 60))

# Set projection of the data (GFS is lat/lon)
datacrs = ccrs.PlateCarree()

# Start figure and limit the graphical area extent
fig = plt.figure(1, figsize=(14, 12))
ax = plt.subplot(111, projection=mapcrs)
ax.set_extent([-130, -72, 20, 55], ccrs.PlateCarree())

# Add map features of Coastlines and States
ax.add_feature(cfeature.COASTLINE.with_scale('50m'))
ax.add_feature(cfeature.STATES.with_scale('50m'))

1 Answers1

0

So the problem is that you need to supply the latitude values so that absolute_vorticity can calculate the f (Coriolis) parameter. You are currently passing latitude=None, which doesn't provide it the needed values.

Luckily, with MetPy 1.1 and when reading your data with xarray, you should be able to forego this entirely. Try this for the calculation:

# Get a sensible datetime format
vtime = ds.time.data[0].astype('datetime64[ms]').astype('O')

#Data Calculations
avor = mpcalc.absolute_vorticity(uwnd_500, vwnd_500)
avor = ndimage.gaussian_filter(avor, sigma=3, order=0) * units('1/s')
vort_adv = mpcalc.advection(avor, uwnd_500, vwnd_500) * 1e9

MetPy 1.0 made some changes from the previous 0.x branch. It looks like you're working from the Unidata training materials. I'd recommend consulting the latest version of this example on GitHub. (We're still working on getting the website updated.)

DopplerShift
  • 5,472
  • 1
  • 21
  • 20