Skip to content

Commit

Permalink
update default units handler to use explicit cache folder
Browse files Browse the repository at this point in the history
  • Loading branch information
jarq6c committed Jun 10, 2024
1 parent 054db88 commit ec71b2a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()}
Expand Down
12 changes: 12 additions & 0 deletions python/nwm_client_new/tests/test_UnitHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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)

0 comments on commit ec71b2a

Please sign in to comment.