Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to MOM6 input parser #6

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion om3utils/mom6_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def _patch_mom6_input_str(mom6_input_str: str) -> tuple[str, dict]:
block_pattern = re.compile(r"KPP%|%KPP|CVMix_CONVECTION%|%CVMix_CONVECTION|CVMIX_DDIFF%|%CVMIX_DDIFF")
override_directive_pattern = re.compile(r"^(#override\s*?)")
incorrect_directive_pattern = re.compile(r"^(#\s+)")
comment_directive_pattern = re.compile(r"^#(?:(?!override)\w+\b\s*=\s*\w+$)")
comment_directive_pattern = re.compile(r"^#((?!override)\w+\b\s*=\s*\w+$)")

# Modify the input while recording the changes
patch = {}
Expand Down Expand Up @@ -229,6 +229,9 @@ class Mom6Input(dict):
# A record of all the changes done to the dictionary that can be passed to f90nml to do round-trip parsing
_nml_patch = None

# A record of keys that have been deleted from the dictionary
_deleted_keys = []

def __init__(self, file_name: str = None):
"""Read NOM6 parameters from file.

Expand Down Expand Up @@ -262,6 +265,10 @@ def __setitem__(self, key, value):
namelist patch used for round-trip parsing.
"""
super().__setitem__(key.upper(), value)

if key.upper() in self._deleted_keys:
self._deleted_keys.remove(key.upper())

if self._nml_patch:
self._nml_patch["mom6"][key.upper()] = value

Expand All @@ -271,6 +278,7 @@ def __getitem__(self, key):

def __delitem__(self, key):
"""Override method to delete item from dict, so that all keys are stored in uppercase."""
self._deleted_keys.append(key.upper())
super().__delitem__(key.upper())

def write(self, file: Path):
Expand All @@ -286,6 +294,15 @@ def write(self, file: Path):
parser = f90nml.Parser()
parser.read(nml_file, self._nml_patch, tmp_file)
mom6_input_str = _unpatch_mom6_input_str(tmp_file.getvalue(), self._file_patch)

# Change keys to uppercase using a regex substitution, as there seems to be no way of doing this with f90nml
# when applying a nml patch.
mom6_input_str = re.sub(r"((?<=^)|(?<=\n))(\w+)", lambda pat: pat.group(2).upper(), mom6_input_str)

# Explicitly removed keys from string
for key in self._deleted_keys:
mom6_input_str = re.sub(r"\s*" + f"{key}" + r"\s*=\s*\S*\s*\n", r"\n", mom6_input_str)

file.write_text(mom6_input_str)

def _keys_to_upper(self):
Expand Down
7 changes: 5 additions & 2 deletions tests/test_mom6_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def complex_mom6_input_file(tmp_path):
DT = 1800.0 ! This is a comment
! This is another comment
!COMMENTED_VAR = 3
TO_BE_REMOVED = 10.0
BOOL = True
"""
return MockFile(file, mom6_input_str)
Expand All @@ -72,7 +73,7 @@ def modified_mom6_input_file(tmp_path):
BOOL = True


added_var = 32
ADDED_VAR = 32
"""
return MockFile(file, mom6_input_str)

Expand All @@ -94,8 +95,10 @@ def test_round_trip_mom6_input(tmp_path, complex_mom6_input_file, modified_mom6_
mom6_input_from_file = Mom6Input(file_name=complex_mom6_input_file.file)
mom6_input_from_file["dt"] = 900.0
mom6_input_from_file["ADDED_VAR"] = 1
del mom6_input_from_file["ADDED_VAR"]
mom6_input_from_file["ADDED_VAR"] = 32
del mom6_input_from_file["N_SMOOTH"]
mom6_input_from_file["N_SMOOTH"] = 4
del mom6_input_from_file["TO_BE_REMOVED"]

write_mom6_input(mom6_input_from_file, tmp_path / "MOM_input_new")

Expand Down
2 changes: 1 addition & 1 deletion tests/test_payu_config_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


@pytest.fixture()
def simple_payu_config(tmp_path):
def simple_payu_config():
return {
"project": "x77",
"ncpus": 48,
Expand Down
Loading