-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_fhist_custom_grid.py
More file actions
executable file
·288 lines (213 loc) · 10.6 KB
/
test_fhist_custom_grid.py
File metadata and controls
executable file
·288 lines (213 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python3
import pytest
import shutil
import os
from pathlib import Path
import tempfile
import time
from ProConPy.config_var import ConfigVar, cvars
from ProConPy.stage import Stage
from ProConPy.csp_solver import csp
from visualCaseGen.cime_interface import CIME_interface
from visualCaseGen.initialize_configvars import initialize_configvars
from visualCaseGen.initialize_widgets import initialize_widgets
from visualCaseGen.initialize_stages import initialize_stages
from visualCaseGen.specs.options import set_options
from visualCaseGen.specs.relational_constraints import get_relational_constraints
from visualCaseGen.custom_widget_types.clm_modifier_launcher import MeshMaskModifierLauncher, FsurdatModifierLauncher
from visualCaseGen.custom_widget_types.case_creator_widget import CaseCreatorWidget
from tests.utils import safe_create_case
# do not show logger output
import logging
logger = logging.getLogger()
logger.setLevel(logging.CRITICAL)
base_temp_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "temp"))
def initialize_vcg():
"""This auxiliary function initializes the system and returns a CIME_interface object."""
ConfigVar.reboot()
Stage.reboot()
cime = CIME_interface()
initialize_configvars(cime)
initialize_widgets(cime)
initialize_stages(cime)
set_options(cime)
csp.initialize(cvars, get_relational_constraints(cvars), Stage.first())
return cime
def construct_standard_fhist_compset(cime):
"""This auxiliary function initializes the system constructs a standard FHIST compset."""
assert os.path.exists(base_temp_dir), "temp testing directory does not exist"
# At initialization, the first stage should be enabled
assert Stage.first().enabled
cvars["COMPSET_MODE"].value = "Standard"
# COMPSET_MODE is the only variable in the first stage, so assigning a value to it should disable the first stage
assert not Stage.first().enabled
# The next stage is Custom Component Set, whose first child is Model Time Period
assert Stage.active().title.startswith("Support Level")
cvars["SUPPORT_LEVEL"].value = "All"
# Apply filters
for comp_class in cime.comp_classes:
if comp_class == "LND":
cvars[f"COMP_{comp_class}_FILTER"].value = "clm"
elif comp_class == "OCN":
cvars[f"COMP_{comp_class}_FILTER"].value = "docn"
else:
cvars[f"COMP_{comp_class}_FILTER"].value = "any"
## Pick a standard compset
cvars["COMPSET_ALIAS"].value = "FHIST"
def construct_custom_fhist_compset():
"""This auxiliary function initializes the system constructs a custom FHIST compset."""
assert os.path.exists(base_temp_dir), "temp testing directory does not exist"
# At initialization, the first stage should be enabled
assert Stage.first().enabled
cvars["COMPSET_MODE"].value = "Custom"
assert Stage.active().title.startswith("Time Period")
cvars["INITTIME"].value = "HIST"
assert Stage.active().title.startswith("Components")
cvars["COMP_ATM"].value = "cam"
cvars["COMP_LND"].value = "clm"
cvars["COMP_ICE"].value = "cice"
cvars["COMP_OCN"].value = "docn"
cvars["COMP_ROF"].value = "mosart"
cvars["COMP_GLC"].value = "sglc"
cvars["COMP_WAV"].value = "swav"
assert Stage.active().title.startswith("Component Physics")
cvars["COMP_ATM_PHYS"].value = "CAM60"
cvars["COMP_LND_PHYS"].value = "CLM50"
assert Stage.active().title.startswith("Component Options")
cvars["COMP_ATM_OPTION"].value = "(none)"
cvars["COMP_LND_OPTION"].value = "SP"
cvars["COMP_ICE_OPTION"].value = "PRES"
cvars["COMP_OCN_OPTION"].value = "DOM"
cvars["COMP_ROF_OPTION"].value = "(none)"
def construct_custom_res_from_std_grids(cime):
"""This auxiliary function constructs a custom resolution from standard comp grids:
atm: f09, lnd: f09, ocn: f09 """
# Create a custom grid
assert Stage.active().title.startswith("2. Grid")
cvars["GRID_MODE"].value = "Custom"
# Set the custom grid path
assert Stage.active().title.startswith("Custom Grid")
with tempfile.TemporaryDirectory(dir=base_temp_dir) as temp_grid_path:
cvars["CUSTOM_GRID_PATH"].value = temp_grid_path
# Set CAM grid
cvars["CUSTOM_ATM_GRID"].value = "0.9x1.25"
# Since the ocean component is DOCN, we should directly skip to the land grid stage
assert Stage.active().title.startswith("Land Grid Mode")
cvars["LND_GRID_MODE"].value = "Standard"
assert Stage.active().title.startswith("Land Grid")
cvars["CUSTOM_LND_GRID"].value = "0.9x1.25"
assert Stage.active().title.startswith("Runoff Grid")
cvars["CUSTOM_ROF_GRID"].value = "r05"
assert Stage.active().title.startswith("3. Launch")
launch_stage = Stage.active()
with tempfile.TemporaryDirectory(dir=base_temp_dir) as temp_case_path:
pass # immediately remove the random temporary directory,
# which will become the caseroot directory below
cvars["CASEROOT"].value = temp_case_path
case_creator = launch_stage._widget._main_body.children[-1]
assert isinstance(case_creator, CaseCreatorWidget)
cvars["PROJECT"].value = "12345"
try:
# *Click* the create_case button
safe_create_case(cime.srcroot, case_creator)
# sleep for a bit to allow the case to be created
time.sleep(15)
except RuntimeError as e:
if "not ported" in str(e):
print("CESM is not ported to the current machine. Skipping case creation.")
else:
# If the error is not related to machine porting, raise it
raise e
# remove the caseroot directory
if os.path.exists(temp_case_path):
shutil.rmtree(temp_case_path)
def construct_custom_res_from_modified_clm_grid(cime):
"""This auxiliary function constructs a custom resolution from 4x5 com grids but
with modified lnd mask and fsurdat datasets"""
# Create a custom grid
assert Stage.active().title.startswith("2. Grid")
cvars["GRID_MODE"].value = "Custom"
# Set the custom grid path
assert Stage.active().title.startswith("Custom Grid")
with tempfile.TemporaryDirectory(dir=base_temp_dir) as temp_grid_path:
cvars["CUSTOM_GRID_PATH"].value = temp_grid_path
# Set CAM grid
cvars["CUSTOM_ATM_GRID"].value = "4x5"
# Since the ocean component is DOCN, we should directly skip to the land grid stage
assert Stage.active().title.startswith("Land Grid Mode")
cvars["LND_GRID_MODE"].value = "Modified"
assert Stage.active().title.startswith("Base Land Grid")
cvars["CUSTOM_LND_GRID"].value = "4x5"
assert Stage.active().title.startswith("Mesh Mask Modifier")
assert cvars["INPUT_MASK_MESH"].value is not None, "INPUT_MASK_MESH should be auto-filled"
cvars["LAND_MASK"].value = "/glade/work/altuntas/cesm.input/vcg/mask_fillIO_f45.nc"
cvars["LAT_VAR_NAME"].value = "lats"
cvars["LON_VAR_NAME"].value = "lons"
cvars["LAT_DIM_NAME"].value = "lsmlat"
cvars["LON_DIM_NAME"].value = "lsmlon"
mesh_mask_modifier_launcher = Stage.active()._widget._main_body.children[-1]
assert isinstance(mesh_mask_modifier_launcher, MeshMaskModifierLauncher)
# click the "Run Mesh Mask Modifier" button
mesh_mask_modifier_launcher._on_launch_clicked(b=None)
assert Stage.active().title.startswith("fsurdat")
assert cvars["INPUT_FSURDAT"].value is not None, "INPUT_FSURDAT should be auto-filled"
assert cvars["FSURDAT_AREA_SPEC"].value.startswith("mask_file:"), "FSURDAT_AREA_SPEC should be auto-filled"
cvars["FSURDAT_IDEALIZED"].value = "True"
cvars["LND_DOM_PFT"].value = 14
cvars["LND_SOIL_COLOR"].value = 10
cvars["LND_STD_ELEV"].value = 0.0
cvars["LND_MAX_SAT_AREA"].value = 0.0
cvars["LND_INCLUDE_NONVEG"].value = "True"
assert cvars["FSURDAT_MATRIX"]._widget.value is not None, "FSURDAT_MATRIX should be auto-filled"
fsurdat_modifier_launcher = Stage.active()._widget._main_body.children[-1]
assert isinstance(fsurdat_modifier_launcher, FsurdatModifierLauncher)
# click the "Run Surface Data Modifier" button
fsurdat_modifier_launcher._on_launch_clicked(b=None)
assert Stage.active().title.startswith("Runoff Grid")
cvars["CUSTOM_ROF_GRID"].value = "r05"
assert Stage.active().title.startswith("3. Launch")
launch_stage = Stage.active()
with tempfile.TemporaryDirectory(dir=base_temp_dir) as temp_case_path:
pass # immediately remove the random temporary directory,
# which will become the caseroot directory below
cvars["CASEROOT"].value = temp_case_path
case_creator = launch_stage._widget._main_body.children[-1]
assert isinstance(case_creator, CaseCreatorWidget)
cvars["PROJECT"].value = "12345"
# *Click* the create_case button
safe_create_case(cime.srcroot, case_creator)
# sleep for a bit to allow the case to be created
time.sleep(5)
# remove the caseroot directory
shutil.rmtree(temp_case_path)
def test_std_fhist_custom_res_standard_comp_grid():
"""Test the standard FHIST compset with a custom resolution consisting of
standard comp grids: f09"""
cime = initialize_vcg()
construct_standard_fhist_compset(cime)
construct_custom_res_from_std_grids(cime)
@pytest.mark.slow
def test_std_fhist_modified_clm_grid():
"""Test the standard FHIST compset with a custom resolution based on 4x5 but
including modified land mask and fsurdat datasets."""
cime = initialize_vcg()
machine = cime.machine
if machine not in ["derecho", "casper"]:
pytest.skip("This test is only for the derecho and casper machines")
construct_standard_fhist_compset(cime)
construct_custom_res_from_modified_clm_grid(cime)
@pytest.mark.slow
def test_custom_fhist_modified_clm_grid():
"""Test custom FHIST compset with a custom resolution including based on 4x5 but
including modified land mask and fsurdat datasets."""
cime = initialize_vcg()
machine = cime.machine
if machine not in ["derecho", "casper"]:
pytest.skip("This test is only for the derecho and casper machines")
construct_custom_fhist_compset()
construct_custom_res_from_modified_clm_grid(cime)
if __name__ == "__main__":
test_std_fhist_custom_res_standard_comp_grid()
test_std_fhist_modified_clm_grid()
test_custom_fhist_modified_clm_grid()
print("All tests passed!")