-
Notifications
You must be signed in to change notification settings - Fork 0
/
parisargreement_cmip5cmip6_plotting.py
293 lines (229 loc) · 13.1 KB
/
parisargreement_cmip5cmip6_plotting.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 6 13:46:43 2020
@author: Rebecca Varney, University of Exeter (rmv203@exeter.ac.uk)
Analysis and Plotting Python Script for Varney et al. 2020 Nature Communications
- script uses relationship-derived deltaCs,tau (x-axis) and model deltaCs,tau (y-axis) calculated in 'parisagreement_cmip5_analysis'
and 'parisagreement_cmip6_analysis', and plots the values against one another for each model considered in this study
- script combines the data to consider models from CMIP6 and CMIP5 as one model ensemble
"""
#%%
# Analysis imports
import numpy as np
import numpy.ma as ma
import csv
import netCDF4
from netCDF4 import Dataset
import iris
import iris.coord_categorisation
import glob
import warnings
from iris.experimental.equalise_cubes import equalise_attributes
# Plotting
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import cm, rcParams, colors
from matplotlib import gridspec as gspec
from matplotlib.backends.backend_pdf import PdfPages
from matplotlib.font_manager import FontProperties
import matplotlib.path as mpat
import matplotlib.ticker as mticker
import matplotlib.patches as mpatches
from matplotlib.lines import Line2D
#%%
# CMIP6 models
cmip6_models = ['ACCESS-ESM1-5', 'BCC-CSM2-MR', 'CanESM5', 'CNRM-ESM2-1', 'IPSL-CM6A-LR', 'MIROC-ES2L', 'UKESM1-0-LL']
n_models_cmip6 = len(cmip6_models)
# SSP senarios
ssp_options = ['ssp126', 'ssp245', 'ssp585']
ssp_options_length = len(ssp_options)
# CMIP5 models
cmip5_models = ['BNU-ESM', 'CanESM2', 'CESM1-CAM5', 'GFDL-ESM2G', 'GISS-E2-R', 'HadGEM2-ES', 'IPSL-CM5A-LR', 'MIROC-ESM', 'NorESM1-M']
n_models = len(cmip5_models)
# RCP senarios
rcp_options = ['rcp26', 'rcp45', 'rcp85']
rcp_options_length = len(rcp_options)
model_shapes = ['o', '^', 's', '*', 'x', '+', 'd', 'p', 'H', 'X', 'D', '|', '_', '>', 'v', '1']
# global mean temperature change
temperature_change_options = [1, 2, 3]
temperature_change_options_length = len(temperature_change_options)
#%%
# loop through each global mean temperature change
for temp_option in range(0, temperature_change_options_length):
min_temperature = temperature_change_options[temp_option] # selecting the temperature change
# set up figure for each temperature change
fig = plt.figure(1, figsize=(24,18))
mpl.rcParams['xtick.direction'] = 'out'
mpl.rcParams['ytick.direction'] = 'out'
mpl.rcParams['xtick.top'] = True
mpl.rcParams['ytick.right'] = True
params = {
'lines.linewidth':3,
'axes.facecolor':'white',
'xtick.color':'k',
'ytick.color':'k',
'axes.labelsize': 22,
'xtick.labelsize':22,
'ytick.labelsize':22,
'font.size':22,
}
plt.rcParams.update(params)
min_axis_value = -850
max_axis_value = 50
#%%
# Loop through each ssp run being considered
for ssp_option in range(0, ssp_options_length):
ssp = ssp_options[ssp_option] # selecting the SSP scenario
rcp = rcp_options[ssp_option] # selecting the RCP scenario
# for loop for each cmip6 model
for model_i in range(0, n_models):
if model_i > 6:
model_cmip5 = cmip5_models[model_i] # seleting the CMIP5 model
print(min_temperature, rcp, model_cmip5)
# loading data
x_data_cmip5 = np.loadtxt('saved_data/x_'+str(min_temperature)+'_degree_warming_cmip5.csv', delimiter=',')
y_data_cmip5 = np.loadtxt('saved_data/y_'+str(min_temperature)+'_degree_warming_cmip5.csv', delimiter=',')
obs_data_cmip5 = np.loadtxt('saved_data/obs_constraint_'+str(min_temperature)+'_degree_warming_cmip5.csv')
#%%
# model data
rcp_option = ssp_option
cmip5_modelshape = model_i+7
# plotting
if rcp == 'rcp85':
plt.plot(x_data_cmip5[rcp_option, model_i], y_data_cmip5[rcp_option, model_i], marker=model_shapes[cmip5_modelshape], color='r', markersize=20, mew=5)
elif rcp == 'rcp45':
plt.plot(x_data_cmip5[rcp_option, model_i], y_data_cmip5[rcp_option, model_i], marker=model_shapes[cmip5_modelshape], color='g', markersize=20, mew=5)
elif rcp == 'rcp26':
plt.plot(x_data_cmip5[rcp_option, model_i], y_data_cmip5[rcp_option, model_i], marker=model_shapes[cmip5_modelshape], color='b', markersize=20, mew=5)
#%%
# observational constraint
obs_data_model_cmip5 = obs_data_cmip5[model_i+(rcp_option*n_models)]
# plotting constrained data line
x_line = np.linspace(min_axis_value, max_axis_value, 100)
global_array = np.zeros([100,1])
global_array = np.squeeze(global_array)
for b in range(0,100):
global_array[b] = obs_data_model_cmip5
plt.plot(global_array, x_line, color='b', linewidth=2, alpha=0.5)
else:
#%% CMIP6
model_cmip6 = cmip6_models[model_i] # seleting the models
print(min_temperature, ssp, model_cmip6)
# loading data
x_data_cmip6 = np.loadtxt('saved_data/x_'+str(min_temperature)+'_degree_warming_cmip6.csv', delimiter=',')
y_data_cmip6 = np.loadtxt('saved_data/y_'+str(min_temperature)+'_degree_warming_cmip6.csv', delimiter=',')
obs_data_cmip6 = np.loadtxt('saved_data/obs_constraint_'+str(min_temperature)+'_degree_warming_cmip6.csv')
# plotting
if ssp == 'ssp585':
plt.plot(x_data_cmip6[ssp_option, model_i], y_data_cmip6[ssp_option, model_i], marker=model_shapes[model_i], color='r', markersize=20, mew=5)
elif ssp == 'ssp245':
plt.plot(x_data_cmip6[ssp_option, model_i], y_data_cmip6[ssp_option, model_i], marker=model_shapes[model_i], color='g', markersize=20, mew=5)
elif ssp == 'ssp126':
plt.plot(x_data_cmip6[ssp_option, model_i], y_data_cmip6[ssp_option, model_i], marker=model_shapes[model_i], color='b', markersize=20, mew=5)
#%%
# observational constraint
obs_data_model_cmip6 = obs_data_cmip6[model_i+(ssp_option*n_models_cmip6)]
# plotting constrained data line
x_line = np.linspace(min_axis_value, max_axis_value, 100)
global_array = np.zeros([100,1])
global_array = np.squeeze(global_array)
for b in range(0,100):
global_array[b] = obs_data_model_cmip6
plt.plot(global_array, x_line, color='b', linewidth=2, alpha=0.5)
#%% CMIP5
model_cmip5 = cmip5_models[model_i] # seleting the models
print(min_temperature, ssp, model_cmip5)
# loading data
x_data_cmip5 = np.loadtxt('saved_data/x_'+str(min_temperature)+'_degree_warming_cmip5.csv', delimiter=',')
y_data_cmip5 = np.loadtxt('saved_data/y_'+str(min_temperature)+'_degree_warming_cmip5.csv', delimiter=',')
obs_data_cmip5 = np.loadtxt('saved_data/obs_constraint_'+str(min_temperature)+'_degree_warming_cmip5.csv')
# plotting
rcp_option = ssp_option
cmip5_modelshape = model_i+7
if rcp == 'rcp85':
plt.plot(x_data_cmip5[rcp_option, model_i], y_data_cmip5[rcp_option, model_i], marker=model_shapes[cmip5_modelshape], color='r', markersize=20, mew=5)
elif rcp == 'rcp45':
plt.plot(x_data_cmip5[rcp_option, model_i], y_data_cmip5[rcp_option, model_i], marker=model_shapes[cmip5_modelshape], color='g', markersize=20, mew=5)
elif rcp == 'rcp26':
plt.plot(x_data_cmip5[rcp_option, model_i], y_data_cmip5[rcp_option, model_i], marker=model_shapes[cmip5_modelshape], color='b', markersize=20, mew=5)
#%%
# observational constraint
obs_data_model_cmip5 = obs_data_cmip5[model_i+(rcp_option*n_models)]
# creating constrained data line
x_line = np.linspace(min_axis_value, max_axis_value, 100)
global_array = np.zeros([100,1])
global_array = np.squeeze(global_array)
for b in range(0,100):
global_array[b] = obs_data_model_cmip5
plt.plot(global_array, x_line, color='b', linewidth=2, alpha=0.5)
#%%
# combining CMIP5 and CMIP6 models
# saving x_data and y_data for CMIP5
flat_x_array_cmip5 = x_data_cmip5.flatten()
flat_y_array_cmip5 = y_data_cmip5.flatten()
flat_x_array_cmip5 = flat_x_array_cmip5[flat_x_array_cmip5==flat_x_array_cmip5]
flat_y_array_cmip5 = flat_y_array_cmip5[flat_y_array_cmip5==flat_y_array_cmip5]
# saving x_data and y_data for CMIP6
flat_x_array_cmip6 = x_data_cmip6.flatten()
flat_y_array_cmip6 = y_data_cmip6.flatten()
flat_x_array_cmip6 = flat_x_array_cmip6[flat_x_array_cmip6==flat_x_array_cmip6]
flat_y_array_cmip6 = flat_y_array_cmip6[flat_y_array_cmip6==flat_y_array_cmip6]
# CONCATENATE CMIP5 and CMIP6
flat_x_array = np.concatenate((flat_x_array_cmip5, flat_x_array_cmip6), axis=0)
flat_y_array = np.concatenate((flat_y_array_cmip5, flat_y_array_cmip6), axis=0)
obs_data = np.concatenate((obs_data_cmip5, obs_data_cmip6), axis=0)
#%%
# unconstrained values
old_ensemble_mean = np.nanmean(flat_y_array)
old_ensemble_std = np.std(flat_y_array)
print('original mean plus uncertainty:', old_ensemble_mean, old_ensemble_std)
r_coeffient = ma.corrcoef(flat_x_array, flat_y_array)
print('Combined CMIP r-coefficent:', r_coeffient)
#%%
# observational constraint
x_obs = np.nanmean(obs_data)
dx_obs = np.nanstd(obs_data)
plt.axvspan(x_obs-dx_obs, x_obs+dx_obs, color='lightblue', alpha=0.8, zorder=20)
# one to one line
one_to_one_line = np.linspace(min_axis_value, max_axis_value, 100) # one to one line
plt.plot(one_to_one_line, one_to_one_line, 'grey', linewidth=1)
# legends
handels_1 = []
handels_1.extend([Line2D([0,0],[0,0], linewidth=20, color='b', label='ssp126')])
handels_1.extend([Line2D([0,0],[0,0], linewidth=20, color='g', label='ssp245')])
handels_1.extend([Line2D([0,0],[0,0], linewidth=20, color='r', label='ssp585')])
label_1 = ['ssp126', 'ssp245', 'ssp585']
leg_1 = plt.legend(handels_1, label_1, loc=4)
plt.gca().add_artist(leg_1)
handels = []
handels.extend([Line2D([0,0],[0,0], linestyle='None', marker='o', markersize=20, color='k', label='ACCESS-ESM1-5')])
handels.extend([Line2D([0,0],[0,0], linestyle='None', marker='^', markersize=20, color='k', label='BCC-CSM2-MR')])
handels.extend([Line2D([0,0],[0,0], linestyle='None', marker='s', markersize=20, color='k', label='CanESM5')])
handels.extend([Line2D([0,0],[0,0], linestyle='None', marker='*', markersize=20, color='k', label='CNRM-ESM2-1')])
handels.extend([Line2D([0,0],[0,0], linestyle='None', marker='x', markersize=20, color='k', label='IPSL-CM6A-LR')])
handels.extend([Line2D([0,0],[0,0], linestyle='None', marker='+', markersize=20, color='k', label='MIROC-ES2L')])
handels.extend([Line2D([0,0],[0,0], linestyle='None', marker='d', markersize=20, color='k', label='UKESM1-0-LL')])
handels.extend([Line2D([0,0],[0,0], linewidth=1, color='grey', label='one to one line')])
handels.extend([Line2D([0,0],[0,0], linewidth=10, color='b', alpha=0.5, label='Observational-derived mean')])
handels.extend([Line2D([0,0],[0,0], linewidth=20, color='lightblue', alpha=0.8, label='CMIP6 Standard Deviation')])
label = ['ACCESS-ESM1-5', 'BCC-CSM2-MR', 'CanESM5', 'CNRM-ESM2-1', 'IPSL-CM6A-LR', 'MIROC-ES2L', 'UKESM1-0-LL', 'one to one line', 'Observational-derived mean', 'CMIP6 Standard Deviation']
leg = plt.legend(handels, label, loc=2)
plt.gca().add_artist(leg)
# axis limits
plt.xlim((min_axis_value, max_axis_value))
plt.ylim((min_axis_value, max_axis_value))
# axis labels
plt.xlabel(r'Estimated $\Delta C_{\mathrm{s, \tau}}$ (PgC)')
plt.ylabel(r'Model $\Delta C_{\mathrm{s, \tau}}$ (PgC)')
#%%
fig.savefig('additional_figures/obs_constraint_cmip6cmip5_'+str(min_temperature)+'_CARDrh.pdf', bbox_inches='tight')
plt.close()
#%%
# saving x_obs and dx_obs values, and x and y values
x_obs = np.array([x_obs])
dx_obs = np.array([dx_obs])
np.savetxt("saved_data/x_obs_"+str(min_temperature)+"_degree_warming_cmip6cmip5.csv", x_obs, delimiter=",")
np.savetxt("saved_data/dx_obs_"+str(min_temperature)+"_degree_warming_cmip6cmip5.csv", dx_obs, delimiter=",")
np.savetxt("saved_data/combined_x_"+str(min_temperature)+"_degree_warming_cmip6cmip5.csv", flat_x_array, delimiter=",")
np.savetxt("saved_data/combined_y_"+str(min_temperature)+"_degree_warming_cmip6cmip5.csv", flat_y_array, delimiter=",")