Where does a piece of code belong?
GeoStack · background · the role ladder, read as grammar
Abstract¶
A library is a small language: nouns, the verbs that act on them, the adverbs that say how, and sentences and paragraphs built from them. The role ladder names seven rungs of that language, from a ⓪ structure to a ⑥ recipe, with setup beside it and orchestration above it, and says what each rung may use. This page gives the ladder and its rules. Two worked examples climb it one rung at a time, on two different sciences: a trace-gas source, from one scene to how often it leaks, and an ocean data-assimilation window.
The problem: code with no address¶
Most scientific code starts as one block that does the whole job: a notebook cell that turns radiance into an emission rate, or a script that runs one assimilation window from reading the background to writing the analysis. Every line in it can be right. What is missing is any answer to “where does each piece belong?”, so every piece belongs to that one block. Three ordinary requests then become hard:
Reuse. Another script needs one piece, such as the noise level of a filter. It has to copy the block.
Swap. Someone wants to try another covariance estimator or another taper. They have to edit the middle of the block.
Save. Someone wants to rerun last month’s settings exactly. Nothing records which version of the block, with which numbers, produced the result.
The ladder is the fix: give every piece a level, and a rule for what each level may touch. Each worked example opens with its own version of the block: the trace-gas cell and the assimilation script.
Reading the figures¶
The line marks used in the figures on this page and in both worked examples.
The ladder at a glance¶
Read it from the bottom: nouns, the verbs that act on them, the adverbs that say how, the noun phrases that carry fitted state, the sentence that does a method, and above the dashed line the sentence with its own settings and the paragraph that makes a job. The two columns are the two worked examples; the trace-gas column shows the retrieval names, and its page adds segmentation, quantification and persistency. Each rung holds a different science’s names and the same kind of thing.
rung · grammartrace gas one scene, retrieval namesocean one DA window
orchestration not a rungevery new EMIT sceneevery window, nightly
⑥ recipe paragraphplume_recipeDACycle
⑤ step sentence + settingsMethaneRetrieval(…)LocalETKF(cutoff_km=300)
carriers above · arrays belowjit boundary: the step unwraps, calls down, rewraps
④ algorithm sentencematched_filter, segmentlocal_etkf, average_apply
③ model noun phrase
Background
obs plan
② strategy adverbLedoitWolf, SigmaThresholdGaspariCohn(c), OverlapAdd
① primitive verbtarget_signature, pixel_areagather, eigh, segment_sum
⓪ structure nounradiance (band, y, x)ensemble (member, field, y, x)
setup beside the ladderBackground.fit (spectra)interp_plan(grid, obs)
::::
:::{div}
:class: gs-figcap
Same rungs, same rules, two sciences. `matched_filter` and `local_etkf` are both ④ sentences; `Background` and the observation plan are both ③ noun phrases built once by setup.
:::| Rung | Grammar | What it is | Trace gas: retrieval; persistency | Ocean |
|---|---|---|---|---|
| ⓪ structure | noun | a thing the others act on | the radiance cube, Σ_y; the event catalogue | the ensemble, a PSD operator |
| ① primitive | verb | one call on a noun, no state | target_signature; compute_E_Pd | gather, eigh |
| ② strategy | adverb | how to do it: settings, no data | LedoitWolf; LognormalMark | GaspariCohn(c) |
| ③ model | noun phrase | a noun with meaning and fitted state | Background (μ_y, Σ_y); InstrumentPOD | obs plan |
| ④ algorithm | sentence | a method over models, as a function | matched_filter; fit_poisson_rate | local_etkf |
| ⑤ step | sentence + settings | one transformation of a carrier, ready to chain | MethaneRetrieval; Persistency | LocalETKF |
| ⑥ recipe | paragraph | steps in order, making one job | plume_recipe; persistency_recipe | DACycle |
| setup | beside | builds a model’s state once, outside jit | Background.fit; load_pod_calibrations | interp_plan |
| orchestration | above | runs recipes on a schedule or behind an API | every new scene; every facility | every window, nightly |
Which rung is it?¶
Ask eight questions in order, top to bottom, and stop at the first yes. The order matters: a recipe also transforms data, and a step also holds settings, so the bigger question goes first.
Ask from the top. The first yes is the rung. What a name is called, or how much happens inside it, does not count; how it is built and what it takes does.
| Name | Rung | Because |
|---|---|---|
Reproject | ⑤ step | complex inside, but one closed block with settings |
RGBRecipe | ⑤ step | despite its name, it does one thing to its input |
UnscentedIntegrator | ② strategy | a choice of how to integrate, like CGSolver is a choice of how to solve |
DACycle, TrainingLoop | ⑥ recipe | templates: compositions with slots for your steps |
GeoArray.crop | ① primitive | a verb on a data noun; it moves data and georeference, and computes no new field |
| An Airflow task that runs a recipe nightly | not a rung | orchestration: it decides when and on what |
The role fixes the kind¶
Each rung is packaged one way, so each contract is written once per kind. Here is the data, what a function needs to know about where the data sits (a transform, cell areas), parameters, fitted state and a step’s config.
| Kind | Rungs | Form | Contract |
|---|---|---|---|
| function | ① ④ | pure, arrays in and out; never sees a name, a CRS or a fill value, so it traces | |
| Module | ⓪ ② ③ | , then | setup once, may be untraceable; the call is pure; never holds a carrier |
| Operator | ⑤ ⑥ | unwrap, compute on arrays, rewrap; is JSON and never reaches |
The cut between the compiled region and the carriers follows. A ⑤ step unwraps with , calls a ④ algorithm and rewraps with :
So the apply context is exactly rungs ⓪ to ④, and the step is the seam where the georeference leaves and comes back. Three invariants hold across the stack: arrays go everywhere; carriers stop at the step; configs stop at Operators. One entanglement is allowed and documented: a verb may take an adverb (solve(A, b, solver=CGSolver())), and some adverbs call verbs.
Beside and above the ladder¶
Setup builds state from data or geometry, once, outside jit: a model’s state, or a structure such as a low-rank factor. It is not a rung because it is not called in the pipeline’s flow; it runs before, and its output is shipped. It is the same thing as the setup execution context, under the same name. In the trace-gas example it fits each scene’s background and loads the instruments’ detection curves; in the ocean example it builds the observation plan and the regridding plans.
Orchestration runs recipes over many items, on a schedule or behind an API, with retries and storage. It decides when, where and how often, never what is computed: the maths was finished at the recipe. Its unit of work is one scene, and then one facility, in the trace-gas example, and one 10-day window in the ocean example.
The rules that make it a ladder¶
| Rule | What it means | Why |
|---|---|---|
| Calls go down | a rung uses the rungs below it, never those above | anything can be tested without what is built on it; the ladder is the build order |
| Arrays below ⑤ | rungs ⓪ to ④ take arrays and numbers, never a georeferenced container | the maths runs on any container, under jit, in any library |
| Settings stop at ⑤ | configs build steps and recipes; lower rungs take plain values | one place where settings are named and saved |
| Composition is closed | a recipe of recipes is a recipe, so the ladder ends at ⑥ | what lies above is running, not composing |
| One name, one rung | a public name has exactly one role | the docs, the tests and the reader all know where to look |
The ladder and the other axes¶
The ladder says how big a piece is. The execution context, the pipeline stage and the map properties say other things about the same pieces, and each meets the ladder at a fixed place:
| Axis | Meets the ladder at | How |
|---|---|---|
| execution context: apply | ⓪ to ④ | the compiled region; vmap and sharding wrap a ④ algorithm |
| execution context: setup | beside ③ | the same word in both: it builds a model’s state, once, outside jit |
| execution context: host I/O | ① and ② too | query and write_cog are ① primitives, LocalCache a ② strategy, but on bytes, not arrays |
| execution context: parallelism | ④, ⑤, ⑥, above | vmap at ④, map_blocks at ⑤, orchestration over ⑥ |
| pipeline stage | ⑤ and ⑥ | each stage is one ⑤ step; the chain of stages is a ⑥ recipe |
| map properties | ① and ④ | a step inherits its algorithm’s map kind, locality and linearity |
So the ladder is not an apply-only ladder. The lower rungs are grammar every context uses: host I/O has its own verbs and adverbs, and so does splitting a domain (SphericalCap is a strategy). What is apply-only is the compiled region, and that is ⓪ to ④.
Who works where¶
Each kind of user works in a band of the ladder and treats one rung as atomic: their floor. A page for one band says what the floor guarantees in one line and does not open it.
Filled: where each user works, coloured by the context of those rungs (blue outside the trace, teal inside). Dashed: their floor, the rung they treat as given (for data engineers and geoscientists, the bottom of their own band).
| Who | Works at | Floor | Speaks in |
|---|---|---|---|
| data engineers | orchestration, ⑥ | recipes, often as data | carriers and files |
| geoscience researchers | ⑤, ⑥ | steps | band names, CRS, fill values |
| ML researchers | ②, ③, ④ | primitives | shapes |
| numerics and CS | ⓪ to ④ | JAX, lineax, diffrax | shapes |
This is why the top two rungs get the strictest stability promises and the friendliest errors: they are the surface most people touch. And since floors leak, every step’s documentation names the algorithm it wraps, so a user can go one rung down without reading source.
The same ladder in libraries you know¶
| Rung | In libraries you know | In the stack |
|---|---|---|
| ⓪ Structure | np.ndarray, xr.DataArray, a SciPy sparse matrix | GeoArray, DimArray, gaussx Kronecker |
| ① Primitive | np.linalg.solve, scipy.ndimage.label | gaussx solve, logdet; geoarray crop |
| ② Strategy | method="L-BFGS-B" in SciPy, an optax optimiser, diffrax.Tsit5 | CGSolver, NystromPreconditioner |
| ③ Model | a fitted scikit-learn estimator, a torch.nn.Module with weights | MultivariateNormal, LGSSM, a sesmax model |
| ④ Algorithm | scipy.optimize.minimize, a training function | kalman_filter, etkf_transform |
| ⑤ Step | a scikit-learn transformer, a Keras layer, a torchvision transform | geoops MatchedFilter, Reproject |
| ⑥ Recipe | a scikit-learn Pipeline, a Keras Sequential, a Pangeo Forge recipe | pipekit Sequential, Graph; DACycle |
| Orchestration | Airflow, Prefect, a DVC pipeline, a FastAPI service | DVC stages, GeoApps |
The correspondences in the middle column are analogies, not claims about how those libraries describe themselves. The pattern is common; what the stack adds is naming the rungs and holding every library to them.
Explaining with the ladder¶
The ladder is also an order for explaining a method. Nouns before verbs, verbs before adverbs, then the model, then the sentence, and only then the rungs above, if the reader works there:
Start from the sentence. Say in one line what the method does.
Name the nouns. Each with its shape and unit.
Name the verbs. One equation each.
Name the adverbs. Each as a family and a trade: what swapping it changes and what it leaves alone.
Name the noun phrases. What each means, what state it holds, and what builds it.
Return to the sentence. Write the method using only the pieces above. If a line needs a piece not yet named, the explanation is missing one.
Go up only if the reader works there. The step and the recipe for a geoscientist; stop at move 6 for an ML reader.
Both worked examples end with this recipe applied: the matched filter in seven moves and the local ETKF in seven moves.
Worked examples¶
Each example climbs the ladder on one application, with the same sections in the same order, so the two can be read side by side.
| Example | Application | The job | What it shows best |
|---|---|---|---|
| Trace gas: from one scene to persistency | sources | retrieve, segment and quantify one scene; then estimate how often the facility emits, from many | every rung filled in four stages; a model fitted once and used twice; a second recipe whose unit of work is a facility, not a scene |
| Ocean: one data-assimilation window | ocean surface | one 10-day local ETKF analysis over the North Atlantic | strategies as a family; setup building plans; a recipe that is a graph; parallelism at ④, ⑤ and above |
Sensor fusion, land extremes and calibration are the next candidates, one per application.
Where this goes next¶
| Idea here | In the design pages |
|---|---|
| The ladder | defined for the whole stack, with the full EMIT example and each library’s rungs (kinds and roles) |
| Function, Module, Operator | the kind axis: how each rung is packaged, in symbols (kinds and roles) |
| Arrays below ⑤, carriers above | the data axis and the rule that carriers stop at GeoStack (kinds and roles, rule 3) |
| Which library may hold which rung | layers and tiers (layer map) |
Sources and status
Source: the role ladder on the kinds and roles design page (6 November 2026) and the role-ladder skill, which this page introduces and does not replace. It merges two earlier pages: “Where does a piece of code belong?” (13 November 2026), whose methane example is now the trace-gas page, and “Seven rungs, read as grammar” (24 November 2026), whose ocean example is now its own page. The library names in the “in the stack” column are from the design page and were not re-checked for this page.