-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcal_cloud_E3SM.py
169 lines (139 loc) · 6.43 KB
/
cal_cloud_E3SM.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
#****************************************************************
#
# Filename: cal_cloud_E3SM.py
#
# Author: Yi Qin - [email protected]
# Description: calculate climatology of control simulation; calculate response per global-mean surface temperature change
# Input: post-processed output from raw model data
# Output: global_cloud_xxxx.nc
# Create: 2021-07-18
# Last Modified: 2021-07-26 18:08:44
# Jul 26, 2021: add low cloud fraction, liquid water path and ice water path
# Feb 01, 2022: add output of '_ab'
#****************************************************************
#IMPORT STUFF:
#=====================
import cdms2 as cdms
import cdutil
import MV2 as MV
import numpy as np
import pylab as pl
import matplotlib as mpl
import sys
## qinyi
import matplotlib.pyplot as plt
import os
import pandas as pd
import cdtime
import time
import ReadData as RD
import genutil
from genutil import statistics
import numpy.ma as ma
from global_land_mask import globe
from joblib import Parallel,delayed
import multiprocessing
###########################################################################
# HELPFUL FUNCTIONS FOLLOW
###########################################################################
def cal_cloud(direc_data,case_stamp,yearS,yearE,fname1,fname2,outdir,figdir,exp1,exp2):
outfile = outdir+'global_cloud_'+case_stamp+'.nc'
if os.path.isfile(outfile):
print('cal_cloud', case_stamp, 'output is ready. Please continue. ')
else:
yearS_4d = "{:04d}".format(yearS)
yearE_4d = "{:04d}".format(yearE)
nyears = yearE - yearS + 1
direc_data1 = direc_data+'/'+fname1+'/'
direc_data2 = direc_data+'/'+fname2+'/'
used_models = 'E3SM-1-0'
yrS=yearS
yrE=yearE
monS=1
monE=12
yrS_4d='{:04d}'.format(yrS)
yrE_4d='{:04d}'.format(yrE)
monS_2d='{:02d}'.format(monS)
monE_2d='{:02d}'.format(monE)
lats = np.arange(-90,92.5,2.5)
lons = np.arange(1.25,360,2.5)
grid = cdms.createGenericGrid(lats,lons)
var3d = ['CLDLIQ', 'CLDICE', 'CLOUD']
var2d = ['tas','TGCLDIWP','TGCLDLWP','CLDLOW','CLDMED','CLDHGH']
var = var2d + var3d
# =============================================
# read 2D variables
# =============================================
dic_all = {}
for svar in var:
if svar in var:
print(svar)
f1 = cdms.open(direc_data+fname1+'/'+svar+'_'+exp1+'_'+yearS_4d+'01-'+yearE_4d+'12.nc')
pi_raw = f1(svar)
f1.close()
f2 = cdms.open(direc_data+fname2+'/'+svar+'_'+exp2+'_'+yearS_4d+'01-'+yearE_4d+'12.nc')
ab_raw = f2(svar)
f2.close()
# reverse lev direction
if svar in var3d:
pi_raw = pi_raw[:,::-1,:,:]
ab_raw = ab_raw[:,::-1,:,:]
#----------------------------------------------------------
# regrid data
#----------------------------------------------------------
pi_raw_grd = pi_raw.regrid(grid,regridTool='esmf',regridMethod='linear')
ab_raw_grd = ab_raw.regrid(grid,regridTool='esmf',regridMethod='linear')
print('pi_raw_grd.shape = ',pi_raw_grd.shape)
print('ab_raw_grd.shape = ',ab_raw_grd.shape)
dic_all[svar+'_ano'] = ab_raw_grd - pi_raw_grd
dic_all[svar+'_pi'] = pi_raw_grd
dic_all[svar+'_ab'] = ab_raw_grd
dic_all[svar+'_ano'].setAxisList(pi_raw_grd.getAxisList())
dic_all[svar+'_pi'].setAxisList(pi_raw_grd.getAxisList())
dic_all[svar+'_ab'].setAxisList(pi_raw_grd.getAxisList())
del(pi_raw, ab_raw, pi_raw_grd, ab_raw_grd)
else:
print('we dont find this variable:',svar,' in your file. Please check it!')
#----------------------------------------------------------
# calculate global mean surface air temperature anomaly
#----------------------------------------------------------
anomtas = cdutil.ANNUALCYCLE.climatology(dic_all['tas_ano']) #(12, 90, 144)
avgdtas = cdutil.averager(MV.average(anomtas,axis=0), axis='xy', weights='weighted') # (scalar)
print('avgdtas = ',avgdtas)
# get time-series of annual mean
dic_all2 = {}
for svar in dic_all.keys():
if '_ano' in svar:
cdutil.setTimeBoundsMonthly(dic_all[svar])
dic_all2[svar+'_ann'] = cdutil.YEAR(dic_all[svar])
tas_ano_ann_gm = cdutil.averager(dic_all2['tas_ano_ann'], axis='xy', weights='weighted')
#----------------------------------------------------------
# calculate climatological control state
#----------------------------------------------------------
dic_out = {}
for svar in dic_all.keys():
if '_pi' in svar:
dic_out[svar+'_clim'] = MV.average(dic_all[svar],axis=0)
if '_ab' in svar:
dic_out[svar+'_clim'] = MV.average(dic_all[svar],axis=0)
elif '_ano' in svar:
if 'coupled' not in case_stamp:
dic_out[svar+'_clim'] = MV.average(dic_all[svar],axis=0)/avgdtas
else:
print('doing regression...')
dic_out[svar+'_clim'],intercept = statistics.linearregression(dic_all2[svar+'_ann'], x=tas_ano_ann_gm)
print('dic_out.keys() = ',dic_out.keys())
#----------------------------------------------------------
# save data into file
#----------------------------------------------------------
value = 0
cdms.setNetcdfShuffleFlag(value) ## where value is either 0 or 1
cdms.setNetcdfDeflateFlag(value) ## where value is either 0 or 1
cdms.setNetcdfDeflateLevelFlag(value) ## where value is a integer between 0 and 9 included
fout = cdms.open(outfile,'w')
for svar in dic_out.keys():
print('svar = ', svar)
tmp = dic_out[svar]
fout.write(tmp, id = svar)
fout.comment = '_pi_clim is the climatological control state, _ab_clim is the climatological warming state and xxx_ano_clim is the anomaly normalized by global mean tas anomaly'
fout.close()