Skip to content

Commit

Permalink
Tidy up mom6's config files
Browse files Browse the repository at this point in the history
- Add a warning when there's no parameter files for MOM_input_nml or (SIS_input_nml if present)
  • Loading branch information
Jo Basevi committed Oct 3, 2023
1 parent 8ace7f6 commit a054ebb
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 84 deletions.
75 changes: 38 additions & 37 deletions payu/models/mom6.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,20 @@ def __init__(self, expt, name, config):

self.config_files = [
'input.nml',
'MOM_input',
'diag_table',
]

# TODO: Need to figure out what's going on here with MOM6
self.optional_config_files = [
'data_table',
'data_table.MOM6',
'data_table.OM4',
'data_table.SIS',
'data_table.icebergs',

'field_table',

'MOM_override',
'MOM_layout',
'MOM_saltrestore',

'SIS_input',
'SIS_override',
'SIS_layout',
'field_table'
]

def setup(self):
# FMS initialisation
super(Mom6, self).setup()

self.init_config()
self.add_parameter_config_files()
self.add_config_files()

def init_config(self):
"""Patch input.nml as a new or restart run."""
Expand All @@ -81,30 +66,46 @@ def init_config(self):

f90nml.write(input_nml, input_fpath, force=True)

def add_parameter_config_files(self):
"""Check that the parameter files listed in input.nml are in the
model's configuration files"""
input_nml = f90nml.read(os.path.join(self.work_path, 'input.nml'))
def add_config_files(self):
"""Add to model configuration files"""

# Add parameter config files
config_files_to_add = self.get_parameter_files()

# Set of all configuration files
config_files = set(self.config_files).union(self.optional_config_files)
all_config_files = set(self.config_files).union(
self.optional_config_files)

for input in ['MOM_input_nml', 'SIS_input_nml']:
input_namelist = input_nml.get(input, {})
parameter_files = input_namelist.get('parameter_filename', [])
for filename in config_files_to_add:
if filename not in all_config_files:
# Extend config files
self.config_files.append(filename)
all_config_files.add(filename)

if isinstance(parameter_files, str):
parameter_files = [parameter_files]
# Copy file from control path to work path
file_path = os.path.join(self.control_path, filename)
shutil.copy(file_path, self.work_path)

def get_parameter_files(self):
"""Return a list of parameter config files defined in input.nml"""
input_nml = f90nml.read(os.path.join(self.work_path, 'input.nml'))

input_namelists = ['MOM_input_nml']
if 'SIS_input_nml' in input_nml:
input_namelists.append('SIS_input_nml')

parameter_files = []
for input in input_namelists:
input_namelist = input_nml.get(input, {})
filenames = input_namelist.get('parameter_filename', [])

for filename in parameter_files:
if filename not in config_files:
print(f"payu: warning: parameter file {filename} "
f"listed under {input} in input.nml is not in "
"mom6's configuration files")
if filenames == []:
print("payu: warning: MOM6: There are no parameter files "
f"listed under {input} in input.nml")

# Extend config files
self.config_files.append(filename)
if isinstance(filenames, str):
parameter_files.append(filenames)
else:
parameter_files.extend(filenames)

# Copy parameter file from control path to work path
file_path = os.path.join(self.control_path, filename)
shutil.copy(file_path, self.work_path)
return parameter_files
84 changes: 37 additions & 47 deletions test/models/test_mom6.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,58 +72,48 @@ def teardown():
print(e)


def make_config_files(parameter_files):
"""Make config files in control directory"""
for file in parameter_files:
@pytest.mark.parametrize(
"input_nml, expected_files_added",
[
(
{
"MOM_input_nml": {
"parameter_filename": "MOM_Input"
}
},
["MOM_Input"]
),
(
{
"SIS_input_nml": {
"parameter_filename": "SIS_Input"
}
},
["SIS_Input"]
),
(
{
"MOM_input_nml": {
"parameter_filename": ["MOM_Input", "MOM_override"]
},
"SIS_input_nml": {
"output_directory": '.'
}
},
["MOM_Input", "MOM_override"]
)
])
def test_add_config_files(input_nml,
expected_files_added):
# Create config files in control directory
for file in expected_files_added:
filename = os.path.join(ctrldir, file)
make_random_file(filename, 8)


def make_input_nml_file(mom_parameter_files, sis_parameter_files=None):
"""Create an input.nml in expt work directory"""
input_nml = {
'MOM_input_nml': {
'parameter_filename': mom_parameter_files,
}
}
if sis_parameter_files:
input_nml['SIS_input_nml'] = {
'parameter_filename': sis_parameter_files,
}
# Create config.nml
input_nml_fp = os.path.join(expt_workdir, 'input.nml')
f90nml.write(input_nml, input_nml_fp)


@pytest.mark.parametrize(
"mom_parameter_files, sis_parameter_files, expected_files_added",
[
(
['MOM_input'],
['SIS_input', 'SIS_layout'],
[]
),
(
['MOM_input', 'MOM_layout'],
['SIS_input', 'New_SIS_file'],
['New_SIS_file']
),
(
['New_MOM_file', 'MOM_input'],
None,
['New_MOM_file']
)
])
def test_add_parameter_config_files(mom_parameter_files,
sis_parameter_files,
expected_files_added):
# Create config files in control directory
make_config_files(mom_parameter_files)
if sis_parameter_files:
make_config_files(sis_parameter_files)

# Create config.nml
make_input_nml_file(mom_parameter_files, sis_parameter_files)

with cd(ctrldir):
lab = payu.laboratory.Laboratory(lab_path=str(labdir))
expt = payu.experiment.Experiment(lab, reproduce=False)
Expand All @@ -132,7 +122,7 @@ def test_add_parameter_config_files(mom_parameter_files,
prior_config_files = model.config_files[:]

# Function to test
model.add_parameter_config_files()
model.add_config_files()

# Check files are added to config_files
added_files = set(model.config_files).difference(prior_config_files)
Expand Down

0 comments on commit a054ebb

Please sign in to comment.