/
hiatus_b.py
278 lines (213 loc) · 7.79 KB
/
hiatus_b.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
from iris.coords import DimCoord
import iris.plot as iplt
import time
import numpy as np
import glob
import iris
import iris.coord_categorisation
import iris.analysis
import subprocess
import os
import iris.quickplot as qplt
import iris.plot as iplt
import matplotlib.pyplot as plt
import numpy.ma as ma
import running_mean as rm
import running_mean_post as rmp
from scipy import signal
import scipy
import scipy.stats
import numpy as np
import statsmodels.api as sm
import running_mean_post
from scipy.interpolate import interp1d
from datetime import datetime, timedelta
import cartopy.crs as ccrs
import iris.analysis.cartography
import numpy.ma as ma
import scipy.interpolate
import gc
import pickle
import biggus
import seawater
import cartopy.feature as cfeature
import scipy.signal
import iris.analysis.stats as istats
import matplotlib.cm as mpl_cm
def unique_models(all_files):
model = []
for file in all_files:
model.append(file.split('/')[-1].split('_')[0])
return np.unique(np.array(model))
def butter_bandpass(lowcut, cutoff):
order = 2
low = 1/lowcut
b, a = scipy.signal.butter(order, low , btype=cutoff,analog = False)
return b, a
def low_pass_filter(cube,limit_years):
b1, a1 = butter_bandpass(limit_years, 'low')
output = scipy.signal.filtfilt(b1, a1, cube,axis = 0)
return output
def high_pass_filter(cube,limit_years):
b1, a1 = butter_bandpass(limit_years, 'high')
output = scipy.signal.filtfilt(b1, a1, cube,axis = 0)
return output
directory = '/data/NAS-ph290/ph290/data_for_reynolds_v2/regridded/'
directory2 = '/media/usb_external1/cmip5/pr_regridded/'
files = glob.glob(directory+'*tas*r1i1p1*.nc')
models = unique_models(files)
variables = ['tas']
###
#read in data
###
models = list(models)
models.remove('CCSM4')
models.remove('CMCC-CESM')
models.remove('CMCC-CM')
models.remove('CMCC-CMS')
models.remove('CESM1-FASTCHEM')
models.remove('HadGEM2-CC')
#models.remove('MIROC-ESM')
models = np.array(models)
upper_limit_years = 30.0
lower_limit_years = 5.0
data = {}
for i,model in enumerate(models):
print 'testing: '+model
file1 = glob.glob(directory+model+'_*'+'tas'+'*r1i1p1_*.nc')
file2 = glob.glob(directory+model+'_*'+'tos'+'*r1i1p1_*.nc')
file3 = glob.glob(directory2+model+'_*'+'pr_piControl*.nc')
if ((np.size(file1) > 0) & (np.size(file2) > 0) & (np.size(file3) > 0)):
print 'processing: '+model
#tas
file = glob.glob(directory+model+'_*'+'tas'+'*r1i1p1_*.nc')
cube = iris.load_cube(file)
file = glob.glob(directory+model+'_*'+'tos'+'*r1i1p1_*.nc')
cube1 = iris.load_cube(file)
file = glob.glob(directory2+model+'_*'+'pr_piControl_*.nc')
cube2 = iris.load_cube(file)
coord = cube1.coord('time')
coord = cube.coord('time')
dt = coord.units.num2date(coord.points)
years = np.array([coord.units.num2date(value).year for value in coord.points])
dt = coord.units.num2date(coord.points)
years1 = np.array([coord.units.num2date(value).year for value in coord.points])
coord = cube2.coord('time')
dt = coord.units.num2date(coord.points)
years2 = np.array([coord.units.num2date(value).year for value in coord.points])
locs = np.in1d(years, years1)
locs = np.in1d(years2,years[locs])
cube = cube[locs]
cube1 = cube1[locs]
cube2 = cube2[locs]
try:
cube.coord('latitude').guess_bounds()
cube.coord('longitude').guess_bounds()
except:
print 'already have bounds'
grid_areas = iris.analysis.cartography.area_weights(cube)
ts = cube.collapsed(['latitude','longitude'],iris.analysis.MEAN,weights = grid_areas)
ts_filtered = ts.copy()
ts_filtered.data = high_pass_filter(ts_filtered.data,upper_limit_years)
ts_filtered.data = low_pass_filter(ts_filtered.data,lower_limit_years)
ts_filtered = ts_filtered[locs]
ts_filtered_2D = cube.copy()
ts_filtered_2D.data = np.swapaxes(np.swapaxes(np.tile(ts_filtered.data,[180,360,1]),1,2),0,1)
cube.data = high_pass_filter(cube.data,upper_limit_years)
cube.data = low_pass_filter(cube.data,lower_limit_years)
data[model] = {}
data[model]['tas'] = {}
data[model]['tas'] = istats.pearsonr(ts_filtered_2D,cube,corr_coords=['time'])
cube1.data = high_pass_filter(cube1.data,upper_limit_years)
cube1.data = low_pass_filter(cube1.data,lower_limit_years)
data[model]['tos'] = {}
data[model]['tos'] = istats.pearsonr(ts_filtered_2D,cube1,corr_coords=['time'])
cube2.data = high_pass_filter(cube2.data,upper_limit_years)
cube2.data = low_pass_filter(cube2.data,lower_limit_years)
data[model]['pr'] = {}
data[model]['pr'] = istats.pearsonr(ts_filtered_2D,cube2,corr_coords=['time'])
models2 = list(data.viewkeys())
models2.remove('HadGEM2-ES')
no_models = np.size(models2)
tas_mean = data[models2[0]]['tas'].copy()
tas_stdev = tas_mean.copy()
tos_mean = data[models2[0]]['tos'].copy()
tos_stdev = tos_mean.copy()
pr_mean = data[models2[0]]['pr'].copy()
pr_stdev = pr_mean.copy()
data_tmp1 = np.empty([no_models,np.shape(data[models2[0]]['tos'].data)[0],np.shape(data[models2[0]]['tos'].data)[1]])*0.0+np.NAN
data_tmp2 = data_tmp1.copy()
data_tmp3 = data_tmp1.copy()
for i,model in enumerate(models2):
data_tmp1[i,:,:] = data[model]['tas'].data
data_tmp2[i,:,:] = data[model]['tos'].data
data_tmp3[i,:,:] = data[model]['pr'].data
tas_mean.data = np.mean(data_tmp1,axis = 0)
tas_stdev.data = np.std(data_tmp1,axis = 0)
tos_mean.data = np.mean(data_tmp2,axis = 0)
tos_stdev.data = np.std(data_tmp2,axis = 0)
pr_mean.data = np.mean(data_tmp3,axis = 0)
pr_stdev.data = np.std(data_tmp3,axis = 0)
brewer_cmap = mpl_cm.get_cmap('brewer_RdBu_11')
plt.close('all')
figure(figsize=(12, 12)
fig, axes = plt.subplots(nrows=2, ncols=3)
plt.subplot(3, 2, 1)
cs1 = iplt.contourf(tos_mean,np.linspace(-1,1,21), cmap=brewer_cmap)
plt.gca().coastlines()
plt.title('SST (ens. mean)')
plt.subplot(3, 2, 2)
cs2 = iplt.contourf(tos_stdev,np.linspace(-1,1,21), cmap=brewer_cmap)
plt.gca().coastlines()
plt.title('SST (ens. stdev)')
plt.subplot(3, 2, 3)
cs3 = iplt.contourf(tas_mean,np.linspace(-1,1,21), cmap=brewer_cmap)
plt.gca().coastlines()
plt.title('surface T (ens. mean)')
plt.subplot(3, 2, 4)
cs4 = iplt.contourf(tas_stdev,np.linspace(-1,1,21), cmap=brewer_cmap)
plt.gca().coastlines()
plt.title('surface T (ens. stdev)')
fig.subplots_adjust(right=0.8)
cbar_ax1 = fig.add_axes([0.85, 0.35, 0.02, 0.6])
cbar1 = plt.colorbar(cs4,orientation='vertical', cax=cbar_ax1)
plt.subplot(3, 2, 5)
cs5 = iplt.contourf(pr_mean,np.linspace(-0.5,0.5,21), cmap=brewer_cmap)
plt.gca().coastlines()
plt.title('precipitation (ens. mean)')
plt.subplot(3, 2, 6)
cs6 = iplt.contourf(pr_stdev,np.linspace(-0.5,0.5,21), cmap=brewer_cmap)
plt.gca().coastlines()
plt.title('precipitation (ens. stdev)')
fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.11, 0.02, 0.2])
cbar = plt.colorbar(cs6,orientation='vertical', cax=cbar_ax)
plt.savefig('/home/ph290/Documents/figures/hiatus_correlations.pdf')
#plt.show(block = False)
plt.close('all')
plt.subplot(5, 6, 1)
for i,model in enumerate(data.viewkeys()):
plt.subplot(5, 6, i)
iplt.contourf(data[model]['tos'],np.linspace(-1,1,21))
plt.gca().coastlines()
plt.title(model)
plt.savefig('/home/ph290/Documents/figures/tos_correlations_b.png')
#plt.show(block = False)
plt.close('all')
plt.subplot(5,6, 1)
for i,model in enumerate(data.viewkeys()):
plt.subplot(5, 6, i)
iplt.contourf(data[model]['tas'],np.linspace(-1,1,21))
plt.gca().coastlines()
plt.title(model)
plt.savefig('/home/ph290/Documents/figures/tas_correlations_b.png')
#plt.show(block = False)
plt.close('all')
plt.subplot(5, 6, 1)
for i,model in enumerate(data.viewkeys()):
plt.subplot(5, 6, i)
iplt.contourf(data[model]['pr'],np.linspace(-1,1,21))
plt.gca().coastlines()
plt.title(model)
plt.savefig('/home/ph290/Documents/figures/pr_correlations_b.png')
#plt.show(block = False)