@@ -823,8 +823,6 @@ def ConvertOutputUnits(oparam: OutputParameter, newUnit: Units, model):
823
823
ConvertOutputUnits Given an output parameter, convert the value(s) from what they contain
824
824
(as calculated by GEOPHIRES) to what the user specified as what they want for outputs. Conversion happens inline.
825
825
826
- TODO switch to pint-based conversion like in ConvertUnitsBack
827
-
828
826
:param oparam: The parameter you want to be converted (value or list of values). Because Parameters know the
829
827
PreferredUnits and CurrentUnits, this routine knows what to do. It will convert the value(s) in the parameter
830
828
to the new units, and then reset the CurrentUnits to the new units. This is done so that the user can see the units
@@ -840,103 +838,37 @@ def ConvertOutputUnits(oparam: OutputParameter, newUnit: Units, model):
840
838
:type model: :class:`~geophires_x.Model.Model`
841
839
:return: None
842
840
"""
841
+
842
+ try :
843
+ oparam .value = _ureg .Quantity (oparam .value , oparam .CurrentUnits .value ).to (newUnit .value ).magnitude
844
+ oparam .CurrentUnits = newUnit
845
+ return
846
+ except AttributeError as ae :
847
+ # TODO refactor to check for/convert currency instead of relying on try/except once currency conversion is
848
+ # re-enabled - https://github.com/NREL/GEOPHIRES-X/issues/236?title=Currency+conversions+disabled
849
+ model .logger .warning (f'Failed to convert units with pint, falling back to legacy conversion code ({ ae } )' )
850
+
843
851
if isinstance (oparam .value , str ):
844
852
return # strings have no units
845
853
elif isinstance (oparam .value , bool ):
846
854
return # booleans have no units
847
855
DefUnit , UnitSystem = LookupUnits (str (newUnit .value ))
848
856
849
857
if UnitSystem not in [Units .CURRENCY , Units .CURRENCYFREQUENCY , Units .COSTPERMASS , Units .ENERGYCOST ]:
850
- if isinstance (oparam .value , float ) or isinstance (oparam .value , int ):
851
- # this is a simple unit conversion: it could be just units (meters->feet) or simple currency ($->EUR)
852
- # or compound Currency (MUSD-EUR)
853
- try :
854
- fromQ = _ureg .Quantity (
855
- oparam .value , str (oparam .PreferredUnits .value )
856
- ) # Make a Pint Quantity out of the value
857
- toQ = _ureg .Quantity (0 , str (newUnit .value )) # Make a Pint Quantity out of the new value
858
- except BaseException as ex :
859
- print (str (ex ))
860
- msg = (
858
+ msg = (
861
859
"Warning: GEOPHIRES failed to initialize your units for "
862
860
+ oparam .Name
863
861
+ " to something it understands. You gave "
864
862
+ str (newUnit .value )
865
- + " - Are the units defined for Pint"
866
- + " library, or have you defined them in the user defined units file (GEOPHIRES3_newunits)?"
867
- + " Continuing without output conversion."
868
- )
869
- print (msg )
870
- model .logger .warning (str (ex ))
871
- model .logger .warning (msg )
872
- return
873
- try :
874
- toQ = fromQ .to (toQ ) # update The quantity to the units that the user wanted
875
- except BaseException as ex :
876
- print (str (ex ))
877
- msg = (
878
- "Warning: GEOPHIRES failed to convert your units for "
879
- + oparam .Name
880
- + " to something it understands. You gave "
881
- + str (newUnit .value )
882
- + " - Are the units defined for Pint"
883
- + " library, or have you defined them in the user defined units file (GEOPHIRES3_newunits)?"
863
+ + " -Are the units defined for"
864
+ + " Pint library, or have you defined them in the user defined units file (GEOPHIRES3_newunits)?"
884
865
+ " Continuing without output conversion."
885
866
)
886
- print (msg )
887
- model .logger .warning (str (ex ))
888
- model .logger .warning (msg )
889
- return
890
- # reset the value and current units
891
- oparam .value = toQ .magnitude
892
- oparam .CurrentUnits = newUnit
893
-
894
- elif isinstance (oparam .value , array ): # handle the array case
895
- i = 0
896
- for arrayval in oparam .value :
897
- try :
898
- fromQ = _ureg .Quantity (
899
- oparam .value [i ], str (oparam .PreferredUnits .value )
900
- ) # Make a Pint Quantity out of from the value
901
- toQ = _ureg .Quantity (0 , str (newUnit .value )) # Make a Pint Quantity out of the new value
902
- except BaseException as ex :
903
- print (str (ex ))
904
- msg = (
905
- "Warning: GEOPHIRES failed to initialize your units for "
906
- + oparam .Name
907
- + " to something it understands. You gave "
908
- + str (newUnit .value )
909
- + " -Are the units defined for"
910
- + " Pint library, or have you defined them in the user defined units file (GEOPHIRES3_newunits)?"
911
- + " Continuing without output conversion."
912
- )
913
- print (msg )
914
- model .logger .warning (str (ex ))
915
- model .logger .warning (msg )
916
- return
917
- try :
918
- toQ = fromQ .to (toQ ) # update The quantity to the units that the user wanted
919
- except BaseException as ex :
920
- print (str (ex ))
921
- msg = (
922
- "Warning: GEOPHIRES failed to convert your units for "
923
- + oparam .Name
924
- + " to something it understands. You gave "
925
- + str (newUnit .value )
926
- + " - Are the units defined for Pint library, or have you defined them in the user defined"
927
- + " units file (GEOPHIRES3_newunits)? continuing without output conversion."
928
- )
929
- print (msg )
930
- model .logger .warning (str (ex ))
931
- model .logger .warning (msg )
932
- return
933
-
934
- # reset the value and current units
935
- oparam .value [i ] = toQ .magnitude
936
- oparam .CurrentUnits = newUnit
937
- i = i + 1
938
-
939
- else : # must be a currency thing.
867
+ print (msg )
868
+ model .logger .warning (msg )
869
+ return
870
+
871
+ else :
940
872
prefType = oparam .PreferredUnits .value
941
873
currType = newUnit .value
942
874
@@ -1032,10 +964,10 @@ def ConvertOutputUnits(oparam: OutputParameter, newUnit: Units, model):
1032
964
print (str (ex ))
1033
965
1034
966
msg = (
1035
- f'Error: GEOPHIRES failed to convert your currency for { oparam .Name } to something it understands.'
1036
- f'You gave { currType } - conversion may be affected by https://github.com/NREL/GEOPHIRES-X/issues/236. '
967
+ f'Error: GEOPHIRES failed to convert your currency for { oparam .Name } to something it understands. '
968
+ f'You gave { currType } - conversion may be affected by https://github.com/NREL/GEOPHIRES-X/issues/236. '
1037
969
f'Please change your units to { oparam .PreferredUnits .value } '
1038
- f'to continue. Cannot continue unless you do. Exiting.'
970
+ f'to continue. Cannot continue unless you do. Exiting.'
1039
971
)
1040
972
1041
973
print (msg )
@@ -1050,6 +982,7 @@ def ConvertOutputUnits(oparam: OutputParameter, newUnit: Units, model):
1050
982
model .logger .info (f'Complete { str (__name__ )} : { sys ._getframe ().f_code .co_name } ' )
1051
983
1052
984
985
+
1053
986
def coerce_int_params_to_enum_values (parameter_dict :dict [str ,Parameter ]) -> None :
1054
987
"""
1055
988
Some modules have intParameters with an int default value whose working value gets set to an
0 commit comments