-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmip6_et_data_processing.py
344 lines (293 loc) · 14 KB
/
cmip6_et_data_processing.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import iris
from cube_funcs import get_dates
from iris.experimental.equalise_cubes import equalise_attributes
from iris.util import unify_time_units
import warnings
warnings.filterwarnings("ignore")
def read_cmip_cube(fname, constraint=None):
#print(fname)
try:
cube = iris.load_cube(fname, constraint=constraint)
except:
cubes = iris.load(fname, constraints=constraint)
equalise_attributes(cubes)
unify_time_units(cubes)
try:
cube = cubes.merge_cube()
except:
cube = cubes.concatenate_cube()
print(cube.shape)
return(cube)
def iris_read(data_path, standard_name, short_name=None):
cubes = iris.load(data_path, standard_name)
if short_name is not None:
var_name_temp = iris.Constraint(cube_func=lambda cube:
cube.var_name == short_name)
cubes = cubes.extract(var_name_temp)
return(cubes)
# change path to location of data
# data should be sorted into folders by model name
path = '/nfs/see-fs-02_users/earjba/b0122/cmip6/'
# edit to read in another variable
var = 'evspsbl'
# 10 CMIP6 models analysed in study
models = ['ACCESS-ESM1-5', 'BCC-CSM2-MR', 'BCC-ESM1',
'CESM2', 'CESM2-WACCM', 'GISS-E2-1-G', 'HadGEM3-GC31-LL',
'HadGEM3-GC31-MM', 'SAM0-UNICON', 'UKESM1-0-LL']
# loop over models, read in data and calculate mean of multiple runs
for model in models:
constraint = iris.Constraint(time=lambda cell:
1850 <= cell.point.year <= 2014)
print(model)
temp1 = temp2 = temp3 = temp4 = temp5 = temp6 = temp7 = None
temp8 = temp9 = temp10 = temp11 = temp12 = temp13 = temp14 = None
temp15 = temp16 = temp17 = temp18 = temp19 = temp20 = None
# path to historical data
fpath = path + var + '/historical/' + model
try:
temp1 = read_cmip_cube(fpath + '/*r1i1p1f*.nc', constraint=constraint)
except OSError:
pass
try:
temp2 = read_cmip_cube(fpath + '/*r2i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp3 = read_cmip_cube(fpath + '/*r3i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp4 = read_cmip_cube(fpath + '/*r4i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp5 = read_cmip_cube(fpath + '/*r5i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp6 = read_cmip_cube(fpath + '/*r6i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp7= read_cmip_cube(fpath + '/*r7i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp8 = read_cmip_cube(fpath + '/*r8i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp9 = read_cmip_cube(fpath + '/*r9i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp10 = read_cmip_cube(fpath + '/*r10i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp11 = read_cmip_cube(fpath + '/*r11i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp12 = read_cmip_cube(fpath + '/*r12i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp13 = read_cmip_cube(fpath + '/*r13i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp14 = read_cmip_cube(fpath + '/*r14i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp15 = read_cmip_cube(fpath + '/*r15i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp16 = read_cmip_cube(fpath + '/*r16i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp17 = read_cmip_cube(fpath + '/*r17i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp18 = read_cmip_cube(fpath + '/*r18i1p1*.nc', constraint=constraint)
except OSError:
pass
try:
temp19= read_cmip_cube(fpath + '/*r19i1p1*.nc', constraint=constraint)
except OSError:
pass
temp_list = [temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8, temp9,
temp10, temp11, temp12, temp13, temp14, temp15, temp16, temp17,
temp18, temp19, temp20]
# get list of model runs
temp_list_trim = [x for x in temp_list if x is not None]
print(len(temp_list_trim))
# if length only 1 save as new netcdf
if len(temp_list_trim) == 1:
ensemble_mean = temp_list_trim[0]
ensemble_mean.standard_name = temp_list_trim[0].standard_name
ensemble_mean.attributes = temp_list_trim[0].attributes
dates = get_dates(ensemble_mean, verbose=False)
outpath = (fpath + '/test_ensemble_mean_historical_' +
model + '_' + var + '_' +
str(dates[0].year) + str(dates[0].month).zfill(2) + '_' +
str(dates[-1].year) + str(dates[-1].month).zfill(2) + '.nc')
print(outpath)
iris.save(ensemble_mean, outpath)
continue
else:
# if multiple runs calculate mean of runs
n = len(temp_list_trim)
print(n)
equalise_attributes(temp_list_trim)
unify_time_units(temp_list_trim)
if n == 2:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1])/n
if n == 3:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2])/n
if n == 4:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3])/n
if n == 5:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4])/n
if n == 6:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4] + temp_list_trim[5])/n
if n == 7:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4] + temp_list_trim[5] +
temp_list_trim[6])/n
if n == 8:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4] + temp_list_trim[5] +
temp_list_trim[6] + temp_list_trim[7])/n
if n == 9:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4] + temp_list_trim[5] +
temp_list_trim[6] + temp_list_trim[7] +
temp_list_trim[8])/n
if n == 10:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4] + temp_list_trim[5] +
temp_list_trim[6] + temp_list_trim[7] +
temp_list_trim[8] + temp_list_trim[9])/n
if n == 11:
if model == 'CESM2': # members 8, 9 and 10 lat match error error
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4] + temp_list_trim[5] +
temp_list_trim[6] + temp_list_trim[7])/(n-3)
else:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4] + temp_list_trim[5] +
temp_list_trim[6] + temp_list_trim[7] +
temp_list_trim[8] + temp_list_trim[9] +
temp_list_trim[10])/n
if n == 12:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4] + temp_list_trim[5] +
temp_list_trim[6] + temp_list_trim[7] +
temp_list_trim[8] + temp_list_trim[9] +
temp_list_trim[10] + temp_list_trim[11])/n
if n == 13:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4] + temp_list_trim[5] +
temp_list_trim[6] + temp_list_trim[7] +
temp_list_trim[8] + temp_list_trim[9] +
temp_list_trim[10] + temp_list_trim[11] +
temp_list_trim[12])/n
if n == 14:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4] + temp_list_trim[5] +
temp_list_trim[6] + temp_list_trim[7] +
temp_list_trim[8] + temp_list_trim[9] +
temp_list_trim[10] + temp_list_trim[11] +
temp_list_trim[12] + temp_list_trim[13])/n
if n == 15:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4] + temp_list_trim[5] +
temp_list_trim[6] + temp_list_trim[7] +
temp_list_trim[8] + temp_list_trim[9] +
temp_list_trim[10] + temp_list_trim[11] +
temp_list_trim[12] + temp_list_trim[13] +
temp_list_trim[14])/n
if n == 16:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4] + temp_list_trim[5] +
temp_list_trim[6] + temp_list_trim[7] +
temp_list_trim[8] + temp_list_trim[9] +
temp_list_trim[10] + temp_list_trim[11] +
temp_list_trim[12] + temp_list_trim[13] +
temp_list_trim[14] + temp_list_trim[15])/n
if n == 17:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4] + temp_list_trim[5] +
temp_list_trim[6] + temp_list_trim[7] +
temp_list_trim[8] + temp_list_trim[9] +
temp_list_trim[10] + temp_list_trim[11] +
temp_list_trim[12] + temp_list_trim[13] +
temp_list_trim[14] + temp_list_trim[15] +
temp_list_trim[16])/n
if n == 18:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4] + temp_list_trim[5] +
temp_list_trim[6] + temp_list_trim[7] +
temp_list_trim[8] + temp_list_trim[9] +
temp_list_trim[10] + temp_list_trim[11] +
temp_list_trim[12] + temp_list_trim[13] +
temp_list_trim[14] + temp_list_trim[15] +
temp_list_trim[16] + temp_list_trim[17])/n
if n == 19:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4] + temp_list_trim[5] +
temp_list_trim[6] + temp_list_trim[7] +
temp_list_trim[8] + temp_list_trim[9] +
temp_list_trim[10] + temp_list_trim[11] +
temp_list_trim[12] + temp_list_trim[13] +
temp_list_trim[14] + temp_list_trim[15] +
temp_list_trim[16] + temp_list_trim[17] +
temp_list_trim[18])/n
if n == 20:
ensemble_mean = (temp_list_trim[0] + temp_list_trim[1] +
temp_list_trim[2] + temp_list_trim[3] +
temp_list_trim[4] + temp_list_trim[5] +
temp_list_trim[6] + temp_list_trim[7] +
temp_list_trim[8] + temp_list_trim[9] +
temp_list_trim[10] + temp_list_trim[11] +
temp_list_trim[12] + temp_list_trim[13] +
temp_list_trim[14] + temp_list_trim[15] +
temp_list_trim[16] + temp_list_trim[17] +
temp_list_trim[18] + temp_list_trim[19])/n
# save as new netcdf
ensemble_mean.standard_name = temp_list_trim[0].standard_name
ensemble_mean.attributes = temp_list_trim[0].attributes
dates = get_dates(ensemble_mean, verbose=False)
outpath = (fpath + '/test_ensemble_mean_historical_' +
model + '_' + var + '_' +
str(dates[0].year) + str(dates[0].month).zfill(2) + '_' +
str(dates[-1].year) + str(dates[-1].month).zfill(2) + '.nc')
print(outpath)
iris.save(ensemble_mean, outpath)