Skip to content

Commit 485c406

Browse files
author
Elizaveta Malinina
committed
add updates to v4 and 2024 data
1 parent 10198f5 commit 485c406

13 files changed

Lines changed: 1571 additions & 287 deletions

esmvaltool/diag_scripts/attribute/attrib_blended.py

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import logging
44
import os
55
import csv
6-
import xarray as xr
76
import iris
7+
import pickle
88
from pprint import pformat
99
from scipy.stats import t
10+
import esmvaltool.diag_scripts.shared.plot as eplot
1011

1112
import numpy
1213
import esmvaltool.diag_scripts.attribute.detatt_mk as da
@@ -88,7 +89,7 @@ def main(cfg):
8889
years=list(numpy.arange(y_start+av_yr/2,y_end+1+av_yr/2,av_yr)) #Used for plotting.
8990
#Added 5 years here.
9091
nyear=y_end - y_start +1 #Number of years, hard-coded.
91-
ldiag=int(nyear/av_yr) #length of diagnostic.
92+
ldiag=int(numpy.ceil(nyear/av_yr)) #length of diagnostic.
9293

9394
anom_max=500 #arbitrary max size for number of anomalies.
9495
mean_diag=numpy.zeros((ldiag,nexp,nmodel))
@@ -105,6 +106,10 @@ def main(cfg):
105106
mean_gmst_comp_warming=numpy.zeros((ldiag,nexp,nmodel))
106107
ci90_gmst_comp_warming=numpy.zeros((ldiag,nexp,nmodel))
107108

109+
# Set matplotlib style
110+
mpl_st_file = eplot.get_path_to_mpl_style('attribute')
111+
plt.style.use(mpl_st_file)
112+
108113
#Loop over models, then datasets, then ensemble members.
109114
for mm, dataset in enumerate(grouped_input_data):
110115
logger.info("*************** Processing model %s", dataset)
@@ -199,15 +204,15 @@ def main(cfg):
199204
plt.plot([ee+1.2],att_out3['beta'][0],color=cols[0,:],marker='+')
200205

201206
plt.subplot(121)
202-
plt.plot([0,enssize+1],[1,1],color='black',linewidth=1,ls=':')
207+
plt.plot([0,enssize+1],[1,1],color='black',linewidth=1,ls=':', zorder=100)
203208
plt.ylabel('Regression coefficients')#,size='x-small')
204-
plt.plot([0,enssize+1],[0,0],color='black',linewidth=1,ls='--')
209+
plt.plot([0,enssize+1],[0,0],color='black',linewidth=1,ls='--', zorder=100)
205210
plt.axis([0,enssize+1,-1,3])
206211
plt.text (-10,3.3,'a',fontsize =7,fontweight='bold', va='center', ha='center')
207212

208213
plt.subplot(122)
209-
plt.plot([0,enssize+1],[1,1],color='black',linewidth=1,ls=':')
210-
plt.plot([0,enssize+1],[0,0],color='black',linewidth=1,ls='--')
214+
plt.plot([0,enssize+1],[1,1],color='black',linewidth=1,ls=':', zorder=100)
215+
plt.plot([0,enssize+1],[0,0],color='black',linewidth=1,ls='--', zorder=100)
211216
plt.axis([0,enssize+1,-1,3])
212217
plt.text (-10,3.3,'b',fontsize =7,fontweight='bold', va='center', ha='center')
213218
plt.savefig(plot_dir+'/reg_obsens_'+exp_flag+'.'+output_file_type)
@@ -221,9 +226,9 @@ def main(cfg):
221226

222227
#Set up main figure.
223228
if rcplot:
224-
plt.figure(0,figsize=[180*mm_conv,180*mm_conv])
229+
fig = plt.figure(0,figsize=[180*mm_conv,180*mm_conv])
225230
else:
226-
plt.figure(0,figsize=[180*mm_conv,120*mm_conv])
231+
fig = plt.figure(0,figsize=[180*mm_conv,120*mm_conv])
227232

228233
#Main attribution analysis.
229234
att_out={}
@@ -478,17 +483,16 @@ def main(cfg):
478483

479484
for ff in [topleft,topright]:
480485
plt.subplot(ff)
481-
plt.plot([0,nmodel_attrib+2],[1,1],color='black',linewidth=1,ls=':')
486+
plt.plot([0,nmodel_attrib+2],[1,1],color='black',linewidth=1,ls=':', zorder=100)
482487
if ff == topleft: plt.ylabel('Regression coefficients')#,size='x-small')
483-
plt.plot([0,nmodel_attrib+2],[0,0],color='black',linewidth=1,ls='--')
488+
plt.plot([0,nmodel_attrib+2],[0,0],color='black',linewidth=1,ls='--', zorder=100)
484489
if pool_int_var:
485-
plt.axis([0,nmodel_attrib+2,-1,3])
486-
# plt.xticks(list(range(1,nmodel_attrib+2)),[''])
490+
plt.ylim(-0.5,3.5)
491+
plt.xticks(list(range(1,nmodel_attrib+2)),['']*(nmodel_attrib+1))
487492
else:
488-
plt.axis([0,nmodel_attrib+1,-1,3])
489-
# plt.xticks(list(range(1,nmodel_attrib+1)),[''])
490-
plt.legend(loc="upper left")
491-
plt.text (-2.5,3.5,panel_labels[panel_counter],fontsize =7,fontweight='bold', va='center', ha='center')
493+
plt.ylim(-0.5,3.5)
494+
plt.xticks(list(range(1,nmodel_attrib+1)),['']*nmodel_attrib)
495+
plt.legend(ncol=3)
492496
panel_counter=panel_counter+1
493497

494498
if rcplot:
@@ -497,13 +501,12 @@ def main(cfg):
497501
plt.plot([0,nmodel_attrib+2],[0.05,0.05],color='black',linewidth=1,ls='--')
498502
plt.plot([0,nmodel_attrib+2],[0.95,0.95],color='black',linewidth=1,ls='--')
499503
if pool_int_var:
500-
plt.axis([0,nmodel_attrib+2,0,1])
501-
# plt.xticks(list(range(1,nmodel_attrib+2)),[''])
504+
plt.ylim(0, 1)
505+
plt.xticks(list(range(1,nmodel_attrib+2)),['']*(nmodel_attrib+1))
502506
else:
503-
plt.axis([0,nmodel_attrib+1,0,1])
504-
# plt.xticks(list(range(1,nmodel_attrib+1)),[''])
507+
plt.ylim(0, 1)
508+
plt.xticks(list(range(1,nmodel_attrib+1)),['']*nmodel_attrib)
505509
if ff == 323: plt.ylabel('RCT P-value')#,size='x-small')
506-
plt.text (-2,1.075,panel_labels[panel_counter],fontsize =7,fontweight='bold', va='center', ha='center')
507510
panel_counter=panel_counter+1
508511

509512
if simple_uncert: #Label lower panels a and b.
@@ -513,35 +516,33 @@ def main(cfg):
513516
plt.subplot(ff)
514517
plt.plot([0,nmodel_attrib+2],[obs_warming,obs_warming],color='black',linewidth=1,label='Had4 GSAT')
515518
if ff == bottomleft:
516-
plt.ylabel(f'Attributable change {warming_years[0]}-{warming_years[1]} vs {warming_base[0]}-{warming_base[1]} ($^\circ$C)')#,size='x-small')
517-
plt.plot([0,nmodel_attrib+2],[0,0],color='black',linewidth=1,ls='--')
519+
plt.ylabel(f'Attributable change {warming_years[0]}-{warming_years[1]}\nvs {warming_base[0]}-{warming_base[1]} ($^\circ$C)')#,size='x-small')
520+
plt.plot([0,nmodel_attrib+2],[0,0],color='black',linewidth=1,ls='--', zorder=100)
518521
if pool_int_var:
519-
plt.axis([0,nmodel_attrib+2,-2,3])
520-
plt.xticks(list(range(1,nmodel_attrib+2)),model_names,rotation=30.,ha="right")
522+
plt.ylim(-1,4)
523+
plt.xticks(list(range(1,nmodel_attrib+2)),model_names,rotation=20.,ha="right")
521524
else:
522-
plt.axis([0,nmodel_attrib+1,-2,3])
523-
plt.xticks(list(range(1,nmodel_attrib+1)),model_names[0:nmodel_attrib],rotation=30.,ha="right")
525+
plt.ylim(-1,4)
526+
plt.xticks(list(range(1,nmodel_attrib+1)),model_names[0:nmodel_attrib],rotation=20.,ha="right")
524527

525-
plt.text (-2,3.3,panel_labels[panel_counter],fontsize =7,fontweight='bold', va='center', ha='center')
526528
panel_counter=panel_counter+1
527529

528-
529-
pool_flag='' if pool_int_var else '_not_pooled'
530-
uncert_flag='__simple_uncert' if simple_uncert else ''
531-
plt.savefig(plot_dir+'/reg_attrib_'+exp_flag+'_'+pool_flag+uncert_flag+'.'+output_file_type)
532-
plt.close()
530+
for n,ax in enumerate(fig.axes):
531+
text_box = ax.text(0.02, 0.96, f"({panel_labels[n]})", transform=ax.transAxes,
532+
fontweight='bold')
533+
text_box.set_bbox(dict(boxstyle='square,pad=0.001', facecolor='white', edgecolor='white'))
534+
ax.set_xlim(0.5, nmodel_attrib+1.5)
535+
536+
fig.suptitle('Results of a detection and attribution analysis for CMIP6 \n mean air temperature in Canada')
537+
fig.tight_layout()
538+
fig.savefig(plot_dir+'/reg_attrib_warming.'+output_file_type)
539+
fig.savefig(plot_dir+'/reg_attrib_warming.png')
540+
plt.close(fig)
533541
plt.figure(2,figsize=[180*mm_conv,60*mm_conv])
534542
for aa in range(anom_index):
535543
plt.plot(years,anom[:,aa])
536544
plt.savefig(plot_dir+'/anom.pdf')
537545
plt.close()
538-
plt.figure(2,figsize=[180*mm_conv,60*mm_conv])
539-
# for mm in range(3):
540-
plt.plot(years,mean_diag[:,1,0],color='black')
541-
plt.plot(years,mean_diag[:,1,1],color='green')
542-
plt.plot(years,mean_diag[:,1,2],color='gray')
543-
plt.savefig(plot_dir+'/mean_diag.pdf')
544-
plt.close()
545546

546547
plt.figure(2,figsize=[180*mm_conv,60*mm_conv])
547548
# for mm in range(3):
@@ -553,6 +554,13 @@ def main(cfg):
553554
plt.savefig(plot_dir+'/fitted_model.pdf')
554555
plt.close()
555556

557+
# save dics to a pickle
558+
with open(os.path.join(cfg['work_dir'], 'att_out.pkl'),'wb') as f:
559+
pickle.dump(att_out, f)
560+
# save 3w dic to a pickle
561+
with open(os.path.join(cfg['work_dir'], 'att_out3.pkl'),'wb') as f:
562+
pickle.dump(att_out3, f)
563+
556564
#Calculate annual mean timeseries for gmst_comp attributable warming, ANT, NAT, GHG, OTH.
557565

558566
with open(plot_dir+'/cmip6_fitted_comp_gmst.csv', mode='w') as file:

0 commit comments

Comments
 (0)