Skip to content

Commit

Permalink
Minor Bug Fixes/Improvements to create_empty, change_MOM_param, & set…
Browse files Browse the repository at this point in the history
…up_run_directory (#20)

* Package type changes for CRR

* Black Formatting

* Change MOM Param

* Change MOM_param to Work

* black

* Remvoe premade_run_dir_arg

* Remove init

* Black

* Update MOM_param_test

* Clean
  • Loading branch information
manishvenu authored Oct 14, 2024
1 parent cd987f5 commit 5b1c816
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
23 changes: 17 additions & 6 deletions regional_mom6/regional_mom6.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import glob
from collections import defaultdict
import json
import copy

warnings.filterwarnings("ignore")

Expand Down Expand Up @@ -685,6 +686,7 @@ def create_empty(
expt.longitude_extent = longitude_extent
expt.ocean_mask = None
expt.layout = None
self.segments = {}
return expt

def __init__(
Expand Down Expand Up @@ -2416,7 +2418,13 @@ def setup_run_directory(
MOM_override_dict[key_DATA]["value"] = (
MOM_override_dict[key_DATA]["value"] + '"'
)

if type(self.date_range[0]) == str:
self.date_range[0] = dt.datetime.strptime(
self.date_range[0], "%Y-%m-%d %H:%M:%S"
)
self.date_range[1] = dt.datetime.strptime(
self.date_range[1], "%Y-%m-%d %H:%M:%S"
)
# Tides OBC adjustments
if with_tides:

Expand Down Expand Up @@ -2520,7 +2528,7 @@ def change_MOM_parameter(
if param_name in MOM_override_dict.keys():
original_val = MOM_override_dict[param_name]["value"]
print(
"This parameter {} is begin replaced from {} to {} in MOM_override".format(
"This parameter {} is being replaced from {} to {} in MOM_override".format(
param_name, original_val, param_value
)
)
Expand Down Expand Up @@ -2558,9 +2566,9 @@ def read_MOM_file_as_dict(self, filename):
lines = file.readlines()

# Set the default initialization for a new key
MOM_file_dict = defaultdict(lambda: default_layout.copy())
MOM_file_dict = defaultdict(lambda: copy.deepcopy(default_layout))
MOM_file_dict["filename"] = filename
dlc = default_layout.copy()
dlc = copy.deepcopy(default_layout)
for jj in range(len(lines)):
if "=" in lines[jj] and not "===" in lines[jj]:
split = lines[jj].split("=", 1)
Expand All @@ -2579,10 +2587,10 @@ def read_MOM_file_as_dict(self, filename):
dlc["value"] = str(value.strip())
dlc["comment"] = None

MOM_file_dict[var.strip()] = dlc.copy()
MOM_file_dict[var.strip()] = copy.deepcopy(dlc)

# Save a copy of the original dictionary
MOM_file_dict["original"] = MOM_file_dict.copy()
MOM_file_dict["original"] = copy.deepcopy(MOM_file_dict)
return MOM_file_dict

def write_MOM_file(self, MOM_file_dict):
Expand All @@ -2596,6 +2604,9 @@ def write_MOM_file(self, MOM_file_dict):
for jj in range(len(lines)):
if "=" in lines[jj] and not "===" in lines[jj]:
var = lines[jj].split("=", 1)[0].strip()
if "#override" in var:
var = var.replace("#override", "")
var = var.strip()
if var in MOM_file_dict.keys() and (
str(MOM_file_dict[var]["value"])
) != str(original_MOM_file_dict[var]["value"]):
Expand Down
7 changes: 4 additions & 3 deletions tests/test_manish_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,11 @@ def test_change_MOM_parameter(self):
shutil.copytree(
base_run_dir / "common_files", self.expt.mom_run_dir, dirs_exist_ok=True
)
self.expt.change_MOM_parameter("OBC_SEGMENT_001", "adasd", "COOL COMMENT")
og = self.expt.change_MOM_parameter("MINIMUM_DEPTH", "adasd", "COOL COMMENT")
MOM_override_dict = self.expt.read_MOM_file_as_dict("MOM_override")
assert MOM_override_dict["OBC_SEGMENT_001"]["value"] == "adasd"
assert MOM_override_dict["OBC_SEGMENT_001"]["comment"] == "COOL COMMENT\n"
assert MOM_override_dict["MINIMUM_DEPTH"]["value"] == "adasd"
assert MOM_override_dict["original"]["OBC_SEGMENT_001"]["value"] == og
assert MOM_override_dict["MINIMUM_DEPTH"]["comment"] == "COOL COMMENT\n"

def test_properties_empty(self):
"""
Expand Down

0 comments on commit 5b1c816

Please sign in to comment.