hydromodpy.core.units.types#
Pydantic-pint annotated types for hydrogeological quantities.
Two flavors are provided:
Length,Area,Volume,Time,FlowRate,Velocity,HydraulicConductivity,SpecificStorage,Dimensionless: Pydantic-pint Quantity types. Values keep their pint Quantity wrapper (useful for ratio arithmetic / serialization).LengthMeters,AreaSquareMeters,VolumetricFlowM3PerSecond,DurationSeconds,HydraulicConductivityMPerS: float-valued annotated types that accept the same string/number inputs but expose a plainfloatin the canonical SI unit. Use these on existing fields whose downstream consumers expect plain numbers.
This module exports Annotated type aliases that can be used directly in Pydantic v2 models to enforce unit consistency. Each type accepts either a bare number (interpreted as the canonical SI unit) or a string “<value> <unit>” that pint can parse; the resulting value is always a pint Quantity in the canonical unit.
Example#
>>> from pydantic import BaseModel
>>> from hydromodpy.core.units import HydraulicConductivity
>>> class Aquifer(BaseModel):
... k: HydraulicConductivity
>>> Aquifer(k="1e-4 m/s").k.to("m/s").magnitude
0.0001
>>> Aquifer(k=1e-4).k.to("m/s").magnitude # bare number, fallback m/s
0.0001
>>> Aquifer(k="0.36 m/h").k.to("m/s").magnitude # auto-convert
0.0001
Design notes#
All annotations reuse the shared
UREGregistry (seeregistry.py) so that quantities from different fields remain comparable.BeforeValidatorcoerces bare numeric inputs (int/float, but NOTbool) to the canonical-unit string before handing off to pydantic-pint. Important: in anAnnotated[...]chain, later entries wrap earlier ones, soBeforeValidatormust be listed afterPydanticPintQuantity- otherwise pydantic-pint’s custom core schema replaces the chain and the before-validator never runs.SpecificYieldis a plainfloatconstrained to [0, 1] (it is genuinely dimensionless with a physical range) - not a pint Quantity.Dimensionlessis the pint way to express a pure-number quantity when the pipeline expects a Quantity object regardless.
Module attributes
|
Length in metres. |
|
Area in square metres. |
|
Volume in cubic metres. |
|
Duration in seconds. |
|
Volumetric flow rate in m^3/s. |
|
Velocity / flux density in m/s. |
|
Hydraulic conductivity (K) in m/s. |
|
Specific storage (Ss) in m^-1. |
|
Specific yield (Sy), a pure number in [0, 1]. |
|
Dimensionless pint Quantity ( |
|
Length in metres expressed as |
|
Area in m^2 expressed as |
|
Volumetric flow rate in m^3/s expressed as |
|
Duration in seconds expressed as |
|
Hydraulic conductivity in m/s expressed as |