Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

xrops.grad — finite-difference calculus

xrops.grad provides partial derivatives and vector-calculus operators (∇, ∇·, ∇×, Δ) on labeled xarray grids, and the Gradient and Difference operators over them. The stencils are geoops’s diagnostic difference kernel (geoops.geom.difference, geostack OP-O7), which follows finitediffx’s rules on NumPy; xrops.grad adds the xarray layer. It replaces the former xrtoolz-grad (xrgrad) distribution, with the same functions and numbers.

Geometry dispatch

Every operator takes geometry=, which selects how coordinate values become physical distances:

GeometryAssumesSpacing
"cartesian"uniform grid, coords in metressingle Δ read from the coord, validated uniform (uniform_rtol)
"rectilinear"non-uniform 1-D coord per dim, in metrestrue coordinate differences
"spherical"lon/lat in degreesmetric factors below

For the spherical case, derivatives are taken in radians and scaled by the metric factors

∂∂x=1Rcos⁡φ∂∂λ,∂∂y=1R∂∂φ,\frac{\partial}{\partial x} = \frac{1}{R\cos\varphi}\frac{\partial}{\partial\lambda}, \qquad \frac{\partial}{\partial y} = \frac{1}{R}\frac{\partial}{\partial\varphi},

with λ\lambda longitude, φ\varphi latitude, and RR the Earth radius (grad.EARTH_RADIUS, 6 371 000 m).

Mixed geometry

divergence also accepts a per-dim sequence of geometries, one per entry in dims. The common case is a 3-D ocean budget: spherical horizontal, rectilinear vertical.

div = grad.divergence(
    ds,
    ("flux_e", "flux_n", "flux_w"),
    dims=("lon", "lat", "depth"),
    geometry=("spherical", "spherical", "rectilinear"),
)

The -(F_y \tan\varphi)/R curvature term is applied when both the longitude and latitude axes are spherical. Marking only one of them (or marking the vertical axis spherical) raises — the metric couples the pair, so neither is meaningful alone. Geometry-specific keywords are routed only to the backends that accept them, so radius= may accompany a mixed call without reaching the vertical axis.

Temporal coordinates

datetime64 and timedelta64 coordinates work anywhere a numeric one does; they are converted to seconds, so ∂c/∂t comes back per second. Values are anchored at the first sample before conversion, which keeps nanosecond steps representable in float64. A uniform time axis takes the cartesian path; an irregular one (a skipped day, monthly means) needs geometry="rectilinear". cftime/object-dtype calendars are rejected with an explicit error rather than silently mis-stepped.

All geometries expect the differentiated dimension to carry a 1-D coordinate: step sizes come from coordinate values, so a dimension without one raises ValueError, and 2-D (curvilinear) coordinates are rejected rather than silently producing a nonsense step. "rectilinear" additionally requires the coordinate to be strictly monotonic — repeated or reversing values would divide by a zero coordinate difference. Strictly descending coordinates are fine, as are scalar coordinates on axes a given derivative does not use (a lat derivative still works after da.sel(lon=...)).

Curvature corrections

On the sphere, divergence and curl are not plain sums of partials — the unit vectors themselves rotate with longitude. xrops.grad adds the curvature terms so results match the equivalent metpy.calc operators on lon/lat fields:

∇⋅F=1Rcos⁡φ∂Fx∂λ+1R∂Fy∂φ−Fytan⁡φR,\nabla\cdot\mathbf{F} = \frac{1}{R\cos\varphi}\frac{\partial F_x}{\partial\lambda} + \frac{1}{R}\frac{\partial F_y}{\partial\varphi} - \frac{F_y\tan\varphi}{R},
ζ=1Rcos⁡φ∂v∂λ−1R∂u∂φ+utan⁡φR.\zeta = \frac{1}{R\cos\varphi}\frac{\partial v}{\partial\lambda} - \frac{1}{R}\frac{\partial u}{\partial\varphi} + \frac{u\tan\varphi}{R}.

On spherical and rectilinear geometries the Laplacian is composed as Δf=∇⋅∇f\Delta f = \nabla\cdot\nabla f, so it inherits the corrections automatically. Only on Cartesian grids, where there is no curvature term to inherit, does laplacian instead sum direct second-derivative stencils ∑d∂2f/∂d2\sum_d \partial^2 f/\partial d^2 — see below.

Accuracy and stencils

accuracy= is finitediffx’s accuracy order; method= selects "central" (default), "forward", or "backward" stencils. Interior points use the requested central stencil; boundary points fall back to one-sided differences of the same order. Two practical notes:

Periodic boundaries

By default the first and last points of an axis fall back to one-sided stencils. On a genuinely periodic axis — global longitude being the usual case — that leaves a visible seam at the dateline. Pass periodic= to wrap instead, so those points are differentiated against the opposite edge and are as accurate as the interior:

zeta = grad.curl(
    ds, ("u", "v"), dims=("lon", "lat"),
    geometry="spherical", periodic="lon",
)

partial takes a bool (it acts on one axis); the multi-dim operators take a dimension name or a sequence of names, which must be among the dims being differentiated.

The axis is treated as endpoint exclusive — the period is n * step, so the last sample sits one step before the first repeats. This is what np.linspace(0, L, n, endpoint=False) and a 0…355 global longitude axis give you. A grid that stores both ends of the period (numpy’s default endpoint=True) repeats a physical location, which makes the seam point its own neighbour and roughly halves its derivative; drop the duplicate with da.isel(x=slice(0, -1)) first.

Which convention a grid uses cannot be read off the coordinate — both are uniform with the same step — so on the Cartesian path this is only a UserWarning, raised when the field happens to take the same value at both ends. A genuinely endpoint-exclusive field can do that too (period 3 over [0, 1, 2] with values [0, 1, 0]), so the warning is advisory and the computation proceeds. Spherical longitude is the exception: its period is known, so a grid that does not span exactly 360° is a hard error.

Further restrictions:

import numpy as np
import xarray as xr
from xrops import grad

x = np.linspace(0.0, 3.0, 4)
X, Y = np.meshgrid(x, x, indexing="xy")
coords = {"y": x, "x": x}
ds = xr.Dataset(
    {
        "u": xr.DataArray(-Y, dims=("y", "x"), coords=coords),
        "v": xr.DataArray(X, dims=("y", "x"), coords=coords),
    }
)

zeta = grad.curl(ds, ("u", "v"), dims=("x", "y"))       # rigid rotation → 2
div = grad.divergence(ds, ("v", "u"), dims=("x", "y"))  # ∇·(x, -y) → 0

For a worked spherical example (geostrophic currents and vorticity from sea-surface height), see the quickstart notebook.

NaN and land masks

A stencil that reads a NaN returns NaN. By default that is exactly what happens, so each masked cell costs a halo of roughly accuracy valid cells in every differentiated direction — on a masked ocean field, a strip of water lost all the way around every coastline, silently.

nan_policy="adaptive" picks, per point, the widest stencil lying wholly on finite data: centred where the full support is valid, then forward, then backward. Masked cells stay NaN.

zeta = grad.curl(
    ds, ("u", "v"), dims=("lon", "lat"),
    geometry="spherical", nan_policy="adaptive",
)

Measured against an analytic field with a synthetic coastline (docs/design/examples/xrgrad-nan-mask-comparison.py), at the cells adjacent to land:

cells lostRMS relative error
propagate (default)62%5.5e-05 on the survivors
fill, differentiate, re-mask0%6.5e-01
nan_policy="adaptive"0%7.2e-03 (accuracy=1), 1.4e-04 (accuracy=2)

Two things to take from that:

method= cannot be combined with nan_policy="adaptive" — the fallback chain is itself method selection — and the option composes with periodic=, so a masked global field can wrap and adapt in one call.

Grid metrics and constants

grid_metrics_from_coords derives the cell-width / face-area Datasets consumed by xrops.budgets from a Dataset’s lon/lat (and optional depth) coordinates. The physical constants EARTH_RADIUS, GRAVITY, and OMEGA are exported at the top level and replace the equivalent metpy.constants lookups.

API reference