Skip to main content

spharmgrid

CI Documentation Status codecov PyPI Python Version PyPI version Conda Version GitHub License DOI

Spherical harmonic tools for filtering, regridding, and kinematics in atmospheric science with Xarray, JAX, and PyTorch.

spharmgrid (spherical harmonic gridding) implements spherical harmonic filtering, regridding, differential operators, and atmospheric kinematics for global Xarray fields, JAX arrays, and PyTorch tensors. It computes relative vorticity, divergence, streamfunction, velocity potential, Helmholtz decomposition, and inverse wind transforms. The Xarray API uses DUCC (ducc0) for spherical harmonic transforms; the optional JAX API uses S2FFT, and the optional PyTorch API uses torch-harmonics.

Supported grids are full rectangular Gauss–Legendre (GL) and Clenshaw–Curtis (CC) grids.

Install

Install with either pip, uv, or conda:

pip install spharmgrid
uv add spharmgrid
conda install -c conda-forge spharmgrid

Optional extras are:

  • spharmgrid[dask] — Dask-backed lazy execution;
  • spharmgrid[cf] — optional cf-xarray coordinate discovery;
  • spharmgrid[cli] — command-line NetCDF, Zarr, and GRIB I/O;
  • spharmgrid[jax] — JAX arrays and S2FFT transforms;
  • spharmgrid[torch] — PyTorch tensors and torch-harmonics transforms.

The spharmgrid.jax API requires JAX and S2FFT. See the JAX API documentation for supported GL and CC/MWSS dimensions, x64 and dtype requirements, and examples. For CPU use, spharmgrid[jax] installs JAX and S2FFT. For GPU or TPU use, install the appropriate JAX accelerator build first by following the official JAX installation instructions, then install spharmgrid[jax]; spharmgrid does not bundle or select CUDA or TPU builds. The JAX API currently requires JAX x64 mode and float64 spatial inputs; it does not enable x64 globally.

The spharmgrid.torch API can be installed with spharmgrid[torch]. See the PyTorch API documentation for installation instructions.

Install spharmgrid[cli,dask] to use the transforming CLI commands.

For a standalone command-line installation:

uv tool install "spharmgrid[cli,dask]"

For a project environment, install the CLI and Dask extras with either:

uv add "spharmgrid[cli,dask]"
pip install "spharmgrid[cli,dask]"

Quick start

Importing spharmgrid registers the .sg accessor on Xarray objects. This example applies a T6–42 spectral filter to a DataArray:

import xarray as xr
import spharmgrid

field = xr.open_dataarray("msl.nc")
filtered = field.sg.filter("T6-42")

For workflows with several spectral operations, analyze once and reuse the private coefficient representation:

spectral = field.sg.analyze()
large_scale = spectral.filter("T5-42").synthesize()
laplacian = spectral.laplacian().synthesize()

See the Quick start for regridding, atmospheric wind diagnostics, direct-function equivalents, and further examples.

For differentiable JAX workflows, configure JAX x64 mode before creating arrays and use spharmgrid.jax:

import jax

jax.config.update("jax_enable_x64", True)

import jax.numpy as jnp
import spharmgrid as sg
import spharmgrid.jax as sgj

grid = sg.gaussian_grid(64, 127)
field = jnp.ones((grid.nlat, grid.nlon))
filtered = sgj.filter(field, grid=grid, truncation="T42")

The last two array dimensions are latitude and longitude.

For differentiable PyTorch workflows, install spharmgrid[torch] and use spharmgrid.torch:

import torch
import spharmgrid as sg
import spharmgrid.torch as sgt

grid = sg.gaussian_grid(64, 128)
field = torch.randn(grid.nlat, grid.nlon)
filtered = sgt.filter(field, grid=grid, truncation="T42")

See the PyTorch API documentation for tensor dimensions, reusable torch.nn modules, device/autograd behavior, and PyTorch bandwidth limits.

Documentation

The optional CLI reads NetCDF, Zarr, and GRIB and writes NetCDF and Zarr. See the CLI documentation for installation and usage.

See the full documentation for grid requirements, coordinate handling, CF metadata, atmospheric kinematics, inverse transforms, zero-mode conventions, and command-line use.

Citation

If you use spharmgrid in research, please cite the software release DOI: 10.5281/zenodo.22559210. Citation metadata are also provided in CITATION.cff.

References

See the documentation References for the scientific literature and software cited by spharmgrid.

License

spharmgrid is distributed under the BSD 3-Clause License.

Metadata

Release files for spharmgrid 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spharmgrid 0.4.0
File Size Uploaded
spharmgrid-0.4.0.tar.gz 333.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for spharmgrid 0.4.0
File Interpreter ABI Platform
spharmgrid-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 420.2 kB

Release files / spharmgrid-0.4.0.tar.gz

Download URL spharmgrid-0.4.0.tar.gz
Size 333.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e9756fd1049944f1e2313fd6bd3244315e76a2d0a3c2449829b602b76c3ac687
BLAKE2b-256 checksum
How to use checksums
ff594211f125f8173de11ba7fac91823731bd28ee072eba14b07796e6ca9c656
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 2, 2026.

Transparency log

Release files / spharmgrid-0.4.0-py3-none-any.whl

Download URL spharmgrid-0.4.0-py3-none-any.whl
Size 86.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b09e01ca6d46ba725c81cd0e216bc6346c56e0d79a100b89e27101ad94f41d57
BLAKE2b-256 checksum
How to use checksums
65fceee9717c54117ef5a9426400586abaacfb6a4ca0a7260539c53e6464d06c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 2, 2026.

Transparency log

Release history Release notifications | RSS feed

0.4.1

2 release files

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page