# Compute the Gradient Wind via an approximation dydx = mpcalc.first_derivative(Z, delta=dx, axis=1) d2ydx2 = mpcalc.first_derivative(dydx, delta=dx, axis=1) R = ((1 + dydx.m**2)**(3. / 2.)) / d2ydx2.m geo_mag = mpcalc.wind_speed(ugeo, vgeo) grad_mag = geo_mag.m - (geo_mag.m**2) / (f.magnitude * R) ugrad, vgrad = mpcalc.wind_components(grad_mag * units('m/s'), wdir) # Calculate Ageostrophic wind uageo = ugrad - ugeo vageo = vgrad - vgeo # Compute QVectors uqvect, vqvect = mpcalc.q_vector(ugeo, vgeo, T * units.degC, 500 * units.hPa, dx, dy) # Calculate divergence of the ageostrophic wind div = mpcalc.divergence(uageo, vageo, dx, dy, dim_order='yx') # Calculate Relative Vorticity Advection relvor = mpcalc.vorticity(ugeo, vgeo, dx, dy, dim_order='yx') adv = mpcalc.advection(relvor, (ugeo, vgeo), (dx, dy), dim_order='yx') ###################################################################### # Create figure containing Geopotential Heights, Temperature, Divergence # of the Ageostrophic Wind, Relative Vorticity Advection (shaded), # geostrphic wind barbs, and Q-vectors. # fig = plt.figure(figsize=(10, 10))
H500 = HGT_DATA.variables['hgt'][TIME_INDEX, 5, :, :] U500 = UWND_DATA.variables['uwnd'][TIME_INDEX, 5, :, :] * units('m/s') V500 = VWND_DATA.variables['vwnd'][TIME_INDEX, 5, :, :] * units('m/s') DX, DY = mpcalc.lat_lon_grid_spacing(LON, LAT) VORT500 = mpcalc.vorticity(U500, V500, DX, DY, dim_order='YX') VORT500 = (VORT500 * (units('1/s'))) VORT_ADV500 = mpcalc.advection(VORT500, [U500, V500], (DX, DY), dim_order='yx') # ============================================================================= # FIG #3: 700: Q-VECTORS+CONVERGENCE, GEOPOTENTIAL HEIGHT # ============================================================================= H700 = HGT_DATA.variables['hgt'][TIME_INDEX, 3, :, :] T700 = AIR_DATA.variables['air'][TIME_INDEX, 3, :, :] * units('kelvin') U700 = UWND_DATA.variables['uwnd'][TIME_INDEX, 3, :, :] * units('m/s') V700 = VWND_DATA.variables['vwnd'][TIME_INDEX, 3, :, :] * units('m/s') PWAT = PWAT_DATA.variables['pr_wtr'][TIME_INDEX, :, :] QVEC700 = mpcalc.q_vector(U700, V700, T700, 700 * units.mbar, DX, DY) QC700 = mpcalc.divergence(QVEC700[0], QVEC700[1], DX, DY, dim_order='yx') * (10**18) QVECX = QVEC700[0] QVECY = QVEC700[1] # ============================================================================= # FIG #4: 850: GEOPOTENTIAL HEIGHT, TEMP, WINDS, TEMP-ADVECTION, FRONTOGENESIS # ============================================================================= H850 = HGT_DATA.variables['hgt'][TIME_INDEX, 2, :, :] T850 = AIR_DATA.variables['air'][TIME_INDEX, 2, :, :] * units('kelvin') U850 = UWND_DATA.variables['uwnd'][TIME_INDEX, 2, :, :] * units('m/s') V850 = VWND_DATA.variables['vwnd'][TIME_INDEX, 2, :, :] * units('m/s') T_ADV850 = mpcalc.advection(T850 * units.kelvin, [U850, V850], (DX, DY), dim_order='yx') * units('K/sec') PT850 = mpcalc.potential_temperature(850 * units.mbar, T850) FRONT_850 = mpcalc.frontogenesis(PT850, U850, V850, DX, DY, dim_order='YX')
# Use MetPy to calculate the wind speed before smoothing, change units to knots from m/s sped_700 = mpcalc.wind_speed(uwnd_700, vwnd_700).to('kt') ## first, smooth everything pretty heavily: tmpk_700 = ndimage.gaussian_filter(tmpk_700, sigma=6, order=0) * units('kelvin') uwnd_700 = ndimage.gaussian_filter(uwnd_700, sigma=6, order=0) * units('m/s') vwnd_700 = ndimage.gaussian_filter(vwnd_700, sigma=6, order=0) * units('m/s') # less smoothing for the height hght_700 = ndimage.gaussian_filter(hght_700, sigma=2, order=0) * units('m') if plot_700qvect: # Compute the Q-vector components uqvect, vqvect = mpcalc.q_vector(uwnd_700, vwnd_700, tmpk_700, 700*units.hPa, dx, dy) # Compute the divergence of the Q-vectors calculated above q_div = -2*mpcalc.divergence(uqvect, vqvect, dx, dy, dim_order='yx') ## smooth it too q_div = ndimage.gaussian_filter(q_div, sigma=4, order=0) * units('meter/(kilogram second)') #Map Creation print('plotting 700 q-vectors') # Set Projection of Data datacrs = ccrs.PlateCarree() # Set Projection of Plot
def metpy_temp_adv(fname, plevels, tidx): ds = xr.open_dataset(fname).metpy.parse_cf().squeeze() ds = ds.isel(Time=tidx) print(ds) dx, dy = mpcalc.lat_lon_grid_deltas(ds.lon.values * units('degrees_E'), ds.lat.values * units('degrees_N')) plevels_unit = plevels * units.hPa ds1 = xr.Dataset() p = units.Quantity(to_np(ds.p), 'hPa') z = units.Quantity(to_np(ds.z), 'meter') u = units.Quantity(to_np(ds.u), 'm/s') v = units.Quantity(to_np(ds.v), 'm/s') w = units.Quantity(to_np(ds.w), 'm/s') tk = units.Quantity(to_np(ds.tk), 'kelvin') th = units.Quantity(to_np(ds.th), 'kelvin') eth = units.Quantity(to_np(ds.eth), 'kelvin') wspd = units.Quantity(to_np(ds.wspd), 'm/s') omega = units.Quantity(to_np(ds.omega), 'Pa/s') z, u, v, w, tk, th, eth, wspd, omega = log_interpolate_1d(plevels_unit, p, z, u, v, w, tk, th, eth, wspd, omega, axis=0) coords, dims = [plevs, ds.lat.values, ds.lon.values], ["level", "lat", "lon"] for name, var in zip( ['z', 'u', 'v', 'w', 'tk', 'th', 'eth', 'wspd', 'omega'], [z, u, v, w, tk, th, eth, wspd, omega]): #g = ndimage.gaussian_filter(var, sigma=3, order=0) ds1[name] = xr.DataArray(to_np(var), coords=coords, dims=dims) # Calculate temperature advection using metpy function for i, plev in enumerate(plevs): uqvect, vqvect = mpcalc.q_vector(u[i, :, :], v[i, :, :], th[i, :, :], plev * units.hPa, dx, dy) #uqvect, vqvect = mpcalc.q_vector(u[i,:,:], v[i,:,:], th.to('degC')[i,:,:], plev*units.hPa, dx, dy) q_div = -2 * mpcalc.divergence(uqvect, vqvect, dx, dy, dim_order='yx') ds1['uq_{:03d}'.format(plev)] = xr.DataArray( np.array(uqvect), coords=[ds.lat.values, ds.lon.values], dims=["lat", "lon"]) ds1['vq_{:03d}'.format(plev)] = xr.DataArray( np.array(vqvect), coords=[ds.lat.values, ds.lon.values], dims=["lat", "lon"]) ds1['q_div_{:03d}'.format(plev)] = xr.DataArray( np.array(q_div), coords=[ds.lat.values, ds.lon.values], dims=["lat", "lon"]) # adv = mpcalc.advection(tk[i,:,:], [u[i,:,:], v[i,:,:]], (dx, dy), dim_order='yx') * units('K/sec') # adv = ndimage.gaussian_filter(adv, sigma=3, order=0) * units('K/sec') # ds1['tk_adv_{:03d}'.format(plev)] = xr.DataArray(np.array(adv), coords=[ds.lat.values,ds.lon.values], dims=["lat","lon"]) # # adv = mpcalc.advection(th[i,:,:], [u[i,:,:], v[i,:,:]], (dx, dy), dim_order='yx') * units('K/sec') # adv = ndimage.gaussian_filter(adv, sigma=3, order=0) * units('K/sec') # ds1['th_adv_{:03d}'.format(plev)] = xr.DataArray(np.array(adv), coords=[ds.lat.values,ds.lon.values], dims=["lat","lon"]) # adv = mpcalc.advection(eth[i, :, :], [u[i, :, :], v[i, :, :]], (dx, dy), dim_order='yx') * units('K/sec') adv = ndimage.gaussian_filter(adv, sigma=3, order=0) * units('K/sec') ds1['eth_adv_{:03d}'.format(plev)] = xr.DataArray( np.array(adv), coords=[ds.lat.values, ds.lon.values], dims=["lat", "lon"]) div = mpcalc.divergence(u[i, :, :], v[i, :, :], dx, dy, dim_order='yx') div = ndimage.gaussian_filter(div, sigma=3, order=0) * units('1/sec') ds1['div_{:03d}'.format(plev)] = xr.DataArray( np.array(div), coords=[ds.lat.values, ds.lon.values], dims=["lat", "lon"]) ds1['Time'] = ds.Time eth2 = mpcalc.equivalent_potential_temperature( ds.slp.values * units.hPa, ds.t2m.values * units('K'), ds.td2.values * units('celsius')) ds1['eth2'] = xr.DataArray(eth2, coords=[ds.lat.values, ds.lon.values], dims=["lat", "lon"]) ds1['sst'] = xr.DataArray( ndimage.gaussian_filter(ds.sst.values, sigma=3, order=0) - 273.15, coords=[ds.lat.values, ds.lon.values], dims=["lat", "lon"]) ds1['t2m'] = xr.DataArray(ds.t2m.values, coords=[ds.lat.values, ds.lon.values], dims=["lat", "lon"]) ds1['th2'] = xr.DataArray(ds.th2.values, coords=[ds.lat.values, ds.lon.values], dims=["lat", "lon"]) ds1['mask'] = xr.DataArray(ds.mask.values, coords=[ds.lat.values, ds.lon.values], dims=["lat", "lon"]) ds1['u10'] = xr.DataArray(ds.u10.values, coords=[ds.lat.values, ds.lon.values], dims=["lat", "lon"]) ds1['v10'] = xr.DataArray(ds.v10.values, coords=[ds.lat.values, ds.lon.values], dims=["lat", "lon"]) ds1['slp'] = xr.DataArray(ds.slp.values, coords=[ds.lat.values, ds.lon.values], dims=["lat", "lon"]) ds1['rain'] = xr.DataArray(ds.rain.values, coords=[ds.lat.values, ds.lon.values], dims=["lat", "lon"]) ds1['accrain'] = xr.DataArray(ds.accrain.values, coords=[ds.lat.values, ds.lon.values], dims=["lat", "lon"]) #ds1['latent'] = xr.DataArray(ds.latent.values, coords=[ds.lat.values,ds.lon.values], dims=["lat","lon"]) #ds1['acclatent'] = xr.DataArray(ds.acclatent.values, coords=[ds.lat.values,ds.lon.values], dims=["lat","lon"]) return ds1
vtime = ds.time.data[0].astype('datetime64[ms]').astype('O') ###################################################################### # Compute Q-vectors # ----------------- # # Use the MetPy module to compute Q-vectors from requisite data and # additionally compute the Q-vector divergence (and multiply by -2) to # calculate the right hand side forcing of the Q-G Omega equation. # # Compute grid spacings for data dx, dy = mpcalc.lat_lon_grid_deltas(lons, lats) # Compute the Q-vector components uqvect, vqvect = mpcalc.q_vector(uwnd_850, vwnd_850, tmpk_850, 850 * units.hPa, dx, dy) # Compute the divergence of the Q-vectors calculated above q_div = -2 * mpcalc.divergence(uqvect, vqvect, dx, dy, dim_order='yx') ###################################################################### # Plot Data # --------- # # Use Cartopy to plot data on a map using a Lambert Conformal projection. # # Set the map projection (how the data will be displayed) mapcrs = ccrs.LambertConformal(central_longitude=-100, central_latitude=35, standard_parallels=(30, 60))