Skip to content

Commit

Permalink
Refactor get_final_parameters()
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvothecoder committed Nov 29, 2023
1 parent 0678613 commit 15d3138
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
3 changes: 2 additions & 1 deletion e3sm_diags/e3sm_diags_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,9 @@ def main(parameters=[]):

# If no parameters are passed, use the parser args as defaults. Otherwise,
# create the dictionary of expected parameters.
if not parameters:
if len(parameters) == 0:
parameters = get_parameters(parser)

expected_parameters = create_parameter_dict(parameters)

if not os.path.exists(parameters[0].results_dir):
Expand Down
8 changes: 2 additions & 6 deletions e3sm_diags/parser/core_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,8 +784,7 @@ def get_cfg_parameters(
RuntimeError
If the parameters input file is not `.json` or `.cfg` format.
"""

parameters = []
params = []

self._parse_arguments()

Expand All @@ -801,10 +800,7 @@ def get_cfg_parameters(
else:
raise RuntimeError("The parameters input file must be a .cfg file")

for p in params:
parameters.append(p)

return parameters
return params

def _get_cfg_parameters(
self, cfg_file, check_values=False, argparse_vals_only=True
Expand Down
40 changes: 16 additions & 24 deletions e3sm_diags/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,42 +66,34 @@ def get_final_parameters(self, parameters):

self._add_parent_attrs_to_children(parameters)

final_params = []

# Get the sets to run using the parameter objects.
# Get the sets to run using the parameter objects via the Python API
sets_to_run = [param.sets for param in parameters]
self.sets_to_run = list(chain.from_iterable(sets_to_run))

api_params = []
for set_name in self.sets_to_run:
# For each of the set_names, get the corresponding parameter.
param = self._get_instance_of_param_class(
api_param = self._get_instance_of_param_class(
SET_TO_PARAMETERS[set_name], parameters
)

# Since each parameter will have lots of default values, we want to remove them.
# Otherwise when calling get_parameters(), these default values
# will take precedence over values defined in other_params.
self._remove_attrs_with_default_values(param)
param.sets = [set_name]

other_params = self._get_diags_from_cfg_file()
params = self.parser.get_parameters(
orig_parameters=param,
other_parameters=other_params,
cmd_default_vars=False,
argparse_vals_only=False,
)
self._remove_attrs_with_default_values(api_param)
api_param.sets = [set_name]

# Makes sure that any parameters that are selectors
# will be in param.
self._add_attrs_with_default_values(param)
# The select() call in get_parameters() was made for the original
# command-line way of using CDP.
# We just call it manually with the parameter object param.
params = self.parser.select(param, params)
# Makes sure that any parameters that are selectors will be in param.
self._add_attrs_with_default_values(api_param)

final_params.extend(params)
api_params.append(api_param)

# Get the diagnostic parameter objects from the .cfg file passed via
# the -d/--diags CLI argument (if set). Otherwise, it will be an empty
# list.
cfg_params = self._get_diags_from_cfg_file()

final_params = api_params + cfg_params
self.parser.check_values_of_params(final_params)

return final_params
Expand Down Expand Up @@ -248,14 +240,14 @@ def _get_instance_of_param_class(self, cls, parameters):
raise RuntimeError(msg.format(class_types))

def _get_diags_from_cfg_file(self) -> Union[List, List[CoreParameter]]:
"""Get diagnostics defined in a cfg file using ``-d`` CLI arg.
"""
Get parameters defined by the cfg file passed to -d/--diags (if set).
If ``-d`` is not passed, return an empty list.
"""
args = self.parser.view_args()
params = []

# If the user has passed in args with -d.
if args.other_parameters:
params = self.parser.get_cfg_parameters(argparse_vals_only=False)
# For each of the params, add in the default values
Expand Down

0 comments on commit 15d3138

Please sign in to comment.