pipeline¶
pipeline
¶
Pipeline orchestrator: execute the config-driven parameterization stages 1--5.
Implement the 5-stage pipeline from design.md section 4:
- Resolve Target Fabric -- load the polygon mesh and apply optional domain filtering (bbox clip).
- Resolve Source Datasets -- match dataset names in the pipeline config to registry entries and resolve variable specifications.
- Compute/Load Weights -- handled internally by gdptools (no explicit
stage function; gdptools
ZonalGencomputes coverage weights on the fly). - Process Datasets -- spatial batching loop for static datasets
(
ZonalGen) and full-fabric temporal processing (WeightGen+AggGen). Per-variable and temporal output files are written incrementally as each dataset completes. - Normalize SIR -- canonical variable naming, unit conversion, and validation. Produces the Standardized Internal Representation consumed by model plugins.
A lazy :meth:PipelineResult.load_sir method assembles a combined
xr.Dataset on demand for downstream consumers. Phase 2 model plugins
(e.g., pywatershed) consume SIR files from disk via SIRAccessor rather
than PipelineResult.load_sir.
This module is model-agnostic by design -- all model-specific logic
(unit conversions, variable renaming, derived math, output formatting)
lives in model plugins under derivations/ and formatters/.
See Also
design.md : Full architecture document (section 4: pipeline stages, section 11: MVP implementation). hydro_param.config : Pydantic config schema consumed by every stage. hydro_param.sir : SIR normalization and validation utilities. hydro_param.sir_accessor : Lazy SIR loader used by Phase 2 plugins.
USER_REGISTRY_DIR
module-attribute
¶
User-local registry overlay directory (~/.hydro-param/datasets/).
YAML files in this directory extend the bundled dataset registry. Overlay entries replace bundled entries on name collision.
Stage4Results
dataclass
¶
Stage4Results(
static_files: dict[str, Path] = dict(),
temporal_files: dict[str, Path] = dict(),
categories: dict[str, str] = dict(),
)
Collect file paths and category metadata from stage 4 processing.
Stage 4 writes per-variable CSV files (static datasets) and per-year NetCDF/Parquet files (temporal datasets) incrementally. This dataclass aggregates the paths so that stage 5 can locate and normalize them.
| ATTRIBUTE | DESCRIPTION |
|---|---|
static_files |
Mapping of result key (e.g.,
TYPE:
|
temporal_files |
Mapping of result key (e.g.,
TYPE:
|
categories |
Mapping of result key to its dataset category (e.g.,
TYPE:
|
PipelineResult
dataclass
¶
PipelineResult(
output_dir: Path,
static_files: dict[str, Path] = dict(),
temporal_files: dict[str, Path] = dict(),
categories: dict[str, str] = dict(),
fabric: GeoDataFrame | None = None,
sir_files: dict[str, Path] = dict(),
sir_schema: list[SIRVariableSchema] = list(),
sir_warnings: list[SIRValidationWarning] = list(),
)
Encapsulate all pipeline outputs with lazy SIR loading.
Per-variable and temporal files are written incrementally during stage 4.
Stage 5 normalizes them into SIR files. Use :meth:load_sir to
assemble a combined xr.Dataset on demand rather than holding all
data in memory.
| ATTRIBUTE | DESCRIPTION |
|---|---|
output_dir |
Root output directory (same as
TYPE:
|
static_files |
Raw (pre-normalization) per-variable CSV paths from stage 4.
TYPE:
|
temporal_files |
Raw temporal NetCDF/Parquet paths from stage 4.
TYPE:
|
categories |
Result key to dataset category mapping.
TYPE:
|
fabric |
Target fabric with
TYPE:
|
sir_files |
Normalized SIR file paths from stage 5.
TYPE:
|
sir_schema |
Schema entries describing each SIR variable (canonical name, units, source dataset, statistic).
TYPE:
|
sir_warnings |
Validation warnings from stage 5 SIR validation.
TYPE:
|
load_sir
¶
Load normalized SIR files into a combined xr.Dataset.
Assemble all per-variable SIR CSV files into a single
xr.Dataset with the fabric id_field as the dimension.
| RETURNS | DESCRIPTION |
|---|---|
Dataset
|
Combined dataset with one data variable per SIR variable. Returns an empty dataset if no SIR files are available. |
Source code in src/hydro_param/pipeline.py
load_raw_sir
¶
Load raw (pre-normalization) static files into a combined xr.Dataset.
Unlike :meth:load_sir, this always uses the stage 4 raw CSV files,
bypassing SIR normalization. Useful for debugging or inspecting
source-native variable names and units.
| RETURNS | DESCRIPTION |
|---|---|
Dataset
|
Combined dataset from raw static files, or an empty dataset if no static files exist. |
Source code in src/hydro_param/pipeline.py
resolve_bbox
¶
Extract the domain bounding box from the pipeline config.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Pipeline configuration containing an optional
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[float]
|
Bounding box as |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If no domain is configured (the caller should use the fabric bounding box directly instead). |
NotImplementedError
|
If the domain type is not |
Source code in src/hydro_param/pipeline.py
stage1_resolve_fabric
¶
Stage 1: Load the target fabric and apply optional domain filtering.
Read the geospatial file specified by config.target_fabric.path,
validate that the id_field column exists, and optionally clip the
fabric to a bounding box domain. The domain bbox is assumed to be in
EPSG:4326 and is reprojected to the fabric CRS if they differ.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Pipeline configuration with
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
GeoDataFrame
|
Target fabric, potentially spatially subsetted by the domain bbox. |
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If the fabric file does not exist on disk. |
ValueError
|
If the |
Source code in src/hydro_param/pipeline.py
stage2_resolve_datasets
¶
stage2_resolve_datasets(
config: PipelineConfig, registry: DatasetRegistry
) -> list[
tuple[
DatasetEntry, DatasetRequest, list[AnyVariableSpec]
]
]
Stage 2: Resolve dataset names to registry entries and variable specs.
For each :class:~hydro_param.config.DatasetRequest in the pipeline
config, look up the corresponding :class:~hydro_param.dataset_registry.DatasetEntry
in the registry. Apply source overrides from the pipeline config,
validate strategy-specific requirements (e.g., local_tiff needs a
source path, temporal datasets need a time_period), and resolve
each requested variable name to its full specification.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Pipeline configuration containing the
TYPE:
|
registry
|
Dataset registry mapping names to entries and variable specs.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[DatasetEntry, DatasetRequest, list[...]]]
|
One tuple per dataset: the registry entry, the pipeline request, and the resolved variable specifications (VariableSpec, DerivedVariableSpec, DerivedCategoricalSpec, or DerivedContinuousSpec). |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If a |
KeyError
|
If a dataset name is not found in the registry (raised by
|
Source code in src/hydro_param/pipeline.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | |
stage4_process
¶
stage4_process(
fabric: GeoDataFrame,
resolved: list[
tuple[
DatasetEntry,
DatasetRequest,
list[AnyVariableSpec],
]
],
config: PipelineConfig,
) -> Stage4Results
Stage 4: Process all datasets with spatial batching and incremental writes.
Iterate over each resolved dataset. Static datasets are processed
through the spatial batch loop (KD-tree batches, per-batch GeoTIFF
fetch, gdptools ZonalGen). Temporal datasets skip batching and
are processed full-fabric via WeightGen + AggGen.
Per-variable CSV files (static) and per-year NetCDF/Parquet files (temporal) are written incrementally as each dataset completes, reducing peak memory usage compared to accumulating all results.
Resume support is provided via a manifest that records dataset
fingerprints and output paths. When config.processing.resume is
True, datasets whose outputs are already current are skipped.
| PARAMETER | DESCRIPTION |
|---|---|
fabric
|
Target fabric with a
TYPE:
|
resolved
|
Resolved dataset entries from :func:
TYPE:
|
config
|
Pipeline configuration (output path, batch size, resume flag, etc.).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Stage4Results
|
Aggregated file paths and category metadata for all processed datasets. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If duplicate result keys are detected across datasets or years (indicates a config collision). |
Notes
Multi-year static datasets (year: [2020, 2021]) produce
year-suffixed result keys (e.g., "land_cover_2020"). Temporal
datasets are split into per-calendar-year chunks via
:func:_split_time_period_by_year.
Source code in src/hydro_param/pipeline.py
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 | |
stage5_normalize_sir
¶
stage5_normalize_sir(
stage4: Stage4Results,
resolved: list[
tuple[
DatasetEntry,
DatasetRequest,
list[AnyVariableSpec],
]
],
config: PipelineConfig,
) -> tuple[
dict[str, Path],
list[SIRVariableSchema],
list[SIRValidationWarning],
_manifest_mod.SIRManifestEntry,
]
Stage 5: Normalize raw stage 4 output to canonical SIR format.
Build a SIR schema from the resolved datasets, then normalize each raw per-variable CSV into a canonical SIR file with standardized variable names and units. Temporal files are also normalized. Finally, validate the SIR files against the schema.
The SIR (Standardized Internal Representation) is the contract between the generic pipeline and model plugins -- plugins consume SIR files with predictable names and units, never raw source output.
| PARAMETER | DESCRIPTION |
|---|---|
stage4
|
Stage 4 results containing raw per-variable file paths.
TYPE:
|
resolved
|
Resolved dataset entries from :func:
TYPE:
|
config
|
Pipeline configuration (output path, id_field, validation mode).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[dict[str, Path], list[SIRVariableSchema], list[SIRValidationWarning], SIRManifestEntry]
|
|
| RAISES | DESCRIPTION |
|---|---|
SIRValidationError
|
If |
See Also
hydro_param.sir.normalize_sir : Per-file normalization logic. hydro_param.sir.validate_sir : SIR validation checks.
Source code in src/hydro_param/pipeline.py
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 | |
run_pipeline_from_config
¶
Execute the full 5-stage pipeline from pre-loaded config and registry.
Orchestrate all pipeline stages in sequence: resolve fabric (stage 1), resolve datasets (stage 2), process datasets with spatial batching (stage 4), and normalize to SIR (stage 5). Stage 3 (weights) is handled internally by gdptools during stage 4.
GDAL HTTP timeout environment variables are set from
config.processing.network_timeout for the duration of the pipeline
and restored afterward.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Validated pipeline configuration.
TYPE:
|
registry
|
Dataset registry for resolving dataset names to entries.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PipelineResult
|
Complete pipeline output including file paths for static and
temporal outputs, the target fabric, normalized SIR files,
schema, and validation warnings. Use :meth: |
See Also
run_pipeline : Convenience wrapper that loads config and registry from paths.
Source code in src/hydro_param/pipeline.py
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 | |
run_pipeline
¶
Execute the full parameterization pipeline from file paths.
Convenience wrapper that loads the pipeline config and dataset registry
from disk, then delegates to :func:run_pipeline_from_config.
| PARAMETER | DESCRIPTION |
|---|---|
config_path
|
Path to the pipeline YAML config file.
TYPE:
|
registry_path
|
Path to a dataset registry YAML file or directory of YAML files. Defaults to the built-in registry bundled with the package.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PipelineResult
|
Complete pipeline output. Use :meth: |
See Also
run_pipeline_from_config : Core pipeline execution with pre-loaded objects. hydro_param.config.load_config : YAML config loader. hydro_param.dataset_registry.load_registry : Registry loader.
Source code in src/hydro_param/pipeline.py
main
¶
Run the pipeline from the command line via python -m hydro_param.pipeline.
Parse sys.argv for a config path and optional registry path, configure
logging, and execute the pipeline. This is a minimal entry point for
debugging; the primary CLI is :mod:hydro_param.cli.
| RETURNS | DESCRIPTION |
|---|---|
int
|
Exit code: 0 on success, 1 on failure. |