@@ -594,17 +594,34 @@ def ConvertUnitsBack(ParamToModify: Parameter, model):
594
594
ParamToModify .value = _ureg .Quantity (ParamToModify .value , ParamToModify .CurrentUnits .value ).to (ParamToModify .PreferredUnits .value ).magnitude
595
595
ParamToModify .CurrentUnits = ParamToModify .PreferredUnits
596
596
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 )
598
619
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
603
620
604
621
model .logger .info (f'Complete { str (__name__ )} : { sys ._getframe ().f_code .co_name } ' )
605
622
606
623
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 :
608
625
"""
609
626
TODO clean up and consolidate with pint-based conversion in ConvertUnitsBack
610
627
"""
@@ -682,7 +699,7 @@ def parameter_with_units_converted_back_to_preferred_units(param: Parameter, mod
682
699
msg = (
683
700
f'Error: GEOPHIRES failed to convert your currency for { param .Name } to something it understands.'
684
701
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 } '
686
703
f'to continue. Cannot continue unless you do. Exiting.'
687
704
)
688
705
print (msg )
@@ -695,66 +712,10 @@ def parameter_with_units_converted_back_to_preferred_units(param: Parameter, mod
695
712
return param_with_units_converted_back
696
713
697
714
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
+ )
758
719
759
720
760
721
def LookupUnits (sUnitText : str ):
0 commit comments