This notebook will introduce the use of MetPy for meteorological calculations. MetPy is an open-source python package designed to provide support for flexible calculations of various meteorological quantities in support of educational, operational, and research needs. This notebook will introduce some basic computations and provide an overview of some available computations within the MetPy package.
import matplotlib.pyplot as plt
import metpy.calc
from metpy.units import units
import numpy as np
Let's first start with some simple calculations. To do this, we will need some data to work with. Let's pretend we have a timeseries of surface observations consisting of wind components, temperature, dewpoint temperature, and pressure.
temperature = (np.random.randn(100) + 1 * 68) * units('degF')
dewpoint = (np.random.randn(100) + 1 * 10) * units('degC')
uwind = (np.random.randn(100) + 1 * 20) * units('kt')
vwind = (np.random.randn(100) + 1 * 5) * units('m/s')
pressure = (np.random.randn(100) + 1 * 1000) * units('hPa')
Now that we have some data we can start making some calculations. Notice that the data units are all different. Ordinarily this would require units conversions before any calculations. However, MetPy will handle those for us, meaning that as long as the units are defined, we can simply tell MetPy what units we want. Let's start with determining the wind speed.
ws = metpy.calc.wind_speed(uwind, vwind).to('m/s')
That's it! We now have a timeseries of wind speed with units of m/s. Let's see what it looks like.
plt.plot(ws)
plt.show()
We can also calculate wind direction
wd = metpy.calc.wind_direction(uwind, vwind)
plt.plot(wd)
plt.show()
What if we want a plot of relative humidity? This can be easily done as well.
rh = metpy.calc.relative_humidity_from_dewpoint(temperature, dewpoint)
plt.plot(rh*100)
plt.show()
Now that we have introduced some basic calculations, let's try some more complicated thermodynamic calculations. First, let's calculate the potential temperature and equivalent potential temperature
theta = metpy.calc.potential_temperature(pressure, temperature)
plt.plot(theta)
plt.show()
theta_e = metpy.calc.equivalent_potential_temperature(pressure, temperature, dewpoint)
plt.plot(theta_e)
plt.show()
We can also calculate the saturation values for vairous moisture variables, such as saturation vapor pressure.
es = metpy.calc.saturation_vapor_pressure(temperature)
plt.plot(es)
plt.show()
MetPy also has support for computation of common indicies and standard atmosphere values. Let's calculate the standard atmosphere height based on the pressure.
height = metpy.calc.pressure_to_height_std(pressure)
plt.plot(height.to('meter'))
plt.show()