-
Notifications
You must be signed in to change notification settings - Fork 0
/
regional_response.py
203 lines (176 loc) · 8.12 KB
/
regional_response.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
import numpy as np
import numpy.ma as ma
import iris as iris
import iris.plot as iplt
import iris.quickplot as qplt
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import mycmaps as mc
import scipy.stats as stats
import sys
import troposave as ta
import prettyplotlib as ppl
import pickle
"""
Define many regions, based on surface temperature response (or moisture?)
get the area mean value in these regions for various variables
plot... in some useful mannner tbc
"""
def regmean(cube,loni,lonf,lati,latf):
""" Define a region and get the area weighted mean
Input: cube, lon_i, lon_f, lat_i, lat_f
Output: cube_reg, cube_regmean
"""
region = iris.Constraint(longitude=lambda l: (loni <= l <= lonf), latitude = lambda l: (lati <= l <= latf))
cube_reg = cube.extract(region)
grid_areas = iris.analysis.cartography.area_weights(cube_reg)
cube_regmean = cube_reg.collapsed(['latitude', 'longitude'],
iris.analysis.MEAN,
weights=grid_areas)
# print 'mean sfc '+str(cube_regmean[0].data)
# print 'mean p=10 '+str(cube_regmean[10].data)
return cube_reg, cube_regmean
def regions(cube,clim=False,maxf=True):
mons = 4
lag = 1
max_i = 35 + lag
max_f = max_i + mons
min_i = 11 + lag
min_f = min_i + mons
if maxf:
forc_i = max_i
forc_f = max_f
else:
forc_i = min_i
forc_f = min_f
if clim:
cube_max = cube.collapsed('time',iris.analysis.MEAN)
else:
cube_max = cube[forc_i:forc_f,:,::].collapsed('time',iris.analysis.MEAN)
# cube_anom = cube-cube_mean
cube_max.coord('latitude').guess_bounds()
cube_max.coord('longitude').guess_bounds()
print cube_max.shape
# ---------- define sst/tland----------
lsmask = iris.load_cube(ncfile_path + 'lsmask.nc')[0,0,::]
landmask = ~(ma.make_mask(lsmask.data.copy()) + np.zeros(cube_max.shape)).astype(bool) # mask sea, show land
seamask = (ma.make_mask(lsmask.data.copy()) + np.zeros(cube_max.shape)).astype(bool) # mask land, show sea
Cocean = cube_max.copy()
Cland = cube_max.copy()
Cocean.data = ma.array(Cocean.data, mask=seamask)
Cland.data = ma.array(Cland.data, mask=landmask)
# --------------
land_cube = Cland
India, India_mean = regmean(land_cube,loni=60,lonf=75,lati=0,latf=30)
MC, MC_mean = regmean(land_cube,loni=90,lonf=140,lati=-10,latf=10)
TropSthAm, TropSthAm_mean = regmean(land_cube,loni=290,lonf=315,lati=-23,latf=0)
SthSthAm, SthSthAm_mean = regmean(land_cube,loni=270,lonf=315,lati=-60,latf=-24)
NthWestAfr, NthWestAfr_mean = regmean(land_cube,loni=-15,lonf=15,lati=10,latf=30)
NthEastAfr, NthEastAfr_mean = regmean(land_cube,loni=15,lonf=50,lati=10,latf=30)
TropAfr, TropAfr_mean = regmean(land_cube,loni=12,lonf=40,lati=-15,latf=5)
SthAfr, SthAfr_mean = regmean(land_cube,loni=12,lonf=40,lati=-35,latf=-15)
Aus, Aus_mean = regmean(land_cube,loni=120,lonf=140,lati=-30,latf=-17)
lat10, lat10_mean = regmean(land_cube,loni=0,lonf=360,lati=-10,latf=10)
latp20, latp20_mean = regmean(land_cube,loni=0,lonf=360,lati=10,latf=20)
latm20, latm20_mean = regmean(land_cube,loni=0,lonf=360,lati=-20,latf=-10)
latp30, latp30_mean = regmean(land_cube,loni=0,lonf=360,lati=20,latf=30)
latm30, latm30_mean = regmean(land_cube,loni=0,lonf=360,lati=-30,latf=-20)
# lat20mean = latp20
return [India_mean , MC_mean , TropSthAm_mean , SthSthAm_mean , NthWestAfr_mean , NthEastAfr_mean, TropAfr_mean, SthAfr_mean, Aus_mean, lat10_mean, latp20_mean, latm20_mean, latp30_mean, latm30_mean]
ncfile_path = '/home/nicholat/project/pacemaker/ncfiles/'
temp_plv = iris.load_cube(ncfile_path + 'temp.plv.4ysl.m48.nc')
temp_reg = regions(temp_plv)
temp_sfc = iris.load_cube(ncfile_path + 'temp.sfc.4ysl.m48.nc')
stemp_reg = regions(temp_sfc)
rhum_plv = iris.load_cube(ncfile_path + 'rhum.plv.4ysl.m48.nc')
rhum_reg = regions(rhum_plv)
smc = iris.load_cube(ncfile_path + 'smc.sfc.4ysl.m48.nc')
smc_clim_reg = regions(smc,clim=True)
smc_reg = regions(smc)
dlwr = iris.load_cube(ncfile_path + 'dlwr.sfc.4ysl.m48.nc')
dlwr_reg = regions(dlwr)
dswr = iris.load_cube(ncfile_path + 'dswr.sfc.4ysl.m48.nc')
dswr_reg = regions(dswr)
precip = iris.load_cube(ncfile_path + 'precip.4ysl.m48.nc')
precip_reg = regions(precip)
cld = iris.load_cube(ncfile_path + 'cld.thlev.4ysl.m48.nc')
cld_reg = regions(cld)
u_thlv = iris.load_cube(ncfile_path + 'u.thlev.4ysl.fix.m48.nc')
u_reg = regions(u_thlv)
v_thlv = iris.load_cube(ncfile_path + 'v.thlev.4ysl.m48.nc')
v_thlv = v_thlv.regrid(u_thlv,iris.analysis.Linear())
v_reg = regions(v_thlv)
lhf = iris.load_cube(ncfile_path + 'lhf.sfc.4ysl.m48.nc')
lhf_reg = regions(lhf)
shf = iris.load_cube(ncfile_path + 'shf.sfc.4ysl.m48.nc')
shf_reg = regions(shf)
high = iris.Constraint(atmosphere_hybrid_height_coordinate = lambda h: 5000 <= h <= 15000)
low = iris.Constraint(atmosphere_hybrid_height_coordinate = lambda h: 0 <= h <= 5000)
full = iris.Constraint(atmosphere_hybrid_height_coordinate = lambda h: 0 <= h <= 10000)
cld_high_reg = {}
cld_low_reg = {}
for n, i in enumerate(cld_reg):
i.coord('Hybrid height').standard_name = 'atmosphere_hybrid_height_coordinate'
cld_high_reg[n] = i.extract(high).collapsed('atmosphere_hybrid_height_coordinate',iris.analysis.MEAN)
cld_low_reg[n] = i.extract(low).collapsed('atmosphere_hybrid_height_coordinate',iris.analysis.MEAN)
temp_700_reg = {}
temp_300_reg = {}
for n, i in enumerate(temp_reg):
temp_700_reg[n] = i.extract(iris.Constraint(p=700))
temp_300_reg[n] = i.extract(iris.Constraint(p=300))
u_high_reg = {}
u_low_reg = {}
for n, i in enumerate(u_reg):
# i.coord('Hybrid height').standard_name = 'atmosphere_hybrid_height_coordinate'
u_high_reg[n] = i.extract(high).collapsed('atmosphere_hybrid_height_coordinate',iris.analysis.MEAN)
u_low_reg[n] = i.extract(low).collapsed('atmosphere_hybrid_height_coordinate',iris.analysis.MEAN)
v_high_reg = {}
v_low_reg = {}
for n, i in enumerate(v_reg):
i.coord('Hybrid height').standard_name = 'atmosphere_hybrid_height_coordinate'
v_high_reg[n] = i.extract(high).collapsed('atmosphere_hybrid_height_coordinate',iris.analysis.MEAN)
v_low_reg[n] = i.extract(low).collapsed('atmosphere_hybrid_height_coordinate',iris.analysis.MEAN)
rhum_700_reg = {}
rhum_300_reg = {}
for n, i in enumerate(rhum_reg):
rhum_700_reg[n] = i.extract(iris.Constraint(p=700))
rhum_300_reg[n] = i.extract(iris.Constraint(p=300))
# Make array of variables var x reg
regarr= np.zeros((14,np.shape(temp_reg)[0]))
for n in xrange(np.shape(temp_reg)[0]):
print n
regarr[0,n] = stemp_reg[n].data[0]
regarr[1,n] = smc_clim_reg[n].data[0]
regarr[2,n] = temp_700_reg[n].data
regarr[3,n] = temp_300_reg[n].data
regarr[4,n] = rhum_700_reg[n].data
regarr[5,n] = rhum_300_reg[n].data
regarr[6,n] = dlwr_reg[n].data[0]
regarr[7,n] = dswr_reg[n].data[0]
regarr[8,n] = smc_reg[n].data[0]
regarr[9,n] = cld_high_reg[n].data
regarr[10,n] = cld_low_reg[n].data
regarr[11,n] = precip_reg[n].data[0]
regarr[12,n] = lhf_reg[n].data
regarr[13,n] = shf_reg[n].data
# regarr[12,n] = u_high_reg[n].data
# regarr[13,n] = u_low_reg[n].data
# regarr[14,n] = v_high_reg[n].data
# regarr[15,n] = v_low_reg[n].data
with open('./pickles/regarr.pickle','wb') as f:
pickle.dump(regarr,f)
for n in xrange(regarr.shape[0]):
regarr[n,:] = regarr[n,:]/(regarr[n,:].std())
var = np.array(['Tsfc','smc clim','T700hPa','T300hpa','RH700hPa','RH300hPa','DLWR','DSWR','smc','Cld High','Cld Low','Precip','lhf','shf']) #,'u_high','u_low','v_high','v_low'])
regs =np.array(['India','MC','TropSthAm','SthSthAm','NthWestAfr','NthEastAfr','TropAfr','SthAfr','Aus','lat10', 'latN20', 'latS20', 'latN30', 'latS30'])
plt.close('all')
fig, axes = plt.subplots(nrows=1) #,ncols=2)
fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.02, 0.7])
ppl.pcolormesh(fig, axes, regarr, ax_colorbar=cbar_ax, yticklabels=var, xticklabels=regs,vmin=-2.6,vmax=2.6)
axes.set_xlabel('Regions')
axes.set_ylabel('variables')
axes.set_title('Response to Max forcing')
plt.show()
fig.set_size_inches(18,5)
plt.savefig('./figures/regional_response_manyvar_max.eps')