units¶
units
¶
Unit conversion registry for hydrologic parameterization.
Centralize all unit conversions (m to ft, mm to in, C to F, etc.) in a single registry to prevent bugs from scattered inline magic numbers. Used by both derivation plugins and output formatters.
Hydrologic models often require specific unit systems. For example, PRMS/pywatershed uses imperial units internally (feet, inches, degrees Fahrenheit, acres), while source datasets typically provide data in SI units (metres, millimetres, degrees Celsius, square metres). This module provides a declarative registry so that conversion logic is defined once and applied consistently.
Notes
Conversions are registered at module import time. The convert()
function is the primary public interface -- callers should never need to
access the internal registry dict directly.
All conversion functions are vectorized (they accept and return NumPy arrays), enabling efficient batch application over large feature sets.
See Also
hydro_param.sir : SIR normalization, which uses a separate conversion mechanism for source-to-canonical transforms (log10, Kelvin).
UnitConversion
dataclass
¶
UnitConversion(
from_unit: str,
to_unit: str,
fn: Callable[[NDArray[floating]], NDArray[floating]],
description: str = "",
)
Represent a single registered unit conversion.
Immutable record binding a source/target unit pair to a vectorized conversion function.
| PARAMETER | DESCRIPTION |
|---|---|
from_unit
|
Source unit identifier (e.g.,
TYPE:
|
to_unit
|
Target unit identifier (e.g.,
TYPE:
|
fn
|
Vectorized conversion function. Must accept a NumPy array of float values and return a NumPy array of the same shape.
TYPE:
|
description
|
Human-readable description of the conversion (e.g.,
TYPE:
|
register
¶
register(
from_unit: str,
to_unit: str,
fn: Callable[[NDArray[floating]], NDArray[floating]],
description: str = "",
) -> None
Register a unit conversion function in the module-level registry.
Add a new (from_unit, to_unit) -> fn mapping. If the pair already
exists, the previous registration is silently overwritten.
| PARAMETER | DESCRIPTION |
|---|---|
from_unit
|
Source unit identifier (e.g.,
TYPE:
|
to_unit
|
Target unit identifier (e.g.,
TYPE:
|
fn
|
Vectorized conversion function. Must accept a NumPy float array and return a NumPy float array of the same shape.
TYPE:
|
description
|
Human-readable description (e.g.,
TYPE:
|
Examples:
Source code in src/hydro_param/units.py
convert
¶
Apply a registered unit conversion to an array of values.
If from_unit == to_unit, return the input array unchanged (no-op
passthrough). Otherwise, look up the registered conversion function
and apply it element-wise.
| PARAMETER | DESCRIPTION |
|---|---|
values
|
Array of values to convert, in
TYPE:
|
from_unit
|
Source unit identifier (e.g.,
TYPE:
|
to_unit
|
Target unit identifier (e.g.,
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Converted values in |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If no conversion is registered for the |
Examples:
Source code in src/hydro_param/units.py
list_conversions
¶
List all registered unit conversions for introspection.
Useful for CLI datasets info output, debugging, and verifying that
expected conversions are available before running a pipeline.
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[str, str, str]]
|
List of |