Skip to content

Commit 82d448f

Browse files
committed
First Try
1 parent 70401f4 commit 82d448f

File tree

1 file changed

+12
-204
lines changed

1 file changed

+12
-204
lines changed

visualCaseGen/custom_widget_types/case_creator.py

Lines changed: 12 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,7 @@ def revert_launch(self, do_exec=True):
4545
"""This function is called when the case creation fails. It reverts the changes made
4646
to the ccs_config xml files."""
4747

48-
mg = "ccs_config/modelgrid_aliases_nuopc.xml"
49-
if (Path(self._cime.srcroot) / f"{mg}.orig").exists():
50-
shutil.move(
51-
Path(self._cime.srcroot) / f"{mg}.orig",
52-
Path(self._cime.srcroot) / f"{mg}"
53-
)
54-
cg = "ccs_config/component_grids_nuopc.xml"
55-
if (Path(self._cime.srcroot) / f"{cg}.orig").exists():
56-
shutil.move(
57-
Path(self._cime.srcroot) / f"{cg}.orig",
58-
Path(self._cime.srcroot) / f"{cg}"
59-
)
60-
61-
def _remove_orig_xml_files(self):
62-
"""This function is called when the case creation and modification process is successful.
63-
It removes the backup xml files created before modifying the ccs_config xml files."""
64-
65-
mg = "ccs_config/modelgrid_aliases_nuopc.xml"
66-
if (Path(self._cime.srcroot) / f"{mg}.orig").exists():
67-
os.remove(Path(self._cime.srcroot) / f"{mg}.orig")
68-
cg = "ccs_config/component_grids_nuopc.xml"
69-
if (Path(self._cime.srcroot) / f"{cg}.orig").exists():
70-
os.remove(Path(self._cime.srcroot) / f"{cg}.orig")
48+
return # Currently no action is needed
7149

7250
def _is_non_local(self):
7351
"""Check if the case is being created on a machine different from the one
@@ -138,12 +116,12 @@ def create_case(self, do_exec):
138116
with self._out:
139117
print(f"{COMMENT}Creating case...{RESET}\n")
140118

141-
# First, update ccs_config xml files to add custom grid information if needed:
142-
self._update_ccs_config(do_exec)
143-
144119
# Run create_newcase
145120
self._run_create_newcase(caseroot, compset, resolution, do_exec)
146121

122+
# Add custom grid information to the case if needed:
123+
self._update_grids(do_exec)
124+
147125
# Navigate to the case directory:
148126
with self._out:
149127
print(f"{COMMENT}Navigating to the case directory:{RESET}\n")
@@ -171,11 +149,10 @@ def create_case(self, do_exec):
171149
f"another case, restart the notebook.{RESET}\n"
172150
)
173151

174-
def _update_ccs_config(self, do_exec):
152+
def _update_grids(self, do_exec):
175153
"""Update the modelgrid_aliases and component_grids xml files with custom grid
176-
information if needed. This function is called before running create_newcase."""
154+
information if needed. This function is called after running create_newcase."""
177155

178-
# If Custom grid is selected, update modelgrid_aliases and component_grids xml files:
179156
if cvars["GRID_MODE"].value == "Standard":
180157
return
181158
else:
@@ -201,109 +178,8 @@ def _update_ccs_config(self, do_exec):
201178
if ocn_grid is None:
202179
raise RuntimeError("No ocean grid specified.")
203180

204-
self._update_modelgrid_aliases(custom_grid_path, ocn_grid, do_exec)
205181
self._update_component_grids(custom_grid_path, ocn_grid, ocn_grid_mode, do_exec)
206182

207-
def _update_modelgrid_aliases(self, custom_grid_path, ocn_grid, do_exec):
208-
"""Update the modelgrid_aliases xml file with custom resolution information.
209-
This function is called before running create_newcase.
210-
211-
Parameters
212-
----------
213-
custom_grid_path : Path
214-
The path to the custom grid directory.
215-
ocn_grid : str
216-
The name of the custom ocean grid.
217-
do_exec : bool
218-
If True, execute the commands. If False, only print them.
219-
"""
220-
221-
resolution_name = custom_grid_path.name
222-
223-
# Component grid names:
224-
atm_grid = cvars["CUSTOM_ATM_GRID"].value
225-
lnd_grid = cvars["CUSTOM_LND_GRID"].value
226-
# modelgrid_aliases xml file that stores resolutions:
227-
srcroot = self._cime.srcroot
228-
ccs_config_root = Path(srcroot) / "ccs_config"
229-
assert (
230-
ccs_config_root.exists()
231-
), f"ccs_config_root {ccs_config_root} does not exist."
232-
modelgrid_aliases_xml = ccs_config_root / "modelgrid_aliases_nuopc.xml"
233-
assert (
234-
modelgrid_aliases_xml.exists()
235-
), f"modelgrid_aliases_xml {modelgrid_aliases_xml} does not exist."
236-
modelgrid_aliases_xml = modelgrid_aliases_xml.as_posix()
237-
238-
# confirm that modelgrid_aliases xml file is writeable:
239-
if not os.access(modelgrid_aliases_xml, os.W_OK):
240-
raise RuntimeError(f"Cannot write to {modelgrid_aliases_xml}.")
241-
242-
# log the modification of modelgrid_aliases.xml:
243-
with self._out:
244-
print(
245-
f'{BPOINT} Updating ccs_config/modelgrid_aliases_nuopc.xml file to include the new '
246-
f'resolution "{resolution_name}" consisting of the following component grids.\n'
247-
f' atm grid: "{atm_grid}", lnd grid: "{lnd_grid}", ocn grid: "{ocn_grid}".\n'
248-
)
249-
250-
# Read in xml file and generate grids object file:
251-
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))
252-
grids_tree = ET.parse(modelgrid_aliases_xml, parser=parser)
253-
grids_root = grids_tree.getroot()
254-
255-
# Check if a resp;iton with the same name already exists. If so, remove it or raise an error
256-
# depending on the value of self._allow_xml_override:
257-
for resolution in grids_root.findall("model_grid"):
258-
if resolution.attrib["alias"] == resolution_name:
259-
if self._allow_xml_override:
260-
grids_root.remove(resolution)
261-
else:
262-
raise RuntimeError(
263-
f"Resolution {resolution_name} already exists in modelgrid_aliases."
264-
)
265-
266-
# Create new resolution entry in xml file:
267-
new_resolution = SubElement(
268-
grids_root,
269-
"model_grid",
270-
attrib={"alias": resolution_name},
271-
)
272-
273-
# Add component grids to resolution entry:
274-
new_atm_grid = SubElement(
275-
new_resolution,
276-
"grid",
277-
attrib={"name": "atm"},
278-
)
279-
new_atm_grid.text = atm_grid
280-
281-
new_lnd_grid = SubElement(
282-
new_resolution,
283-
"grid",
284-
attrib={"name": "lnd"},
285-
)
286-
new_lnd_grid.text = lnd_grid
287-
288-
new_ocnice_grid = SubElement(
289-
new_resolution,
290-
"grid",
291-
attrib={"name": "ocnice"},
292-
)
293-
new_ocnice_grid.text = ocn_grid
294-
295-
if not do_exec:
296-
return
297-
298-
# back up modelgrid_aliases.xml in case case creation fails:
299-
shutil.copy(
300-
Path(self._cime.srcroot) / "ccs_config/modelgrid_aliases_nuopc.xml",
301-
Path(self._cime.srcroot) / "ccs_config/modelgrid_aliases_nuopc.xml.orig",
302-
)
303-
304-
# update modelgrid_aliases.xml to include new resolution:
305-
ET.indent(grids_tree)
306-
grids_tree.write(modelgrid_aliases_xml, encoding="utf-8", xml_declaration=True)
307183

308184
def _update_component_grids(
309185
self, custom_grid_path, ocn_grid, ocn_grid_mode, do_exec
@@ -332,92 +208,24 @@ def _update_component_grids(
332208
)
333209
assert ocn_mesh.exists(), f"Ocean mesh file {ocn_mesh} does not exist."
334210

335-
# component_grids xml file that stores resolutions:
336-
srcroot = self._cime.srcroot
337-
ccs_config_root = Path(srcroot) / "ccs_config"
338-
assert (
339-
ccs_config_root.exists()
340-
), f"ccs_config_root {ccs_config_root} does not exist."
341-
component_grids_xml = ccs_config_root / "component_grids_nuopc.xml"
342-
assert (
343-
component_grids_xml.exists()
344-
), f"component_grids_xml {component_grids_xml} does not exist."
345-
component_grids_xml = component_grids_xml.as_posix()
346-
347-
# confirm that component_grids xml file is writeable:
348-
if not os.access(component_grids_xml, os.W_OK):
349-
raise RuntimeError(f"Cannot write to {component_grids_xml}.")
350211

351212
# log the modification of component_grids.xml:
352213
with self._out:
353214
print(
354-
f'{BPOINT} Updating ccs_config/component_grids_nuopc.xml file to include '
215+
f'{BPOINT} Updating case xml variables to include '
355216
f'newly generated ocean grid "{ocn_grid}" with the following properties:\n'
356217
f' nx: {cvars["OCN_NX"].value}, ny: {cvars["OCN_NY"].value}.'
357218
f' ocean mesh: {ocn_mesh}.{RESET}\n'
358219
)
359220

360-
# Read in xml file and generate component_grids object file:
361-
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))
362-
domains_tree = ET.parse(component_grids_xml, parser=parser)
363-
# ET.indent(domains_tree, space=" ", level=0)
364-
domains_root = domains_tree.getroot()
365-
366-
# Check if a domain with the same name already exists. If so, remove it or raise an error
367-
# depending on the value of self._allow_xml_override:
368-
for domain in domains_root.findall("domain"):
369-
if domain.attrib["name"] == ocn_grid:
370-
if self._allow_xml_override:
371-
domains_root.remove(domain)
372-
else:
373-
raise RuntimeError(
374-
f"Ocean grid {ocn_grid} already exists in component_grids."
375-
)
376-
377-
# Create new domain entry in xml file:
378-
new_domain = SubElement(
379-
domains_root,
380-
"domain",
381-
attrib={"name": ocn_grid},
382-
)
383-
384-
nx = SubElement(
385-
new_domain,
386-
"nx",
387-
)
388-
nx.text = str(cvars["OCN_NX"].value)
221+
xmlchange("OCN_NX", cvars["OCN_NX"].value, do_exec, self._is_non_local(), self._out)
389222

390-
ny = SubElement(
391-
new_domain,
392-
"ny",
393-
)
394-
ny.text = str(cvars["OCN_NY"].value)
223+
xmlchange("OCN_NY", cvars["OCN_NY"].value, do_exec, self._is_non_local(), self._out)
395224

396-
mesh = SubElement(
397-
new_domain,
398-
"mesh",
399-
)
400-
mesh.text = ocn_mesh.as_posix()
225+
xmlchange("OCN_DOMAIN_MESH", ocn_mesh.as_posix(), do_exec, self._is_non_local(), self._out)
401226

402-
desc = SubElement(
403-
new_domain,
404-
"desc",
405-
)
406-
desc.text = f"New ocean grid {ocn_grid} generated by mom6_bathy"
407-
408-
if not do_exec:
409-
return
410-
411-
shutil.copy(
412-
Path(self._cime.srcroot) / "ccs_config/component_grids_nuopc.xml",
413-
Path(self._cime.srcroot) / "ccs_config/component_grids_nuopc.xml.orig",
414-
)
415-
416-
# write to xml file:
417-
ET.indent(domains_tree)
418-
domains_tree.write(
419-
component_grids_xml, encoding="utf-8", xml_declaration=True
420-
)
227+
xmlchange("MASK_MESH", ocn_mesh.as_posix(), do_exec, self._is_non_local(), self._out)
228+
421229

422230
def _run_create_newcase(self, caseroot, compset, resolution, do_exec):
423231
"""Run CIME's create_newcase tool to create a new case instance.

0 commit comments

Comments
 (0)