-
Notifications
You must be signed in to change notification settings - Fork 1
/
forecast.py
354 lines (245 loc) · 11.1 KB
/
forecast.py
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# pylint: disable=invalid-name,no-member,too-many-arguments
from argparse import Namespace
import os
import shlex
import shutil
import subprocess
import jinja2 as j2
import yaml
import f90nml
import errors
import utils
class BatchJob():
def __init__(self, config, machine, starttime, **kwargs):
self.config = self.config_namespace(config)
self.machine = self.config_namespace(machine)
self.starttime = starttime
overwrite = kwargs.get('overwrite', False)
self.workdir = self.create_workdir(overwrite)
@staticmethod
def config_namespace(config):
ns = Namespace()
utils.namespace(ns, config)
return ns
def create_workdir(self, overwrite=True):
cycle = self.starttime.strftime('%Y%m%d%H')
workdir = self.config.paths.workdir.format(
n=self.config,
cycle=cycle
)
if os.path.exists(workdir):
if overwrite:
shutil.rmtree(workdir)
else:
msg = f"create_workdir: {workdir} exists & will not be removed!"
raise errors.DirectoryExists(msg)
os.makedirs(workdir)
return workdir
def stage_files(self, action, links):
if not links:
return
if action not in ['copy', 'link']:
msg = 'link_static_files: action = {action} is not copy or link.'
raise ValueError(msg)
if not isinstance(links, dict):
msg = f'link_static_files: links is type {type(links)}, expected dict.'
raise ValueError(msg)
n = self.config
starttime = self.starttime.strftime('%Y%m%d%H')
files = []
for path_name, filelist in links.items():
path_dir = vars(n.paths).get(
path_name,
vars(self.machine.dirs).get(path_name),
)
if not path_dir:
msg = f'stage_files: cannot find a path entry for {path_name}'
raise ValueError(msg)
for src_dst in filelist:
# First item of list will be the name of the source. Join with
# the path specified by the section header.
filepath = os.path.join(path_dir, src_dst[0])
# Last item of list will be the name of the destination
dest_name = src_dst[-1]
destination = os.path.join(self.workdir, dest_name)
# Add the processed src_dst to the filelist
files.append((filepath, destination))
for src, dst in files:
if action == 'link':
print(f'Linking {src.format(n=n, starttime=starttime)} to {dst}')
utils.safe_link(src.format(n=n, starttime=starttime), dst)
if action == 'copy':
print(f'Copying {src.format(n=n, starttime=starttime)} to {dst}')
utils.safe_copy(src.format(n=n, starttime=starttime), dst)
@staticmethod
def create_yml(outfile, settings):
with open(outfile, 'w') as fn:
yaml.dump(settings, fn)
@staticmethod
def render_template(outfile, template, tmpl_vars):
with open(template, 'r') as tmpl_file:
template = j2.Template(tmpl_file.read())
xml_contents = template.render(**tmpl_vars)
with open(outfile, 'w') as out_file:
out_file.write(xml_contents)
@property
def executable(self):
pass
def parallel_run(self, exe):
run_cmd = self.machine.run_command.format(n=self.config)
cmd = shlex.split(f'{run_cmd} {exe}')
pc = subprocess.run(cmd, check=True, stdout=subprocess.PIPE)
while True:
output = pc.stdout.readline()
if not output and pc.poll():
break
if output:
print(output.strip())
rc = pc.poll()
return rc
class Forecast(BatchJob):
def __init__(self, config, machine, starttime, **kwargs):
BatchJob.__init__(self, config, machine, starttime, **kwargs)
# Set of configuration Namespace objects.
grid = self.config_namespace(kwargs.get('grid'))
self.grid = grid
self.nml = {}
for sect, keys in kwargs.get('nml', {}).items():
self.nml[sect] = {}
for key, value in keys.items():
if isinstance(value, str):
try:
tmp = value.format(grid=self.grid, n=self.config)
except AttributeError:
tmp = eval("f'{}'".format(value))
self.nml[sect][key] = utils.to_number(tmp)
elif isinstance(value, list):
tmp = [v.format(grid=self.grid, n=self.config) for v in value]
self.nml[sect][key] = [utils.to_number(v) for v in tmp]
else:
self.nml[sect][key] = value
def run(self, dry_run=False):
# Create INPUT dir
os.makedirs(os.path.join(self.workdir, 'INPUT'))
os.makedirs(os.path.join(self.workdir, 'RESTART'))
# Link/copy in static and cycle dependent files
for section in ['static', 'cycledep']:
self.stage_all(section)
# Create diag_table
self.create_diag_table()
# Create model_config
self.create_model_config()
# Create input.nml
self.create_nml()
# Run the forecast
if not dry_run:
self.parallel_run(self.config.static.copy.fv3_exec[0])
def create_diag_table(self):
outfile = os.path.join(self.workdir, 'diag_table')
template = self.config.paths.diag_tmpl.format(n=self.config)
template_vars = {
'res': self.grid.res,
'starttime': self.starttime,
}
self.render_template(outfile, template, template_vars)
def create_model_config(self):
# Output file
model_config_out = os.path.join(self.workdir, 'model_configure')
# Aliasing object variables for consistency with YAML
# pylint: disable=possibly-unused-variable
config = self.config
grid = self.grid
machine = self.machine
# pylint: enable=possibly-unused-variable
# Start with config items set in fv3_script list
model_config_items = config.model_config
# Loop through each item and add it to the model_config dict
model_config = {}
for item in model_config_items:
# Each item should be a key, value pair, or a single string item.
if isinstance(item, dict):
# For key, value pairs, set the model_config dict with the key,
# and use the value as a reference to a configuration set by one
# of the object variables: config, grid, machine, etc. If this
# the value does not reference a local variable, then set it to
# the value provided in the config.
for key, value in item.items():
var_val = locals().get(value)
value = var_val.__dict__.get(key, value) if var_val else value
value = f'.{str(value).lower()}.' if isinstance(value, bool) else value
model_config[key] = value
else: # item is a single item string
try:
# Call the method named by the item
model_config.update(self.__getattribute__(item)())
except KeyError:
msg = f'{__name__}: {item} is not an available method!'
print(msg)
self.create_yml(model_config_out, model_config)
def _pe_member01(self):
mpi_tasks = self.grid.layout_x * self.grid.layout_y
if self.config.quilting:
mpi_tasks += self.grid.quilting.write_groups * self.grid.quilting.write_tasks_per_group
# The number of processors will be used when running non-slurm
# subprocess. Make note of it in the config.
self.config.__dict__['nproc'] = mpi_tasks
return {'PE_MEMBER01': mpi_tasks}
def _quilting(self):
quilting = self.config.quilting
quilting = f'.{str(quilting).lower()}.' if isinstance(quilting, bool) else quilting
ret = {'quilting': quilting}
# Add the grid section to the returned dict.
if self.config.quilting:
ret.update(vars(self.grid.quilting))
return ret
def _start_times(self):
times = ['year', 'month', 'day', 'hour', 'minute', 'second']
return {f'start_{t}': int(self.starttime.__getattribute__(t)) for t in times}
def create_nml(self):
fv3_nml = os.path.join(self.workdir, 'input.nml')
# Read in the namelist that has all the base settings.
with open(self.config.paths.base_nml.format(n=self.config), 'r') as nml_file:
base_nml = f90nml.read(nml_file)
# Update the base namelist with settings for the current configuration
# Send self.nml, a Namespace object, as dict to update_dict.
# Update_dict modifies dict in place.
utils.update_dict(base_nml, self.nml)
with open(fv3_nml, 'w') as fn:
base_nml.write(fn)
def namsfc_files(self):
'''
A "callable" used by stage_all. Must return a list of lists.
Returns the list of namsfc files from the namelist section.
'''
filedict = vars(self.config.namelist.namsfc)
return [[fn] for key, fn in filedict.items() if key[:2] == 'fn' and len(fn.split('.')) > 1]
def stage_all(self, section):
allowed_sections = ['static', 'cycledep']
if section not in allowed_sections:
msg = f'stage_all: {section} is not in {allowed_sections}.'
raise ValueError(msg)
file_section = vars(self.config).get(section)
n = self.config
g = self.grid
all_files = {
'copy': {},
'link': {},
}
for action in all_files.keys():
files_to_stage = vars(file_section).get(action)
if files_to_stage:
for path_name, files in vars(files_to_stage).items():
all_files[action][path_name] = []
for list_item in files:
# files_to_stage items can either be a list or a callable
if isinstance(list_item, list):
all_files[action][path_name].append([tmpl.format(n=n, g=g) for tmpl in list_item])
elif callable(self.__getattribute__(list_item)):
# Note: any callable function supported by this functionality must return a list of lists, as
# expected by the all_files dict.
all_files[action][path_name].extend(self.__getattribute__(list_item)())
else:
msg = f'stage_all: {list_item} in {path_name} is not a list or callable!'
raise errors.InvalidConfigSetting(msg)
for action in ['copy', 'link']:
self.stage_files(action, all_files[action])