plugins¶
plugins
¶
Plugin protocols, context, and registries.
Define the contracts that all model plugins (derivation + formatter) must
satisfy, the typed DerivationContext input bundle, and factory functions
for plugin discovery.
This module enforces the two-phase separation between the generic pipeline and model-specific logic. The pipeline produces a normalized Standardized Internal Representation (SIR); plugins consume SIR data and transform it into model-specific parameters and output files. This module is the single source of truth for "what is a plugin?" and how plugins are discovered.
See Also
hydro_param.derivations.pywatershed : pywatershed derivation plugin. hydro_param.formatters.pywatershed : pywatershed output formatter plugin.
Notes
Plugin discovery uses lazy imports so that heavy model-specific dependencies are only loaded when a plugin is actually requested.
DerivationContext
dataclass
¶
DerivationContext(
sir: SIRAccessor,
temporal: dict[str, Dataset] | None = None,
fabric: GeoDataFrame | None = None,
segments: GeoDataFrame | None = None,
waterbodies: GeoDataFrame | None = None,
fabric_id_field: str = "nhm_id",
segment_id_field: str | None = None,
config: dict = dict(),
precomputed: dict[str, dict[str, str]] | None = None,
lookup_tables_dir: Path | None = None,
)
Bundle all inputs a derivation plugin needs into a single immutable object.
DerivationContext is the sole interface between the generic pipeline and
model-specific derivation logic. It packages a lazy SIR accessor, target
fabric geometry, segment topology, and plugin configuration so that
derivation plugins never reach back into the pipeline internals.
Validates on construction that fabric_id_field exists as a column in
the fabric GeoDataFrame (if provided). This fail-fast validation prevents
silent dimension mismatches downstream.
| ATTRIBUTE | DESCRIPTION |
|---|---|
sir |
Lazy accessor for normalized SIR output files. Loads variables on
demand via
TYPE:
|
temporal |
SIR-normalized temporal datasets keyed by name (e.g.,
TYPE:
|
fabric |
Target HRU polygon GeoDataFrame with a geometry column and an ID column
named by
TYPE:
|
segments |
Stream segment line GeoDataFrame for routing derivations (step 12).
TYPE:
|
waterbodies |
NHDPlus waterbody polygon GeoDataFrame for depression storage
parameters. When
TYPE:
|
fabric_id_field |
Column name for HRU identifiers in
TYPE:
|
segment_id_field |
Column name for segment identifiers in
TYPE:
|
config |
Plugin-specific configuration dict passed through from the pipeline YAML.
TYPE:
|
precomputed |
Declared pre-computed parameters from the consumer config. Each key
is a PRMS parameter name (e.g.,
TYPE:
|
lookup_tables_dir |
Override path to lookup table YAML files. When
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If |
See Also
DerivationPlugin : Protocol that consumes this context.
Notes
Frozen dataclass -- all fields are immutable after construction. This prevents derivation plugins from accidentally mutating shared state.
resolved_lookup_tables_dir
property
¶
Resolve the lookup tables directory to an absolute path.
Return the explicit override if set, otherwise the package-bundled
default under hydro_param/data/pywatershed/lookup_tables/ discovered via
importlib.resources.
| RETURNS | DESCRIPTION |
|---|---|
Path
|
Absolute path to a directory containing lookup table YAML files. |
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If |
DerivationPlugin
¶
Bases: Protocol
Define the contract for model-specific parameter derivation.
Implementations transform a normalized SIR dataset into model-specific parameters. This includes unit conversions (e.g., metres to feet, Celsius to Fahrenheit), variable renaming, lookup-table reclassification, majority extraction from categorical fractions, gap-filling, and derived math.
All model-specific logic lives in derivation plugins -- the generic pipeline never performs these transforms.
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
Unique plugin identifier used by
TYPE:
|
See Also
DerivationContext : Immutable input bundle consumed by derive().
FormatterPlugin : Companion protocol for output formatting.
derive
¶
Derive model-specific parameters from the normalized SIR.
Execute the full derivation pipeline for a target model, including unit conversions, reclassification, lookup-table application, and derived math.
| PARAMETER | DESCRIPTION |
|---|---|
context
|
Immutable input bundle containing the SIR dataset, target fabric geometry, segment topology, and plugin configuration.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dataset
|
Model-specific parameter dataset with variables in model-native names and units (e.g., feet, acres, degrees Fahrenheit for PRMS). |
Source code in src/hydro_param/plugins.py
FormatterPlugin
¶
Bases: Protocol
Define the contract for model-specific output formatting.
Implementations serialize derived parameters to the file format(s) expected by the target model (e.g., PRMS parameter files, NextGen configuration, or generic NetCDF/Parquet).
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
Unique plugin identifier used by
TYPE:
|
See Also
DerivationPlugin : Companion protocol for parameter derivation. NetCDFFormatter : Generic NetCDF implementation. ParquetFormatter : Generic Parquet implementation.
validate
¶
Validate derived parameters before writing output files.
Check that required variables are present, values are within physically plausible ranges, and units are consistent.
| PARAMETER | DESCRIPTION |
|---|---|
parameters
|
Derived model parameters to validate.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
Validation warning messages. Empty list if all checks pass. |
Source code in src/hydro_param/plugins.py
write
¶
Write derived parameters to model-specific output files.
Create the output directory if it does not exist, then serialize the parameter dataset into one or more files in the format expected by the target model.
| PARAMETER | DESCRIPTION |
|---|---|
parameters
|
Derived model parameters (output of
TYPE:
|
output_path
|
Directory to write output files into. Created if absent.
TYPE:
|
config
|
Formatter-specific configuration options (e.g.,
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Path]
|
Absolute paths to all files written. |
| RAISES | DESCRIPTION |
|---|---|
OSError
|
If file I/O fails (e.g., permission denied, disk full). |
Source code in src/hydro_param/plugins.py
NetCDFFormatter
¶
Format parameters as a single NetCDF-4 file.
Generic formatter that writes the full parameter dataset to one NetCDF file without any model-specific transformations. Suitable for archival, inspection, or consumption by downstream tools that read CF-compliant NetCDF.
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
Formatter identifier (
TYPE:
|
See Also
ParquetFormatter : Alternative tabular output format.
write
¶
Write parameters as a single NetCDF-4 file.
| PARAMETER | DESCRIPTION |
|---|---|
parameters
|
Parameter dataset to serialize.
TYPE:
|
output_path
|
Directory to write into (created if absent).
TYPE:
|
config
|
Options. Recognized keys:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Path]
|
Single-element list containing the path to the written file. |
| RAISES | DESCRIPTION |
|---|---|
OSError
|
If the NetCDF write fails (wrapped with the target path for easier debugging). |
Source code in src/hydro_param/plugins.py
validate
¶
Perform no-op validation (generic NetCDF has no schema constraints).
| PARAMETER | DESCRIPTION |
|---|---|
parameters
|
Parameter dataset (unused).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
Always returns an empty list. |
Source code in src/hydro_param/plugins.py
ParquetFormatter
¶
Format parameters as a single Apache Parquet file.
Generic formatter that converts the parameter dataset to a pandas DataFrame and writes it as a single Parquet file. Parquet provides efficient columnar storage with compression, suitable for downstream analysis in pandas, Spark, or DuckDB.
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
Formatter identifier (
TYPE:
|
See Also
NetCDFFormatter : Alternative multidimensional output format.
write
¶
Write parameters as a single Parquet file.
Convert the xarray Dataset to a pandas DataFrame via
to_dataframe() before serializing. Index columns are
preserved.
| PARAMETER | DESCRIPTION |
|---|---|
parameters
|
Parameter dataset to serialize.
TYPE:
|
output_path
|
Directory to write into (created if absent).
TYPE:
|
config
|
Options. Recognized keys:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Path]
|
Single-element list containing the path to the written file. |
| RAISES | DESCRIPTION |
|---|---|
OSError
|
If the Parquet write fails (wrapped with the target path for easier debugging). |
Source code in src/hydro_param/plugins.py
validate
¶
Perform no-op validation (generic Parquet has no schema constraints).
| PARAMETER | DESCRIPTION |
|---|---|
parameters
|
Parameter dataset (unused).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
Always returns an empty list. |
Source code in src/hydro_param/plugins.py
get_derivation
¶
Look up and instantiate a derivation plugin by name.
Factory function that lazily imports the requested derivation plugin module. This avoids loading heavy model-specific dependencies until they are actually needed.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Plugin name. Currently supported:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DerivationPlugin
|
A freshly instantiated derivation plugin. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
See Also
get_formatter : Companion factory for output formatters.
Source code in src/hydro_param/plugins.py
get_formatter
¶
Look up and instantiate an output formatter by name.
Factory function that returns a formatter matching the requested output
format. Model-specific formatters (e.g., "pywatershed") are lazily
imported to avoid loading unused dependencies.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Formatter name. Currently supported:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FormatterPlugin
|
A freshly instantiated formatter plugin. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
See Also
get_derivation : Companion factory for derivation plugins.