Skip to content

Commit

Permalink
Mechanism for GCM to disallow vars in input_file
Browse files Browse the repository at this point in the history
In CESM+MOM6, we want the values of abio_dic_on and base_bio_on to be
determined by the MARBL_TRACER_OPTS variable in env_run.xml; in that case, we
do not want users to set those variables in user_nl_marbl. This commit allows
GCMs to give MARBL a dictionary, where the key is a variable name and the value
is an error message to return if that variable appears in the input_file.
  • Loading branch information
mnlevy1981 committed Mar 7, 2024
1 parent 644cadc commit c15e634
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions MARBL_tools/MARBL_settings_file_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MARBL_settings_class(object):

def __init__(self, default_settings_file, saved_state_vars_source="settings_file",
base_bio_on=True, abio_dic_on=False, grid=None, input_file=None,
unit_system='cgs'):
unit_system='cgs', exclude_dict={}):
""" Class constructor: set up a dictionary of config keywords for when multiple
default values are provided, read the JSON file, and then populate
self.settings_dict and self.tracers_dict.
Expand Down Expand Up @@ -46,7 +46,7 @@ def __init__(self, default_settings_file, saved_state_vars_source="settings_file
MARBL_tools.abort(1)

# 4. Read settings input file
self._input_dict = _parse_input_file(input_file)
self._input_dict = _parse_input_file(input_file, exclude_dict)

# 5. Use an ordered dictionary for keeping variable, value pairs
# Also, tracer information is in its own dictionary (initialized to None)
Expand Down Expand Up @@ -610,7 +610,7 @@ def _string_to_substring(str_in, separator):

################################################################################

def _parse_input_file(input_file):
def _parse_input_file(input_file, exclude_dict):
""" 1. Read an input file; ignore blank lines and non-quoted Fortran comments.
2. Turn lines of the form
variable = value
Expand All @@ -633,6 +633,9 @@ def _parse_input_file(input_file):
line_list = line_loc.strip().split('=')
# keys in input_dict are all lowercase to allow case-insensitive match
var_name = line_list[0].strip().lower()
if var_name in exclude_dict:
logger.error(exclude_dict[var_name])
MARBL_tools.abort(1)
value = line_list[1].strip()
val_array = _string_to_substring(value, ',')
if len(val_array) > 1:
Expand All @@ -644,10 +647,7 @@ def _parse_input_file(input_file):
# Single value
input_dict[var_name] = value
f.close()
except TypeError:
# If inputfile == None then the open will result in TypeError
pass
except:
except FileNotFoundError:
logger.error("input_file '%s' was not found" % input_file)
MARBL_tools.abort(1)
return input_dict

0 comments on commit c15e634

Please sign in to comment.