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.

xrops as the xarray layer

Design · kernels written once on plain arrays, run on DataArray through KernelOp, and xrops keeping only the work that needs labels

1Summary

xarray runs array maths through apply_ufunc, so array maths does not need to be written in xarray. We propose that xrops keeps only the work that needs labels and runs every kernel it uses through pipekit-array’s KernelOp, with the kernels written once on plain arrays in the lowest library that may hold them.

This page answers PL-Q12 on package layout. If it is decided, it supersedes OP-O1 (state-domain kernels live in xrops) and the xrops-L1 part of SL-O1, and it narrows PL-Q6: the atm, ocn and budgets families stay in xrops as operators on Dataset, while their kernels move to diagnostix. It assumes nothing about one workspace (PL-F), though every step is one PR instead of two if PL-F lands first.

2What xrops holds today

We read the main branch of xrtoolz on 12 Nov 2026. xrtoolz-ops has about 35,900 lines in 188 files, of which about 10,100 are docstrings. The split between kinds of code below is an estimate from sampling each family’s files and counting their apply_ufunc calls and imports, not a measurement.

FamilyLinesKernelDelegatedNeeds labels
metrics5,97940%20%40%
interpolate5,72240%15%45%
viz4,7300%5%95%
transforms3,60725%35%40%
grad2,57120%15%65%
geom2,4795%55%40%
ocn2,30930%0%70%
atm1,68225%0%75%
wavelets1,44660%15%25%
budgets94720%0%80%
the rest (learn, cf, mask, anomaly, extremes, inference, utils, the base)4,400about 5%about 20%about 75%
All35,900about 25%about 20%about 55%

“Kernel” means array maths that would run on NumPy or JAX given the positions of its dims; “delegated” means calls to xrft, xskillscore, regionmask, pywt, scikit-image, scikit-learn, functax, rioxarray or pipekit-array; “needs labels” means work that reads coordinates by value, groups by time, sets CF metadata, handles several variables, plans dask chunks or draws.

Four findings shape the proposal.

  1. About a quarter of xrops is kernel maths, and little of it is written as a kernel. metrics has separate NumPy kernels (_pixel_kernels.py, _segmented_psd_kernels.py) and interpolate has _smooth_kernels.py, but most families write the maths inline in functions that take a DataArray. ocn, atm and budgets are written as labelled arithmetic, for example u = -(grad.GRAVITY / f_field) * deta_dy in ocn/_src/kinematics.py, followed by CF attributes.

  2. There is no shared way to run a kernel on a DataArray. Each module writes its own apply_ufunc wrapper (_apply_pixel_kernel in metrics/_src/pixel.py, _apply_kernel_to_dataarray in interpolate/_src/smooth.py). xrops.lift exists but is narrow: it runs a geoops geometry operator item by item along one dim, through GeoArray.from_xarray and to_xarray, and nothing inside xrops uses it.

  3. Only grad calls geoops, and it duplicates part of what it calls. grad/_src/cartesian.py imports geoops.geom.difference and difference_offsets, but its _difference_adaptive and _select_by_finiteness are the same algorithm as geoops.geom._src.array.adaptive_difference, which was ported from xrtoolz’s design.

  4. Most of xrops needs labels and stays. viz alone is 4,730 lines, anomaly is all groupby("time.dayofyear") and rolling windows, and most of grad is dask map_overlap halo planning and coordinate spacing rather than stencils.

3Duplicates

These are the overlaps between xrops and geoops that the audit found. Only the first was compared line by line; the rest are candidates whose maths has to be compared before one copy is deleted.

Mathsxropsgeoops or functaxStatus
adaptive finite difference (the first finite of central, forward and backward, at falling accuracy)grad/_src/cartesian.py: _difference_adaptive, _select_by_finitenessgeoops.geom._src.array.adaptive_differencethe same algorithm
gap fill (Laplace, biharmonic, IDW)interpolate/_src/gap_fill.py and its own bridge _functax.py, plus a NumPy red–black SOR for boundary="wrap"geoops.restore._src.array (gap_fill_laplacian, gap_fill_biharmonic, gap_fill_idw) over the same functax planstwo bridges to one library
morphology (remove small objects, opening, clean mask)transforms/_src/morphology.py, on scikit-imagegeoops.mask._src.array, on scipy.ndimagesame maths, two backends
points to a grid (binning, IDW)interpolate/_src/binning.py, points_to_grid.pygeoops.geom._src.coregister.array (points_to_raster_binned, points_to_raster_idw)candidate
divergence, curl, Laplaciangrad/operators.py, with the spherical metric 1/(R cos φ)geoops.geom (divergence_2d, curl_2d, laplacian_2d)candidate; the spherical metric is xrops’ own
Gaussian smoothinginterpolate’s gaussian_smooth (gaussian_filter1d)geoops.restore’s gaussian_denoisecandidate

4How a kernel runs on a DataArray

pipekit-array’s KernelOp already does what each xrops module does by hand. A kernel declares the dims it consumes and produces as a generalized-ufunc signature, such as "(c)->()", and KernelOp runs it on raw arrays as is, on named carriers by moving the core dims last by name, and on a DataArray through the xarray adapter’s own path, one apply_ufunc that stays lazy on dask. geoops’ index operators are built this way (geoops._src.kernels.band_kernel over indices/_src/array.py), and GA-Q5 already lets them take a DataArray directly.

The kernel conventions decide what fits.

An operator in xrops then has three parts, as rule 7 on the package layout page and the three levels already require: the kernel (in xrops’ _src/array.py for now, in a lower library later), a KernelOp that runs it, and the xarray work around it (picking variables, coordinates in, CF attributes out).

5Where the kernels go

The homes come from each library’s “Takes over” line on its design page and from the stack layer map.

Kernels in xrops todayHomeWhat stays in xrops
the partials and grid_metrics_from_coords of grad; the vertical remap kernels behind remap_axisgridaxthe grad operators, dask halos, coordinate spacing
the maths of ocn, atm (vertical and wind) and budgets; climatology and anomaly maths; the spectra and decompositions of transforms and wavelets; the scores in metricsdiagnostixthe operators on Dataset, variable names, CF attributes, the groupby and rolling logic of anomaly
Earth’s radius, gravity, Ω, Coriolis, seawater density, column-unit conversionsgeophysixnothing
gap fill, IDW, RBF and conservative regriddingfunctaxthe interpolate operators
the empty Lagrangian stub in metrics/_src/lagrangian.pytraceraxDataArray views of trajectories
morphology and point binninggeoops’ copies (see the duplicates)the operators
CF, CRS handling in geom, regionmask masks, learn, inference, vizstaysall of it

When this page was written none of these libraries could take code. gridax, geophysix and tracerax now have a repository and code, and tracerax’s trajectory analysis (geostack#304) is there for the Lagrangian views to wrap. The diagnostix repository was created on 9 Nov 2026; on 24 Nov 2026 it has its scaffold, its test harness and a backlog (geostack#309), and no public module yet. This is why XR-D2 comes before XR-D3: splitting each family’s kernels into an array module inside xrops is useful on its own, because it removes the per-module wrappers and makes each kernel testable on NumPy and JAX. It also makes the later move a plain file move with no change to the operators.

6Stages

Each stage is a set of issues on xrtoolz (or geotoolz, after PL-F) and lands family by family.

  1. Remove the duplicates (XR-D1). Start with the adaptive difference, which is confirmed. For each candidate in the duplicates table, compare the maths on the fixtures of both packages, keep the lower copy, and make xrops call it.

  2. One kernel path (XR-D2). xrops gets a kernel_op helper on pipekit-array’s KernelOp, as geoops has band_kernel, and the per-module apply_ufunc wrappers go. Then each family in the table above, in order of kernel share (wavelets, metrics, interpolate, ocn, transforms, atm, grad, budgets), gets its _src/array.py, with labelled arithmetic rewritten as functions of arrays and dim arguments.

  3. Move down (XR-D3). As each target library is adopted and released, the matching array modules move there with their tests, and xrops imports them. geophysix’s constants go first, because every other library uses them.

  4. What remains (XR-D4). After stage 3, xrops holds operators, label work and plots. Its family names still match geoops’ where the job is the same (gate D1 on the package layout page).

7Gates

GatePasses when
X1 No duplicateseach row of the duplicates table is resolved, and a test asserts that xrops’ adaptive difference is geoops’ function
X2 One kernel pathno xrops module outside its kernel helper calls xr.apply_ufunc on a kernel of its own
X3 Array modulesevery _src/array.py in xrops imports neither xarray nor xrops, checked by an import scan
X4 Every backendcheck_kernel_op passes for each kernel on NumPy, on JAX and on a dask-backed DataArray
X5 Same resultseach moved operator matches its output from before the move on the existing fixtures, within float tolerance
X6 Still lazyan operator given a dask-backed DataArray returns a dask-backed result without computing
X7 Same public APIxrops’ __all__ lists the same names before and after each stage

8Risks

9Open questions

IDQuestionRecommendedAlternativeStatus
XR-Q1Does xrops expose an operator for a kernel that needs no label work?only when it adds label work (CF attributes, variable names, coordinates); otherwise callers run the lower library’s KernelOp on a DataArray directly, as GA-Q5 allows for geoopsan xrops operator for every kernel, so that every xarray user finds everything in xropsopen
XR-Q2Morphology: which backend survivesscipy.ndimage, as in geoops, which has no scikit-image in its slim basescikit-image, which xrops uses todayopen
XR-Q3Where the spherical metric 1/(R cos φ) livesgridax, with the other grid metricsxrops, as label work on latitudeopen
XR-Q4Does xrops.lift stayyes, for geoops geometry operators, which KernelOp cannot expressfold it into the kernel helperopen