@@ -107,14 +107,23 @@ class Parameter(HasQuantity):
107
107
InputComment : str = ""
108
108
ToolTipText : str = Name
109
109
UnitType : IntEnum = Units .NONE
110
- PreferredUnits : Enum = Units . NONE
110
+ PreferredUnits : Enum = None
111
111
112
112
# set to PreferredUnits assuming that the current units are the preferred units
113
113
# - they will only change if the read function reads a different unit associated with a parameter
114
114
CurrentUnits : Enum = PreferredUnits
115
- UnitsMatch : bool = True
115
+ #UnitsMatch: bool = True
116
+
117
+ @property
118
+ def UnitsMatch (self ) -> bool :
119
+ return self .PreferredUnits == self .CurrentUnits
120
+
116
121
parameter_category : str = None
117
122
123
+ def __post_init__ (self ):
124
+ if self .PreferredUnits is None :
125
+ self .PreferredUnits = self .CurrentUnits
126
+
118
127
119
128
@dataclass
120
129
class boolParameter (Parameter ):
@@ -176,6 +185,7 @@ class floatParameter(Parameter):
176
185
def __post_init__ (self ):
177
186
if self .value is None :
178
187
self .value = self .DefaultValue
188
+ super ().__post_init__ ()
179
189
180
190
value : float = None
181
191
@@ -271,7 +281,7 @@ def ReadParameter(ParameterReadIn: ParameterEntry, ParamToModify, model):
271
281
else :
272
282
# The value came in without any units, so it must be using the default PreferredUnits
273
283
ParamToModify .CurrentUnits = ParamToModify .PreferredUnits
274
- ParamToModify .UnitsMatch = True
284
+ # ParamToModify.UnitsMatch = True
275
285
276
286
def default_parameter_value_message (new_val : Any , param_to_modify_name : str , default_value : Any ) -> str :
277
287
return (
@@ -414,7 +424,7 @@ def ConvertUnits(ParamToModify, strUnit: str, model) -> str:
414
424
# user has provided a currency that is the currency expected, so just strip off the currency
415
425
if prefType == currType :
416
426
strUnit = str (val )
417
- ParamToModify .UnitsMatch = True
427
+ # ParamToModify.UnitsMatch = True
418
428
ParamToModify .CurrentUnits = currType
419
429
return strUnit
420
430
@@ -470,7 +480,7 @@ def ConvertUnits(ParamToModify, strUnit: str, model) -> str:
470
480
471
481
val = float (val ) * Factor
472
482
strUnit = str (val )
473
- ParamToModify .UnitsMatch = True
483
+ # ParamToModify.UnitsMatch = True
474
484
ParamToModify .CurrentUnits = currType
475
485
return strUnit
476
486
@@ -494,7 +504,7 @@ def ConvertUnits(ParamToModify, strUnit: str, model) -> str:
494
504
495
505
New_val = (conv_rate * float (val )) * Factor
496
506
strUnit = str (New_val )
497
- ParamToModify .UnitsMatch = False
507
+ # ParamToModify.UnitsMatch = False
498
508
ParamToModify .CurrentUnits = parts [1 ]
499
509
500
510
if len (prefSuff ) > 0 :
@@ -562,7 +572,7 @@ def ConvertUnits(ParamToModify, strUnit: str, model) -> str:
562
572
if new_val_units_lookup is not None and new_val_units_lookup [0 ] is not None :
563
573
ParamToModify .CurrentUnits = new_val_units_lookup [0 ]
564
574
565
- ParamToModify .UnitsMatch = False
575
+ # ParamToModify.UnitsMatch = False
566
576
else :
567
577
# if we come here, we must have a unit declared, but the unit must be the same as the preferred unit,
568
578
# so we need to just get rid of the extra text after the space
@@ -585,10 +595,19 @@ def ConvertUnitsBack(ParamToModify: Parameter, model):
585
595
:return: None
586
596
"""
587
597
model .logger .info (f'Init { str (__name__ )} : { sys ._getframe ().f_code .co_name } for { ParamToModify .Name } ' )
588
- param_modified : Parameter = parameter_with_units_converted_back_to_preferred_units (ParamToModify , model )
589
- ParamToModify .value = param_modified .value
590
- ParamToModify .CurrentUnits = param_modified .CurrentUnits
591
- ParamToModify .UnitType = param_modified .UnitsMatch
598
+ # param_modified: Parameter = parameter_with_units_converted_back_to_preferred_units(ParamToModify, model)
599
+ # ParamToModify.value = param_modified.value
600
+ # ParamToModify.CurrentUnits = param_modified.CurrentUnits
601
+ # ParamToModify.UnitType = param_modified.UnitType
602
+
603
+ try :
604
+ ParamToModify .value = _ureg .Quantity (ParamToModify .value , ParamToModify .CurrentUnits .value ).to (ParamToModify .PreferredUnits .value ).magnitude
605
+ ParamToModify .CurrentUnits = ParamToModify .PreferredUnits
606
+ except AttributeError as ae :
607
+ # FIXME WIP
608
+ model .logger .warning (f'Error: { ae } ' )
609
+ #ParamToModify.UnitType = param_modified.UnitType
610
+
592
611
model .logger .info (f'Complete { str (__name__ )} : { sys ._getframe ().f_code .co_name } ' )
593
612
594
613
@@ -651,7 +670,7 @@ def parameter_with_units_converted_back_to_preferred_units(param: Parameter, mod
651
670
# this is true, then we just have a conversion between KUSD and USD, MUSD to KUSD, MUER to EUR, etc.,
652
671
# so just do the simple factor conversion
653
672
param_with_units_converted_back .value = param .value * Factor
654
- param_with_units_converted_back .UnitsMatch = True
673
+ # param_with_units_converted_back.UnitsMatch = True
655
674
param_with_units_converted_back .CurrentUnits = currType
656
675
return param_with_units_converted_back
657
676
@@ -677,7 +696,7 @@ def parameter_with_units_converted_back_to_preferred_units(param: Parameter, mod
677
696
raise RuntimeError (msg , ex )
678
697
679
698
param_with_units_converted_back .value = (conv_rate * float (param .value )) / prefFactor
680
- param_with_units_converted_back .UnitsMatch = False
699
+ # param_with_units_converted_back.UnitsMatch = False
681
700
return param_with_units_converted_back
682
701
683
702
else :
@@ -703,7 +722,7 @@ def parameter_with_units_converted_back_to_preferred_units(param: Parameter, mod
703
722
if isinstance (param .CurrentUnits , pint .Quantity ):
704
723
currQ = param .CurrentUnits
705
724
else :
706
- currQ = _ureg .Quantity (float ( val ) , currType ) # Make a Pint Quantity out of the new value
725
+ currQ = _ureg .Quantity (val , currType ) # Make a Pint Quantity out of the new value
707
726
except BaseException as ex :
708
727
print (str (ex ))
709
728
msg = (
@@ -1061,5 +1080,5 @@ def ConvertOutputUnits(oparam: OutputParameter, newUnit: Units, model):
1061
1080
1062
1081
oparam .value = Factor * conv_rate * float (oparam .value )
1063
1082
oparam .CurrentUnits = DefUnit
1064
- oparam .UnitsMatch = False
1083
+ # oparam.UnitsMatch = False
1065
1084
model .logger .info (f'Complete { str (__name__ )} : { sys ._getframe ().f_code .co_name } ' )
0 commit comments