Skip to content

Commit 707ef39

Browse files
use convertible_unit in conversions
1 parent 97c65d5 commit 707ef39

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

src/geophires_x/Economics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,7 @@ def __init__(self, model: Model):
13831383
)
13841384

13851385
fir_default_unit = PercentUnit.PERCENT
1386-
fir_default_val = self.discountrate.quantity().to('percent').magnitude # pint can't handle '%' in python 3.8
1386+
fir_default_val = self.discountrate.quantity().to(convertible_unit(fir_default_unit)).magnitude # pint can't handle '%' in python 3.8
13871387
self.FixedInternalRate = self.ParameterDict[self.FixedInternalRate.Name] = floatParameter(
13881388
"Fixed Internal Rate",
13891389
DefaultValue=fir_default_val,

src/geophires_x/GeoPHIRESUtils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import CoolProp.CoolProp as CP
1919

2020
from geophires_x.Parameter import ParameterEntry, Parameter
21-
from geophires_x.Units import get_unit_registry
21+
from geophires_x.Units import get_unit_registry, convertible_unit
2222

2323
_logger = logging.getLogger('root') # TODO use __name__ instead of root
2424

@@ -224,7 +224,7 @@ def quantity(value: float, unit: str) -> PlainQuantity:
224224
:rtype: pint.registry.Quantity - note type annotation uses PlainQuantity due to issues with python 3.8 failing
225225
to import the Quantity TypeAlias
226226
"""
227-
return _ureg.Quantity(value, unit)
227+
return _ureg.Quantity(value, convertible_unit(unit))
228228

229229

230230
@lru_cache

src/geophires_x/Parameter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def UnitsMatch(self) -> str:
7676

7777
def with_preferred_units(self) -> Any: # Any is a proxy for Self
7878
ret: OutputParameter = dataclasses.replace(self)
79-
ret.value = ret.quantity().to(ret.PreferredUnits).magnitude
79+
ret.value = ret.quantity().to(convertible_unit(ret.PreferredUnits)).magnitude
8080
ret.CurrentUnits = ret.PreferredUnits
8181
return ret
8282

@@ -609,7 +609,7 @@ def ConvertUnitsBack(ParamToModify: Parameter, model):
609609
model.logger.info(f'Init {str(__name__)}: {sys._getframe().f_code.co_name} for {ParamToModify.Name}')
610610

611611
try:
612-
ParamToModify.value = _ureg.Quantity(ParamToModify.value, ParamToModify.CurrentUnits.value).to(ParamToModify.PreferredUnits.value).magnitude
612+
ParamToModify.value = _ureg.Quantity(ParamToModify.value, convertible_unit(ParamToModify.CurrentUnits)).to(convertible_unit(ParamToModify.PreferredUnits)).magnitude
613613
ParamToModify.CurrentUnits = ParamToModify.PreferredUnits
614614
except AttributeError as ae:
615615
# TODO refactor to check for/convert currency instead of relying on try/except once currency conversion is
@@ -848,7 +848,7 @@ def ConvertOutputUnits(oparam: OutputParameter, newUnit: Units, model):
848848
"""
849849

850850
try:
851-
oparam.value = _ureg.Quantity(oparam.value, oparam.CurrentUnits.value).to(newUnit.value).magnitude
851+
oparam.value = _ureg.Quantity(oparam.value, oparam.CurrentUnits.value).to(convertible_unit(newUnit.value)).magnitude
852852
oparam.CurrentUnits = newUnit
853853
return
854854
except AttributeError as ae:

src/geophires_x/Units.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ def get_unit_registry():
1515

1616
return _UREG
1717

18+
19+
def convertible_unit(u:str) -> str:
20+
if u == Units.PERCENT or u == Units.PERCENT.value:
21+
return 'percent'
22+
23+
return u
24+
1825
class Units(IntEnum):
1926
"""All possible systems of measure"""
2027
NONE = auto()

0 commit comments

Comments
 (0)