Skip to content

Commit 642c945

Browse files
clean up WIP, break out _read_list_parameter
1 parent 430b3bf commit 642c945

File tree

1 file changed

+42
-73
lines changed

1 file changed

+42
-73
lines changed

src/geophires_x/Parameter.py

Lines changed: 42 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -393,79 +393,8 @@ def default_parameter_value_message(new_val: Any, param_to_modify_name: str, def
393393
ParamToModify.Provided = True # set provided to true because we are using a user provide value now
394394
ParamToModify.Valid = True # set Valid to true because it passed the validation tests
395395
elif isinstance(ParamToModify, listParameter):
396-
if ' ' in ParamToModify.Name:
397-
New_val = float(ParameterReadIn.sValue)
398-
# Some list parameters are read in with enumerated parameter names; in these cases we use the last
399-
# character of the description to get the position i.e., "Gradient 1" is position 0.
400-
parts = ParameterReadIn.Name.split(' ')
401-
position = int(parts[1]) - 1
402-
if position >= len(ParamToModify.value):
403-
ParamToModify.value.append(New_val) # we are adding to the list, so use append
404-
else: # we are replacing a value, so pop the value we want to replace, then insert a new one
405-
ParamToModify.value.pop(position)
406-
ParamToModify.value.insert(position, New_val)
407-
else:
408-
# In an ideal world this would be handled in ParameterEntry such that its sValue and Comment are
409-
# correct; however that would only be practical if ParameterEntry had typing information to know
410-
# whether to treat text after a second comma as a comment or list entry.
411-
ParamToModify.value = [float(x.strip()) for x in ParameterReadIn.raw_entry.split('--')[0].split(',')[1:]
412-
if x.strip() != '']
413-
414-
# FIXME WIP TODO validate entries comply with min/max (as implemented in commented code below)
415-
# FIXME WIP TODO set ParamToModify.Valid/Provided
416-
417-
# def _read_list_parameter():
418-
# read_list = []
419-
# if ParameterReadIn.raw_entry is None:
420-
# # Shouldn't happen...
421-
# model.logger.error(f'Invalid parameter entry for {ParamToModify.Name}: {ParameterReadIn.sValue}')
422-
# return
423-
#
424-
# raw_split = ParameterReadIn.raw_entry.split(',')
425-
# for i in range(len(raw_split)):
426-
# raw_entry = raw_split[i].strip()
427-
# read_entry = raw_entry
428-
# try:
429-
# read_entry = float(raw_entry)
430-
# except ValueError:
431-
# if i == len(raw_split) - 1:
432-
# pass # assumed to be comment
433-
# else:
434-
# model.logger.error(f'Invalid parameter entry for {ParamToModify.Name}: {raw_entry}')
435-
#
436-
# read_list.append(read_entry)
437-
438-
# _read_list_parameter()
439-
440-
# New_val = float(ParameterReadIn.sValue)
441-
# if (New_val < float(ParamToModify.Min)) or (New_val > float(ParamToModify.Max)):
442-
# # user provided value is out of range, so announce it, leave set to whatever it was set to (default value)
443-
# if len(ParamToModify.ErrMessage) > 0:
444-
# msg = (
445-
# f'Parameter given ({str(New_val)}) for {ParamToModify.Name} outside of valid range.'
446-
# f'GEOPHIRES will {ParamToModify.ErrMessage}'
447-
# )
448-
# print(f'Warning: {msg}')
449-
# model.logger.warning(msg)
450-
# model.logger.info(f'Complete {str(__name__)}: {sys._getframe().f_code.co_name}')
451-
# return
452-
# else:
453-
# if ' ' in ParamToModify.Name:
454-
# # Some list parameters are read in with enumerated parameter names; in these cases we use the last
455-
# # character of the description to get the position i.e., "Gradient 1" is position 0.
456-
# parts = ParameterReadIn.Name.split(' ')
457-
# position = int(parts[1]) - 1
458-
# if position >= len(ParamToModify.value):
459-
# ParamToModify.value.append(New_val) # we are adding to the list, so use append
460-
# else: # we are replacing a value, so pop the value we want to replace, then insert a new one
461-
# ParamToModify.value.pop(position)
462-
# ParamToModify.value.insert(position, New_val)
463-
# else:
464-
# # In an ideal world this would be handled in ParameterEntry such that its sValue and Comment are
465-
# # correct; however that would only be practical if ParameterEntry had typing information to know
466-
# # whether to treat text after a second comma as a comment or list entry.
467-
# ParamToModify.value = [float(x.strip()) for x in ParameterReadIn.raw_entry.split('--')[0].split(',')[1:]
468-
# if x.strip() != '']
396+
_read_list_parameter(ParameterReadIn, ParamToModify, model)
397+
469398
elif isinstance(ParamToModify, boolParameter):
470399
if ParameterReadIn.sValue == "0":
471400
New_val = False
@@ -493,6 +422,46 @@ def default_parameter_value_message(new_val: Any, param_to_modify_name: str, def
493422
model.logger.info(f'Complete {str(__name__)}: {sys._getframe().f_code.co_name}')
494423

495424

425+
def _read_list_parameter(ParameterReadIn: ParameterEntry, ParamToModify, model) -> None:
426+
"""
427+
:type model: :class:`~geophires_x.Model.Model`
428+
"""
429+
430+
if ' ' in ParamToModify.Name:
431+
New_val = float(ParameterReadIn.sValue)
432+
# Some list parameters are read in with enumerated parameter names; in these cases we use the last
433+
# character of the description to get the position i.e., "Gradient 1" is position 0.
434+
parts = ParameterReadIn.Name.split(' ')
435+
position = int(parts[1]) - 1
436+
if position >= len(ParamToModify.value):
437+
ParamToModify.value.append(New_val) # we are adding to the list, so use append
438+
else: # we are replacing a value, so pop the value we want to replace, then insert a new one
439+
ParamToModify.value.pop(position)
440+
ParamToModify.value.insert(position, New_val)
441+
else:
442+
# In an ideal world this would be handled in ParameterEntry such that its sValue and Comment are
443+
# correct; however that would only be practical if ParameterEntry had typing information to know
444+
# whether to treat text after a second comma as a comment or list entry.
445+
ParamToModify.value = [float(x.strip()) for x in ParameterReadIn.raw_entry.split('--')[0].split(',')[1:]
446+
if x.strip() != '']
447+
448+
ParamToModify.Provided = True
449+
450+
valid = True
451+
for i in range(len(ParamToModify.value)):
452+
New_val = ParamToModify.value[i]
453+
if (New_val < float(ParamToModify.Min)) or (New_val > float(ParamToModify.Max)):
454+
msg = (
455+
f'Value given ({str(New_val)}) for {ParamToModify.Name} outside of valid range '
456+
f'({ParamToModify.Min}{ParamToModify.Max}).'
457+
)
458+
print(f'Warning: {msg}')
459+
model.logger.warning(msg)
460+
valid = False
461+
462+
ParamToModify.Valid = valid
463+
464+
496465
def ConvertUnits(ParamToModify, strUnit: str, model) -> str:
497466
"""
498467
ConvertUnits gets called if a unit version is needed: either currency or standard units like F to C or m to ft

0 commit comments

Comments
 (0)