Skip to content

Budgets

Conservation-budget diagnostics. For a tracer \(\theta\) with flux \(\mathbf{F}\) and source \(Q\), the budget residual measures how far a dataset departs from exact conservation:

\[ r = \frac{\partial\theta}{\partial t} + \nabla\cdot\mathbf{F} - Q. \]

ControlVolumeIntegral and BoundaryFlux assemble the volume and surface terms, the second as the net flux through the faces that bound a region; the tracer-specific residuals specialise \(\theta\) to heat, salt, volume, or kinetic energy.

Building blocks

ControlVolumeIntegral

ControlVolumeIntegral(variable: str, *, volume_metrics: Dataset, region: DataArray | None = None, dims: Sequence[str] = ('z', 'lat', 'lon'), cell_volume_var: str = 'cell_volume')

Bases: Operator

Volume-weighted integral of a field over a control volume.

Integrates variable over dims weighted by the cell volumes in volume_metrics (optionally masked to region). The metrics are explicit, never auto-derived — build them with :func:xrops.grad.grid_metrics_from_coords if the data does not ship them.

Parameters:

Name Type Description Default
variable str

Name of the field to integrate.

required
volume_metrics Dataset

Dataset carrying the cell_volume_var weights.

required
region DataArray | None

Optional boolean mask selecting the control volume.

None
dims Sequence[str]

Dimensions integrated over.

('z', 'lat', 'lon')
cell_volume_var str

Name of the cell-volume variable in volume_metrics.

'cell_volume'

Returns:

Type Description

The volume integral as a DataArray (reduced over dims).

Source code in packages/xrtoolz-ops/src/xrops/budgets/operators.py
def __init__(
    self,
    variable: str,
    *,
    volume_metrics: xr.Dataset,
    region: xr.DataArray | None = None,
    dims: Sequence[str] = ("z", "lat", "lon"),
    cell_volume_var: str = "cell_volume",
) -> None:
    self.variable = variable
    self.volume_metrics = volume_metrics
    self.region = region
    self.dims = tuple(dims)
    self.cell_volume_var = cell_volume_var

BoundaryFlux

BoundaryFlux(region: DataArray, *, variable: str | None = None, u: str = 'u', v: str = 'v', thickness: str | None = None, dims: tuple[str, str] = ('lon', 'lat'), geometry: Geometry = 'spherical')

Bases: Operator

Net flux of a quantity out of a region, through its own faces.

See :func:xrops.budgets.boundary_flux.

Parameters:

Name Type Description Default
region DataArray

Boolean mask on the two horizontal dims.

required
variable str | None

Name of the quantity carried by the flow, or None for the volume flux.

None
u, v

Velocity component names.

required
thickness str | None

Name of the layer thickness, or None.

None
dims tuple[str, str]

(x_dim, y_dim) horizontal dims.

('lon', 'lat')
geometry Geometry

"spherical", "cartesian" or "rectilinear".

'spherical'

Returns:

Type Description

The flux as a DataArray, positive outward.

Source code in packages/xrtoolz-ops/src/xrops/budgets/operators.py
def __init__(
    self,
    region: xr.DataArray,
    *,
    variable: str | None = None,
    u: str = "u",
    v: str = "v",
    thickness: str | None = None,
    dims: tuple[str, str] = ("lon", "lat"),
    geometry: Geometry = "spherical",
) -> None:
    self.region = region
    self.variable = variable
    self.u = u
    self.v = v
    self.thickness = thickness
    self.dims = tuple(dims)
    self.geometry = geometry

BudgetResidual

Bases: Operator

Generic conservation residual ∂φ/∂t + ∇·F − source + sink.

Combines a precomputed tendency and flux divergence (with optional source / sink terms) into the budget-closure residual; a perfectly closed budget is zero. Inputs are passed to __call__, not the constructor.

Parameters:

Name Type Description Default
tendency

Local time tendency ∂φ/∂t.

required
flux_divergence

Divergence of the flux ∇·F.

required
source

Optional source term.

required
sink

Optional sink term.

required

Returns:

Type Description

The budget-closure residual DataArray.

Tracer-specific residuals

HeatBudgetResidual

HeatBudgetResidual(*, temp_var: str = 'theta', **kw: Any)

Bases: _TracerBudgetOp

Per-cell heat-budget residual ∂θ/∂t + ∇·(u θ) − Q.

Returns the per-cell residual (zero for a closed budget), with the flux through the faces of each cell. It does not consume volume/face metrics — multiply by cell_volume and integrate with :class:ControlVolumeIntegral for a control-volume closure.

Parameters:

Name Type Description Default
temp_var str

Temperature (θ) variable name.

'theta'
u_var

Eastward velocity variable name.

required
v_var

Northward velocity variable name.

required
w_var

Vertical velocity variable name, or None.

required
surface_flux_var

Optional surface heat-flux variable name.

required
time_dim

Time dimension name.

required
lat

Latitude coordinate name.

required
lon

Longitude coordinate name.

required
depth

Vertical coordinate name, or None for a 2-D budget.

required

Returns:

Type Description

The per-cell heat-budget residual DataArray.

Source code in packages/xrtoolz-ops/src/xrops/budgets/operators.py
def __init__(self, *, temp_var: str = "theta", **kw: Any) -> None:
    super().__init__(tracer_var=temp_var, **kw)

SaltBudgetResidual

SaltBudgetResidual(*, salt_var: str = 'so', **kw: Any)

Bases: _TracerBudgetOp

Per-cell salt-budget residual ∂S/∂t + ∇·(u S) − F.

Like :class:HeatBudgetResidual but for salinity; returns the per-cell residual (zero for a closed budget).

Parameters:

Name Type Description Default
salt_var str

Salinity (S) variable name.

'so'
u_var

Eastward velocity variable name.

required
v_var

Northward velocity variable name.

required
w_var

Vertical velocity variable name, or None.

required
surface_flux_var

Optional surface salt-flux variable name.

required
time_dim

Time dimension name.

required
lat

Latitude coordinate name.

required
lon

Longitude coordinate name.

required
depth

Vertical coordinate name, or None for a 2-D budget.

required

Returns:

Type Description

The per-cell salt-budget residual DataArray.

Source code in packages/xrtoolz-ops/src/xrops/budgets/operators.py
def __init__(self, *, salt_var: str = "so", **kw: Any) -> None:
    super().__init__(tracer_var=salt_var, **kw)

VolumeBudgetResidual

VolumeBudgetResidual(*, u_var: str = 'u', v_var: str = 'v', w_var: str | None = None, lat: str = 'lat', lon: str = 'lon', depth: str | None = 'depth')

Bases: Operator

Per-cell volume (continuity) residual ∇·u.

The horizontal + vertical velocity divergence; zero for a non-divergent (volume-conserving) flow.

Parameters:

Name Type Description Default
u_var str

Eastward velocity variable name.

'u'
v_var str

Northward velocity variable name.

'v'
w_var str | None

Vertical velocity variable name, or None.

None
lat str

Latitude coordinate name.

'lat'
lon str

Longitude coordinate name.

'lon'
depth str | None

Vertical coordinate name, or None for a 2-D budget.

'depth'

Returns:

Type Description

The per-cell continuity residual DataArray.

Source code in packages/xrtoolz-ops/src/xrops/budgets/operators.py
def __init__(
    self,
    *,
    u_var: str = "u",
    v_var: str = "v",
    w_var: str | None = None,
    lat: str = "lat",
    lon: str = "lon",
    depth: str | None = "depth",
) -> None:
    self.u_var = u_var
    self.v_var = v_var
    self.w_var = w_var
    self.lat = lat
    self.lon = lon
    self.depth = depth

KineticEnergyBudgetResidual

KineticEnergyBudgetResidual(*, u_var: str = 'u', v_var: str = 'v', forcing_vars: Sequence[str] | None = None, time_dim: str = 'time', lat: str = 'lat', lon: str = 'lon', depth: str | None = 'depth', w_var: str | None = None)

Bases: Operator

Per-cell kinetic-energy budget residual.

Residual of ∂KE/∂t + ∇·(u KE) − forcing with KE = ½(u² + v²); zero for a closed KE budget.

Parameters:

Name Type Description Default
u_var str

Eastward velocity variable name.

'u'
v_var str

Northward velocity variable name.

'v'
forcing_vars Sequence[str] | None

Optional forcing / dissipation variable names summed as the source term.

None
time_dim str

Time dimension name.

'time'
lat str

Latitude coordinate name.

'lat'
lon str

Longitude coordinate name.

'lon'
depth str | None

Vertical coordinate name, or None for a 2-D budget.

'depth'
w_var str | None

Vertical velocity variable name, or None.

None

Returns:

Type Description

The per-cell KE-budget residual DataArray.

Source code in packages/xrtoolz-ops/src/xrops/budgets/operators.py
def __init__(
    self,
    *,
    u_var: str = "u",
    v_var: str = "v",
    forcing_vars: Sequence[str] | None = None,
    time_dim: str = "time",
    lat: str = "lat",
    lon: str = "lon",
    depth: str | None = "depth",
    w_var: str | None = None,
) -> None:
    self.u_var = u_var
    self.v_var = v_var
    self.forcing_vars = None if forcing_vars is None else tuple(forcing_vars)
    self.time_dim = time_dim
    self.lat = lat
    self.lon = lon
    self.depth = depth
    self.w_var = w_var

Functional primitives (Layer 0)

These pure functions back the operators above; each takes xr.DataArray/xr.Dataset and a dim: argument.

control_volume_integral

control_volume_integral(ds: Dataset, *, variable: str, volume_metrics: Dataset, region: DataArray | None = None, dims: Sequence[str] = ('z', 'lat', 'lon'), cell_volume_var: str = 'cell_volume') -> DataArray

Volume-weighted integral ∫ var dV over a control volume.

Parameters:

Name Type Description Default
ds Dataset

Dataset containing variable.

required
variable str

Variable to integrate.

required
volume_metrics Dataset

Dataset carrying cell_volume_var (m³) on the same horizontal / vertical grid as ds[variable].

required
region DataArray | None

Optional boolean mask selecting the integration volume. True cells are kept. None integrates over the full grid.

None
dims Sequence[str]

Spatial dims to integrate over. Any other dims (e.g. time) are preserved on the result.

('z', 'lat', 'lon')
cell_volume_var str

Name of the volume variable in volume_metrics. Default "cell_volume".

'cell_volume'

Returns:

Type Description
DataArray

DataArray with the integrated value, reduced over dims.

Notes

For 2-D budgets without a vertical coordinate, pass volume_metrics whose cell_volume equals cell_area (the V4.4 helper does this automatically when depth=None) and set dims=("lat", "lon").

Source code in packages/xrtoolz-ops/src/xrops/budgets/_src/volume.py
def control_volume_integral(
    ds: xr.Dataset,
    *,
    variable: str,
    volume_metrics: xr.Dataset,
    region: xr.DataArray | None = None,
    dims: Sequence[str] = ("z", "lat", "lon"),
    cell_volume_var: str = "cell_volume",
) -> xr.DataArray:
    """Volume-weighted integral ``∫ var dV`` over a control volume.

    Args:
        ds: Dataset containing ``variable``.
        variable: Variable to integrate.
        volume_metrics: Dataset carrying ``cell_volume_var`` (m³) on
            the same horizontal / vertical grid as ``ds[variable]``.
        region: Optional boolean mask selecting the integration volume.
            ``True`` cells are kept. ``None`` integrates over the full
            grid.
        dims: Spatial dims to integrate over. Any other dims (e.g.
            ``time``) are preserved on the result.
        cell_volume_var: Name of the volume variable in
            ``volume_metrics``. Default ``"cell_volume"``.

    Returns:
        DataArray with the integrated value, reduced over ``dims``.

    Notes:
        For 2-D budgets without a vertical coordinate, pass
        ``volume_metrics`` whose ``cell_volume`` equals ``cell_area``
        (the V4.4 helper does this automatically when ``depth=None``)
        and set ``dims=("lat", "lon")``.
    """
    if cell_volume_var not in volume_metrics:
        raise ValueError(
            f"volume_metrics is missing variable {cell_volume_var!r}; "
            f"got {tuple(volume_metrics.data_vars)}."
        )
    field = ds[variable]
    cv = volume_metrics[cell_volume_var]
    weighted = field * cv
    if region is not None:
        weighted = weighted.where(region)
    int_dims = [d for d in dims if d in weighted.dims]
    out = weighted.sum(dim=int_dims, skipna=True)
    return out.rename(f"{variable}_volume_integral").astype(np.float64)

boundary_flux

boundary_flux(ds: Dataset, *, region: DataArray, variable: str | None = None, u: str = 'u', v: str = 'v', thickness: str | None = None, dims: tuple[str, str] = ('lon', 'lat'), geometry: Geometry = 'spherical') -> DataArray

The net flux of a quantity out of a region, through its own faces.

∮ q H u·n dl over the faces between the cells of the region and the others (diagnostix.budgets.boundary_flux): what passes from one cell of the region to another cancels. Its sum over a partition of a region is the flux out of the whole.

Parameters:

Name Type Description Default
ds Dataset

Dataset with the velocities and, if named, the quantity and the thickness.

required
region DataArray

Boolean mask on the two horizontal dims, True inside. It may not touch an edge of the domain; a whole circle of longitudes has none along lon.

required
variable str | None

Name of the quantity q; None for 1, the volume flux.

None
u, v

Velocity component names (m/s).

required
thickness str | None

Name of the thickness H (m) of a layer, such as the heights of the levels; None for 1, the flux per metre of depth.

None
dims tuple[str, str]

(x_dim, y_dim) horizontal dims. Default ("lon", "lat").

('lon', 'lat')
geometry Geometry

"spherical" (default; lon/lat in degrees), "cartesian" or "rectilinear".

'spherical'

Returns:

Type Description
DataArray

DataArray "boundary_flux", positive outward, on the dims of the

DataArray

fields but the two horizontal ones. The units are those of q m²/s,

DataArray

or m³/s with a thickness. A NaN beside a face of the boundary gives NaN.

Raises:

Type Description
ValueError

If region is not boolean on the two dims, has no cell or every cell, or touches an edge of the domain.

Source code in packages/xrtoolz-ops/src/xrops/budgets/_src/flux.py
def boundary_flux(
    ds: xr.Dataset,
    *,
    region: xr.DataArray,
    variable: str | None = None,
    u: str = "u",
    v: str = "v",
    thickness: str | None = None,
    dims: tuple[str, str] = ("lon", "lat"),
    geometry: Geometry = "spherical",
) -> xr.DataArray:
    """The net flux of a quantity out of a region, through its own faces.

    ``∮ q H u·n dl`` over the faces between the cells of the region and the
    others (``diagnostix.budgets.boundary_flux``): what passes from one cell of
    the region to another cancels. Its sum over a partition of a region is the
    flux out of the whole.

    Args:
        ds: Dataset with the velocities and, if named, the quantity and the
            thickness.
        region: Boolean mask on the two horizontal dims, ``True`` inside. It may
            not touch an edge of the domain; a whole circle of longitudes has
            none along ``lon``.
        variable: Name of the quantity ``q``; ``None`` for 1, the volume flux.
        u, v: Velocity component names (m/s).
        thickness: Name of the thickness ``H`` (m) of a layer, such as the
            heights of the levels; ``None`` for 1, the flux per metre of depth.
        dims: ``(x_dim, y_dim)`` horizontal dims. Default ``("lon", "lat")``.
        geometry: ``"spherical"`` (default; lon/lat in degrees),
            ``"cartesian"`` or ``"rectilinear"``.

    Returns:
        DataArray ``"boundary_flux"``, positive outward, on the dims of the
        fields but the two horizontal ones. The units are those of ``q`` m²/s,
        or m³/s with a thickness. A NaN beside a face of the boundary gives NaN.

    Raises:
        ValueError: If ``region`` is not boolean on the two dims, has no cell or
            every cell, or touches an edge of the domain.
    """
    xdim, ydim = dims
    if set(region.dims) != set(dims) or region.dtype != bool:
        raise ValueError(
            f"region must be boolean on the dims {dims!r}; got {region.dtype} on "
            f"{region.dims!r}."
        )
    inside = np.asarray(region.transpose(ydim, xdim).values)
    q = xr.ones_like(ds[u]) if variable is None else ds[variable]
    fields = [q, ds[u], ds[v]]
    if thickness is not None:
        # the kernel multiplies q by H, whatever dims H has
        fields[0] = q * ds[thickness]

    def kernel(q_, u_, v_, grid):
        volume = budgets.ControlVolume.from_region(inside, grid)
        return budgets.boundary_flux(q_, u_, v_, grid, volume)

    out = over_grid(kernel, *fields, dims=dims, geometry=geometry)
    return out.rename("boundary_flux")

budget_residual

budget_residual(tendency: DataArray, flux_divergence: DataArray, *, source: DataArray | None = None, sink: DataArray | None = None) -> DataArray

Combine the four canonical budget terms into a residual.

Sign convention (matches the V4.3 tracer / KE residuals)::

residual = tendency + flux_divergence - source + sink

where flux_divergence is ∇·(u φ) — the divergence of the advective flux out of each cell. The conservation equation ∂φ/∂t + ∇·(u φ) = source - sink rearranges to residual ≈ 0.

A closed budget has residual ≈ 0 within float tolerance.

Source code in packages/xrtoolz-ops/src/xrops/budgets/_src/residual.py
def budget_residual(
    tendency: xr.DataArray,
    flux_divergence: xr.DataArray,
    *,
    source: xr.DataArray | None = None,
    sink: xr.DataArray | None = None,
) -> xr.DataArray:
    """Combine the four canonical budget terms into a residual.

    Sign convention (matches the V4.3 tracer / KE residuals)::

        residual = tendency + flux_divergence - source + sink

    where ``flux_divergence`` is ``∇·(u φ)`` — the divergence of the
    advective flux *out* of each cell. The conservation equation
    ``∂φ/∂t + ∇·(u φ) = source - sink`` rearranges to ``residual ≈ 0``.

    A closed budget has ``residual ≈ 0`` within float tolerance.
    """
    res = tendency + flux_divergence
    if source is not None:
        res = res - source
    if sink is not None:
        res = res + sink
    return res.rename("budget_residual")

heat_budget_residual

heat_budget_residual(ds: Dataset, *, temp_var: str = 'theta', u_var: str = 'u', v_var: str = 'v', w_var: str | None = None, surface_flux_var: str | None = None, time_dim: str = 'time', lat: str = 'lat', lon: str = 'lon', depth: str | None = 'depth') -> DataArray

Per-cell heat-budget residual.

Computes ∂θ/∂t + ∇·(u θ) - F_surface where F_surface is an optional surface heat-flux source field. ρ c_p factors are omitted: the residual is in units of [θ] / s, not W/m³. For closure tests this is sufficient.

Parameters:

Name Type Description Default
ds Dataset

Dataset with potential temperature + velocities.

required
temp_var, u_var, v_var, w_var

Variable names. w_var=None silently skips the vertical-advection term — the residual is then a surface-only diagnostic.

required
surface_flux_var str | None

Optional source field on the surface plane.

None
time_dim str

Name of the time coordinate.

'time'
lat, lon

Names of horizontal coords (degrees).

required
depth str | None

Vertical coord name. None skips vertical advection.

'depth'

Returns:

Type Description
DataArray

Per-cell residual, same dims as ds[temp_var], NaN on the edge

DataArray

cells of the domain, which have a face outside the data; a whole

DataArray

circle of longitudes has no edge.

Notes

ρ c_p θ would be the proper heat content; we use θ directly so the closure test does not require a density field. Multiply the output by ρ_0 c_p upstream if you need W/m³.

Source code in packages/xrtoolz-ops/src/xrops/budgets/_src/heat.py
def heat_budget_residual(
    ds: xr.Dataset,
    *,
    temp_var: str = "theta",
    u_var: str = "u",
    v_var: str = "v",
    w_var: str | None = None,
    surface_flux_var: str | None = None,
    time_dim: str = "time",
    lat: str = "lat",
    lon: str = "lon",
    depth: str | None = "depth",
) -> xr.DataArray:
    """Per-cell heat-budget residual.

    Computes ``∂θ/∂t + ∇·(u θ) - F_surface`` where ``F_surface`` is an
    optional surface heat-flux source field. ``ρ c_p`` factors are
    omitted: the residual is in units of ``[θ] / s``, not W/m³. For
    closure tests this is sufficient.

    Args:
        ds: Dataset with potential temperature + velocities.
        temp_var, u_var, v_var, w_var: Variable names. ``w_var=None``
            silently skips the vertical-advection term — the residual
            is then a surface-only diagnostic.
        surface_flux_var: Optional source field on the surface plane.
        time_dim: Name of the time coordinate.
        lat, lon: Names of horizontal coords (degrees).
        depth: Vertical coord name. ``None`` skips vertical advection.

    Returns:
        Per-cell residual, same dims as ``ds[temp_var]``, NaN on the edge
        cells of the domain, which have a face outside the data; a whole
        circle of longitudes has no edge.

    Notes:
        ``ρ c_p θ`` would be the proper heat content; we use ``θ``
        directly so the closure test does not require a density field.
        Multiply the output by ``ρ_0 c_p`` upstream if you need W/m³.
    """
    return _cell_residual(
        ds,
        ds[temp_var],
        u_var=u_var,
        v_var=v_var,
        w_var=w_var,
        sources=() if surface_flux_var is None else (surface_flux_var,),
        time_dim=time_dim,
        lat=lat,
        lon=lon,
        depth=depth,
        name=f"{temp_var}_budget_residual",
    )

salt_budget_residual

salt_budget_residual(ds: Dataset, *, salt_var: str = 'so', u_var: str = 'u', v_var: str = 'v', w_var: str | None = None, surface_flux_var: str | None = None, time_dim: str = 'time', lat: str = 'lat', lon: str = 'lon', depth: str | None = 'depth') -> DataArray

Per-cell salt-budget residual ∂S/∂t + ∇·(u S) - F_surface.

As :func:~xrops.budgets.heat_budget_residual, NaN on the edge cells too.

Source code in packages/xrtoolz-ops/src/xrops/budgets/_src/salt.py
def salt_budget_residual(
    ds: xr.Dataset,
    *,
    salt_var: str = "so",
    u_var: str = "u",
    v_var: str = "v",
    w_var: str | None = None,
    surface_flux_var: str | None = None,
    time_dim: str = "time",
    lat: str = "lat",
    lon: str = "lon",
    depth: str | None = "depth",
) -> xr.DataArray:
    """Per-cell salt-budget residual ``∂S/∂t + ∇·(u S) - F_surface``.

    As :func:`~xrops.budgets.heat_budget_residual`, NaN on the edge cells too.
    """
    return _cell_residual(
        ds,
        ds[salt_var],
        u_var=u_var,
        v_var=v_var,
        w_var=w_var,
        sources=() if surface_flux_var is None else (surface_flux_var,),
        time_dim=time_dim,
        lat=lat,
        lon=lon,
        depth=depth,
        name=f"{salt_var}_budget_residual",
    )

volume_budget_residual

volume_budget_residual(ds: Dataset, *, u_var: str = 'u', v_var: str = 'v', w_var: str | None = None, lat: str = 'lat', lon: str = 'lon', depth: str | None = 'depth') -> DataArray

∇·u through the faces of each cell, + ∂w/∂z if w_var is given.

Closes to ≈ 0 for an incompressible flow. w_var=None returns the surface 2-D divergence only. The edge cells of the domain are NaN, as in :func:~xrops.budgets.heat_budget_residual.

Source code in packages/xrtoolz-ops/src/xrops/budgets/_src/volume_budget.py
def volume_budget_residual(
    ds: xr.Dataset,
    *,
    u_var: str = "u",
    v_var: str = "v",
    w_var: str | None = None,
    lat: str = "lat",
    lon: str = "lon",
    depth: str | None = "depth",
) -> xr.DataArray:
    """``∇·u`` through the faces of each cell, ``+ ∂w/∂z`` if ``w_var`` is given.

    Closes to ≈ 0 for an incompressible flow. ``w_var=None`` returns the
    surface 2-D divergence only. The edge cells of the domain are NaN, as in
    :func:`~xrops.budgets.heat_budget_residual`.
    """
    u = ds[u_var]
    return _flux_divergence(
        xr.ones_like(u),
        u=u,
        v=ds[v_var],
        w=ds[w_var] if w_var is not None else None,
        lat=lat,
        lon=lon,
        depth=depth,
    ).rename("volume_budget_residual")

kinetic_energy_budget_residual

kinetic_energy_budget_residual(ds: Dataset, *, u_var: str = 'u', v_var: str = 'v', forcing_vars: Sequence[str] | None = None, time_dim: str = 'time', lat: str = 'lat', lon: str = 'lon', depth: str | None = 'depth', w_var: str | None = None) -> DataArray

Per-cell KE-budget residual.

KE per unit mass E = 0.5 (u² + v²). Residual::

∂E/∂t + ∇·(u E) - Σ forcings

Forcings (e.g. wind-stress work) are supplied as additional source fields summed into the source term. The edge cells of the domain are NaN, as in :func:~xrops.budgets.heat_budget_residual.

Source code in packages/xrtoolz-ops/src/xrops/budgets/_src/ke.py
def kinetic_energy_budget_residual(
    ds: xr.Dataset,
    *,
    u_var: str = "u",
    v_var: str = "v",
    forcing_vars: Sequence[str] | None = None,
    time_dim: str = "time",
    lat: str = "lat",
    lon: str = "lon",
    depth: str | None = "depth",
    w_var: str | None = None,
) -> xr.DataArray:
    """Per-cell KE-budget residual.

    KE per unit mass ``E = 0.5 (u² + v²)``. Residual::

        ∂E/∂t + ∇·(u E) - Σ forcings

    Forcings (e.g. wind-stress work) are supplied as additional source
    fields summed into the source term. The edge cells of the domain are NaN,
    as in :func:`~xrops.budgets.heat_budget_residual`.
    """
    return _cell_residual(
        ds,
        0.5 * (ds[u_var] ** 2 + ds[v_var] ** 2),
        u_var=u_var,
        v_var=v_var,
        w_var=w_var,
        sources=tuple(forcing_vars or ()),
        time_dim=time_dim,
        lat=lat,
        lon=lon,
        depth=depth,
        name="kinetic_energy_budget_residual",
    )