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.1

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.1
File Size Uploaded
spharmgrid-0.4.1.tar.gz 328.6 kB Details

Built distribution (wheel)

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

Total release size: 415.1 kB

Release files / spharmgrid-0.4.1.tar.gz

Download URL spharmgrid-0.4.1.tar.gz
Size 328.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ad40b0bf2aa8d33cce158570d322ba7d52a640e4ca6d540672d2548b4b729be4
BLAKE2b-256 checksum
How to use checksums
802ef92fcb0fc07b2315b95a7c3fbf86ba3ffa83834b1fa2aba0d3157925c509
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 10, 2026.

Transparency log

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

Download URL spharmgrid-0.4.1-py3-none-any.whl
Size 86.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b615ee5349046359f01071ddfb7becbd9c7ede9f3af7ab0ccc048f749d1705cd
BLAKE2b-256 checksum
How to use checksums
ab030bed0f4e5b2dd4a51136e7b432d52e4d793a883b9aac811be1d9d0a01a1f
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 10, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.4.0

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