Spherical harmonic spectra

Project a field on the sphere onto spherical harmonics and read off the degree (ℓ) energy spectrum. We synthesize a field from known modes Y₂¹ and Y₅⁻³ on FastTransforms' Fejér first-rule grid and recover them with the structured FSHTSpectralBackend.

using FlowFieldSpectra: FlowFieldSpectra as FFS
using FastSphericalHarmonics: FastSphericalHarmonics as FSH
using SpectralBackends: SpectralBackends as SB
using FlowGeometries: FlowGeometries as FG

lmax = 16
Nθ = lmax + 1
Nφ = 2 * lmax + 1

# Fejér first-rule grid (FlowGeometries) — the grid FastSphericalHarmonics transforms on. Fields are
# (nlon, nlat) tensors in FlowGeometries' longitude-first layout.
grid = FG.Connectivity.structured_grid(Float64, FG.SphericalSampling.Fejer1Sampling(), Nθ)

# Real coefficients of the real spherical harmonics give a real field.
C = zeros(Nθ, Nφ)
C[FFS.sph_mode_index(2, 1)] = 1.0
C[FFS.sph_mode_index(5, -3)] = 0.5
f = FFS.synthesize(grid, C, (Nθ, Nφ))             # (nlon, nlat) field on the grid

coeffs, _ = FFS.calculate_spectrum(grid, f, (Nθ, Nφ); transform = SB.FSHTSpectralBackend())
deg, E_l = FFS.spherical_energy_spectrum(coeffs)

Degree energy spectrum E(ℓ) with energy only at ℓ = 2 and ℓ = 5

Energy appears only at degrees ℓ = 2 and ℓ = 5, as expected. Scattered points on the sphere are handled the same way with an FG.Grids.UnstructuredGrid over a SphericalGeometry and the NUFSHTSpectralBackend, by quadrature (the default) or by least-squares fit with analysis = LeastSquares() (use a well-distributed set such as a Fibonacci sphere so the fit is well-conditioned; analysis_status(plan) reports whether it converged).