-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_workflow_combi.py
230 lines (191 loc) · 7.86 KB
/
example_workflow_combi.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Run a multi dates workflow."""
import pathlib
import datetime
import click
import yaml
from aiida import orm, plugins, engine, cmdline, common
from aiida_flexpart.utils import reformat_locations
def read_yaml_data(data_filename: str, names=None) -> dict:
"""Read in a YAML data file as a dictionary"""
data_path = pathlib.Path(data_filename)
with data_path.open('r', encoding='utf-8') as fp:
yaml_data = yaml.safe_load(fp)
return {key: value
for key, value in yaml_data.items()
if key in names} if names else yaml_data
def make_locations_list(list_locations):
"""makes location list"""
list_locations_ = read_yaml_data('inputs/location_groups.yaml',
names=list_locations)
list_ = []
if list_locations_:
for _, j in list_locations_.items():
list_ += j
return sorted(set(list_locations + list_))
def simulation_dates_parser(date_list: list) -> list:
"""
Parse a range of dates and returns a list of date strings.
Examples:
2021-01-02--2021-01-10 -> [2021-01-02 00:00:00, 2021-01-02 00:00:00, ..., 2021-01-10 00:00:00]
2021-01-02, 2021-01-10 -> [2021-01-02 00:00:00, 2021-01-10 00:00:00]
2021-01-02 -> [2021-01-02 00:00:00,]
"""
dates = []
for date_string in date_list:
if ',' in date_string:
dates += [
date.strip() + ' 00:00:00' for date in date_string.split(',')
]
elif '--' in date_string:
date_start, date_end = list(
map(lambda date: datetime.datetime.strptime(date, '%Y-%m-%d'),
date_string.split('--')))
dates += [
date.strftime('%Y-%m-%d 00:00:00') for date in [
date_start + datetime.timedelta(days=x)
for x in range(0, (date_end - date_start).days + 1)
]
]
else:
dates += [date_string.strip() + ' 00:00:00']
return orm.List(list=dates)
def test_run(flexpart_code):
"""Run workflow."""
simulation_dates = simulation_dates_parser(['2020-10-01'])
model = ['cosmo7']
model_offline = []
username = 'lfernand'
outgrid_main = 'Europe'
outgrid_nest = 'Switzerland'
integration_time = 24
integration_time_offline = 0
users_address = f'/users/{username}/resources/flexpart/'
scratch_address = f'/scratch/snx3000/{username}/FLEXPART_input/'
#list of locations and/or groups of locations
list_locations = ['KIT_200magl']
# Links to the remote files/folders.
glc = orm.RemoteData(remote_path=users_address + 'GLC2000',
computer=flexpart_code.computer)
glc_ifs = orm.RemoteData(remote_path=users_address + 'IGBP_int1.dat',
computer=flexpart_code.computer)
species = orm.RemoteData(remote_path=users_address + 'SPECIES',
computer=flexpart_code.computer)
surfdata = orm.RemoteData(remote_path=users_address + 'surfdata.t',
computer=flexpart_code.computer)
surfdepo = orm.RemoteData(remote_path=users_address + 'surfdepo.t',
computer=flexpart_code.computer)
# parent_folder = orm.load_node(pk previous tsk)
# parent_folder = orm.RemoteData(
# remote_path = '/scratch/snx3000/lfernand/aiida/76/8d/cb2c-2fc6-46c4-b609-1d33fce0f60c',
# computer=flexpart_code.computer)
parent_folder = None
#builder starts
workflow = plugins.WorkflowFactory('flexpart.multi_dates')
builder = workflow.get_builder()
builder.fcosmo_code = flexpart_code
builder.fifs_code = orm.load_code('flexpart_ifs@daint')
builder.check_meteo_ifs_code = orm.load_code(
'check-ifs-data@daint-direct-106')
builder.check_meteo_cosmo_code = orm.load_code(
'check-cosmo-data@daint-direct-106')
builder.post_processing_code = orm.load_code('post-processing@daint')
#basic settings
builder.simulation_dates = simulation_dates
builder.integration_time = orm.Int(integration_time)
builder.offline_integration_time = orm.Int(integration_time_offline)
#meteo realted settings
builder.model = orm.List(model)
builder.model_offline = orm.List(model_offline)
meteo_path = orm.List([scratch_address + mod for mod in model])
builder.meteo_path = meteo_path
builder.meteo_inputs = orm.Dict(
dict=read_yaml_data('inputs/meteo_inputs.yaml', names=[
model[-1],
])[model[-1]])
if model_offline:
meteo_path_offline = orm.List(
[scratch_address + mod for mod in model_offline])
builder.meteo_path_offline = meteo_path_offline
builder.meteo_inputs_offline = orm.Dict(dict=read_yaml_data(
'inputs/meteo_inputs.yaml', names=[
model_offline[-1],
])[model_offline[-1]])
builder.gribdir = orm.Str(scratch_address)
#model settings
builder.command = orm.Dict(dict=read_yaml_data(
'inputs/command.yaml')) #simulation date will be overwritten
builder.input_phy = orm.Dict(dict=read_yaml_data('inputs/input_phy.yaml'))
dict_ = read_yaml_data('inputs/locations.yaml',
names=make_locations_list(list_locations))
reformated_dict_locations = reformat_locations(dict_, model[-1])
builder.locations = orm.Dict(dict=reformated_dict_locations)
builder.release_settings = orm.Dict(
dict=read_yaml_data('inputs/release.yaml'))
#other
builder.outgrid = orm.Dict(
dict=read_yaml_data('inputs/outgrid.yaml', names=[
outgrid_main,
])[outgrid_main])
builder.outgrid_nest = orm.Dict(
dict=read_yaml_data('inputs/outgrid.yaml', names=[
outgrid_nest,
])[outgrid_nest])
builder.species = species
builder.land_use = {
'glc': glc,
'surfdata': surfdata,
'surfdepo': surfdepo,
}
builder.land_use_ifs = {
'glc': glc_ifs,
'surfdata': surfdata,
'surfdepo': surfdepo,
}
builder.parent_calc_folder = parent_folder
builder.flexpartcosmo.metadata.options.stash = {
'source_list':
['aiida.out', 'header*', 'partposit_inst', 'grid_time_*.nc'],
'target_base': f'/store/empa/em05/{username}/aiida_stash',
'stash_mode': common.StashMode.COPY.value,
}
builder.flexpartifs.metadata.options.stash = {
'source_list':
['aiida.out', 'header*', 'partposit_inst*', 'grid_time_*.nc'],
'target_base':
f'/store/empa/em05/{username}/aiida_stash',
'stash_mode':
common.StashMode.COPY.value,
}
builder.flexpartpost.metadata.options.stash = {
'source_list':
['aiida.out', 'boundary_sensitivity_*.nc', 'grid_time_*.nc'],
'target_base': f'/store/empa/em05/{username}/aiida_stash',
'stash_mode': common.StashMode.COPY.value,
}
prepend_text_ = """#SBATCH --partition=low
#SBATCH --account=s1152
#SBATCH --constraint=mc
export OMP_NUM_THREADS=${SLURM_CPUS_PER_TASK:-1}
source $MODULESHOME/init/bash
ulimit -s unlimited
"""
#change wall time for cosmo and ifs in seconds
builder.flexpartcosmo.metadata.options.max_wallclock_seconds = 1800
#builder.flexpartifs.metadata.options.max_wallclock_seconds = 2700
builder.flexpartcosmo.metadata.options.prepend_text = prepend_text_
builder.flexpartifs.metadata.options.prepend_text = prepend_text_
engine.run(builder)
@click.command()
@cmdline.utils.decorators.with_dbenv()
@cmdline.params.options.CODE()
def cli(code):
"""Run example.
Example usage: $ ./example_01.py --code diff@localhost
Alternative (creates diff@localhost-test code): $ ./example_01.py
Help: $ ./example_01.py --help
"""
test_run(code)
if __name__ == '__main__':
cli() # pylint: disable=no-value-for-parameter