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.

ODC-3.1 — Spectral KE flux + dual-cascade companions (enstrophy flux, integral / Taylor scales, slope fitter, compensated spectra)

Source survey item: ocean-data-challenges-survey.md §3.1 Status: proposed Maps to upstream: src/mod_powerspec.py from 2024c_DC_4DMedSea-ESA.


1. Motivation

The 2024c MedSea repo’s mod_powerspec.py (635 LOC) is mostly redundant with xrft + our existing power_spectrum / cross_spectrum / coherence. The genuinely new piece is spectra_flux(u, v, …) — the kinetic-energy spectral flux Π(k) that diagnoses energy exchange between scales:

“Energy injected at scale k_inj cascades upscale (large eddies) or downscale (dissipation) — the sign and shape of Π(k) tells you which.”

Spectral KE flux is the standard mesoscale-turbulence diagnostic and absent from xrtoolz. While we’re in the neighborhood, four companion primitives that are universally used alongside KE flux — all sharing the same Fourier machinery — fold in for ~30 LOC each:

This issue ships all five in one PR, all in transforms/_src/fourier.py alongside the existing power_spectrum / cross_spectrum.

2. User stories

2.1 KE flux + enstrophy flux on a 2-D velocity field (primary)

I have a gridded (u, v) snapshot and want both KE and enstrophy spectral fluxes to check the dual cascade.

import xarray as xr
from xrtoolz.transforms import ke_spectral_flux, enstrophy_spectral_flux

ds_uv = xr.open_dataset("velocity_snapshot.nc")  # has u, v on (x, y)

ds_ke = ke_spectral_flux(ds_uv["u"], ds_uv["v"], dim=("x", "y"))
ds_z  = enstrophy_spectral_flux(ds_uv["u"], ds_uv["v"], dim=("x", "y"))

# Each Dataset has data_vars: transfer (T(k)), flux (Π(k))
# coord: freq_r (isotropic wavenumber)

2.2 Time-mean flux

ds_ke = ke_spectral_flux(
    ds["u"], ds["v"],
    dim=("x", "y"),
    avg_dims=("time",),
)

2.3 Diagnose inertial-range slope

I want the slope of E(k) between 50 km and 200 km wavelengths.

from xrtoolz.transforms import power_spectrum, fit_spectral_slope

psd = power_spectrum(ds["ssh"], dim=("x", "y"), isotropic=True)

slope, intercept = fit_spectral_slope(
    psd,
    wavenumber_dim="freq_r",
    k_min=1 / 200.0,    # 1/km
    k_max=1 / 50.0,
)
print(f"slope = {slope:.2f}")   # e.g. -2.0 → SQG

2.4 Compensated spectrum for a clean visual

from xrtoolz.transforms import compensated_spectrum

psd_compensated = compensated_spectrum(psd, wavenumber_dim="freq_r", exponent=2.0)
psd_compensated.plot()    # flat plateau wherever true k^{-2} regime holds

2.5 Single-number length scales

from xrtoolz.transforms import integral_scale

ell    = integral_scale(psd, wavenumber_dim="freq_r", moment=1)   # integral scale
lambda_T = integral_scale(psd, wavenumber_dim="freq_r", moment=2) # Taylor microscale

2.6 Operators inside a Sequential

from xrtoolz.transforms import KESpectralFlux
from xrtoolz.core import Sequential

pipeline = Sequential([
    KESpectralFlux(u_var="u", v_var="v", dim=("x", "y"), avg_dims=("time",)),
    # downstream: plot or compute integral diagnostics
])

3. What we already have / what’s missing

CapabilityCurrentThis proposal
power_spectrum (1-D and 2-D isotropic)fourier.py:43unchanged
cross_spectrum, coherence, stftfourier.pyunchanged
Window / detrend kwargsvia xrftreuse
Radial binning (2-D → 1-D iso)via xrft.isotropic_power_spectrumreuse
KE spectral flux—add ke_spectral_flux
Enstrophy spectral flux—add enstrophy_spectral_flux
Integral scale / Taylor microscale—add integral_scale(..., moment=)
Spectral slope fitter—add fit_spectral_slope
Compensated spectrum—add compensated_spectrum
Operator wrappers—add KESpectralFlux, EnstrophySpectralFlux

4. Design

4.1 Algorithm — KE spectral flux

Following Frisch (1995) / Aluie (2018) / standard mesoscale practice:

1. Window + detrend u, v.
2. Fourier-space gradients via 1j·2π·k:
     ∂u/∂x = ifft(2π·1j·k_x · fft(u)); etc. for ∂u/∂y, ∂v/∂x, ∂v/∂y.
3. Advection terms in physical space:
     φ₁ = u·∂u/∂x + v·∂u/∂y
     φ₂ = u·∂v/∂x + v·∂v/∂y
4. 2-D KE transfer:
     T(kx, ky) = -Re(û*·F[φ₁] + v̂*·F[φ₂]) / (Ni·Nj)²
5. Radially integrate to T(k).
6. Cumulative sum from high-k:
     Π(k) = Σ_{k' ≥ k} T(k')   ≡  np.cumsum(T[::-1])[::-1]

Π(k) is the rate at which KE crosses scale k from larger scales toward smaller scales. Sign convention is the standard upstream convention.

4.2 Algorithm — enstrophy spectral flux

Same template with vorticity ζ = ∂v/∂x − ∂u/∂y in place of (u, v):

T_Z = -Re(ẑ* · F[u·∂ζ/∂x + v·∂ζ/∂y]) / (Ni·Nj)²
Π_Z(k) = Σ_{k' ≥ k} T_Z(k')

Computes ζ from the same Fourier-space gradients of u, v — no additional FFT cost beyond what KE flux already pays.

4.3 Layer 0 — primitives

# src/xrtoolz/transforms/_src/fourier.py

def ke_spectral_flux(
    u: xr.DataArray, v: xr.DataArray, *,
    dim: Sequence[str],                         # 2 spatial dims, e.g. ("x", "y")
    window: str | None = "tukey",
    detrend: str | None = "linear",
    avg_dims: Sequence[str] | None = None,
    return_2d: bool = False,
) -> xr.Dataset:
    """Kinetic-energy spectral flux Π(k) from 2-D velocity (u, v).

    Returns Dataset with:
      - transfer: T(k), the per-scale KE transfer rate
      - flux:     Π(k), cumulative integral from k_max downward
      - (transfer_2d if ``return_2d=True``)
    """

def enstrophy_spectral_flux(
    u: xr.DataArray, v: xr.DataArray, *,
    dim: Sequence[str],
    window: str | None = "tukey",
    detrend: str | None = "linear",
    avg_dims: Sequence[str] | None = None,
    return_2d: bool = False,
) -> xr.Dataset:
    """Enstrophy spectral flux Π_Z(k). Same structure as ke_spectral_flux."""

def integral_scale(
    psd: xr.DataArray, *,
    wavenumber_dim: str = "freq_r",
    moment: int = 1,
) -> xr.DataArray:
    """Energy-weighted length scale.

    moment=1 → integral scale ℓ = ∫ψ dk / ∫k·ψ dk
    moment=2 → Taylor microscale λ where λ² = ∫ψ dk / ∫k²·ψ dk
    moment=p → generalized: ℓ_p = (∫ψ dk / ∫k^p·ψ dk)^(1/p) for p > 1
    """

def fit_spectral_slope(
    psd: xr.DataArray, *,
    wavenumber_dim: str = "freq_r",
    k_min: float, k_max: float,
) -> tuple[float, float]:
    """Linear fit on log-log over (k_min, k_max). Returns (slope, intercept).

    `numpy.polyfit(log(k), log(psd), 1)` over the inertial range.
    """

def compensated_spectrum(
    psd: xr.DataArray, *,
    wavenumber_dim: str = "freq_r",
    exponent: float,
) -> xr.DataArray:
    """Return psd * k^exponent. Flat where psd ∝ k^{-exponent}."""

fit_spectral_slope and compensated_spectrum are dim-agnostic — work on isotropic 2-D PSDs, 1-D along-track PSDs, frequency PSDs, anything 1-D in the chosen dim.

4.4 Implementation re-use

Both flux functions share an internal helper for the 4 Fourier-space gradients:

def _fourier_uv_gradients(u, v, *, dim, window, detrend, avg_dims):
    """Return (du_dx, du_dy, dv_dx, dv_dy, k_radial, freq_r_coord, fft_norm)."""

Used by ke_spectral_flux (forms φ₁, φ₂) and enstrophy_spectral_flux (forms ζ then u·∇ζ). Saves ~40 LOC of duplication.

4.5 Layer-1 Operators

# src/xrtoolz/transforms/operators.py
class KESpectralFlux(Operator):
    """Single-Dataset KE spectral flux operator.

    Reads u and v from the Dataset by name; returns a Dataset with
    transfer + flux variables.
    """

    def __init__(self, *,
                 u_var: str, v_var: str,
                 dim: Sequence[str],
                 window: str | None = "tukey",
                 detrend: str | None = "linear",
                 avg_dims: Sequence[str] | None = None,
                 return_2d: bool = False): ...

class EnstrophySpectralFlux(Operator):     # mirrors KESpectralFlux exactly

integral_scale, fit_spectral_slope, and compensated_spectrum are small enough to skip Operator promotion in v1 — primitive functions suffice.

5. Library leverage

NeedLibrary
FFT / IFFT / window / detrendxrft (already a dep, used by power_spectrum)
1j·k derivativexrft.fft(...) * (1j * 2π * k) (manual)
Radial 2-D → 1-D binningxrft.isotropic_power_spectrum’s internals; alternative: numpy.histogram(weights=...)
Cumulative integralnumpy.cumsum
Log-log slope fitnumpy.polyfit
Power-law multiplicationxarray broadcasting

No new dependencies. Pure xrft + numpy + xarray.

6. Public API surface

# Layer 0 primitives
xrtoolz.transforms.ke_spectral_flux(u, v, *, dim, window, detrend,
                                     avg_dims, return_2d)
xrtoolz.transforms.enstrophy_spectral_flux(u, v, *, dim, window, detrend,
                                            avg_dims, return_2d)
xrtoolz.transforms.integral_scale(psd, *, wavenumber_dim, moment)
xrtoolz.transforms.fit_spectral_slope(psd, *, wavenumber_dim, k_min, k_max)
xrtoolz.transforms.compensated_spectrum(psd, *, wavenumber_dim, exponent)

# Operators
xrtoolz.transforms.KESpectralFlux(...)
xrtoolz.transforms.EnstrophySpectralFlux(...)

All re-exported from xrtoolz.transforms.__init__.

7. Tests

TestAsserts
ke_spectral_flux Taylor–Green vortexT(k) peaks at the imposed wavenumber; off-peak ≈ 0
ke_spectral_flux KE conservationT(k) integrated over all k ≈ 0 to fp tol
ke_spectral_flux flux endpointsΠ(0) ≈ -Π(k_max) ≈ 0 (pure transfer, no source/sink)
ke_spectral_flux avg_dims=("time",)output shape correct; mean of per-step fluxes matches
ke_spectral_flux return_2d=Trueexposes transfer_2d with correct shape
enstrophy_spectral_flux on Taylor–GreenΠ_Z opposite sign to Π_KE in the inertial range (dual cascade)
enstrophy_spectral_flux conservationenstrophy budget closes within fp tol
integral_scale moment=1 on Gaussian PSDmatches analytic mean wavenumber
integral_scale moment=2 on Gaussian PSDmatches analytic Taylor microscale
integral_scale moment=1 on monochromatic spikereturns spike’s wavenumber
fit_spectral_slope on synthetic k^{-5/3}slope ≈ -5/3 within 1%
fit_spectral_slope on synthetic k^{-3}slope ≈ -3 within 1%
fit_spectral_slope k_min/k_max window honouredexcluded points don’t affect fit
compensated_spectrum flatnessk^n · k^{-n} ≈ const (fp noise)
KESpectralFlux Operator round-trip via get_configreconstructed produces identical Dataset
EnstrophySpectralFlux Operator round-tripidentical Dataset

Target: ~16 cases.

8. Out of scope

9. Effort

≈190 LOC implementation + ≈140 LOC tests. Single PR.

SliceLOC
_fourier_uv_gradients shared helper30
ke_spectral_flux40
enstrophy_spectral_flux30
integral_scale (with moment= kwarg)15
fit_spectral_slope15
compensated_spectrum5
KESpectralFlux, EnstrophySpectralFlux operators40
Tests~140
Docs / re-exports15

10. Risks / open questions

  1. Where it lives. Two options: (a) transforms/_src/fourier.py alongside power_spectrum (recommended — math is generic Fourier transfer), (b) new ocn/_src/spectral_flux.py (since the formula assumes incompressible momentum advection). Recommend (a).

  2. Sign convention. Π(k) = downscale flux (energy from large to small) per upstream convention. Document explicitly; sign matters when comparing across papers.

  3. Default window/detrend. Upstream defaults to Tukey + Both (mean + linear). Closest xrft equivalent: window='tukey', detrend='linear'. Document the small mismatch with upstream “Both” (which detrends along both axes separately).

  4. Radial binning. Reuse xrft.isotropic_power_spectrum’s binning strategy when possible. If not directly callable on a transfer field, write a minimal _radial_integrate(field_2d, k_2d, k_1d) helper.

  5. Periodic BC assumption. All FFT-based fluxes assume periodic boundary conditions. Document; users with non-periodic domains should window aggressively (Tukey with small α, or Hanning).

  6. integral_scale moment > 2. Generalized (∫ψ / ∫k^p·ψ)^(1/p) handles arbitrary moment. Document common cases (1, 2); allow anything via the kwarg.

  7. Operator promotion for slope/integral/compensated. Skip in v1 — primitive functions are ergonomic enough. Promote later if a Sequential calls for them.