MetPy
MetPy is a collection of tools in Python for reading, visualizing and performing calculations with weather data. It provides meteorological calculations, data input/output, and specialized plotting capabilities like Skew-T diagrams and station plots. Following semantic versioning, MetPy 1.x releases are backward compatible, with breaking changes reserved for major version increments. The current version is 1.7.1 and it requires Python >= 3.10.
Warnings
- breaking The MetPy 1.0 release introduced significant API changes, particularly in `metpy.calc` functions. Many argument names were changed for consistency (e.g., `rh` to `relative_humidity`, `dewpt` to `dewpoint`). Functions like `geostrophic_wind()` and `ageostrophic_wind()` now expect `latitude` instead of the `coriolis_parameter f`. Code using keyword arguments with old names will break.
- breaking Prior to MetPy 1.0, many `metpy.calc` functions consistently returned `pint.Quantity` objects even when provided `xarray.DataArray` inputs. From MetPy 1.0 onwards, these functions will now properly return an `xarray.DataArray` when given `xarray.DataArray` input, preserving xarray's coordinate information.
- gotcha MetPy heavily relies on unit-aware quantities provided by the `pint` library. Most MetPy functions explicitly require inputs to have units attached (e.g., `25 * units.degC`). Passing raw numerical arrays without units will often result in a `UnitsError` or incorrect calculations.
- gotcha Import errors related to `pint` (MetPy's unit library) can occur if the `typing_extensions` package in your environment is outdated. This conflict prevents proper loading of MetPy's dependencies.
Install
-
pip install metpy -
conda install -c conda-forge metpy
Imports
- units
from metpy.units import units
- mpcalc
import metpy.calc as mpcalc
- io
from metpy import io
- plots
from metpy import plots
Quickstart
from metpy.units import units
import metpy.calc as mpcalc
import numpy as np
# Define temperature and relative humidity with units
temperature = 25 * units.degC
relative_humidity = 75 * units.percent
# Calculate dewpoint
dewpoint = mpcalc.dewpoint_from_relative_humidity(temperature, relative_humidity)
print(f"Temperature: {temperature:.1f}")
print(f"Relative Humidity: {relative_humidity:.1f}")
print(f"Dewpoint: {dewpoint:.1f}")
# Example of converting units
temp_f = temperature.to('degF')
print(f"Temperature in Fahrenheit: {temp_f:.1f}")