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-2.2 — Laplacian (Gauss–Seidel) gap-fill + verify 3D regrid path

Source survey item: ocean-data-challenges-survey.md §2.2 Status: proposed Maps to upstream: src/mod_regrid.py from 2023_SSH_mapping_train_eNATL60_test_NATL60-.


1. Motivation

The upstream rec_regrid(ds_source, ds_target, field='ssh') does two things:

  1. 3-D (lon, lat, time) regridding via pyinterp.Grid3D + trivariate interpolation onto the target mesh.

  2. NaN extrapolation via pyinterp.fill.gauss_seidel — iterative Laplacian relaxation that fills masked cells consistent with the surrounding finite values (harmonic infill).

Of those two:

This issue closes the gap with a pure-numpy fillnan_laplacian — Gauss–Seidel (optionally SOR) relaxation, ~30 LOC, no pyinterp. The existing 3-D regrid path is verified by test rather than re-implemented.

2. User stories

2.1 Fill NaN holes in a regridded SSH field (primary)

I have an SSH map regridded onto a target grid. Land borders + sparse coverage left holes. I want them filled by harmonic infilling, like pyinterp.fill.gauss_seidel, without adding pyinterp as a dep.

import xarray as xr
from xrtoolz.interpolate import regrid_like, fillnan_laplacian

ds_target = regrid_like(
    ds_source, ds_target,
    dims=("lat", "lon", "time"),
    method="linear",
)
ds_filled = fillnan_laplacian(
    ds_target["ssh"],
    max_iter=1000, tol=1e-4, relaxation=1.0,
)

2.2 SOR for faster convergence on dense holes

ds_filled = fillnan_laplacian(
    ds_target["ssh"],
    relaxation=1.5,    # SOR; ~2× fewer iterations on typical fields
    max_iter=500,
)

2.3 As a Layer-1 Operator inside a Sequential

from xrtoolz.interpolate import RegridLike, FillNaNLaplacian
from xrtoolz.core import Sequential

regrid_and_fill = Sequential([
    RegridLike(target=ds_target, dims=("lat", "lon", "time")),
    FillNaNLaplacian(max_iter=1000, tol=1e-4),
])

3. What we already have / what’s missing

CapabilityCurrentThis proposal
3-D (lon, lat, time) regridregrid_like — xr.Dataset.interp handles datetime64verify via test, document
Convex-hull griddata infillfillnan_spatialunchanged
RBF infillfillnan_rbfunchanged
Temporal interpolate_nafillnan_temporalunchanged
Iterative Laplacian relaxation—add fillnan_laplacian
Operator wrapper—add FillNaNLaplacian

4. Design

4.1 Algorithm

Given a 2-D field u(i, j) with a NaN mask M, find the harmonic extension that satisfies ∇²u = 0 on M with Dirichlet BCs from the finite cells. Discretise the 5-point Laplacian and apply Gauss–Seidel relaxation in-place (with optional SOR over-relaxation):

For each iteration:
  for each (i, j) in M (in raster order):
    u_avg = 0.25 * (u[i-1, j] + u[i+1, j] + u[i, j-1] + u[i, j+1])
    u[i, j] = (1 - ω) * u[i, j] + ω * u_avg
  if max(|u - u_old|) < tol: break

True Gauss–Seidel (in-place updates) converges ~2× faster than Jacobi (snapshot updates). SOR with ω ≈ 1.5 typically halves Gauss–Seidel iteration count again on smooth fields.

Initial guess: nanmean(u) over the finite support — biased but stable and converges to the harmonic solution regardless.

Boundary condition: Neumann (reflective). Domain edges get zero-gradient mirrors — appropriate for ocean fields and avoids the zero-Dirichlet artifacts that biased pyinterp output near domain borders. Optionally boundary="wrap" for cylindrical longitude.

Implementation note: vectorise the sweep using array shifts (np.roll + edge fixups) rather than Python-level for (i, j) loops. This trades strict raster order for “checkerboard Jacobi-like in-place on red/black sweep” behaviour, with virtually identical convergence on smooth fields. For maximum-fidelity Gauss–Seidel users can pass mode="strict" (Cython-free Python loop, slower).

4.2 Layer 0 — xarray primitive

# src/xrtoolz/interpolate/_src/gap_fill.py — new function alongside fillnan_*
def fillnan_laplacian(
    da: xr.DataArray, *,
    max_iter: int = 1000,
    tol: float = 1e-4,
    relaxation: float = 1.0,            # 1.0 = Gauss-Seidel; 1.5 ≈ optimal SOR
    boundary: str = "reflect",          # "reflect" | "wrap"
    lon: str = "lon",
    lat: str = "lat",
) -> xr.DataArray:
    """Fill NaN cells via iterative harmonic relaxation (Gauss-Seidel / SOR).

    Solves ∇²u = 0 on the masked region with Dirichlet BCs from the
    finite cells. Stops when max(|u_new − u_old|) < ``tol`` or after
    ``max_iter`` iterations, whichever comes first.

    Operates slice-by-slice along any leading dims via xr.apply_ufunc.
    All-NaN and all-finite slices pass through unchanged.

    Parameters
    ----------
    da
        DataArray with at least ``lon`` and ``lat`` dims.
    max_iter
        Iteration cap. Default 1000.
    tol
        Convergence threshold on `max(|Δu|)` per iteration. Default 1e-4.
    relaxation
        Successive over-relaxation factor ω. ω=1.0 → vanilla
        Gauss–Seidel; 1.0<ω<2.0 → SOR (faster but can diverge for
        pathological masks). Default 1.0.
    boundary
        ``"reflect"`` (Neumann, default) or ``"wrap"`` (cylindrical).
    lon, lat
        Coordinate names.

    Returns
    -------
    xr.DataArray
        Same-shape array with NaN cells filled.
    """

~35 LOC.

4.3 Layer-1 Operator

# src/xrtoolz/interpolate/operators.py
class FillNaNLaplacian(Operator):
    """Iterative Laplacian gap-fill operator."""

    def __init__(self, *,
                 max_iter: int = 1000,
                 tol: float = 1e-4,
                 relaxation: float = 1.0,
                 boundary: str = "reflect",
                 lon: str = "lon",
                 lat: str = "lat"): ...

    def __call__(self, ds: xr.Dataset) -> xr.Dataset:
        return ds.map(
            lambda da: fillnan_laplacian(da, **self._kw)
            if {self.lat, self.lon} <= set(da.dims) else da
        )

    def get_config(self) -> dict[str, Any]: ...

Standard pattern. Variables that don’t carry both lat and lon pass through.

4.4 3-D regrid: verified, not re-implemented

regrid_like(dims=("lat","lon","time")) already does what the upstream pyinterp.trivariate step does. The required test fixtures:

def test_regrid_like_3d_with_datetime64_time():
    src = make_synthetic_field(dims=("time", "lat", "lon"))
    tgt = make_target_grid(diff_in_all_three_dims=True, datetime64=True)
    out = regrid_like(src, tgt, dims=("lat", "lon", "time"), method="linear")
    assert_allclose(out, expected_analytic_on_target)

Plus a one-line docstring update on regrid_like noting the 3-D pattern explicitly.

4.5 Module docstring nudge

Soften the gap_fill.py header — replace:

“for Gauss-Seidel or ESMF-conservative regridding, use those libraries directly”

with:

“for ESMF-conservative regridding use xesmf directly. For Gauss-Seidel-style harmonic infill see :func:fillnan_laplacian.”

5. Library leverage

NeedLibrary
Array shifts / boundary handlingnumpy.roll + edge slices
Slice-by-slice over leading dimsxarray.apply_ufunc(vectorize=True) (matches existing fillnan_* pattern)
Datetime64 interp on time axisxr.Dataset.interp (built-in)

No new dependencies. The implementation is pure numpy.

6. Public API surface

xrtoolz.interpolate.fillnan_laplacian(da, *, max_iter, tol, relaxation,
                                       boundary, lon, lat)
xrtoolz.interpolate.FillNaNLaplacian(...)

Re-exported from xrtoolz.interpolate.__init__.

7. Tests

TestAsserts
Analytic harmonic field with a holeInfilled values match analytic within tol
All-NaN sliceReturns all-NaN unchanged
All-finite sliceNo change
Convergence: SOR ω=1.5Fewer iterations than ω=1.0 on the same field
max_iter capHonoured even when tol not met
tol early stopIteration count drops as tol loosens
Neumann boundaryCorner NaN filled via 2-neighbor reflection
boundary="wrap"Longitude-edge NaN filled across 0/360 seam
Multi-leading-dim DataArray (time, lat, lon)Iterates per slice independently
Operator round-trip via get_configReconstructed Operator produces identical output
3-D regrid_like(dims=("lat","lon","time")) with datetime64Reproduces analytic on a different target grid

Target: ~11 cases.

8. Out of scope

9. Effort

≈55 LOC implementation + ≈80 LOC tests. Single PR.

SliceLOC
fillnan_laplacian35
FillNaNLaplacian operator20
3-D regrid integration test + docstring nudge5 + tests
Tests~80
Docs / re-exports10

10. Risks / open questions

  1. Naming. fillnan_laplacian vs fillnan_gauss_seidel vs fillnan_relaxation. Recommend fillnan_laplacian — describes the math (harmonic infill via Laplace’s equation) and matches the existing family style (fillnan_rbf, fillnan_spatial).

  2. Default relaxation. ω=1.0 (vanilla Gauss–Seidel) is robust; ω=1.5 (SOR) is faster but can diverge on pathological masks. Default to 1.0; document SOR for advanced users.

  3. Default boundary. Neumann ("reflect") recommended for ocean fields; avoids zero-Dirichlet artifacts. Wrap mode available for cylindrical longitude.

  4. Strict-Gauss–Seidel vs vectorised red/black sweep. Vectorised default for speed. Optional mode="strict" (Python loop) for exact reference behaviour — skip in v1 unless needed.

  5. Dask compatibility. apply_ufunc(vectorize=True) works on dask-backed inputs but materialises per slice. For multi-time-step fields this is fine; for huge spatial fields users may want parallel=True. Document; add as kwarg if needed.

  6. Initial guess. nanmean(u) is biased but stable. Linear-from- griddata would converge faster but couples gap_fill modules. Recommend nanmean for v1 simplicity.