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_temperatureequivalent_potential_temperaturedewpointvorticityfrontogenesisq_vectorcompute_cape_cincompute_srhcompute_shearcompute_pwcomposite_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 |
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.calcfunctions (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. A small parity-sensitive subset may delegate to MetPy when it is installed; otherwise those paths still fall back to local metrust implementations. The optional met-cu backend is an explicit accelerator, not a requirement.
Examples
See examples/ for complete drop-in demos:
cookbook_sounding.py— MetPy Cookbook sounding analysiscookbook_500hpa_grid.py— MetPy Cookbook 500 hPa vorticity advectionsounderpy_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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file metrust-0.4.2.tar.gz.
File metadata
- Download URL: metrust-0.4.2.tar.gz
- Upload date:
- Size: 1.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa0c2fcf5ffe38b70e529d32dec978ac2d23797eabff7a2a4bc4f56335277f19
|
|
| MD5 |
966dd53d858c4d57da72ecefcef25f72
|
|
| BLAKE2b-256 |
8b62081faee553b9bb34990d13840690d553b82e4c294081932ab1d52a6b9cb4
|
File details
Details for the file metrust-0.4.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: metrust-0.4.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.5 MB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd0d15701f8245d5e1befd891bcbe82b74b90834a4684b7fbddbe9a2c6237fbc
|
|
| MD5 |
1bbe265f33861f903b823c20fa2b9c99
|
|
| BLAKE2b-256 |
e1902b0d09423f51b1c7c66ae8610bb1c4d60263b5c844b9e79bcfee010b83fb
|
File details
Details for the file metrust-0.4.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: metrust-0.4.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 1.5 MB
- Tags: CPython 3.13, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ceba8173533d3fa4681238a3bab9f6dcfad26a91414db6af63f9bc237908b366
|
|
| MD5 |
dc9676ebf819b80e287d4c36606cb166
|
|
| BLAKE2b-256 |
22da186b897e38f4c2f9cb201506edbf4cec65813e10213d74d29ead0ae981fb
|
File details
Details for the file metrust-0.4.2-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: metrust-0.4.2-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ee6f585deeba66553dc4a1b4870c85e5e40d12d70f4560fbf6f54eed193e6e0
|
|
| MD5 |
efe8a659fd98dd25d49bb57172aa3971
|
|
| BLAKE2b-256 |
8ab1169f413553feff09c12fdd17ad1415b23417343ed27c7fa71f4653c3cfaa
|
File details
Details for the file metrust-0.4.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: metrust-0.4.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.5 MB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ffe76df674b517446313e2d2b032bd9117d96c09e435352b8982aa4ae7c6c02f
|
|
| MD5 |
ae3c840046e5eaf9769c1605a244bba1
|
|
| BLAKE2b-256 |
295a74dc319a10f12363656b9aa92bd9ec96febbd09b294a6005f23bb6dc55ad
|
File details
Details for the file metrust-0.4.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: metrust-0.4.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 1.5 MB
- Tags: CPython 3.12, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
24cfc4dd29fa19a03398bcb3ae30aa05aa2597bde8885bab7a27c34619295274
|
|
| MD5 |
db06e7bf4df54d0c3f84de05525b1cc4
|
|
| BLAKE2b-256 |
dbbc1d93553c29e8637fea6ce6df99fe60119d2310efdfe100add4b33b9b6d8e
|
File details
Details for the file metrust-0.4.2-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: metrust-0.4.2-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.4 MB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
76b9f9f90226af1b1d5d97125fb4ea39faee2ea389d82d4f5ba86480d7c76d77
|
|
| MD5 |
b82f7e065706636948db3ca088077d0c
|
|
| BLAKE2b-256 |
e6daec0c8c9ec3bba61ee1de189ee8ac7960aab230c9efc7d3c7730e2f6e10cf
|
File details
Details for the file metrust-0.4.2-cp312-cp312-macosx_10_12_x86_64.whl.
File metadata
- Download URL: metrust-0.4.2-cp312-cp312-macosx_10_12_x86_64.whl
- Upload date:
- Size: 1.4 MB
- Tags: CPython 3.12, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2144729e98e735c5edebdc94a875bef8675454c7ed14ab5aba9f3cb9fb6411dd
|
|
| MD5 |
9ed076fe70179bedea134076de59257d
|
|
| BLAKE2b-256 |
8577cc743c5c46ae56ed877fc74e7924191a087eaacc27e9b1ce0a055539c7e7
|
File details
Details for the file metrust-0.4.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: metrust-0.4.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.5 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b418b1a4dec4fe2ccf52630e6a2a9c13f8ba4235e8073e9223313be3dbef3951
|
|
| MD5 |
b48d65276a2c1215ebc5980d67341ad0
|
|
| BLAKE2b-256 |
906933db1f6753d393d135888e270ebeb2a81cbe717d743aed527da557166269
|
File details
Details for the file metrust-0.4.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: metrust-0.4.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 1.5 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
24b609b0b0757ec565619c208943520dd4d2991f8d8bcc3f41bbe269ac12fd67
|
|
| MD5 |
c537da7927bf8b59b376adf31a78d068
|
|
| BLAKE2b-256 |
1b96ea0747d1a3e914e8fa18de97114e1242943121f7d9b222536caed6e157be
|
File details
Details for the file metrust-0.4.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: metrust-0.4.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.5 MB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a18c3094385bf0ed67daedbfd4f0ea4f9c1e938e86373f6a0ff0dab547e23253
|
|
| MD5 |
c8f29b98a0e38d796819a6ef57ee3db0
|
|
| BLAKE2b-256 |
c2386f31433359c53a28d22703baeb17aeaac616c74bcb80db654e5a6bd18dcb
|
File details
Details for the file metrust-0.4.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: metrust-0.4.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 1.5 MB
- Tags: CPython 3.10, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1533a927308858580cb59e210c6d3024ea78155233ba25e130b5eb861de3930b
|
|
| MD5 |
20349ab51981ed767a54ffd50ff94703
|
|
| BLAKE2b-256 |
bb6f3e8a88f427008cebcb8dc2ea4f4d0e1884f5278fa4a9b3c49ca02146ac03
|