Package: catchment#
The catchment layer takes a DEM and an outlet and gives the catchment, its river network and the ground the model sits on. It conditions the DEM and routes flow on it through a terrain engine, which is a port (terrain/, with its own README). It delineates the catchment, cuts the domain polygons and extracts the streams. It also holds the surfaces of the domain, the routing of water on the cells of the model top, the stream-network criterion on arrays and the French departments used to pick data tiles. The package is a layer because the mesh, the model, the solvers and the results all start from the same ground and the same network, and it knows no solver and no data provider. The DEM arrives as a resolved dem_path with its digest and state, and a geology map is read by a function the caller passes as an argument.
The package reads core and schema, and nothing else of HydroModPy. It is read by mesh, model, data, solver, results, calibration, workflow, config and project. The order of its own parts is in tests/unit/architecture/catchment_layout.yaml, and another layer may import only the modules that file lists as public.
The package has nine parts: administrative/, delineation/, terrain/, algorithms/, synthetic/, preprocess/, drainage/, stream_network/ and the three surface modules at its root. The mesh itself is in hydromodpy/mesh, and the aquifer geometry, the fields and the forcings are in hydromodpy/model. The page of the package lists every module with the first line of its docstring, and it describes the Whitebox backend, the seven steps of a preprocess run and where to add what.
Modules#
administrative/
Module |
What it does |
Lines |
|---|---|---|
france.py |
French administrative subdivision (département) detection. |
200 |
regions.py |
Names and codes of the French regions, and the spelling rules that read them. |
106 |
algorithms/
Module |
What it does |
Lines |
|---|---|---|
bed_regularization.py |
Least-change regularization of a channel bed on its drainage tree. |
505 |
catchment_domain.py |
Build domain support polygons from one catchment boundary. |
420 |
catchment_from_point.py |
Delineate a catchment polygon from one outlet coordinate. |
292 |
catchment_zones.py |
Build categorical zone rasters on geographic support. |
179 |
catchment_zones_field.py |
Catchment zonation object (3 classes: domain / buffer / core). |
93 |
channel_bed_fit.py |
Least-change fit of the channel bed along the D8 tree of the channel pixels. |
332 |
crossing_notch.py |
The notch of the DEM at each road, railway or bridge crossing of the channel. |
714 |
dem_aggregation.py |
Aggregate the initial DEM to a coarser cell before anything is delineated. |
110 |
dem_metadata.py |
Read and normalize DEM-derived metadata from watershed rasters. |
222 |
derived_features.py |
Structured geographic-derived features shared across runtime layers. |
258 |
direct_dem_domain.py |
Build domain artifacts directly from a DEM extent. |
139 |
domain_dem.py |
Clip a regional DEM to canonical domain rectangular support. |
88 |
flow_products.py |
Generate core hydrologic rasters from a source DEM. |
320 |
geographic_paths.py |
Path container for geographic processing outputs. |
95 |
ground_bed.py |
The channel bed of the ground G, regularized on the pixels of the channel tree A. |
350 |
ground_corrections.py |
The ground: the DEM plus the corrections the project declares, each bounded and logged. |
267 |
ground_inputs.py |
Read the lake footprint polygons that |
44 |
hydrographic_network.py |
Canonical hydrographic-network contract shared across geographic workflows. |
465 |
hydrographic_network_comparison.py |
Geometric comparison helpers for canonical hydrographic networks. |
248 |
job_inputs.py |
The checks a spatial capability job makes on its input files, and the hashes of its outputs. |
106 |
lake_catchments.py |
The area each lake drains on the D8 grid of the regional flow products. |
164 |
metric_crs.py |
Areas of polygon layers, measured in one projected CRS. |
24 |
network_heads.py |
The free ends of a mapped stream network, where its heads may lie. |
272 |
river_mesh_trace.py |
In-memory river-trace payload used by river-conformal meshing. |
150 |
river_network.py |
Build DEM-derived river-network products on catchment support. |
416 |
river_network_config.py |
The |
174 |
river_network_vector.py |
The river network vector that Whitebox writes, read back as a GeoDataFrame. |
170 |
sfr_network.py |
Delineate a MODFLOW 6 SFR reach network from DEM-derived flow products. |
667 |
sfr_traces.py |
Delineate the SFR reach trace of each declared network from the river-network products. |
96 |
stream_dem_agreement.py |
Measure how far the computed flow paths stray from the mapped stream network. |
186 |
surface_from_dem.py |
Convert a DEM raster into HydroModPy domain surface objects. |
30 |
terrain_stored.py |
Write and read one terrain product as its rasters and |
175 |
delineation/whitebox_workflows_backend/
Module |
What it does |
Lines |
|---|---|---|
component.py |
The part every Whitebox sub-backend shares with its raster backend. |
63 |
delineation.py |
Watershed delineation: pour points, watersheds, stream networks. |
275 |
depression_child.py |
Child entry point: run one Whitebox depression tool from file to file. |
26 |
flow.py |
DEM flow analysis: pit-filling, pointer, accumulation, mass flux, downslope. |
203 |
isolated.py |
Whitebox depression removal, run in a child interpreter. |
106 |
raster.py |
Raster/vector IO and format conversions on a shared environment. |
424 |
drainage/
Module |
What it does |
Lines |
|---|---|---|
channel_tree.py |
The generated river network A: its rasters, read around a mesh and projected onto the cells. |
305 |
descent.py |
The descent D: where water goes on the model top, as a computation only. |
518 |
distance.py |
Downslope topographic distance on a static receiver graph. |
461 |
filling.py |
Raise closed depressions of a surface until every path reaches an outlet. |
183 |
graph.py |
The receiver graph of a mesh: which cell drains into which, and what accumulates downhill. |
245 |
pins.py |
The channel pins of the descent D: validate them and apply them to the receiver graph. |
111 |
routing.py |
Route a per-cell release downstream on a receiver graph, to get a discharge anywhere. |
147 |
preprocess/
Module |
What it does |
Lines |
|---|---|---|
bed_regularization_config.py |
The |
57 |
capped_breach_config.py |
The |
76 |
catchment_capability.py |
|
194 |
catchment_config.py |
The catchment definitions of |
241 |
catchment_delineation.py |
* Copyright (C) 2023-2025 Alexandre Gauvain, Ronan Abherve, Jean-Raynald de Dreuzy |
376 |
catchment_worker.py |
|
319 |
crossings_config.py |
The |
115 |
delineated_catchment.py |
The catchment polygon a geographic delineated, read in one place. |
55 |
domain_rasters.py |
Build geographic raster artifacts on the canonical watershed support. |
298 |
enforce_lakes_config.py |
The |
61 |
geographic_config.py |
The |
452 |
geographic_tree.py |
The file suffixes that make a shapefile reopenable. |
8 |
pipeline.py |
Assemble the full geographic artifact set from one config + workspace. |
569 |
pipeline_steps.py |
Shared orchestration helpers for geographic pipelines. |
208 |
preprocessing_cleanup.py |
Removal of the geographic preprocessing tree once the run holds what it needs. |
83 |
store_ingestion.py |
Ingest geographic preprocessing outputs into the Catalog. |
248 |
subbasin.py |
* Copyright (C) 2023-2025 Alexandre Gauvain, Ronan Abhervé, Jean-Raynald de Dreuzy |
201 |
top level
Module |
What it does |
Lines |
|---|---|---|
raster_support.py |
Raster support metadata used to place 2D surfaces in space. |
165 |
surface.py |
Surface abstractions for domain topography and derived vertical supports. |
501 |
surface_sampling.py |
Prepared samplers for repeated georeferenced surface sampling. |
178 |
stream_network/
Module |
What it does |
Lines |
|---|---|---|
cost.py |
The signed gap between an excess of simulated stream and a missing one. |
449 |
extent.py |
When a simulated cell flows, how often it flows in a year, and the two extent masks. |
528 |
geometry.py |
The static geometry of a stream-network comparison, and its partition. |
965 |
heads.py |
The mapped network drawn from its heads, down the model’s own descent. |
269 |
network.py |
What the model calls a stream, from a per-cell release flux. |
150 |
synthetic/
Module |
What it does |
Lines |
|---|---|---|
catchment.py |
Runtime object exposing a synthetic geographic contract. |
300 |
synthetic_config.py |
Typed configuration for synthetic geographic contexts. |
256 |
topography.py |
Analytical topography laws for synthetic geographic contexts. |
94 |
terrain/: The terrain port routes water over a DEM for the rest of HydroModPy and hides the library that does the routing. port.py holds the Protocol, the product types and the checks the engines share, and registry.py holds terrain_engines. An engine needs port.py, artifacts.py and core.exceptions and nothing else of HydroModPy.
Module |
What it does |
Lines |
|---|---|---|
artifacts.py |
Readbacks every terrain engine owes its products, and the point layers it writes. |
146 |
d8.py |
Where each cell of a D8 pointer raster drains, read from the code table of the port. |
44 |
dem_contract.py |
The contract a DEM must hold before the terrain chain routes it. |
293 |
numpy_engine.py |
A second terrain engine, on numpy, with no third-party flow routing. |
496 |
port.py |
Terrain port: five members, the product types and the checks the two engines share. |
499 |
registry.py |
|
57 |
undrained.py |
Count the cells a conditioned DEM leaves without a descent. |
61 |
whitebox_engine.py |
The Whitebox Workflows engine, behind the terrain port. |
311 |
Inside the package#
The Whitebox backend#
delineation/whitebox_workflows_backend/ wraps Whitebox Workflows in one facade,
WhiteboxWorkflowsBackend, built once per process by get_whitebox_backend() in
delineation/__init__.py. Importing delineation does not load Whitebox, so a caller that
needs a class of the backend imports it from whitebox_workflows_backend. Only raster.py
and the child script depression_child.py import whitebox_workflows.
Whitebox 1.3.5 can end the process that calls it, with no exception, when two of its threads race
in a depression tool. So isolated.py runs depression removal in a child Python process, and
the parent runs it again after a crash, three attempts in all.
How a preprocess run goes#
preprocess/pipeline.py runs the steps and algorithms/ holds the code of each one.
pipeline_steps.pychecks the DEM header and the outlet throughterrain/dem_contract.pyand aggregates the DEM to a coarser cell when[geographic.catchment]asks for it.The run asks the product store for the terrain, which is the conditioned DEM with its D8 directions and its accumulation, keyed by the digest of the DEM, the method, the engine and the code. A present product is read back and the conditioning of step 4 is skipped.
algorithms/ground_corrections.pybuilds the ground, which is the DEM plus the corrections the project declares.algorithms/flow_products.pyconditions the ground and computes the D8 direction and accumulation rasters through a terrain engine.terrain_stored.pywrites a missing product.algorithms/catchment_from_point.pydelineates the catchment, ordirect_dem_domain.pytakes the DEM extent.catchment_domain.pycuts the polygons andriver_network.pyextracts the streams.When
[geographic.crossings]is on,crossing_notch.pylowers the embankments where they cross the channel, and the delineation runs a second time on the notched ground. Thenpreprocess/domain_rasters.pyclips the rasters to the domain. When[geographic.bed_regularization]is on,ground_bed.pyfits the channel bed.dem_metadata.pyreads the metadata of the clipped rasters, andpipeline.pyreturns oneGeographicRuntimeContext.
source_mode = "synthetic" skips all of this. synthetic/catchment.py returns a
SyntheticGeographic with the same public attributes. The runtime object that others read is
CatchmentDelineation, and BuildGeographicStep in workflow/steps/setup.py is the caller.
preprocessing_cleanup.py removes the tree once the run holds what it needs, and
geographic_tree.py lists the sidecar suffixes of a shapefile.
The catchment-delineate capability runs the checks of step 1, then steps 4 and 5, as a job
for outside tools, with the fill and window defaults of [geographic], so it cuts the
catchment a run with those defaults cuts on the same DEM. It takes a DEM and an outlet and seals watershed.gpkg, watershed.tif,
outlet.gpkg and catchment.json. The declaration is preprocess/catchment_capability.py and the job is
preprocess/catchment_worker.py, and hmp process run catchment-delineate <job> runs it.
From a catchment to a mesh#
hydromodpy/model/domain/build.py builds a Domain from the surface, and
model/domain/structure_binders.py adds the zones later. [mesh] then picks an engine in
hydromodpy/mesh. Triangles and Voronoi cells follow the zones and the river
trace, and the regular grid makes a staircase along them. The planner reads the river lines
through resolve_river_mesh_trace in algorithms/derived_features.py.
Boundaries of the package#
The package reads core and schema only, and never data, so the caller passes the DEM
path and the reader of a vector geology map as arguments. The import table is the generated page Layer graph. Another layer may import only the
modules listed under public in tests/unit/architecture/catchment_layout.yaml. The inside
of the pipeline stays private.
Four rules hold inside. First, terrain/ imports core and itself, and one thing more.
WhiteboxTerrainEngine.__init__ imports catchment.delineation inside the function, and only
when it is given no backend. So a third-party engine needs nothing of HydroModPy but the port
module. Second, drainage/ reads terrain/ and algorithms/, and terrain/ and
preprocess/ do not read it, because they work on D8 pixels before the mesh exists. Third,
algorithms/ reads no configuration of the pipeline, so a module there takes the config it
needs as an argument. Fourth, stream_network/ is a science folder, so a module there may import
only numpy and one neighbour of its folder. The imports it still needs beyond that are listed in
ALLOWED_OUTSIDE of
tests/unit/architecture/test_science_module_imports.py, and that table can only shrink.
Where to add what#
A Whitebox tool is a method of the part it belongs to in
delineation/whitebox_workflows_backend/, written as a pair, a *_raster or *_vector
method in memory and a method that reads and writes files. A depression tool that can end the process also goes to
DEPRESSION_TOOLS in isolated.py. A terrain engine is a class that satisfies the port in
terrain/port.py, registered by terrain_engines.register in terrain/registry.py or on
the hydromodpy.terrain.engine entry-point group, and tests/ports/test_terrain.py runs on
every engine the registry resolves. A geographic product is a module in algorithms/, called
from preprocess/pipeline.py, with a field on GeographicConfig for its options. A
[geographic] key is a field of the Pydantic model in preprocess/geographic_config.py or in
the file of its sub-table, with its description. An output path is in algorithms/geographic_paths.py. A routing rule or a distance on the model top goes to
drainage/, and the way the river network A is read on the cells is in
drainage/channel_tree.py. A rule of the stream-network criterion goes to the module of
stream_network/ that owns it, with its defaults in core/scoring/stream_defaults.py. A mesh
engine satisfies the port in hydromodpy/mesh/port.py, as Mesh engines says.
Tests#
The tests mirror the code under tests/unit/catchment/ and build their DEMs and trees with
tests/_helpers/geographic_cases.py. The CI runs
tests/unit/catchment/delineation/whitebox_workflows_backend/test_whitebox_workflows_backend.py
in its own step, because the Whitebox binding is not safe under pytest-xdist fork distribution.