or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

calculation-functions.mddata-io.mdindex.mdinterpolation.mdphysical-constants.mdplotting.mdxarray-integration.md
tile.json

tessl/pypi-metpy

Comprehensive Python library for meteorological data analysis and weather visualization.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/metpy@1.7.x

To install, run

npx @tessl/cli install tessl/pypi-metpy@1.7.0

index.mddocs/

MetPy

MetPy is a comprehensive Python library designed for meteorological data analysis and weather visualization. It provides tools for reading, processing, and performing calculations with weather data, offering functionality equivalent to GEMPAK for reading meteorological data from various formats, calculating derived fields, and creating weather maps and atmospheric soundings including Skew-T log-P diagrams.

The library integrates seamlessly with the scientific Python ecosystem (NumPy, SciPy, Matplotlib, Pandas, Xarray) and supports multiple data sources including NEXRAD radar data, surface observations, and upper-air soundings. MetPy is designed for researchers, educators, and anyone needing to script weather analysis, providing a well-documented and well-tested foundation for meteorological applications with features like unit handling through Pint, geographic projections via PyProj, and extensive calculation functions for atmospheric science applications.

Package Information

  • Package Name: MetPy
  • Package Type: Library
  • Language: Python
  • Installation: pip install metpy

Core Imports

import metpy
import metpy.calc as mpcalc
from metpy.units import units
import metpy.constants as constants

For xarray integration:

import xarray as xr
import metpy.xarray  # Enables .metpy accessor

For plotting:

from metpy.plots import SkewT, Hodograph, StationPlot
import metpy.plots as plots

Basic Usage

import metpy.calc as mpcalc
from metpy.units import units
import numpy as np

# Basic thermodynamic calculations with proper units
temperature = 25 * units.celsius
pressure = 1013 * units.hPa
dewpoint = 15 * units.celsius

# Calculate relative humidity
rh = mpcalc.relative_humidity_from_dewpoint(temperature, dewpoint)
print(f"Relative Humidity: {rh}")

# Calculate potential temperature
theta = mpcalc.potential_temperature(pressure, temperature)
print(f"Potential Temperature: {theta}")

# Work with arrays and units
temps = np.array([20, 25, 30]) * units.celsius
pressures = np.array([1000, 850, 700]) * units.hPa

# Calculate multiple values at once
potential_temps = mpcalc.potential_temperature(pressures, temps)
print(f"Potential Temperatures: {potential_temps}")

Architecture

MetPy is organized around several key components that work together:

  • Units System: Built on Pint, provides dimensional analysis and unit conversion for all meteorological quantities
  • Calculation Functions: Over 150 functions covering thermodynamics, dynamics, and kinematics for atmospheric science
  • XArray Integration: Seamless integration with xarray for coordinate-aware meteorological data analysis
  • I/O System: Readers for major meteorological file formats (GEMPAK, NEXRAD, GINI, METAR)
  • Plotting Tools: Specialized meteorological plots including Skew-T diagrams and hodographs
  • Constants Library: Complete set of physical constants for atmospheric and earth science calculations

This modular design allows MetPy to serve as both a comprehensive toolkit and a foundation for domain-specific meteorological applications.

Capabilities

Meteorological Calculations

Comprehensive set of 150+ atmospheric science calculation functions covering thermodynamics, dynamics, and kinematics. Includes temperature conversions, moisture calculations, stability indices, wind analysis, and advanced atmospheric physics.

# Thermodynamic functions
def potential_temperature(pressure, temperature): ...
def equivalent_potential_temperature(pressure, temperature, dewpoint): ...
def mixing_ratio(partial_press, total_press): ...
def relative_humidity_from_dewpoint(temperature, dewpoint): ...
def dewpoint_from_relative_humidity(temperature, relative_humidity): ...
def specific_humidity_from_dewpoint(pressure, dewpoint): ...
def precipitable_water(pressure, dewpoint): ...

# Dynamic meteorology functions  
def geostrophic_wind(geopotential, f=None, dx=None, dy=None): ...
def vorticity(u, v, dx=None, dy=None): ...
def divergence(u, v, dx=None, dy=None): ...
def advection(scalar, u, v, dx=None, dy=None): ...
def q_vector(geopotential, temperature, pressure, u, v): ...
def shearing_deformation(u, v, dx=None, dy=None): ...

# Stability and parcel functions
def lifted_index(pressure, temperature, parcel_profile): ...
def cape_cin(pressure, temperature, dewpoint, parcel_profile): ...
def surface_based_cape_cin(pressure, temperature, dewpoint): ...
def most_unstable_cape_cin(pressure, temperature, dewpoint): ...
def parcel_profile(pressure, temperature, dewpoint): ...
def lfc(pressure, temperature, dewpoint): ...
def el(pressure, temperature, dewpoint): ...
def ccl(pressure, temperature, dewpoint): ...

# Stability indices
def k_index(pressure, temperature, dewpoint): ...
def showalter_index(pressure, temperature, dewpoint): ...
def total_totals_index(pressure, temperature, dewpoint): ...

# Severe weather parameters
def bulk_shear(pressure, u, v, height=None, bottom=None, depth=None): ...
def storm_relative_helicity(height, u, v, depth): ...

Meteorological Calculations

Physical Constants

Complete library of 60+ physical constants for Earth, atmospheric, and water properties. All constants include proper units and are ready for use in calculations without unit conversion errors.

# Earth constants
earth_gravity = 9.80665 * units('m/s^2')
earth_avg_radius = 6371008.7714 * units.m

# Atmospheric constants
dry_air_gas_constant = 287.0 * units('J/(kg*K)')
atmos_pressure_sea_level = 101325.0 * units.Pa

# Water/moisture constants  
water_heat_vaporization = 2.501e6 * units('J/kg')
water_molecular_weight = 18.0153 * units('g/mol')

Physical Constants

Data Input/Output

Robust I/O support for major meteorological file formats including GEMPAK surface/sounding/grid data, NEXRAD Level 2/3 radar data, GINI satellite imagery, and METAR observations with automatic format detection and metadata preservation.

# File format readers
class GempakSurface: ...
class GempakSounding: ...
class GempakGrid: ...
class Level2File: ...  # NEXRAD
class Level3File: ...  # NEXRAD  
class GiniFile: ...    # Satellite data

# METAR parsing
def parse_metar_file(filename): ...
def parse_metar_to_dataframe(filename): ...

Data Input/Output

Meteorological Plotting

Specialized plotting system for atmospheric science including Skew-T log-P diagrams, hodographs, station plots, and declarative plotting framework for publication-quality meteorological visualizations.

# Specialized meteorological diagrams
class SkewT: ...
class Hodograph: ...
class StationPlot: ...

# Declarative plotting framework
class ImagePlot: ...
class ContourPlot: ...
class FilledContourPlot: ...
class BarbPlot: ...    # Wind barbs
class ArrowPlot: ...   # Wind vectors
class MapPanel: ...
class PanelContainer: ...

Meteorological Plotting

XArray Integration

Seamless integration with xarray providing meteorology-aware data analysis through custom accessors. Automatic coordinate identification, unit handling, CRS support, and coordinate transformations for atmospheric data.

# DataArray accessor methods (via .metpy)
@property
def units: ...         # Get/set units
def convert_units(units): ...
def quantify(): ...    # Convert to pint quantities
def assign_crs(cf_attributes): ...
def coordinates(*args): ...  # Get coordinates by type

# Dataset accessor methods (via .metpy)  
def parse_cf(varname=None): ...
def assign_latitude_longitude(): ...
def quantify(): ...

XArray Integration

Spatial Interpolation

Advanced interpolation functions for meteorological data including natural neighbor, inverse distance weighting, Barnes and Cressman objective analysis, and cross-section extraction from 3D atmospheric data.

# Grid interpolation methods
def natural_neighbor_to_grid(xp, yp, variable, interp_type='linear'): ...
def inverse_distance_to_grid(xp, yp, variable, hres, search_radius=None): ...  
def barnes_to_grid(xp, yp, variable, hres, search_radius): ...
def cressman_to_grid(xp, yp, variable, hres, search_radius): ...

# Cross-section analysis
def cross_section(data, start, end): ...
def interpolate_1d(x, xp, *args, axis=0): ...

Spatial Interpolation

Types

# Common types used throughout MetPy
from typing import Union, Optional, Sequence
import numpy as np
import xarray as xr
from pint import Quantity

# Meteorological data types
ArrayLike = Union[np.ndarray, xr.DataArray, Sequence, Quantity]
Pressure = Quantity  # Must be pressure units
Temperature = Quantity  # Must be temperature units  
Length = Quantity  # Must be length units
Speed = Quantity  # Must be velocity units

# Coordinate system types
CRS = object  # Coordinate reference system