Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def f(heights, **kwargs):
c = metpy.calc.coriolis_parameter(lat * _ureg.degrees)
if dim_order == "yx":
dy, dx = kwargs['deltas']
else:
dx, dy = kwargs['deltas']
return metpy.calc.geostrophic_wind(xr.DataArray(heights), c, dx, dy,
dim_order=kwargs['dim_order'])
a value for vapor pressure assuming both 1000mb and 850mb ambient air
pressure values. It also demonstrates converting the resulting dewpoint
temperature to degrees Fahrenheit.
"""
import metpy.calc as mpcalc
from metpy.units import units
###########################################
# Create a test value of mixing ratio in grams per kilogram
mixing = 10 * units('g/kg')
print(mixing)
###########################################
# Now throw that value with units into the function to calculate
# the corresponding vapor pressure, given a surface pressure of 1000 mb
e = mpcalc.vapor_pressure(1000. * units.mbar, mixing)
print(e)
###########################################
# Take the odd units and force them to millibars
print(e.to(units.mbar))
###########################################
# Take the raw vapor pressure and throw into the dewpoint function
td = mpcalc.dewpoint(e)
print(td)
###########################################
# Which can of course be converted to Fahrenheit
print(td.to('degF'))
###########################################
# Select and grab 500-hPa geopotential heights and smooth with n-point smoother
level = 500 * units.hPa
hght_500 = mpcalc.smooth_n_point(ds.Geopotential_height_isobaric.metpy.sel(
vertical=level).squeeze(), 9, 50)
# Select and grab 500-hPa wind components
uwnd_500 = ds['u-component_of_wind_isobaric'].metpy.sel(vertical=level).squeeze()
vwnd_500 = ds['v-component_of_wind_isobaric'].metpy.sel(vertical=level).squeeze()
# Compute north-relative wind components for plotting purposes
uwnd_er, vwnd_er = earth_relative_wind_components(uwnd_500, vwnd_500)
# Smooth wind components as desired
uwnd_er = mpcalc.smooth_n_point(uwnd_er, 9, 50)
vwnd_er = mpcalc.smooth_n_point(vwnd_er, 9, 50)
# Create a clean datetime object for plotting based on time of Geopotential heights
vtime = ds.time.data[0].astype('datetime64[ms]').astype('O')
######################################################################
# MetPy Absolute Vorticity Calculation
# ------------------------------------
#
# This code first uses MetPy to calcualte the grid deltas (sign aware) to
# use for derivative calculations with the funtcion
# ``lat_lon_grid_deltas()`` and then calculates ``absolute_vorticity()``
# using the wind components, grid deltas, and latitude values.
#
# Calculate grid spacing that is sign aware to use in absolute vorticity calculation
###########################################
# Create a test value of a directional text
dir_str = 'SOUTH SOUTH EAST'
print(dir_str)
###########################################
# Now throw that string into the function to calculate
# the corresponding angle
angle_deg = mpcalc.parse_angle(dir_str)
print(angle_deg)
###########################################
# The function can also handle arrays of string
# in many different abbrieviations and capitalizations
dir_str_list = ['ne', 'NE', 'NORTHEAST', 'NORTH_EAST', 'NORTH east']
angle_deg_list = mpcalc.parse_angle(dir_str_list)
print(angle_deg_list)
def plot_metpy(data, title="", saveplot=None, showplot=True):
# Convert data into a suitable format for metpy.
_altitude = data[:,0] * units('m')
p = mpcalc.height_to_pressure_std(_altitude)
T = data[:,3] * units.degC
Td = data[:,4] * units.degC
wind_speed = data[:,1] * units('m/s')
wind_direction = data[:,2] * units.degrees
u, v = mpcalc.wind_components(wind_speed, wind_direction)
fig = plt.figure(figsize=(6,8))
skew = SkewT(fig=fig)
skew.plot(p, T, 'r')
skew.plot(p, Td, 'g')
my_interval = np.arange(300, 1000, 50) * units('mbar')
ix = mpcalc.resample_nn_1d(p, my_interval)
skew.plot_barbs(p[ix], u[ix], v[ix])
skew.ax.set_ylim(1000,300)
skew.ax.set_xlim(-40, 30)
skew.plot_dry_adiabats()
heights = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) * units.km
std_pressures = mpcalc.height_to_pressure_std(heights)
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))
ax = plt.subplot(111)
# Plot Geopotential Height Contours
cs = ax.contour(lons, lats, Z, range(0, 12000, 120), colors='k')
plt.clabel(cs, fmt='%d')
# Plot Temperature Contours