Skip to content

Commit

Permalink
Switch to ruamel.yaml for manipulating config.yaml
Browse files Browse the repository at this point in the history
This package provides a "round-trip" mode which preserves ordering and
comments.
  • Loading branch information
angus-g committed Nov 30, 2023
1 parent 19e7836 commit fca3849
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies = [
"scipy>=1.2.0",
"xarray",
"xesmf>=0.8",
"PyYAML>=6.0.1",
"ruamel.yaml>=0.18.0",
"f90nml>=1.4.1",
]

Expand Down
32 changes: 12 additions & 20 deletions regional_mom6/regional_mom6.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import shutil
import os
from .utils import vecdot
import yaml
from ruamel.yaml import YAML

warnings.filterwarnings("ignore")

Expand Down Expand Up @@ -1300,27 +1300,20 @@ def setup_run_directory(self, demo_run_dir=False, using_payu=False):
f.writelines(lines)

Check warning on line 1300 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1299-L1300

Added lines #L1299 - L1300 were not covered by tests

## If using payu to run the model, create a payu configuration file
if not using_payu and os.path.exists(f"{self.mom_run_dir}/config.yaml"):
os.remove(f"{self.mom_run_dir}/config.yaml")

config_yaml_file = self.mom_run_dir / "config.yaml"
if not using_payu:
config_yaml_file.unlink(missing_ok=True)

Check warning on line 1305 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1303-L1305

Added lines #L1303 - L1305 were not covered by tests
else:
with open(f"{self.mom_run_dir}/config.yaml", "r") as file:
lines = file.readlines()

inputfile = open(f"{self.mom_run_dir}/config.yaml", "r")
lines = inputfile.readlines()
inputfile.close()
for i in range(len(lines)):
if "ncpus" in lines[i]:
lines[i] = f"ncpus: {str(ncpus)}\n"
if "jobname" in lines[i]:
lines[i] = f"jobname: mom6_{self.mom_input_dir.name}\n"
yaml = YAML() # round-trip yaml parser
with open(config_yaml_file, "r") as f:
conf = yaml.load(f)

Check warning on line 1309 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1307-L1309

Added lines #L1307 - L1309 were not covered by tests

if "input:" in lines[i]:
lines[i + 1] = f" - {self.mom_input_dir}\n"
conf["ncpus"] = ncpus
conf["jobname"] = f"mom6_{self.mom_input_dir.name}"
conf["input"].append(self.mom_input_dir)

Check warning on line 1313 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1311-L1313

Added lines #L1311 - L1313 were not covered by tests

with open(f"{self.mom_run_dir}/config.yaml", "w") as file:
file.writelines(lines)
with open(config_yaml_file, "w") as f:
yaml.dump(conf, f)

Check warning on line 1316 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1315-L1316

Added lines #L1315 - L1316 were not covered by tests

# Modify input.nml
nml = f90nml.read(self.mom_run_dir / "input.nml")
Expand All @@ -1333,7 +1326,6 @@ def setup_run_directory(self, demo_run_dir=False, using_payu=False):
0,
]
nml.write(self.mom_run_dir / "input.nml", force=True)

Check warning on line 1328 in regional_mom6/regional_mom6.py

View check run for this annotation

Codecov / codecov/patch

regional_mom6/regional_mom6.py#L1328

Added line #L1328 was not covered by tests
return

def setup_era5(self, era5_path):
"""
Expand Down

0 comments on commit fca3849

Please sign in to comment.