Search code examples
pythonmetpy

How to use vorticiy calculation in metpy version 1.0?


I want to used metpy.calc.vorticity. Previously, its structure was (u, v, dx, dy), but change to (u, v, *, dx=None, dy=None, x_dim=- 1, y_dim=- 2).

  1. What is * at the new structure?
  2. When I use (u, v), it seems other arguments are optional, I faced ValueError: Must provide dx/dy arguments or input DataArray with latitude/longitude coordinates.
  3. When I used (u , v, dx, dy), I faced TypeError: too many positional arguments.

However, I don't have problem with geostrophic_wind(height,dx,dy,latitude) in my code and my data, but what is the problem with vorticity? Some parts of my code is as below:

PATH_nc = '/home/python_script/'
out_fff='gfs.t12z.pgrb2.0p50.f168'
ncfile = Dataset('{}{}.nc'.format(PATH_nc,out_fff))
lon = ncfile.variables['longitude'][:]
lon[lon < 0] = 360 + lon[lon < 0]
lat = ncfile.variables['latitude'][:]
dx, dy = mpcalc.lat_lon_grid_deltas(lon, lat)
long1, lat1 = np.meshgrid(lon, lat)
hght_850  = ncfile.variables['HGT_850mb'][:]
HGT_850  = gaussian_filter((hght_850)/10, sigma=  2)
height850 = ndimage.gaussian_filter(hght_850, sigma=1.5, order=0)
geo_wind_u_850, geo_wind_v_850 = mpcalc.geostrophic_wind(np.squeeze(height850)*units.m, dx, dy, lat1)
vwnd_700 =  units('m/s') *ncfile.variables['VGRD_700mb'][:]
avor700 = mpcalc.vorticity(np.squeeze(uwnd_700), np.squeeze(vwnd_700), dx, dy)

I hope to write all necessary parts of my code, related to the problem. I'll be thankful for any help.


Solution

  • So in the definition of vorticity:

    metpy.calc.vorticity(u, v, *, dx=None, dy=None, x_dim=-1, y_dim=-2)
    

    Everything after the * is defined as a keyword-only argument, which means the function argument/parameter can only be passed via keyword. Because your current code is not passing dx and dy via keyword, but only by position, this is by the error you get is: TypeError: too many positional arguments. The fix is to pass dx and dy using keyword arguments as:

    avor700 = mpcalc.vorticity(np.squeeze(uwnd_700), np.squeeze(vwnd_700), dx=dx, dy=dy)