Skip to content

Commit

Permalink
add(icon): dump core namelists to files
Browse files Browse the repository at this point in the history
  • Loading branch information
leclairm committed Dec 19, 2024
1 parent 66f5f5d commit 00107ee
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/sirocco/core/_tasks/icon_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
class IconTask(ConfigIconTaskSpecs, Task):
core_namelists: dict[str, f90nml.Namelist] = field(default_factory=dict)

def init_namelists(self):
def init_core_namelists(self):
"""Read in or create namelists"""
self.core_namelists = {}
for name, cfg_nml in self.namelists.items():
if (nml_path := self.config_root / cfg_nml.path).exists():
self.core_namelists[name] = f90nml.read(nml_path)
else:
# If namelist does not exist, build it from the users given specs
self.core_namelists[name] = f90nml.Namelist()

def update_nml_from_config(self):
def update_core_namelists_from_config(self):
"""Update namelists from user input"""

# TODO: implement format for users to reference parameters and date in their specs
Expand All @@ -31,9 +32,9 @@ def update_nml_from_config(self):
for section, params in cfg_nml.specs.items():
section_name, k = self.section_index(section)
# Create section if non existant
# NOTE: f90nml will automatially create the corresponding nested f90nml.Namelist
# objects, no need to explicitly use the f90nml.Namelist class constructor
if section_name not in core_nml:
# NOTE: f90nml will automatially create the corresponding nested f90nml.Namelist
# objects, no need to explicitly use the f90nml.Namelist class constructor
core_nml[section_name] = {} if k is None else [{}]
# Update namelist with user input
# NOTE: unlike FORTRAN convention, user index starts at 0 as in Python
Expand All @@ -42,14 +43,30 @@ def update_nml_from_config(self):
nml_section = core_nml[section_name] if k is None else core_nml[section_name][k]
nml_section.update(params)

def update_nml_from_workflow(self):
def update_core_namelists_from_workflow(self):
self.core_namelists["icon_master.namelist"]["master_time_control_nml"].update(
{"experimentStartDate": self.start_date, "experimentStopDate": self.end_date}
{
"experimentStartDate": self.start_date.isoformat() + "Z",
"experimentStopDate": self.end_date.isoformat() + "Z",
}
)
self.core_namelists["icon_master.namelist"]["master_nml"]["lrestart"] = any(
in_data.type == "icon_restart" for in_data in self.inputs
)

def dump_core_namelists(self):
for name, cfg_nml in self.namelists.items():
nml_path = self.config_root / cfg_nml.path
suffix = ("_".join([str(p) for p in self.coordinates.values()])).replace(" ", "_")
dump_path = nml_path.parent / (nml_path.name + "_" + suffix)
self.core_namelists[name].write(dump_path)

def create_workflow_namelists(self):
self.init_core_namelists()
self.update_core_namelists_from_config()
self.update_core_namelists_from_workflow()
self.dump_core_namelists()

@staticmethod
def section_index(section_name):
multi_section_pattern = re.compile(r"(.*)\[([0-9]+)\]$")
Expand Down

0 comments on commit 00107ee

Please sign in to comment.