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.

Spherical-harmonic derivatives

Part 5.2: Spherical-Harmonic Derivatives on a Lat-Lon Grid

Computing derivatives on the sphere via finite differences on a regular lat-lon grid runs into three problems: the meridian is not periodic (so naive Fourier along latitude is wrong), the zonal spacing Δx=acos⁡ϕ Δλ\Delta x = a\cos\phi\,\Delta\lambda shrinks to zero at the poles, and the metric factor 1/cos⁡ϕ1/\cos\phi in the vector calculus identities is singular there.

The standard fix — used in every spectral atmospheric model since the 1970s — is to expand the field in spherical harmonics YℓmY_\ell^m. Derivatives become algebraic operations on the spectral coefficients, and the pole singularity disappears because the basis functions themselves are regular on the whole sphere.

This notebook builds a spectral transform by hand: Fourier in longitude, normalized associated Legendre in latitude on a Gauss–Legendre grid. All derivatives live inside a coordax Field round-trip, so the spectral machinery composes with the rest of the stack.

1. Gauss–Legendre latitude grid

The Legendre transform along latitude uses

aℓm  =  ∫−11Pˉℓm(μ) f~m(μ) dμa_\ell^m \;=\; \int_{-1}^{1} \bar P_\ell^m(\mu)\, \tilde f_m(\mu) \, \mathrm{d}\mu

where μ=sin⁡ϕ\mu = \sin\phi. Gauss–Legendre quadrature with NϕN_\phi nodes evaluates this integral exactly when the integrand is a polynomial of degree ≤2Nϕ−1\le 2N_\phi - 1, which covers every truncation up to ℓmax⁡≤Nϕ−1\ell_{\max} \le N_\phi - 1.

Equispaced latitudes lose this property — they require twice as many points for the same spectral accuracy and still introduce a small quadrature error.

Gauss-Legendre grid: 48 lats × 96 lons, lmax=47
First 3 lats: [-87.15909456 -83.47893667 -79.77704565]
Last  3 lats: [79.77704565 83.47893667 87.15909456]
Quadrature weight sum: 2.000000  (should be 2.0 = ∫_{-1}^{1} dμ)

2. Normalized associated Legendre functions

We use the fully normalized associated Legendre functions Pˉℓm\bar P_\ell^m defined by

Pˉℓm(μ)  =  (2ℓ+1)(ℓ−m)!2(ℓ+m)!  Pℓm(μ),\bar P_\ell^m(\mu) \;=\; \sqrt{\tfrac{(2\ell+1)(\ell-m)!}{2(\ell+m)!}}\; P_\ell^m(\mu),

so that ∫−11Pˉℓm(μ) Pˉℓ′m(μ) dμ=δℓℓ′\int_{-1}^{1} \bar P_\ell^m(\mu)\,\bar P_{\ell'}^m(\mu)\, \mathrm{d}\mu = \delta_{\ell\ell'}. With this normalization the Gauss–Legendre quadrature identity becomes the discrete orthogonality relation

∑jwj Pˉℓm(μj) Pˉℓ′m(μj)  =  δℓℓ′.\sum_j w_j\, \bar P_\ell^m(\mu_j)\, \bar P_{\ell'}^m(\mu_j) \;=\; \delta_{\ell\ell'}.

We evaluate PℓmP_\ell^m via scipy.special.lpmv (Ferrers form, no Condon–Shortley phase) and multiply by the normalization factor computed in log-space so factorials stay numerically stable.

P̄[ℓ, m, μ] shape: (48, 48, 48)
Orthogonality error for m=2, 2 ≤ ℓ ≤ 5: 4.11e-15

3. Forward and inverse spherical-harmonic transforms

The full SHT is a Fourier transform in longitude followed by a Legendre transform in latitude. Working in the real-valued representation for a real scalar field ff:

  1. Longitude analysis: f~m(ϕj)=1Nλ∑kf(ϕj,λk) e−imλk\tilde f_m(\phi_j) = \frac{1}{N_\lambda} \sum_k f(\phi_j, \lambda_k)\,e^{-im\lambda_k} — a row-wise FFT delivering nonnegative wavenumbers m=0,1,…,Nλ/2m = 0, 1, \dots, N_\lambda/2.
  2. Latitude analysis: aℓm=∑jwj Pˉℓm(μj) f~m(ϕj)a_\ell^m = \sum_j w_j\, \bar P_\ell^m(\mu_j)\, \tilde f_m(\phi_j) — a matrix multiply of shape (ℓmax⁡+1)×Nϕ(\ell_{\max}+1) \times N_\phi times (Nϕ)(N_\phi) for each mm.

The inverse reverses both steps. The code is compact because numpy handles the Fourier transform and the Legendre transform is a plain einsum.

Round-trip (noise, lossy): max diff = 3.61e+00

Round-trip on a spherical-harmonic eigenfunction

By construction, Y53=Pˉ53(sin⁡ϕ) cos⁡(3λ)Y_5^3 = \bar P_5^3(\sin\phi)\,\cos(3\lambda) has a single nonzero real coefficient and should reconstruct to machine precision after a forward + inverse SHT.

Y_5^3 round-trip: max diff = 7.66e-15
Peak coefficient at (ℓ=5, m=3): |a| = 0.500000
Total energy off-peak: 0.00e+00

4. Derivatives in spectral space

With the basis in hand, derivatives become algebra on the coefficients.

OperatorSpectral action on aℓma_\ell^m
∂λ\partial_\lambda  im aℓm\;im\, a_\ell^m
∇2\nabla^2 (Laplace–Beltrami on sphere)  −ℓ(ℓ+1)/a2⋅aℓm\;-\ell(\ell+1)/a^2 \cdot a_\ell^m
cos⁡ϕ ∂ϕ\cos\phi\,\partial_\phithree-term recurrence in ℓ\ell (see below)

Note the absence of any 1/cos⁡ϕ1/\cos\phi factor — the pole singularity lives entirely in the physical-space metric, not in the spectral coefficients.

Meridional derivative via a three-term recurrence

The associated Legendre functions satisfy

(1−μ2) dPˉℓmdμ  =  −ℓ ϵℓ+1m Pˉℓ+1m  +  (ℓ+1) ϵℓm Pˉℓ−1m,ϵℓm=ℓ2−m24ℓ2−1.(1-\mu^2)\,\frac{\mathrm{d}\bar P_\ell^m}{\mathrm{d}\mu} \;=\; -\ell\,\epsilon_{\ell+1}^m\,\bar P_{\ell+1}^m \;+\; (\ell+1)\,\epsilon_\ell^m\,\bar P_{\ell-1}^m, \qquad \epsilon_\ell^m = \sqrt{\tfrac{\ell^2 - m^2}{4\ell^2 - 1}}.

Because μ=sin⁡ϕ\mu = \sin\phi and dμ=cos⁡ϕ dϕ\mathrm{d}\mu = \cos\phi\,\mathrm{d}\phi, the left-hand side equals cos⁡ϕ ∂ϕ\cos\phi\,\partial_\phi applied to Pˉℓm\bar P_\ell^m. Summing over the expansion f=∑ℓmaℓmPˉℓmeimλf = \sum_{\ell m} a_\ell^m \bar P_\ell^m e^{im\lambda} gives the spectral coefficients of cos⁡ϕ ∂ϕf\cos\phi\,\partial_\phi f:

bℓm  =  (ℓ−1) ϵℓm aℓ−1m  −  (ℓ+2) ϵℓ+1m aℓ+1m.b_\ell^m \;=\; (\ell-1)\,\epsilon_\ell^m\, a_{\ell-1}^m \;-\; (\ell+2)\,\epsilon_{\ell+1}^m\, a_{\ell+1}^m.

Working with cos⁡ϕ ∂ϕ\cos\phi\,\partial_\phi rather than ∂ϕ\partial_\phi is natural: every geophysical flux divergence picks up the same cos⁡ϕ\cos\phi weight through the volume element cos⁡ϕ dϕ dλ\cos\phi\,\mathrm{d}\phi\,\mathrm{d}\lambda, so the pole metric cancels out in the physics.

Sign and scale check on a spherical-harmonic eigenfunction

We already have f_y53 = Pˉ53(sin⁡ϕ)cos⁡(3λ)\bar P_5^3(\sin\phi)\cos(3\lambda). The spectral Laplacian predicts ∇2f=−ℓ(ℓ+1)/a2⋅f\nabla^2 f = -\ell(\ell+1)/a^2 \cdot f with ℓ=5\ell = 5, which we verify against a finite-difference Laplacian on a finer grid.

Spectral ∇² vs analytic: max err = 1.98e-25
Analytic ∇² magnitude:  max |Δf| = 7.99e-13 /m²

5. Wrapping the transforms into coordax

The pattern matches every other derivative primitive in this chapter: take a Field tagged with ('latitude', 'longitude'), untag to raw NumPy, apply the spectral operator, retag. The latitude axis carries the Gauss-Legendre nodes and the quadrature weights are implicit in the transform.

U field: ('time', 'latitude', 'longitude'), shape: (4, 48, 96)

A spectral derivative primitive

spectral_derivative untag-applies-retags. The helper takes a scalar spatial field (or a batched one, with leading dims vmapped) and a spectral operator on coefficient arrays.

∂U/∂λ: ('time', 'latitude', 'longitude'), range: -5.97e+01 to 5.97e+01
∇²U:   ('time', 'latitude', 'longitude'), range: -5.65e-11 to 2.11e-11 /(m²·(m/s))

6. Comparison to finite differences

The spectral method is exact (to round-off) for any field exactly representable in the truncated basis. To make the comparison honest we need a test field that actually lives in the basis — that means a smooth function on the sphere that vanishes at the poles for every m≠0m \neq 0.

Take f(ϕ,λ)=cos⁡2 ⁣ϕ cos⁡(2λ)=(1−sin⁡2ϕ)cos⁡(2λ)f(\phi, \lambda) = \cos^2\!\phi \, \cos(2\lambda) = (1 - \sin^2\phi) \cos(2\lambda). The (1−μ2)(1 - \mu^2) factor places this in the m=2m=2 Legendre sector, where every Pˉℓ2\bar P_\ell^2 already carries that factor; the field is a single SH mode (up to normalization) and its analytical λ-derivative is ∂λf=−2cos⁡2 ⁣ϕ sin⁡(2λ)\partial_\lambda f = -2 \cos^2\!\phi \, \sin(2\lambda).

Spectral ∂f/∂λ: max error vs analytic = 3.42e-14
Finite-difference ∂f/∂λ: max error vs analytic = 5.70e-03

The spectral method hits round-off while centered FD carries its usual O(Δλ2)\mathcal{O}(\Delta\lambda^2) error. The gap widens for higher-wavenumber signals — this is the spectral accuracy result that motivated the spectral transform method in atmospheric modeling in the first place.

A cautionary note: the comparison is only meaningful when the test field is actually bandlimited. A physically sensible field like sin⁡ϕcos⁡(2λ)\sin\phi \cos(2\lambda) is discontinuous at the poles (its value depends on λ at ϕ=±π/2\phi = \pm\pi/2, but the meridian converges to a single point there). Projecting it onto the SH basis introduces Gibbs-style errors near the pole that have nothing to do with the spectral method itself, and FD will often look “better” on the mismatch. Fit your diagnostics to the physics of your field, not just its Cartesian appearance.

7. Key patterns

  • Gauss–Legendre nodes in μ=sin⁡ϕ\mu = \sin\phi plus equispaced longitudes are the natural quadrature grid for spherical harmonics.
  • FFT in longitude handles the eimλe^{im\lambda} factor; a normalized associated Legendre matrix handles the Pˉℓm(μ)\bar P_\ell^m(\mu) factor.
  • Spectral-space derivatives are algebraic: ∂λ→im\partial_\lambda \to im, ∇2→−ℓ(ℓ+1)/a2\nabla^2 \to -\ell(\ell+1)/a^2, cos⁡ϕ ∂ϕ→\cos\phi\,\partial_\phi \to three-term ℓ\ell-recurrence.
  • No 1/cos⁡ϕ1/\cos\phi appears inside the spectral flow. The pole singularity — where it exists — is confined to the physical-space metric factor cos⁡ϕ\cos\phi after transform back.
  • Spectral accuracy: for smooth fields, error falls faster than any polynomial in Δx\Delta x.