forked from IPCC-WG1/Chapter-11
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwarming_levels.py
338 lines (254 loc) · 8.43 KB
/
warming_levels.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
# compute year of warming level
import os
from datetime import datetime
import conf
from utils import computation
FOLDER_WARMING_LEVELS = "../../cmip_warming_levels/warming_levels/"
def warming_level_to_str(warming_level):
return str(warming_level).replace(".", "")
def _fmt_str_yml(beg, end, warming_level, add_grid_info, **meta):
fmt = " {{model: {model}, ensemble: {ens}, exp: {exp}"
if add_grid_info:
fmt += ", grid: {grid}"
if beg:
fmt = "-" + fmt + ", start_year: {beg}, end_year: {end}}}\n"
out = fmt.format(beg=beg, end=end, **meta)
else:
fmt = "#" + fmt + "}} -- did not reach {warming_level}°C\n"
out = fmt.format(warming_level=warming_level, **meta)
return out
def _fmt_str_csv(beg, end, warming_level, add_grid_info, **meta):
fmt = "{model}, {ens}, {exp}"
if add_grid_info:
fmt += ", {grid}"
if beg:
fmt = fmt + ", {warming_level}, {beg}, {end}\n"
out = fmt.format(beg=beg, end=end, warming_level=warming_level, **meta)
else:
out = ""
return out
def warming_level_years(
tas_list,
warming_level,
start_clim,
end_clim,
print_warming_level=True,
add_grid_info=False,
check_years=True,
):
out_yml = f"# warming level: {warming_level}°C above {start_clim}-{end_clim}\n"
how = "absolute"
if not check_years:
how = "no_check_absolute"
out_csv = ""
# creates yml dict
if print_warming_level:
out_yml += f"warming_level_{warming_level_to_str(warming_level)}:\n"
for ds, meta in tas_list:
anomaly = computation.calc_anomaly(
ds.tas, start=start_clim, end=end_clim, meta=meta, how=how
)
# skip if too short
if not len(anomaly):
continue
beg, end, mid = computation.calc_year_of_warming_level(anomaly, warming_level)
out_yml += _fmt_str_yml(beg, end, warming_level, add_grid_info, **meta)
out_csv += _fmt_str_csv(beg, end, warming_level, add_grid_info, **meta)
out_yml += "\n"
return out_yml, out_csv
def write_info(fid, start_clim, end_clim, check_years):
fid.write("# author: Mathias Hauser (mathias.hauser@env.ethz.ch)\n")
fid.write("# script: warming_levels.py\n")
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
fid.write(f"# executed: {now}\n")
fid.write(f"# reference years: {start_clim} to {end_clim}\n")
if not check_years:
fid.write(f"# NOTE: may include simulations starting after {start_clim}!\n")
def _get_filename(
cmip_version,
start_clim,
end_clim,
all_ens,
check_years,
add_grid_info,
file_ending,
subfolder="",
what="warming_levels",
):
one_or_all = "one_ens"
folder_one_or_all = ""
check_years_ = ""
if all_ens:
one_or_all = "all_ens"
folder_one_or_all = "_all_ens"
if not check_years:
check_years_ = "_no_bounds_check"
folder = os.path.join(FOLDER_WARMING_LEVELS, cmip_version)
grid = "_grid" if add_grid_info else ""
fN = os.path.join(
folder + folder_one_or_all,
subfolder,
f"{cmip_version}_{what}_{one_or_all}_{start_clim}_{end_clim}{check_years_}{grid}.{file_ending}",
)
return fN
def write_start_years_no_boundscheck(
conf_cmip, tas_list, start_clim, end_clim, all_ens
):
fN = _get_filename(
conf_cmip.cmip_version,
start_clim,
end_clim,
all_ens,
False,
add_grid_info=False,
file_ending="txt",
subfolder="",
what="start_years",
)
txt = ""
txt += "# author: Mathias Hauser (mathias.hauser@env.ethz.ch)\n"
txt += "# script: warming_levels.py\n"
txt += f"# reference years: {start_clim} to {end_clim}\n"
txt += f"# start_years of models starting AFTER {start_clim}\n"
txt += "model, ensemble, exp, start, end_year\n"
models = list()
for ds, meta in tas_list:
anomaly = computation.calc_anomaly(
ds.tas, start=start_clim, end=end_clim, meta=meta, how="absolute"
)
fmt = "{model}, {ens}, {exp}, {start}, {end_clim}\n"
# skip if too short
if not len(anomaly):
start = ds.year[0].item()
models.append(fmt.format(start=start, end_clim=end_clim, **meta))
# sort the by model
txt += "".join(sorted(models))
with open(fN, "w") as fid:
fid.writelines(txt)
def write_warming_level_to_file(
tas_list,
conf_cmip,
warming_levels,
clim=None,
add_grid_info=False,
all_ens=False,
check_years=True,
):
# all warming levels in the same file
if clim is None:
start_clim = conf_cmip.ANOMALY_YR_START
end_clim = conf_cmip.ANOMALY_YR_END
else:
start_clim, end_clim = clim
fN_yml = _get_filename(
conf_cmip.cmip_version,
start_clim,
end_clim,
all_ens,
check_years,
add_grid_info,
"yml",
)
file_ending = subfolder = "csv"
fN_csv = _get_filename(
conf_cmip.cmip_version,
start_clim,
end_clim,
all_ens,
check_years,
add_grid_info,
file_ending,
subfolder,
)
string_yml = ""
string_csv = ""
for warming_level in warming_levels:
string_yml_, string_csv_ = warming_level_years(
tas_list,
warming_level,
start_clim,
end_clim,
add_grid_info=add_grid_info,
check_years=check_years,
)
string_yml += string_yml_
string_csv += string_csv_
with open(fN_yml, "w") as fid:
write_info(fid, start_clim, end_clim, check_years)
fid.write("\n")
fid.writelines(string_yml)
grid = "grid, " if add_grid_info else ""
header = f"model, ensemble, exp, {grid}warming_level, start_year, end_year\n"
with open(fN_csv, "w") as fid:
write_info(fid, start_clim, end_clim, check_years)
fid.write(header)
fid.writelines(string_csv)
if not check_years:
write_start_years_no_boundscheck(
conf_cmip, tas_list, start_clim, end_clim, all_ens
)
def write_cmip5(warming_levels1, warming_levels2):
# load data
c5_tas = conf.cmip5.load_post_all_concat(
varn="tas",
postprocess="global_mean",
anomaly="no_check_no_anom",
ensnumber=None,
)
# ======================
# all ensnumbers
write_warming_level_to_file(c5_tas, conf.cmip5, warming_levels1, all_ens=True)
write_warming_level_to_file(
c5_tas, conf.cmip5, warming_levels1, all_ens=True, check_years=False
)
write_warming_level_to_file(
c5_tas, conf.cmip5, warming_levels2, clim=(1995, 2014), all_ens=True
)
write_warming_level_to_file(
c5_tas, conf.cmip5, warming_levels1, clim=(1861, 1900), all_ens=True
)
# only ensnumber = 0
c5_tas = computation.select_by_metadata(c5_tas, ensnumber=0)
write_warming_level_to_file(c5_tas, conf.cmip5, warming_levels1, all_ens=False)
write_warming_level_to_file(
c5_tas, conf.cmip5, warming_levels1, all_ens=False, check_years=False
)
write_warming_level_to_file(
c5_tas, conf.cmip5, warming_levels2, clim=(1995, 2014), all_ens=False
)
write_warming_level_to_file(
c5_tas, conf.cmip5, warming_levels1, clim=(1861, 1900), all_ens=False
)
def write_cmip6(warming_levels1, warming_levels2, all_ens=False):
ensnumber = 0
if all_ens:
ensnumber = None
c6_tas = conf.cmip6.load_post_all_concat(
varn="tas",
postprocess="global_mean",
ensnumber=ensnumber,
anomaly="no_check_no_anom",
)
# write warming levels to file
write_warming_level_to_file(c6_tas, conf.cmip6, warming_levels1, all_ens=all_ens)
write_warming_level_to_file(
c6_tas, conf.cmip6, warming_levels1, add_grid_info=True, all_ens=all_ens
)
write_warming_level_to_file(
c6_tas, conf.cmip6, warming_levels2, clim=(1995, 2014), all_ens=all_ens
)
write_warming_level_to_file(
c6_tas,
conf.cmip6,
warming_levels2,
clim=(1995, 2014),
add_grid_info=True,
all_ens=all_ens,
)
if __name__ == "__main__":
warming_levels1 = [1.0, 1.5, 2.0, 3.0, 4.0, 0.61]
# for Chapter 4
warming_levels2 = [0.94, 3.43]
write_cmip5(warming_levels1, warming_levels2)
write_cmip6(warming_levels1, warming_levels2)
write_cmip6(warming_levels1, warming_levels2, all_ens=True)