API reference¶
The public Python API of nwm-gdrom is intentionally small: one function for loading a
catalog, one dataclass for the catalog itself, one exception, and three numeric-code
enums. Everything else lives in submodules and is considered internal. The build-side
helpers, the parser, the OOD-threshold computation, and the reference evaluator may
evolve without notice.
The names below are all importable as attributes of the top-level nwm_gdrom namespace;
resolution is lazy via PEP 562 so import nwm_gdrom itself is cheap and does not pull
in pandas unless a catalog is actually loaded.
Loading a catalog¶
nwm_gdrom.load_catalog ¶
load_catalog(
path: Path | str,
*,
expected_rule_version: str | None = None,
expected_crosswalk_version: str | None = None,
) -> GDROMCatalog
Load a catalog from disk and validate its version stamps.
Reads a compressed NumPy archive previously written by
:func:save_catalog and returns a :class:GDROMCatalog. The archive
is opened with allow_pickle=False so loading never executes
arbitrary code. If the caller provides expected_rule_version or
expected_crosswalk_version, those are validated against the
embedded stamps and a mismatch raises
:class:CatalogVersionMismatchError rather than silently continuing
with an unexpected catalog. This is the recommended consumption
pattern for operational T-Route deployments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to a |
required |
expected_rule_version
|
str
|
If provided, the catalog's embedded |
None
|
expected_crosswalk_version
|
str
|
If provided, the catalog's embedded |
None
|
Returns:
| Type | Description |
|---|---|
GDROMCatalog
|
Fully populated catalog with all array fields cast to their canonical dtypes. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If |
ValueError
|
If the archive is missing any required array field or version stamp. |
CatalogVersionMismatchError
|
If either expected version is provided and does not match the embedded stamp. |
Examples:
The catalog dataclass¶
nwm_gdrom.GDROMCatalog
dataclass
¶
GDROMCatalog(
grand_ids: NDArray[int64],
state: NDArray[bytes_],
category: NDArray[int8],
storage_cap_m3: NDArray[float32],
min_storage_m3: NDArray[float32],
ood_inflow_p01_af: NDArray[float32],
ood_inflow_p99_af: NDArray[float32],
reservoir_modules_start: NDArray[int32],
modules_kind: NDArray[int8],
modules_ptr: NDArray[int32],
modules_flat: NDArray[float64],
conditions_branch_start: NDArray[int32],
conditions_ptr: NDArray[int32],
conditions_flat: NDArray[float64],
rule_version: str,
crosswalk_version: str,
)
Flat-array catalog of GDROM rules for N reservoirs.
Per-reservoir fields (all length N, sorted by ascending grand_ids):
| field | dtype | description |
|---|---|---|
| grand_ids | int64 | GRanD identifier (≥10000 for non-GRanD). |
| state | S2 | 2-letter state code for PDSI mapping; empty. |
| category | int8 | 0=Res_R, 1=Res_L, 2=Res_M. |
| storage_cap_m3 | float32 | Capacity in m³ (acre-ft x 1233.48 at build time). |
| min_storage_m3 | float32 | Dead-pool / minimum operating storage in m³. |
| ood_inflow_p01_af | float32 | Lower OOD threshold, acre-ft/day; -inf disables. |
| ood_inflow_p99_af | float32 | Upper OOD threshold, acre-ft/day; +inf disables. |
Modules (flat-packed, CSR-indexed):
| field | dtype | description |
|---|---|---|
| reservoir_modules_start | int32[N+1] | First module index per reservoir. |
| modules_kind | int8[M_total] | See ModuleKind (EXPR / TREE). |
| modules_ptr | int32[M_total+1] | Offsets into modules_flat. |
| modules_flat | float64[...] | Packed params; see module docstring. |
Dispatcher conditions (flat-packed, CSR-indexed):
| field | dtype | description |
|---|---|---|
| conditions_branch_start | int32[N+1] | First branch per reservoir; empty range = no dispatcher. |
| conditions_ptr | int32[B+1] | Offsets into conditions_flat. |
| conditions_flat | float64[...] | Packed branches; see module docstring. |
Versioning: rule_version and crosswalk_version strings, validated on
load_catalog against an optional expected value.
n_reservoirs
property
¶
Number of reservoirs in the catalog.
Returns:
| Type | Description |
|---|---|
int
|
Length of the |
n_modules ¶
Return the number of modules for a reservoir.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reservoir_idx
|
int
|
Row index in |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of modules attached to this reservoir (always at least one). |
Raises:
| Type | Description |
|---|---|
IndexError
|
If |
has_dispatcher ¶
Return whether a reservoir has a dispatcher tree.
Reservoirs with a single module have no dispatcher; reservoirs with multiple modules have a dispatcher CART that selects among them on each daily evaluation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reservoir_idx
|
int
|
Row index in |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
IndexError
|
If |
category_name ¶
Return the human-readable GDROM category for a reservoir.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reservoir_idx
|
int
|
Row index in |
required |
Returns:
| Type | Description |
|---|---|
str
|
One of |
Raises:
| Type | Description |
|---|---|
IndexError
|
If |
Version-mismatch exception¶
nwm_gdrom.CatalogVersionMismatchError ¶
Bases: ValueError
Raised when a loaded catalog's version stamps do not match expectations.
Either rule_version or crosswalk_version (or both) on disk differ
from the values the caller passed to :func:load_catalog via
expected_rule_version / expected_crosswalk_version. Inherits from
:class:ValueError so callers that only catch ValueError still
handle it.
Numeric-code enums¶
nwm_gdrom.ModuleKind ¶
Bases: IntEnum
nwm_gdrom.OpCode ¶
Bases: IntEnum
Comparison operator code used in predicate triples.
nwm_gdrom.VarCode ¶
Bases: IntEnum
Variable index used in predicate triples; matches input-tuple ordering.
Build-side helpers (submodule, internal)¶
Build-side functions live in submodules (nwm_gdrom.metadata, nwm_gdrom.rule_parser,
nwm_gdrom.rule_catalog, nwm_gdrom.timeseries, nwm_gdrom.evaluator) and are
considered internal. They remain importable for ad-hoc programmatic builds, but the
recommended way to produce a catalog is the nwm-gdrom CLI. The internal
submodule structure may change between releases without notice.