Package: core#

The kernel leaf of HydroModPy. Every other package may read core, and core reads no sibling package, not even under TYPE_CHECKING. It holds what several layers need and none should own: the base of every config model, units, the paths of a project and a workspace, run state, the I/O primitives, the typed exceptions and a few kernels that two layers must compute the same way, such as the sign of a budget flux and the digest of a block of values.

A sub-package answers one question, and scoring/ is the port of the goodness-of-fit metrics, with a README of its own. The modules at the root hold the words every layer shares, for example the step names of pipeline_steps.py, the hierarchy of exceptions.py and the version string.

Each exception carries one of sixteen Category values, set once on the class that owns it and inherited below. cli maps a category to its exit code, so a new exception needs no line in the command line. logging.py keeps the handlers that a run adds in one SinkRouter, so no handler is added to the process logger while a run writes its log.

Importing hydromodpy.core loads neither the configuration nor the data stack. A helper used by one layer stays in that layer, and it moves here when a second layer needs it and the layer matrix forbids the edge.

The page of the package in the developer docs lists the modules with the first line of their docstring and tells how the folders fit together.

Modules#

budget/

Module

What it does

Lines

flux.py

Per-cell budget fluxes: their sign, the drain and release terms, the seepage mask.

456

terms.py

Per-cell budget terms: which ones a field reads, and what a store kept.

220

top level

Module

What it does

Lines

code_fingerprint.py

The fingerprint of the code that computes: one digest per layer of the package.

362

diagnostic.py

One finding of a check on a configuration document, in the one shape every producer gives.

94

exceptions.py

Typed exception hierarchy for HydroModPy.

960

licensing.py

The SPDX token this repository writes when nobody declared a licence.

21

logging.py

Console and file logging policy for HydroModPy.

521

old_members.py

Members of the public objects that left before the first stable version.

29

pipeline_steps.py

The names of the steps of a simulation, the words every layer shares.

33

plugin_registry.py

One registry engine, shared by every id -> class plugin surface.

495

scope.py

The words the stored tables share.

83

summary.py

Short text summaries, for the objects a user reads at the Python prompt.

44

version.py

Canonical source of the HydroModPy version string.

55

config_kit/

Module

What it does

Lines

base.py

Root Pydantic base class shared by every HydroModPy config model.

395

calibrable.py

The annotation that marks a configuration field calibrable.

102

dump.py

Write a config model as a commented TOML document.

225

field_metadata.py

Typed convention for Field(json_schema_extra=...) metadata.

108

introspect.py

Introspection helpers for HydroModPy Pydantic config fields.

381

persistence.py

Single-switch persistence configuration (Principe 8).

82

profile.py

Hierarchical visibility profiles for Pydantic config fields.

34

registry.py

Single source of truth for HydroModPy root TOML sections.

96

root.py

Root configuration wrapper and migration callables.

36

said.py

Where the warnings of a validator go while a check collects them.

59

stored.py

How a stored document is read, and the words a stored field is written with.

96

types.py

Shared constrained config types and reusable Literal aliases.

170

visible_when.py

Profile visibility tags and helpers.

39

written.py

What a configuration was given, as opposed to the defaults it took.

279

config_kit/field_param/

Module

What it does

Lines

config.py

Field-parameter TOML payloads: their Pydantic schema, their resolution and its check.

387

sections.py

Pydantic schemas for field-parameter TOML sections.

502

units.py

Unit aliases and normalization for field-parameter configuration.

51

console/

Module

What it does

Lines

ndjson.py

NDJSON progress sink, independent of the rich console renderer.

170

progress.py

Live console progress for long-running operations.

595

contracts/

Module

What it does

Lines

observables.py

Named observables a solver adapter can produce from one run.

185

overview.py

Runtime state threaded through the data-overview pipeline phases.

37

solver_model.py

What every solver model shares, and it is not much.

38

fields/

Module

What it does

Lines

grid_reference.py

Plain geometric description of a solver grid (kernel-level POPO).

86

nodata.py

Shared nodata sentinels.

38

precision.py

Storage precision of time-varying field arrays.

106

vertical_column.py

The vertical column of a layered model, read from its idomain.

92

io/

Module

What it does

Lines

atomic_replace.py

Replace one file by another in a single rename, readers included.

148

canonical_json.py

Deterministic JSON serialization.

53

crs.py

CRS helpers built on pyproj and rasterio.

318

db_retry.py

Single open point for every DuckDB file HydroModPy owns.

321

document_version.py

The version rule of every stored document.

231

fetch.py

Unified HTTP download client, URL safety guard and pinned download.

825

file_digest.py

The one SHA-256 digest of a file on disk.

28

filesystem.py

Filesystem utilities: folders, sizes, and the paths Windows bounds.

269

geoparquet.py

OGC GeoParquet 1.1 atomic writer.

90

lock_file.py

A file lock taken with O_CREAT | O_EXCL, held by one process on one host.

316

parquet.py

Canonical plain-Parquet write options shared across layers.

77

proj_bootstrap.py

PROJ database bootstrap (opt-in).

281

raster_io.py

Raster I/O: loading, masking, reprojecting, and exporting GeoTIFF/NetCDF.

320

value_digest.py

The values/1 rule: one digest for a block of values, whatever their dtype.

170

vector_io.py

Vector I/O helpers (shapefile / GeoPackage / GeoParquet).

26

payload/

Module

What it does

Lines

index.py

INSTALL.toml: the install index of a package.

346

install.py

Place the files and station rows of an install index under a workspace.

244

resolve.py

Resolve the data a project’s TOML files reference, the way a run resolves them.

347

runtime/

Module

What it does

Lines

bounds.py

The bounds of one call: the hosts it may reach, the roots it may use.

180

env.py

Every HMP_* environment variable HydroModPy reads, in one table.

404

image.py

The container image a process runs in: its tag and its digest.

50

interrupts.py

Make a termination signal or a stop request unwind the stack instead of cutting it.

116

stopwatch.py

The one stopwatch of a run: how long a step, a solve, a figure or an export took.

119

scoring/: This folder answers how well a simulated series matches an observed one, and when two costs are a tie. Every layer must compute these the same way, so they live in core, and display, calibration, results and catchment read them. The metric names form a closed table, METRIC_SPECS of metric_table.py, where a new metric is a row added in the code and never a plugin.

Module

What it does

Lines

baseflow.py

The baseflow index of a daily discharge, by the Wallingford filter, for one or two series.

102

cost_margin.py

The margin a cost may sit from the best before a trial stops counting as tied with it.

67

metric_table.py

The table of the eighteen metrics a score or a trial may be named after.

248

metrics.py

Hydrological efficiency and error metrics.

554

stream_defaults.py

The numeric defaults of the stream-network criterion, in one Pydantic model.

295

site/

Module

What it does

Lines

config.py

The [site] section: which catchment a project models.

177

row.py

One row of a site table: the columns of data/sites/catalog.csv.

149

state/

Module

What it does

Lines

host.py

The name of this host, and the proof that a process of it is gone.

37

layout.py

The layout of a project: the names of its folders and the paths built from them.

272

names.py

The rule of a project name and of a run name, written once.

146

portable.py

Portable paths: platform cache and state folders, workspace-relative strings, root URIs.

166

result_dirs.py

A result directory relative to its project, and the project that holds it.

246

rng.py

Centralized RNG seed manager.

59

run_state.py

Mutable runtime state threaded through workflow steps.

347

scratch.py

The scratch folder of a live run: where it works, and the sweep of the dead ones.

247

time/

Module

What it does

Lines

coverage.py

Which samples of a forcing record a stress period takes, and when it is covered.

210

instant.py

The one check of an instant a user writes: a date, first, last or a period index.

60

period_aggregation.py

Put a finely sampled series onto the stress periods a coarse index stands for.

582

selection.py

Turn a date a user writes into the stress period a run stored.

413

steady_initialization.py

Temporal helpers for same-solver steady-state initializations.

68

time_method.py

How a simulated series relates to the time of its stamps.

100

window.py

Canonical simulation-time utilities for launcher workflows.

889

toml_io/

Module

What it does

Lines

error_locator.py

Map Pydantic ValidationError paths back to TOML source lines.

304

generator.py

Auto-generate commented TOML templates from Pydantic models.

337

loader.py

TOML loading with base_config inheritance, and the base of relative paths.

398

merge.py

TOML payload merge helpers.

486

model_types.py

Which Pydantic model a field holds, and which member of a union.

231

overrides.py

Overrides of a document given on a command line or in the environment.

84

paths.py

Config-declared file paths, and the one parser of dotted key paths.

468

render.py

Value formatting, field defaults and comment lines of the TOML template.

402

section.py

Walk a Pydantic model and write its [section] of the TOML template.

465

writer.py

Render a plain mapping as TOML text with tomli-w.

29

tracking/

Module

What it does

Lines

input_file.py

Marker and result types for input-file tracking.

50

walker.py

Walk a Pydantic config tree and collect InputFile-annotated paths.

190

units/

Module

What it does

Lines

kinds.py

Quantity kinds: what a stored variable measures and how a figure shows it.

103

parse.py

Pint-based helpers to parse TOML-style unit payloads.

198

physical_bounds.py

Central registry of physical bounds for HydroModPy parameters.

337

quantities.py

Unit tables and conversions to SI for the quantities read from user input.

310

registry.py

Pint unit registry for HydroModPy.

65

scalar.py

Helpers to parse scalar values with optional inline units.

78

time.py

Time-unit helpers centered on SI (seconds), with the MODFLOW and pandas spellings.

132

types.py

Pydantic-pint annotated types for hydrogeological quantities.

354

workspace/

Module

What it does

Lines

config.py

Workspace configuration with project-local result catalogs.

253

exceptions.py

Workspace-resolution errors.

9

path_registry.py

Canonical workspace path registry shared by runtime components.

83

resolve.py

Workspace-root lookup, for runtime code and for the commands.

85

workspace.py

* Copyright (C) 2023-2025 Alexandre Gauvain, Ronan Abherve, Jean-Raynald de Dreuzy

65

workspace_toml.py

workspace.toml template generator and Pydantic validator.

295

Inside the package#

The layer rules are checked by tests/unit/architecture/, and the unit tests of a folder are in tests/unit/core/. core receives what it needs as an argument and never imports a higher layer.

Configuration. Every config class of every layer inherits HydroModelBase of config_kit/base.py, which sets extra="forbid" and validate_assignment=True, so the strictness is written once. profile.py and visible_when.py decide which field shows in which profile, and a field visibility rule goes there and in introspect.py. A shared constrained type goes to types.py. calibrable.py lives here so that model and calibration need not import each other. root.py holds RootConfig, the root model class with its two migrations. Every helper that reads sections or migrates a file receives it as its root argument, which is how core reaches the root config without importing config. A section that must be listed at the root is added to the root model, never to this folder. field_param/ is the schema of a field-parameter payload. It lives here because model/properties builds the field from it and model/flow_config.py embeds it in [flow.param.<id>].

toml_io is the path of a file into a document. The loader follows base_config, migrates through the RootConfig it receives, and error_locator.py points a Pydantic error at a line of the file. A new merge rule goes to merge.py or overrides.py, and a path key to paths.py.

Units and fields. A bare number is read in SI and a string such as "0.36 m/h" is converted, with Pint loaded only when needed. A new alias of a config type goes to units/types.py with its canonical unit, a new physical bound to physical_bounds.py and a new axis label to labels.py, so no figure writes one. In fields, a new nodata convention goes to nodata.py once, and a new way to store a field goes to precision.py with its error bound. budget fixes one sign for every solver, the one MODFLOW gives it: positive when water enters the aquifer, negative when it leaves. A drain or release key goes to the tuples of flux.py, and a derived field that needs a new term to the table behind budget_terms_for in terms.py.

Paths, names and the host. state/layout.py holds the names under .hmp/, runs/, sessions/, jobs/ and trash/, so a new folder name inside a project goes there with its *_dir_for helper. workspace/path_registry.py holds the canonical paths that runtime components share under a workspace, and the metadata of a workspace is workspace_toml.py. The workspace is resolved in four steps, from the [workspace] keys, then HMP_WORKSPACE, then the scaffold <workspace>/projects/<name>/, then the project folder itself. A random consumer asks RngManager of state/rng.py for its seed, and nothing seeds globally. A host name or the proof that a process died comes from state/host.py, never from socket.gethostname() or a pid test in the caller.

Files. A new file format is one module of io that writes through atomic_replace.py. A new stored document takes a schema id and a check through document_version.py. A hard link and a rename onto a reserved folder go through filesystem.py, and a DuckDB open through db_retry.py, never duckdb.connect. A package that installs files into a workspace, whether an example, a published run or an exported project, writes the same INSTALL.toml that payload/index.py reads and writes. The installer plans every placement before it writes one, so a new placement rule goes to payload/install.py.

The running process. A bound on what one call may reach or take is a member of Bounds in runtime/bounds.py, and the check that applies it stays with the owner of the download or the delivery. A new HMP_* variable is one entry of runtime/env.py, a new signal rule or a new reader of the stop flag goes to interrupts.py, and a new kind of timed block goes to MEASURE_KINDS. For progress, a new kind of line goes to console/progress.py with its event in ndjson.py, and a new consumer of the events reads the NDJSON instead of adding a second display. A new protocol between two layers is one module of contracts with the concrete class in the layer that owns it, and real code never goes there.

Sites and time. A new column of the site table goes to site/row.py, and a new site key to site/config.py, after a check that site_fingerprint still ignores what must not change the identity of a site. time/selection.py is the one resolver from a date to a period that figures and exports share, and time/window.py cuts a long run into windows.