Data scopes and schema evolution#

HydroModPy keeps its state in two scopes: the data folder of a workspace and the project. The data folder is made of files and sidecars, and has no database. The project owns one DuckDB file, its index. This page states the role of each scope, how their schemas evolve, and which formats are canonical.

Neither is a place where irreplaceable results live. The data folder is a cache over upstream sources, and the project index is a view over the run directories. The disk is the source of truth; the index makes it queryable. No file on the machine registers projects: a workspace is read by walking its tree.

The full on-disk contract, the three classes of data and the rebuild invariants live in Storage layout.

Data cache: <workspace>/data/#

Holds the files that providers delivered, one folder per variable. Next to each file, a sidecar <file>.json records the variable, the source, the station or the product, the extent and the period, the sha256 and the size, the licence and the origin. The sidecars are the whole cache: a row is a sidecar, and nothing is kept in SQL. The folder is shared by every project of the workspace, and a project with no workspace keeps its own <project>/data/. It is purgeable, and reconstructible from upstream sources at the cost of downloading again.

The code is hydromodpy/data/provenance/sidecars.py:

  • scan(data_root) walks the folder once and returns the rows;

  • CacheIndex keeps those rows in memory and answers by variable, source, station, product, extent and period;

  • prune removes rows, and never a file that a run or a lockfile cites;

  • subsume removes the grids a larger one contains when it arrives, and keeps one that a run or a lockfile cites. data cannot read the runs, so the citations come from hydromodpy.workflow.cited_inputs, handed to the cache of a run and of hmp data get.

Three rules say what is a row. A sidecar names its variable, and a sidecar written without one is listed apart and is no row. Locks, writes in progress, hidden names, blobs/, sites/ and extracted_ign/ are skipped by name. A file of the user, under inputs/, is no row. A cache.duckdb of HydroModPy 2.0.0 in the folder is refused, and hmp workspace clean --data-cache removes it.

Exposed through the hmp data verbs (ls, get, remove, prune, lock, archive, restore) and the project.data accessor, which scopes the same folder to one project. Each row carries a POSIX path relative to data/, so a cache stays portable between machines.

A project pins the state of this cache in hydromodpy.lock, at its root. The file holds one [[input]] record per row, and one per file of the user that the project declares, with its variable, source, station_id, product, path, sha256, bytes, license and fetched_at. hmp run --frozen, hmp data lock --check, a restored archive and the frozen matching of the cache all read that one record. Storage layout gives the whole document.

Many writers, no merge. When a project launches its cases, the prepare phase runs each case up to its data (hmp run <case>.toml --until load_data). Each case writes its fetched files and sidecars straight into data/. A file and a sidecar go through .part and os.replace, and a station chronicle is written under a file lock named after the station, so two cases that fetch the same station write the same bytes in turn. hmp data ls reads, and hmp run --frozen checks the folder, without writing.

Project index: <project>/.hmp/index.duckdb#

Indexes the runs of one project: identity, parameters, metrics, the label of the day, the sites and the cases. It is the query layer over runs/ and sessions/, not their owner. It lives under .hmp/ precisely because that directory is disposable.

The project index is explicitly not a store of irreplaceable output. What a run needs in order to be read, replayed, resumed or compared is written into its run folder, and the index is derived from it. A run and a calibration never open it: the indexer writes it, and Catalog reads it. A change that makes a result reachable only through SQL is a regression of this contract.

Exposed through:

  • hydromodpy.results.catalog.facade.Catalog, a read-only view

  • hydromodpy.results.run.view.Run

  • hydromodpy.results.run.group.RunSet

  • hmp.open(project_path), which returns the Project; its reads go through Catalog

  • CLI verb hmp catalog ...

  • hydromodpy.results.catalog.federation.read_workspace_runs(), the read across the projects of a workspace, on the simulation.parquet of each run and never on an index, with the label of each run computed on read as the index computes it

  • hydromodpy.core.state.layout.project_roots_under(), the single place that turns a directory into the project roots it stands for

Rebuilding it#

hmp catalog reindex reads every run directory under runs/ and every calibration session under sessions/, and rebuilds the index from them. Per run it reads tables.parquet/simulation.parquet, parameters.parquet, metrics.parquet, the geographic payloads, manifest.json, provenance.json and annotations.json. A directory with an outcome.json and no manifest is indexed too, with its effective status, so a failed case is visible. Per session it reads session.json, phases/*/session.json and trials.jsonl. After a launch the same indexer applies a diff keyed by the relative directory, instead of a full rebuild. Both write a staging database next to the index and install it with one atomic rename, so index.duckdb is never absent nor half-written: a reader that was reading keeps reading the file it opened, the next opener gets the new one, and building it twice describes the project identically. That rename is hydromodpy.core.io.atomic_replace.rename_over_open_file(), the only publishing path: os.replace on POSIX, and on Windows, where MoveFileEx refuses as soon as another handle is on the index, the same rename asked of the kernel with FILE_RENAME_POSIX_SEMANTICS. There is no non-atomic fallback: where the kernel supports neither, the write fails, the previous index stays untouched and readable, and the fix is to close the processes reading the project and rebuild again. Storage layout carries the full statement.

The run name comes from the directory, never from the files: a rename moves the directory, so the tree is what the name is. A directory whose manifest names another run is reported and left out rather than indexed under a doubtful identity.

What survives a rebuild#

The rule is stated once, in Storage layout, and summarized here. The authoritative description is the module docstring of hydromodpy/results/catalog/indexer.py, which is the code that performs the rebuild.

Class

Content

Rebuild behavior

Reconstructible

Run identity and geometry, catchment metadata, parameters, metrics, input provenance, run environment, geographic features, declared observation points, tags and notes (annotations.json), calibration sessions and trials. The frozen config.toml stays in the run folder

Obligation: written in the run or session folder, restored identically by the rebuild

Losable

The index as a whole, and the tables with no directory behind them. What they recorded has its replacement on disk: exports/index.json and figures/index.json for what was produced, published.jsonl for what left the project, jobs/*/journal.jsonl for what was launched, trials.jsonl for the link between a trial and its promoted run

Dropped with the file. The runs, their results and their lineage are unaffected

Input

Observation series and station metadata

Out of the results scope. Repopulated from the data cache and the data loaders

Two consequences are worth naming. A deleted run lives in trash/, not as a flag in the index, so a rebuild neither revives it nor forgets it. A deleted name is never given out again: the version of a name is computed on disk, over the live directories, the trash and the tombstones.

Provenance bridge#

Each run seals, in its own inputs.json, which inputs it loaded: provider, product, version, the full request, path, SHA-256 and licence. run.record.inputs returns them. The SHA-256 of an entry is the key to the matching row of the data cache, so a run received alone still says what it read. Cross-project lookups read the simulation.parquet of each run, never an index.

Why two scopes#

  • Data folder: input sharing between projects covering the same geographic area, so a regional DEM is downloaded once.

  • Project index: fast queries over one project’s runs, scoped so a project stays usable while other projects write to the same data folder.

Two scopes, two rebuild costs: the data folder from upstream sources (network cost), the project index from the project’s own run directories (local cost). No scope is a single point of loss for a scientific result.

Shared architecture#

One set of patterns governs the two databases.

  • No migration runner. The data cache has no database, and a sidecar gains optional fields, which an older reader of the folder ignores. The project index is derived, so a schema change there is a new DDL in results/catalog/index_schema.py, a bump of INDEX_SCHEMA_VERSION and a rebuild. The reader refuses an index written under another version and names the command that rebuilds it.

  • Per-scope doors: one entry point per database. hmp.open(path) returns a Project for the directory holding project.toml, a Run for a run directory received alone and a Workspace for a workspace root; the project reads its .hmp/index.duckdb through Catalog. The data folder is read by scan and CacheIndex, which need no door.

  • Backend: DuckDBBackend (hydromodpy/results/catalog/adapters/duckdb.py) holds execute / query / fetch_one / fetch_all / insert / upsert / transaction / close, and the catalog mixins call it. It is the one backend, with no Protocol in front of it.

  • Authentication: hydromodpy.cli.auth.AuthBackend is the one actor today, the local user. It exposes current_user / can_read / can_write and allows every read and write. Only the command line reads it.

  • URI-aware paths: every workspace, cache and state argument is typed Path | UPath. The runtime accepts local paths and file:// URIs; any other scheme raises NotImplementedError.

Schema evolution#

Three things move differently, and none is migrated in place.

The data cache is made of files and sidecars, so it has no schema to move. A sidecar gains optional fields, and a reader accepts a field it does not know. The project index is derived, so its schema moves forward by replacing the DDL and rebuilding from disk with hmp catalog reindex.

The stored documents of a run or a session (the Zarr field store, the Parquet tables, manifest.json, session.json, the ledgers, PUBLICATION.json) are never rewritten. Each family reads a window of versions, and every later 2.x reads every 2.0.0 document, by the rule in Storage layout and in hydromodpy.core.io.document_version. hydromodpy.lock follows the same rule with its schema id. Its window holds one version, hydromodpy.lock.v2, so a lock outside it is refused and written again with hmp data lock.

User TOML files are out of scope: Pydantic with extra="forbid" handles them, and hmp doctor --fix-config rewrites their grammar. workspace.toml also says the oldest HydroModPy that opens the workspace, hydromodpy_version_min, and an older one refuses it, naming both.

Rules for a change#

  1. One version per component. The project index has its INDEX_SCHEMA_VERSION. Each Zarr store carries zarr_schema_version in its root attributes. Each Parquet file carries hmp.schema_version in its key-value metadata. SQL and columnar versions never share a number.

  2. Additive first. Prefer ALTER TABLE ... ADD COLUMN with a default to a deletion or a rename. Zarr fields only grow, and a new Parquet column or JSON key is optional. None of this changes the version of a stored document.

  3. Monotone numbering. Integers, one by one, no gaps. Downgrades are not supported.

  4. A test for every refusal. A document outside its window is refused with a message that names the command that writes it again, and a test holds the message.

  5. A reader-breaking change raises the current version of its family. A change of shape, dtype, column order or meaning of an existing field keeps the oldest version of the window and brings the reader of the old version. Nothing upgrades the stored document in place. A refactor that does not touch the disk changes nothing. The float width of a Zarr field is the exception: float32 (field_precision = "compact") and float64 ("exact") is a per-store choice written in each array’s metadata, and every reader returns float64.

  6. A published copy carries PUBLICATION.json with a SHA-256 for every file. hmp catalog import checks every digest before it writes anything into the project.

A reader of a stored run accepts a column or a key it does not know, as rule 2 says.

Where each version is declared#

The values are in the code. Read them there, not here.

What

Where

Data cache lockfile

LOCKFILE_SCHEMA in hydromodpy/data/provenance/lockfile.py

Project index

INDEX_SCHEMA_VERSION in hydromodpy/results/catalog/index_schema.py

ZARR_SCHEMA_VERSION

hydromodpy/results/zarr_store/constants.py

PARQUET_SCHEMA_VERSION

hydromodpy/results/storage/parquet_schemas.py

GEOPARQUET_SCHEMA_VERSION

hydromodpy/core/io/geoparquet.py

RUN_RECORD_MANIFEST_VERSION (run seal: manifest.json)

hydromodpy/results/record/manifest.py

PROVENANCE_VERSION (run seal: provenance.json)

hydromodpy/results/record/record.py

CHECKS_SCHEMA (the checks block of manifest.json)

hydromodpy/results/record/checks.py

SESSION_SCHEMA (session.json)

hydromodpy/results/session/descriptor.py

PHASE_SCHEMA (phases/<p>/session.json)

hydromodpy/results/session/phase.py

TRIAL_SCHEMA (a line of trials.jsonl)

hydromodpy/results/session/journal.py

ANNOTATIONS_VERSION (annotations.json)

hydromodpy/results/annotations.py

LEDGER_SCHEMA (figures/index.json, exports/index.json)

hydromodpy/results/ledger.py

The documents of a job (hmp-manifest/v2, hmp-inputset/v1, hmp-provenance/v1, hmp-outcome/v1, hmp-chain-outcome/v1)

hydromodpy/schema/job/, beside the writer of each document. The chain document is written by hydromodpy_annex/schema/job_chain/

Artifact policy#

The results of a run live on disk, in Zarr and Parquet, and those files are the truth. DuckDB holds the indexes, which are rebuilt from them. Other formats are allowed only when their role is explicit.

Category

Formats

Contract

Project catalog

DuckDB

Index of cases, simulations, sites, metrics and parameters. Rebuilt from the run folders.

Data cache

Files and JSON sidecars

The files that providers delivered, each with its sidecar. The sidecars are the cache, and there is no database.

Field arrays

Zarr

The persisted store of gridded and mesh fields.

Tabular outputs

Parquet, GeoParquet

Persisted tables and vector layers.

User exports

CSV, NetCDF, GeoTIFF, VTU, Shapefile, GeoPackage, STAC, PROV-O, RO-Crate

Documented outputs for humans and external tools. Never the source of truth.

Input derivatives

Parquet, NetCDF, JSON sidecars

Cache artifacts tied to data variables and provenance.

Solver diagnostics

NPY, NPZ, JSON, CSV

Sidecars for debugging solver state. Not a stable API unless a page names the schema.

Validation and calibration truth packages

NPZ, CSV, JSON

Named benchmark artifacts with local schema expectations.

Legacy hydrology helpers

HDF5

Localized PyHELP output. Stable only for the owning helper.

Direct DuckDB access#

The normal path is the catalog adapter. A direct duckdb.connect is allowed for backend adapters and the concrete catalog constructor, read-only diagnostics, tests and benchmarks, and developer-only CLI inspection commands. A new direct call in the public API or in a user-facing command is a regression, and tests/unit/architecture/test_layers.py fails on it unless the file is added to its allowlist with a rationale.

See also#