Skip to content

Commit 1b7e1c6

Browse files
Strip out obsolete conversion cruft and tighten up boundaries around disabled currency conversion (NREL#236)
1 parent 29cfa46 commit 1b7e1c6

File tree

2 files changed

+63
-71
lines changed

2 files changed

+63
-71
lines changed

src/geophires_x/Parameter.py

Lines changed: 28 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -594,17 +594,34 @@ def ConvertUnitsBack(ParamToModify: Parameter, model):
594594
ParamToModify.value = _ureg.Quantity(ParamToModify.value, ParamToModify.CurrentUnits.value).to(ParamToModify.PreferredUnits.value).magnitude
595595
ParamToModify.CurrentUnits = ParamToModify.PreferredUnits
596596
except AttributeError as ae:
597-
model.logger.warning(f'Failed to convert units with pint, falling back to legacy conversion heuristics ({ae})')
597+
# TODO refactor to check for/convert currency instead of relying on try/except once currency conversion is
598+
# re-enabled - https://github.com/NREL/GEOPHIRES-X/issues/236?title=Currency+conversions+disabled
599+
model.logger.warning(f'Failed to convert units with pint, attempting currency conversion ({ae})')
600+
601+
try:
602+
param_modified: Parameter = parameter_with_currency_units_converted_back_to_preferred_units(ParamToModify,
603+
model)
604+
ParamToModify.value = param_modified.value
605+
ParamToModify.CurrentUnits = param_modified.CurrentUnits
606+
ParamToModify.UnitType = param_modified.UnitType
607+
except AttributeError as cce:
608+
model.logger.error(f'Currency conversion failed ({cce})')
609+
610+
msg = (
611+
f'Error: GEOPHIRES failed to convert your units for {ParamToModify.Name} to something it understands. '
612+
f'You gave {ParamToModify.CurrentUnits} - Are the units defined for Pint library, '
613+
f' or have you defined them in the user defined units file (GEOPHIRES3_newunits)? '
614+
f'Cannot continue. Exiting.'
615+
)
616+
model.logger.critical(msg)
617+
618+
raise RuntimeError(msg)
598619

599-
param_modified: Parameter = parameter_with_units_converted_back_to_preferred_units(ParamToModify, model)
600-
ParamToModify.value = param_modified.value
601-
ParamToModify.CurrentUnits = param_modified.CurrentUnits
602-
ParamToModify.UnitType = param_modified.UnitType
603620

604621
model.logger.info(f'Complete {str(__name__)}: {sys._getframe().f_code.co_name}')
605622

606623

607-
def parameter_with_units_converted_back_to_preferred_units(param: Parameter, model) -> Parameter:
624+
def parameter_with_currency_units_converted_back_to_preferred_units(param: Parameter, model) -> Parameter:
608625
"""
609626
TODO clean up and consolidate with pint-based conversion in ConvertUnitsBack
610627
"""
@@ -682,7 +699,7 @@ def parameter_with_units_converted_back_to_preferred_units(param: Parameter, mod
682699
msg = (
683700
f'Error: GEOPHIRES failed to convert your currency for {param.Name} to something it understands.'
684701
f'You gave {currType} - Are these currency units defined for forex-python? '
685-
f'or perhaps the currency server is down? Please change your units to {param.PreferredUnits.value}'
702+
f'or perhaps the currency server is down? Please change your units to {param.PreferredUnits.value} '
686703
f'to continue. Cannot continue unless you do. Exiting.'
687704
)
688705
print(msg)
@@ -695,66 +712,10 @@ def parameter_with_units_converted_back_to_preferred_units(param: Parameter, mod
695712
return param_with_units_converted_back
696713

697714
else:
698-
# must be something other than currency
699-
if isinstance(param.CurrentUnits, pint.Quantity):
700-
val = param.CurrentUnits.value
701-
currType = str(param.CurrentUnits.value)
702-
else:
703-
if ' ' in param.CurrentUnits.value:
704-
parts = param.CurrentUnits.value.split(' ')
705-
val = parts[0].strip()
706-
currType = parts[1].strip()
707-
else:
708-
val = param.value
709-
currType = param.CurrentUnits.value
710-
711-
try:
712-
if isinstance(param.PreferredUnits, pint.Quantity):
713-
prefQ = param.PreferredUnits
714-
else:
715-
# Make a Pint Quantity out of the old value
716-
prefQ = param.PreferredUnits
717-
if isinstance(param.CurrentUnits, pint.Quantity):
718-
currQ = param.CurrentUnits
719-
else:
720-
currQ = _ureg.Quantity(val, currType) # Make a Pint Quantity out of the new value
721-
except BaseException as ex:
722-
print(str(ex))
723-
msg = (
724-
f'Error: GEOPHIRES failed to initialize your units for {param.Name} to something it understands. '
725-
f'You gave {currType} - Are the units defined for Pint library, '
726-
f'or have you defined them in the user defined units file (GEOPHIRES3_newunits)? '
727-
f'Cannot continue. Exiting.'
728-
)
729-
print(msg)
730-
model.logger.critical(str(ex))
731-
model.logger.critical(msg)
732-
733-
raise RuntimeError(msg)
734-
try:
735-
# update The quantity back to the current units (the units that we started with) units
736-
# so the display will be in the right units
737-
currQ = currQ.to(prefQ)
738-
except BaseException as ex:
739-
print(str(ex))
740-
msg = (
741-
f'Error: GEOPHIRES failed to convert your units for {param.Name} to something it understands. '
742-
f'You gave {currType} - Are the units defined for Pint library, '
743-
f' or have you defined them in the user defined units file (GEOPHIRES3_newunits)? '
744-
f'Cannot continue. Exiting.'
745-
)
746-
print(msg)
747-
model.logger.critical(str(ex))
748-
model.logger.critical(msg)
749-
750-
raise RuntimeError(msg)
751-
752-
# reset the values
753-
if param.value != currQ.magnitude:
754-
param_with_units_converted_back.value = currQ.magnitude
755-
param_with_units_converted_back.CurrentUnits = param.PreferredUnits
756-
757-
return param_with_units_converted_back
715+
raise AttributeError(
716+
f'Unit/unit type ({param.CurrentUnits}/{param.UnitType} for {param.Name} '
717+
f'is not a recognized currency unit'
718+
)
758719

759720

760721
def LookupUnits(sUnitText: str):

tests/test_parameter.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
from pathlib import Path
55

66
from geophires_x.Model import Model
7+
from geophires_x.Parameter import ConvertUnitsBack
78
from geophires_x.Parameter import OutputParameter
89
from geophires_x.Parameter import Parameter
910
from geophires_x.Parameter import floatParameter
1011
from geophires_x.Parameter import listParameter
11-
from geophires_x.Parameter import parameter_with_units_converted_back_to_preferred_units
12+
from geophires_x.Units import CostPerMassUnit
13+
from geophires_x.Units import CurrencyUnit
1214
from geophires_x.Units import EnergyCostUnit
1315
from geophires_x.Units import LengthUnit
1416
from geophires_x.Units import PressureUnit
@@ -38,10 +40,10 @@ def test_convert_units_back(self):
3840
)
3941
self.assertFalse(param_to_modify.UnitsMatch)
4042

41-
result = parameter_with_units_converted_back_to_preferred_units(param_to_modify, model)
43+
ConvertUnitsBack(param_to_modify, model)
4244

43-
self.assertEqual(result.value, 7.0)
44-
self.assertEqual(result.CurrentUnits, LengthUnit.INCHES)
45+
self.assertEqual(param_to_modify.value, 7.0)
46+
self.assertEqual(param_to_modify.CurrentUnits, LengthUnit.INCHES)
4547

4648
def test_set_default_value(self):
4749
without_val = floatParameter(
@@ -149,6 +151,35 @@ def test_output_parameter_with_preferred_units(self):
149151
self.assertEqual(5.5, result.value[0])
150152
self.assertEqual(5.5, result.value[-1])
151153

154+
def test_convert_units_back_currency(self):
155+
model = self._new_model()
156+
157+
param = floatParameter(
158+
'CAPEX',
159+
DefaultValue=1379.0,
160+
UnitType=Units.COSTPERMASS,
161+
PreferredUnits=CostPerMassUnit.DOLLARSPERMT,
162+
CurrentUnits=CostPerMassUnit.CENTSSPERMT,
163+
)
164+
165+
ConvertUnitsBack(param, model)
166+
self.assertEqual(param.CurrentUnits, CostPerMassUnit.DOLLARSPERMT)
167+
self.assertAlmostEqual(param.value, 13.79, places=2)
168+
169+
with self.assertRaises(RuntimeError) as re:
170+
# TODO update once https://github.com/NREL/GEOPHIRES-X/issues/236?title=Currency+conversions+disabled is
171+
# addressed
172+
param2 = floatParameter(
173+
'OPEX',
174+
DefaultValue=240,
175+
UnitType=Units.CURRENCY,
176+
PreferredUnits=CurrencyUnit.DOLLARS,
177+
CurrentUnits=CurrencyUnit.EUR,
178+
)
179+
ConvertUnitsBack(param2, model)
180+
181+
self.assertIn('GEOPHIRES failed to convert your units for OPEX', str(re))
182+
152183
def _new_model(self) -> Model:
153184
stash_cwd = Path.cwd()
154185
stash_sys_argv = sys.argv

0 commit comments

Comments
 (0)