hydrotools.nwm_client.UnitHandler module¶
- class hydrotools.nwm_client.UnitHandler.UnitHandler(unit_registry: ~pint.registry.UnitRegistry = <pint.registry.UnitRegistry object>)¶
Bases:
object
Engine to handle unit of measurement conversions.
- unit_registry¶
pint.UnitRegistry that handles all units used by UnitHandler.
- Type:
pint.UnitRegistry, default pint.UnitRegistry(cache_folder=”:auto:”)
- conversion_factor(from_units: str, to_units: str) float ¶
Compute and return a conversion factor from from_units to to_units.
- Parameters:
from_units (pint.UnitRegistry.Quantity compatible str) – Units from which to convert (e.g. “m^3/s”)
to_units (pint.UnitRegistry.Quantity compatible str) – Desired conversion units (e.g. “ft^3/s”)
- Returns:
result – Conversion factor to transform from_units to to_units.
- Return type:
float
Example
>>> unit_handler = UnitHandler.UnitHandler() >>> unit_handler.conversion_factor("ft", "m") 0.30479999999999996
- convert_values(value: _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], from_units: str, to_units: str) _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] ¶
Convert value from from_units to to_units.
- Parameters:
value (array-like, required) – Values to convert.
from_units (pint.UnitRegistry.Quantity compatible str) – Units from which to convert (e.g. “m^3/s”)
to_units (pint.UnitRegistry.Quantity compatible str) – Desired conversion units (e.g. “ft^3/s”)
- Returns:
result – Converted values same shape as value.
- Return type:
array-like
Example
>>> unit_handler = UnitHandler.UnitHandler() >>> unit_handler.convert_values([1, 1, 1], "ft", "m") array([0.3048, 0.3048, 0.3048])
- unit_registry: UnitRegistry = <pint.registry.UnitRegistry object>¶