Skip to main content

Rust-powered MetPy-compatible calculation layer with optional GPU acceleration

Project description

metrust

MetPy-compatible calculation layer, powered by Rust.

150/150 metpy.calc functions implemented natively, plus 36 extras. Often faster on real-world workflows. Verified against MetPy on SounderPy, MetPy Cookbook examples, and synthetic global grids.

# The only change: swap the import
from metrust.calc import cape_cin, potential_temperature, vorticity
from metrust.units import units

Installation

pip install metrust

Optional GPU acceleration for supported calculations:

pip install "metrust[gpu]"

For plotting, xarray accessor, or Level2File (forwarded to MetPy):

pip install metrust metpy

What It Does

metrust implements every function in metpy.calc with a Rust backend compiled via PyO3. The Python API now matches MetPy's public metpy.calc signatures and is designed for MetPy-compatible units, return types, and runtime behavior on the shared calculation surface:

import numpy as np
from metrust.calc import (
    cape_cin, parcel_profile, bunkers_storm_motion,
    storm_relative_helicity, significant_tornado_parameter,
    vorticity, divergence, advection,
)
from metrust.units import units

# Sounding analysis (same API as MetPy)
p = np.array([1000, 925, 850, 700, 500, 300]) * units.hPa
T = np.array([25, 20, 15, 5, -15, -40]) * units.degC
Td = np.array([20, 15, 10, -5, -25, -50]) * units.degC

prof = parcel_profile(p, T[0], Td[0])
cape, cin = cape_cin(p, T, Td, prof)  # MetPy parcel_profile form works

# Grid kinematics with xarray (dx/dy inferred from lat/lon coords)
vort = vorticity(u_xarray, v_xarray)  # spherical metric corrections included
div = divergence(u_xarray, v_xarray)

Optional GPU Backend

Current users do not need to change anything. metrust stays on the Rust CPU backend by default.

When met-cu is installed, you can opt in explicitly:

import metrust.calc as mcalc

mcalc.set_backend("gpu")
theta = mcalc.potential_temperature(pressure, temperature)

with mcalc.use_backend("cpu"):
    theta_cpu = mcalc.potential_temperature(pressure, temperature)

The GPU backend currently targets the overlap where met-cu is already strong and verified:

  • potential_temperature
  • equivalent_potential_temperature
  • dewpoint
  • vorticity
  • frontogenesis
  • q_vector
  • compute_cape_cin
  • compute_srh
  • compute_shear
  • compute_pw
  • composite_reflectivity_from_hydrometeors

Eligible dispatch currently focuses on scalar thermo plus uniform 2-D Cartesian grid workloads. Latitude/longitude-derived spacing, map-scale corrections, and other projection-aware cases fall back to the Rust CPU backend.

metrust still returns the same Pint/NumPy-facing API surface. Unsupported cases automatically stay on the Rust CPU path.

Speed

Benchmarked on real-world workflows (v0.3.3, validated by Codex against MetPy):

Workflow Speedup Notes
MetPy Cookbook sounding analysis 6.0x Full severe weather stack
MetPy Cookbook 500 hPa grid 6.1x Vorticity, smoothing, advection
MetPy Cookbook Q-vectors 6.1x Q-vector divergence
SounderPy compute-heavy subset 29.7x Thermo + wind + severe params
MetPy isentropic example 2.3x Isentropic interpolation + Montgomery
Vorticity/divergence (global grid) 2.3x Spherical corrections on 721x1440

Current replay-harness snapshot from benches/bench_workflows.py on Windows 11 / Python 3.13.7: sounding 2.16x, grid diagnostics 8.53x, xarray workflow 1.46x.

Three-Way Benchmark: MetPy vs Rust vs CUDA

Real HRRR model output (40 isobaric levels, 1059 × 1799 grid, ~1.9 M points). RTX 5090, python tests/benchmark_gpu.py.

Scalar Thermodynamics (2D: 1059×1799)

Function MetPy Rust CUDA Rust/MetPy CUDA/Rust
potential_temperature ★ 11.2 ms 13.0 ms 7.9 ms 0.9x 1.6x
equiv_potential_temperature ★ 303.1 ms 16.5 ms 9.1 ms 18x 1.8x
dewpoint ★ 33.6 ms 10.8 ms 8.8 ms 3.1x 1.2x
saturation_vapor_pressure 66.7 ms 8.2 ms 8.1x
saturation_mixing_ratio 78.6 ms 13.7 ms 5.7x
dewpoint_from_rh 110.3 ms 7.6 ms 14x
rh_from_dewpoint 138.7 ms 10.9 ms 13x
virtual_temperature 31.1 ms 19.7 ms 1.6x
mixing_ratio 11.6 ms 8.5 ms 1.4x
wet_bulb_temperature >10 min 26.9 ms

Grid Kinematics (2D: 1059×1799)

Function MetPy Rust CUDA Rust/MetPy CUDA/Rust
vorticity ★ 98.3 ms 92.8 ms 9.3 ms 1.1x 10x
divergence 96.1 ms 91.2 ms 1.1x
frontogenesis ★ 733.0 ms 339.4 ms 12.2 ms 2.2x 28x
q_vector ★ 390.3 ms 310.1 ms 10.8 ms 1.3x 29x
advection 161.8 ms 87.7 ms 1.8x

1D Sounding (40 levels, single column)

Function MetPy Rust Rust/MetPy
parcel_profile 5.5 ms 0.074 ms 74x
cape_cin 1.4 ms 0.254 ms 5.4x
lcl 0.118 ms 0.065 ms 1.8x
lfc 6.7 ms 0.107 ms 62x
el 6.6 ms 0.112 ms 59x
precipitable_water 2.1 ms 0.057 ms 36x

Grid Composites (3D: 40×1059×1799 → 2D) — MetPy has no grid equivalents

Function Rust CUDA CUDA/Rust
compute_cape_cin ★ 2.96 s 674.5 ms 4.4x
compute_srh ★ 223.5 ms 135.8 ms 1.6x
compute_shear ★ 190.4 ms 166.5 ms 1.1x
compute_pw ★ 191.6 ms 107.9 ms 1.8x
composite_refl_hydrometeors ★ 154.2 ms 232.2 ms 0.7x

Summary

MetPy Rust CUDA
Scalar thermo (×10) 785 ms 136 ms 26 ms
Grid kinematics (×5) 1.48 s 921 ms 32 ms
1D sounding (×6) 22 ms 0.67 ms
Grid composites (×5) 3.72 s 1.32 s
★ GPU-eligible total 4.50 s 1.37 s (3.3x)

★ = dispatches to CUDA when set_backend("gpu"). All other functions stay on Rust CPU regardless of backend setting.

Array Throughput

1M elements, 32-core Ryzen, rayon parallel:

Function Time Throughput
potential_temperature 1.8 ms 550 M/s
wet_bulb_temperature 7.3 ms 137 M/s
wind_speed 1.5 ms 660 M/s

Numerical Parity

Verified on the MetPy OUN 2011-05-22 12Z test sounding:

Metric Difference from MetPy
CAPE +4.0 J/kg
MUCAPE +7.6 J/kg
SRH (0-1 km) +0.3 m^2/s^2
Critical angle +0.2 deg
Bunkers RM +0.02 m/s
STP +0.01
Montgomery streamfunction corr 1.0000
Isentropic pressure 7e-13 hPa diff
Vorticity (global lat/lon) corr 1.0000

Uses MetPy-exact physical constants (Rd, Cp, Lv, epsilon), MetPy's CAPE integration formula (g * dTv/Tv * dz), pressure-weighted Bunkers algorithm, Newton solver for isentropic interpolation, and spherical metric tensor corrections for lat/lon grid kinematics.

Coverage

  • 150/150 metpy.calc functions (100% coverage)
  • 36 extras not in MetPy (grid composites, fire weather indices, etc.)
  • 28 Rust array bindings with rayon parallelism and GIL release
  • Pint application registry shared with MetPy (no cross-registry errors)
  • xarray support with coordinate inference and shape preservation

What's Not Native

These forward to MetPy when installed:

  • metrust.plots (matplotlib-based plotting)
  • metrust.xarray (xarray accessor)
  • metrust.io.Level2File (NEXRAD Level II)

Core metrust.calc stays native Rust by default with no required MetPy dependency. The shared calc surface now stays on native metrust implementations even when MetPy is installed. The optional met-cu backend is an explicit accelerator, not a requirement.

End-to-end replay benchmarks for sounding, grid, and xarray workflows live in benches/bench_workflows.py and the published docs page workflow-benchmarks.md.

Examples

See examples/ for complete drop-in demos:

  • cookbook_sounding.py — MetPy Cookbook sounding analysis
  • cookbook_500hpa_grid.py — MetPy Cookbook 500 hPa vorticity advection
  • sounderpy_dropin.py — SounderPy-style full sounding pipeline

Testing

cargo test --workspace          # 1,186 Rust tests
python -m pytest tests/ -q      # 30 Python tests (including MetPy compatibility regression)

Documentation

Full docs at fahrenheitresearch.github.io/metrust-py, including:

  • API reference for all 186 functions
  • Beginner tutorials (Weather 101, soundings, grids, recipes)
  • Migration guide from MetPy
  • Performance benchmarks

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

metrust-0.4.7.tar.gz (1.2 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

metrust-0.4.7-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

metrust-0.4.7-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

metrust-0.4.7-cp312-cp312-win_amd64.whl (1.4 MB view details)

Uploaded CPython 3.12Windows x86-64

metrust-0.4.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

metrust-0.4.7-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

metrust-0.4.7-cp312-cp312-macosx_11_0_arm64.whl (1.4 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

metrust-0.4.7-cp312-cp312-macosx_10_12_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

metrust-0.4.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

metrust-0.4.7-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.5 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

metrust-0.4.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

metrust-0.4.7-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ ARM64

File details

Details for the file metrust-0.4.7.tar.gz.

File metadata

  • Download URL: metrust-0.4.7.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.13.1

File hashes

Hashes for metrust-0.4.7.tar.gz
Algorithm Hash digest
SHA256 a487dffe0df266b06d9f68f552cceeea7d0d7f76c9efb2845eb2f5bedcdbf587
MD5 06b78b4fadb2cee025f64cd4b5566100
BLAKE2b-256 e6c860efa0c585980147ed5b4e855b73089f1db12e1df8dbbfe1601bdbbba4c5

See more details on using hashes here.

File details

Details for the file metrust-0.4.7-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for metrust-0.4.7-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7307b778441ddb1496b6605ed322c3971724b9bc5e3b5ee38051e9dfe70973c8
MD5 56b450871e4a6b7b54d338054fd62cab
BLAKE2b-256 73b300e59a910a22758d8e96e36a87b6bde6c0b469f903eced3d9f935a28596f

See more details on using hashes here.

File details

Details for the file metrust-0.4.7-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for metrust-0.4.7-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 460c0a35ba686d9bf627e4f158f8e0484c383d9c9dc82c8975c057a09e011c6c
MD5 da0e42e521461f7c900f70f9b83512a4
BLAKE2b-256 0becc120895ac1bf0530fd8335f2f9f520900c719542dbcc6c1373dbbfce901b

See more details on using hashes here.

File details

Details for the file metrust-0.4.7-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: metrust-0.4.7-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.13.1

File hashes

Hashes for metrust-0.4.7-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ceefadc3ee9498c42b18bdac81988c065685e0cbcc7cfef66e594695d72d400e
MD5 12e5fdd584a0073fc1b5ebafb0002260
BLAKE2b-256 23236b3657a9d729ad57ee47a4af6084ed94243eb9c0a6c7cc53c8fcf214045f

See more details on using hashes here.

File details

Details for the file metrust-0.4.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for metrust-0.4.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e454a48b3d45787c1acd342c5fb7b537af169679673a1af7b15a8dfc3248e2b0
MD5 99f24ee48d2de5a829a76ae1de3943d7
BLAKE2b-256 c8a4f8423405a9224db868ee917efab6e73bb7e6861948a641ee61cdd7fd6c87

See more details on using hashes here.

File details

Details for the file metrust-0.4.7-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for metrust-0.4.7-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c3731426aac0a82df4ed19fb1cd2d460e01fc50dc529260b25f836a429fb6c7a
MD5 097d056aa9b4ddcb3bcfb5aa25305858
BLAKE2b-256 8548b856f5fad699e3805f052878d88ebf4f78fabc4aa894b31cea6b60939d4c

See more details on using hashes here.

File details

Details for the file metrust-0.4.7-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for metrust-0.4.7-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1597b59cb625ecea93a487c9e7070410ad9bfc219c7fb81a8419908d01f3fea9
MD5 e609eb1bb260914080e0239525281fd6
BLAKE2b-256 f3aaeb938a1cf15efb12179e21dd03db1c4a2b82ebf5634011a4d9a2fefd9dfe

See more details on using hashes here.

File details

Details for the file metrust-0.4.7-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for metrust-0.4.7-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2204334349dc1ef38a43110896bfb8bc37aa6f845f927cbcb70ea673fad47466
MD5 630ab39ea73aba481ed7c5a5b0617613
BLAKE2b-256 9a18e4a36eb8ee7a1eb49328d066c22b4937158b17d2962ac08414bd22ef8454

See more details on using hashes here.

File details

Details for the file metrust-0.4.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for metrust-0.4.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cbf3cd40692bd3fcf9fc5e2b7c95232fd8a9f902778e3d0c7e5f9d8c62a3de79
MD5 da82bb2006c5eb802c7ad416e4657c33
BLAKE2b-256 307882cd5ba6ecf50dc1f63408bfdf88968091ad221f055121d08c1857a5a2ae

See more details on using hashes here.

File details

Details for the file metrust-0.4.7-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for metrust-0.4.7-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ffd9528d659f8d4cb62abf7bbc8a2e64f4f2fc11f91fd8a92ae4fd99befeca47
MD5 9a6eb4d7bb4b82cb870b9e4fa1aa43d8
BLAKE2b-256 600e78276bbf535615057273fb37e7f089f7038c0d717f0fa1831f95d911cdd5

See more details on using hashes here.

File details

Details for the file metrust-0.4.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for metrust-0.4.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 395730ece364fad64ac06f36959771047902e70f0656c357c8c0354f3a5e503e
MD5 facffb7806bc78a327d140f48c42070e
BLAKE2b-256 7fff5438f1d31142718dddda7bdf4fa2033a6ee7c82621c00347b75518e26dd6

See more details on using hashes here.

File details

Details for the file metrust-0.4.7-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for metrust-0.4.7-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 f8f12341aa749ba94eaba077e748942c9a7cee84a9d972bd9e122d6baa41b0da
MD5 04dd20dd57ecac22b856a4c880dfaa7b
BLAKE2b-256 a30ab7b8b88240f2b46a831c631c23a37e54a39efea667395fab095672f42fbf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page