From d283d8deb33138530e00239025f45a59d5d9146c Mon Sep 17 00:00:00 2001 From: "Jason A. Regina" Date: Mon, 10 Jun 2024 12:31:09 -0500 Subject: [PATCH] update default units handler to use explicit cache folder (#254) * update default units handler to use explicit cache folder * bump nwm client new to 7.4.3 --- .../hydrotools/nwm_client_new/NWMClientDefaults.py | 3 ++- .../src/hydrotools/nwm_client_new/_version.py | 2 +- python/nwm_client_new/tests/test_UnitHandler.py | 12 ++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/python/nwm_client_new/src/hydrotools/nwm_client_new/NWMClientDefaults.py b/python/nwm_client_new/src/hydrotools/nwm_client_new/NWMClientDefaults.py index a07acc0a..47a2ab22 100644 --- a/python/nwm_client_new/src/hydrotools/nwm_client_new/NWMClientDefaults.py +++ b/python/nwm_client_new/src/hydrotools/nwm_client_new/NWMClientDefaults.py @@ -21,6 +21,7 @@ from pathlib import Path from typing import List, Dict from enum import Enum +from pint import UnitRegistry class MeasurementUnitSystem(Enum): """A system of units to define measurements. @@ -98,7 +99,7 @@ def CROSSWALK(self) -> pd.DataFrame: def NWM_TO_US_UNIT_CONVERSION(self) -> Dict[str, Dict[str, float]]: """Mapping from NWM units to US standard units and respective conversion factors.""" # Set up unit handler - unit_handler = UnitHandler() + unit_handler = UnitHandler(unit_registry=UnitRegistry(cache_folder=Path("hydrotools/pint_cache"))) # Build conversion from NWM to US nwm_to_us_mapping = {key: self.SI_TO_US_UNIT_MAPPING[val] for key, val in self.NWM_TO_SI_UNIT_MAPPING.items()} diff --git a/python/nwm_client_new/src/hydrotools/nwm_client_new/_version.py b/python/nwm_client_new/src/hydrotools/nwm_client_new/_version.py index b98b7d25..b5194969 100644 --- a/python/nwm_client_new/src/hydrotools/nwm_client_new/_version.py +++ b/python/nwm_client_new/src/hydrotools/nwm_client_new/_version.py @@ -1 +1 @@ -__version__ = "7.4.2" +__version__ = "7.4.3" diff --git a/python/nwm_client_new/tests/test_UnitHandler.py b/python/nwm_client_new/tests/test_UnitHandler.py index c2bbdb06..bb84e5fe 100644 --- a/python/nwm_client_new/tests/test_UnitHandler.py +++ b/python/nwm_client_new/tests/test_UnitHandler.py @@ -2,6 +2,8 @@ from hydrotools.nwm_client_new import UnitHandler import numpy as np import pandas as pd +from tempfile import TemporaryDirectory +from pint import UnitRegistry @pytest.fixture def setup_unit_handler(): @@ -24,3 +26,13 @@ def test_convert_values(setup_unit_handler): ) assert (np.all(np.isclose(s, 0.3048))) + +def test_alt_unit_registry(): + with TemporaryDirectory() as td: + uh = UnitHandler.UnitHandler( + unit_registry=UnitRegistry(cache_folder=td) + ) + test_conversion_factor(uh) + test_convert_values(uh) + from pathlib import Path + assert next(Path(td).iterdir(), None)