-
Notifications
You must be signed in to change notification settings - Fork 0
/
imogen_func.py
366 lines (333 loc) · 12.2 KB
/
imogen_func.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
355
356
357
358
359
360
361
362
363
364
365
366
"""
imogen specific functions for postprocessing
dont need to look at if not processing imogen
"""
import sys
import os
import glob
import iris
import iris.coords as icoords
import numpy as np
from read_input import parse_args
from read_input import config_parse_args
from read_input import read_mip_info_no_rose
import jules_xarray
from process_jules import USE_JULES_PY
sys.path.append("/home/h03/hadea/bin")
import jules
MIPNAME, L_TESTING, L_BACKFILL_MISSING_FILES, L_JULES_ROSE = parse_args()
if L_JULES_ROSE:
CONFIG_ARGS = config_parse_args(MIPNAME)
elif not L_JULES_ROSE:
MIP_INFO = read_mip_info_no_rose(MIPNAME)
imodel_dict_cmip5 = {
"cen_bcc_mod_bcc-csm1-1": 1,
"cen_ipsl_mod_ipsl-cm5a-lr": 2,
"cen_mri_mod_mri-cgcm3": 3,
"cen_noaa-gfdl_mod_gfdl-esm2m": 4,
"cen_bcc_mod_bcc-csm1-1-m": 5,
"cen_ipsl_mod_ipsl-cm5a-mr": 6,
"cen_nasa-giss_mod_giss-e2-h": 7,
"cen_nsf-doe-ncar_mod_cesm1-bgc": 8,
"cen_bnu_mod_bnu-esm": 9,
"cen_ipsl_mod_ipsl-cm5b-lr": 10,
"cen_nasa-giss_mod_giss-e2-h-cc": 11,
"cen_nsf-doe-ncar_mod_cesm1-cam5": 12,
"cen_cccma_mod_canesm2": 13,
"cen_miroc_mod_miroc-esm": 14,
"cen_nasa-giss_mod_giss-e2-r": 15,
"cen_nsf-doe-ncar_mod_cesm1-waccm": 16,
"cen_cmcc_mod_cmcc-cms": 17,
"cen_miroc_mod_miroc-esm-chem": 18,
"cen_nasa-giss_mod_giss-e2-r-cc": 19,
"cen_cnrm-cerfacs_mod_cnrm-cm5": 20,
"cen_miroc_mod_miroc5": 21,
"cen_ncar_mod_ccsm4": 22,
"cen_csiro-bom_mod_access1-0": 23,
"cen_mohc_mod_hadgem2-cc": 24,
"cen_ncc_mod_noresm1-m": 25,
"cen_csiro-bom_mod_access1-3": 26,
"cen_mohc_mod_hadgem2-es": 27,
"cen_ncc_mod_noresm1-me": 28,
"cen_csiro-qccce_mod_csiro-mk3-6-0": 29,
"cen_mpi-m_mod_mpi-esm-lr": 30,
"cen_noaa-gfdl_mod_gfdl-cm3": 31,
"cen_inm_mod_inmcm4": 32,
"cen_mpi-m_mod_mpi-esm-mr": 33,
"cen_noaa-gfdl_mod_gfdl-esm2g": 34,
}
# imodel_dict_cmip5 = {'cen_cnrm-cerfacs_mod_cnrm-cm5':1,
# 'cen_csiro-bom_mod_access1-3':2,
# 'cen_ipsl_mod_ipsl-cm5a-lr':3}
imodel_dict_cmip6 = {
"ACCESS-CM2": 1,
"ACCESS-ESM1-5": 2,
"CNRM-CM6-1-HR": 3,
"CNRM-CM6-1": 4,
"CNRM-ESM2-1": 5,
"CanESM5": 6,
"EC-Earth3-Veg": 7,
"FGOALS-g3": 8,
"HadGEM3-GC31-LL": 9,
"HadGEM3-GC31-MM": 10,
"INM-CM4-8": 11,
"IPSL-CM6A-LR": 12,
"MIROC-ES2L": 13,
"MIROC6": 14,
"MPI-ESM1-2-HR": 15,
"MPI-ESM1-2-LR": 16,
"MRI-ESM2-0": 17,
"UKESM1-0-LL": 18,
}
def add_ensemble(imodel_dict, drive_model, cube):
"""
Add our own realization coordinate if it doesn't already exist
Also add time coordinate year and transfer time to middle of period
"""
# #####################################################################
# BEGIN add our own realization coordinate if it doesn't already exist.
if not cube.coords("realization"):
realization = imodel_dict[drive_model]
ensemble_coord = icoords.AuxCoord(
realization, standard_name="realization", var_name="realization"
)
cube.add_aux_coord(ensemble_coord)
for key in list(cube.attributes.keys()):
del cube.attributes[key]
return cube
# #############################################################################
def read_ensemble(files_in, variable_cons, time_cons, diag_in, drive_model):
"""
Read imogen ensembles defined by imodel_dict
"""
# #########################################################################
def model_ensemble_callback(cube, field, filename):
"""
Add our own realization coordinate if it doesn't already exist
Also add time coordinate year and transfer time to middle of period
"""
# #####################################################################
# BEGIN add our own realization coordinate if it doesn't already exist.
if not cube.coords("realization"):
realization = imodel_dict[filename.split("/")[-2]]
ensemble_coord = icoords.AuxCoord(
realization, standard_name="realization", var_name="realization"
)
cube.add_aux_coord(ensemble_coord)
for key in list(cube.attributes.keys()):
del cube.attributes[key]
# from add_time_middle function in process jules
cube.coord("time").bounds = 3600 * (
np.rint(cube.coord("time").bounds.astype("int64") / 3600.0)
)
cube.coord("time").points = (
cube.coord("time").bounds[:, 0] + cube.coord("time").bounds[:, 1]
) / 2.0
# END add our own realization coordinate if it doesn't already exist.
# #####################################################################
keyall = []
cubeall = iris.cube.CubeList([])
imodel_dict = imodel_dict_cmip5
try:
if "imogen6" in MIP_INFO["model"][MIPNAME].lower():
imodel_dict = imodel_dict_cmip6
except:
if "imogen6" in CONFIG_ARGS["MODEL_INFO"]["mipname"].lower():
imodel_dict = imodel_dict_cmip6
keys = imodel_dict.keys()
for key in keys:
files_read = list()
if drive_model is None:
files_tmp = [f.replace("*", key) for f in files_in]
else:
files_tmp = [f.replace(drive_model, key) for f in files_in]
files_tmp = [glob.glob(f) for f in files_tmp]
files_read = [f for f in files_tmp if f]
files_read = [f for sublist in files_read for f in sublist]
if len(files_read) > 0:
if USE_JULES_PY:
cube = jules.load(
files_read,
variable_cons & time_cons,
missingdata=np.ma.masked,
callback=model_ensemble_callback,
)
for ijk, icube in enumerate(cube):
if ijk > 0:
cube[ijk].coord("time").convert_units(cube[0].coord("time").units)
cube = cube.concatenate_cube()
else:
cube = jules_xarray.load(files_read,variable_cons & time_cons)
coord_names = [coord.name() for coord in cube.coords()]
if "scpool" in coord_names:
cube = cube.collapsed("scpool", iris.analysis.SUM)
if diag_in in [
"imogen_radf",
"dtemp_g",
"c_emiss_out",
"d_ocean_atmos",
"d_land_atmos_co2",
]:
cube = cube.collapsed(["latitude", "longitude"], iris.analysis.MEAN)
cube.remove_coord("latitude")
cube.remove_coord("longitude")
cube = add_ensemble(imodel_dict, key, cube)
keyall.append(key)
cubeall.append(cube)
sel_dict = {"model: " + key: imodel_dict[key] for key in keyall if key in imodel_dict}
cubeall = cubeall.merge_cube()
cubeall.attributes = sel_dict
coord_names = [coord.name() for coord in cubeall.coords()]
if "latitude" in coord_names:
cubeall.coord("latitude").guess_bounds()
cubeall.coord("latitude").long_name = "latitude"
if "longitude" in coord_names:
cubeall.coord("longitude").guess_bounds()
cubeall.coord("longitude").long_name = "longitude"
return cubeall
# #############################################################################
# #############################################################################
def make_outfilename_imogen(out_dir, outprofile, var, syr, eyr, diag_dic):
"""
sort out filename for outputfile
"""
errorcode = 0
outfilename = ""
if outprofile not in ["monthly"]:
key = f"{var}_{outprofile}"
outprofile = outprofile+"_"
else:
key = var
# remove timestep of data from output filename
outprofile=""
if not key in diag_dic.keys():
print(f"ERROR: {key} not in diag_dic")
errorcode = 1
return outfilename, errorcode
if not L_JULES_ROSE:
if diag_dic[key]["cmip_varname"] is not None:
outfilename = (
out_dir
+ "/"
+ MIP_INFO["model"][MIPNAME]
+ "_"
+ MIP_INFO["out_scenario"][MIPNAME]
+ "_"
+ diag_dic[key]["cmip_varname"]
+ "_"
+ outprofile
+ str(syr)
+ "_"
+ str(eyr)
+ ".nc"
)
else:
if diag_dic[key]["cmip_varname"] is not None:
outfilename = (
out_dir
+ "/"
+ CONFIG_ARGS["OUT_INFO"]["model_out_id"]
+ "_"
+ CONFIG_ARGS["MODEL_INFO"]["configname"]
+ "_"
+ CONFIG_ARGS["MODEL_INFO"]["climate_scenario"]
+ "_"
+ diag_dic[key]["cmip_varname"]
+ "_"
+ outprofile
+ str(syr)
+ "_"
+ str(eyr)
+ ".nc"
)
else:
print(f"ERROR: cmip_varname for {var}, {outprofile} is none in diag_dic")
errorcode = 1
return outfilename, errorcode
# #############################################################################
# #############################################################################
def make_infilename_imogen(src_dir, jules_profname, years):
"""
sort out filename for input file
"""
files_in = []
errorcode = 0
if not L_JULES_ROSE:
if isinstance(jules_profname, list):
for profname in jules_profname:
files_in.extend(
[
[
f"{src_dir}/*_{MIP_INFO['in_scenario'][MIPNAME]}{MIP_INFO['run_name'][MIPNAME]}{profname}.{year}.nc"
for year in years
]
]
)
else:
files_in = [
src_dir
+ "/*_"
+ MIP_INFO["in_scenario"][MIPNAME]
+ MIP_INFO["run_name"][MIPNAME]
+ jules_profname
+ "."
+ year
+ ".nc"
for year in years
]
else:
if isinstance(jules_profname, list):
for profname in jules_profname:
files_in.extend(
[
src_dir
+ "/"
+ CONFIG_ARGS["MODEL_INFO"]["driving_model"]
+ "/"
+ CONFIG_ARGS["MODEL_INFO"]["driving_model"]
+ "_"
+ CONFIG_ARGS["MODEL_INFO"]["climate_scenario"]
+ "_"
+ CONFIG_ARGS["MODEL_INFO"]["configname"]
+ "."
+ profname
+ "."
+ year
+ ".nc"
for year in years
]
)
else:
files_in = [
src_dir
+ "/"
+ CONFIG_ARGS["MODEL_INFO"]["driving_model"]
+ "/"
+ CONFIG_ARGS["MODEL_INFO"]["driving_model"]
+ "_"
+ CONFIG_ARGS["MODEL_INFO"]["climate_scenario"]
+ "_"
+ CONFIG_ARGS["MODEL_INFO"]["configname"]
+ "."
+ jules_profname
+ "."
+ year
+ ".nc"
for year in years
]
existing_files = [file for file in files_in if os.path.isfile(file)]
if len(existing_files) == 0:
print("ERROR: No input files found of form:")
if not L_JULES_ROSE:
print(
[
f"{src_dir}*/*_{MIP_INFO['in_scenario'][MIPNAME]}{MIP_INFO['run_name'][MIPNAME]}{profname}.*.nc"
for profname in jules_profname
]
)
else:
print(
f"{src_dir}/{CONFIG_ARGS['MODEL_INFO']['driving_model']}/{CONFIG_ARGS['MODEL_INFO']['driving_model']}_{CONFIG_ARGS['MODEL_INFO']['climate_scenario']}_{CONFIG_ARGS['MODEL_INFO']['configname']}.{jules_profname}.*.nc"
)
errorcode = 1
return existing_files, errorcode