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 Quickstart — Calculus on a lon/lat Grid

Open In Colab

xrops.grad is the finite-difference calculus of xrops, on geoops’s difference kernels: partial derivatives and vector-calculus operators (∇, ∇·, ∇×, Δ) on labeled xarray grids, with geometry dispatch that turns lon/lat degrees into physical metres automatically. This notebook builds a synthetic sea-surface-height eddy and walks the full chain from field to derived dynamics.

What you’ll learn:

  1. Geometry dispatch — the same partial call on cartesian and spherical grids.

  2. Spherical derivatives — geostrophic velocities from SSH via the metric factors 1Rcos⁡φ∂λ\tfrac{1}{R\cos\varphi}\partial_\lambda and 1R∂φ\tfrac{1}{R}\partial_\varphi.

  3. Relative vorticity via curl, including the spherical curvature correction.

  4. Cell areas from grid_metrics_from_coords for area-weighted integrals.

Setup

Python implementation: CPython
Python version       : 3.13.12
IPython version      : 9.16.1

numpy     : 2.5.2
xarray    : 2026.7.0
matplotlib: 3.11.1
xrops     : 0.4.0
geoops    : 0.10.3

Compiler    : Clang 21.1.4 
OS          : Linux
Release     : 6.8.0-1044-azure
Machine     : x86_64
Processor   : x86_64
CPU cores   : 16
Architecture: 64bit

A synthetic eddy on a lon/lat grid

A Gaussian sea-surface-height anomaly on a 0.25° grid in the mid-latitude North Atlantic. In geostrophic balance an SSH high drives an anticyclonic (clockwise, in the northern hemisphere) circulation — exactly what the derivatives below should recover.

<Figure size 1200x500 with 2 Axes>

Geometry dispatch in one line

Every xrops.grad operator takes geometry=. On a "spherical" grid the coordinate values are degrees, so a longitude derivative must be scaled by 1Rcos⁡φ\tfrac{1}{R\cos\varphi} to become a true ∂/∂x\partial/\partial x in SI units — xrops.grad does that internally:

∂η∂x=1Rcos⁡φ∂η∂λ\frac{\partial \eta}{\partial x} = \frac{1}{R\cos\varphi}\frac{\partial \eta}{\partial \lambda}
peak |∂η/∂x| ≈ 1.32e-06 m/m

Geostrophic velocities

With the Coriolis parameter f=2Ωsin⁡φf = 2\Omega\sin\varphi (using grad.OMEGA), geostrophic balance gives

ug=−gf∂η∂y,vg=gf∂η∂x.u_g = -\frac{g}{f}\frac{\partial \eta}{\partial y}, \qquad v_g = \frac{g}{f}\frac{\partial \eta}{\partial x}.
<Figure size 1200x500 with 2 Axes>

Relative vorticity

curl computes ζ=∂v/∂x−∂u/∂y\zeta = \partial v/\partial x - \partial u/\partial y and, for spherical geometry, adds the curvature correction +utan⁡φ/R+u\tan\varphi/R so the result matches metpy.calc.vorticity on lon/lat fields. The anticyclone shows as a negative-vorticity core ringed by positive shear, and normalising by ff gives the dimensionless Rossby number of the eddy.

<Figure size 1200x500 with 2 Axes>

Cell areas and area-weighted integrals

grid_metrics_from_coords derives cell widths and areas from the coordinates alone — on a 0.25° grid the meridional spacing is a constant ≈28 km while the zonal spacing shrinks with cos⁡φ\cos\varphi. These metric Datasets are what xrtoolz.budgets consumes, and they make area-weighted diagnostics one line.

dy ≈ 27.8 km per cell
dx at 30°N ≈ 24.1 km per cell
eddy volume anomaly ≈ 42.9 km³

Takeaways

  1. One geometry= keyword moves the same operator between uniform-cartesian, rectilinear, and lon/lat-degree grids — the metric factors and curvature corrections are applied for you.

  2. partial / gradient / curl / divergence / laplacian compose into real dynamics in a few lines: SSH → geostrophic velocities → vorticity.

  3. grid_metrics_from_coords turns coordinates into the cell/face metric Datasets used for budgets and area-weighted statistics.

For the full API, see the Calculus reference; for the maths, the xrtoolz-grad package page.