-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathntiris.py
266 lines (230 loc) · 8.55 KB
/
ntiris.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
import iris
import numpy as np
import iris.coord_categorisation
import iris.plot as iplt
import iris.quickplot as qplt
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import scipy.stats as stats
import sys
def bc(aux_cube, dim_cube, comparable_coord):
if type(aux_cube.coord(comparable_coord)) is not iris.coords.AuxCoord:
raise TypeError
if type(dim_cube.coord(comparable_coord)) is not iris.coords.AuxCoord:
raise TypeError
aux_cube_coord = aux_cube.coord(comparable_coord)
dim_cube_coord = dim_cube.coord(comparable_coord)
aux_cube_dim, = aux_cube.coord_dims(aux_cube_coord)
dim_cube_dim, = aux_cube.coord_dims(dim_cube_coord)
s_aux_cube = [slice(None)]*len(aux_cube.shape)
s_aux_cube[aux_cube_dim] = 0
s_dim_cube = [slice(None)]*len(dim_cube.shape)
s_dim_cube[dim_cube_dim] = 0
a = aux_cube[tuple(s_aux_cube)]
a.attributes = None
a.cell_methods = None
b = dim_cube[tuple(s_dim_cube)]
b.attributes = None
b.cell_methods = None
if not a.is_compatible(b):
iris.util.describe_diff(a, b)
raise RuntimeError("Cubes are not compatible")
ind = []
for p in aux_cube.coord(comparable_coord).points:
i = np.where(dim_cube.coord(comparable_coord).points == p)
ind.append(i[0][0])
s = [slice(None)]*len(dim_cube.shape)
s[dim_cube_dim] = ind
new_data = dim_cube.data[tuple(s)]
new_cube = aux_cube.copy()
new_cube.data = new_data
new_cube.history = "%s comparable to %s in terms of %s" % (dim_cube.name(),
aux_cube.name(),
comparable_coord)
return new_cube
def remove_seascyc(cube, time_name='time'):
"""
Remove seasonal cycle from montly timeseries
Input: cube, time_name='time'
Output: cube_rsc
"""
try:
cube.coord('t').standard_name = 'time'
except:
pass
try:
iris.coord_categorisation.add_month_number(cube, time_name, 'month_number')
except:
pass
else:
print "month_number added as coord"
cube_mean = cube.collapsed(time_name,iris.analysis.MEAN)
cube_anom = cube-cube_mean
cube_mon_mean = cube_anom.aggregated_by('month_number', iris.analysis.MEAN)
seasonal_cycle = bc(cube_anom, cube_mon_mean, 'month_number')
cube_rsc = cube_anom - seasonal_cycle
return cube_rsc
def enscyc_ag(cube):
ens = np.tile(np.linspace(1,48,48),24)
#trim cube
cube = cube[0:ens.shape[0]]
cube.coord('month_number').long_name = '48_months'
cube.coord('48_months').points = ens
m48 = cube.aggregated_by('48_months',iris.analysis.MEAN)
return m48
def composite_m48(cube_name, ncfile_path='/home/nicholat/project/pacemaker/ncfiles/',notanom=False):
"""
Create a 48 month long cube, for use with
pacemaker 4 yr oscillating runs
Input: cube name (string), ncfilepath
"""
cube = iris.load_cube(ncfile_path+cube_name)
try:
cube.coord('t').standard_name='time'
except:
pass
else:
print "t coord changed to time"
if notanom:
cube_rsc = cube
iris.coord_categorisation.add_month_number(cube_rsc, 'time', 'month_number')
else:
cube_rsc = remove_seascyc(cube)
cube_m48 = enscyc_ag(cube_rsc)
cube_m48.long_name = cube.long_name
if notanom:
new_name = cube_name[:-2]+'m48.abs.nc'
else:
new_name = cube_name[:-2]+'m48.nc'
iris.save(cube_m48,ncfile_path+new_name)
return cube_m48, cube_rsc, cube
# u_cube = composite_m48('u.thlev.4ysl.fix.nc')
# rhum_cube, rhum_rsc, cube = composite_m48('rhum.plv.4ysl.nc',notanom=True)
#
# sys.exit('all done')
# temp_cube, temp_rsc, cube = composite_m48('temp.plv.4ysl.nc',notanom=True)
# gpht_cube = composite_m48('gpht.plv.4ysl.nc')
# rh_cube = composite_m48('rhum.plv.4ysl.nc')
# p_cube = composite_m48('pres.sfc.4ysl.nc')
# v_cube = composite_m48('v.plev.4ysl.nc')
# u_cube = composite_m48('u.plev.4ysl.nc')
# w_cube = composite_m48('w.thlev.4ysl.fix.nc')
# composite_m48('lwflux.clsky.sfc.4ysl.nc')
# composite_m48('lhf.sfc.4ysl.nc')
# composite_m48('dlwr.sfc.4ysl.nc')
# composite_m48('dswr.sfc.4ysl.nc')
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
"""
cube.coord('latitude').guess_bounds()
cube.coord('longitude').guess_bounds()
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 nino3_plot(cube):
"""
Plots the nino 3 timeseries for cube, surface temp wold make sense
Input: Iris cube (sfc temp)
Output: plot of nino3 timeseries, and cube of same thing
"""
try:
cube.coord('t').standard_name='time'
except:
pass
else:
print "t coord changed to time"
cube_rsc = remove_seascyc(cube)
loni = 210; lonf = 270; lati = -5; latf = 5
# latlon = [-5,5,210,270] #for data from 0 - 360 degress
nino3, nino3_mean = regmean(cube_rsc,loni,lonf,lati,latf)
plt.ion()
plt.clf()
qplt.plot(nino3_mean[:,0])
plt.title('NINO3 timeseries')
return nino3, nino3_mean
def linregts(cube1,cube2,name1='name1',name2='name2',ncfile_path='/home/nicholat/project/pacemaker/ncfiles/'):
"""
Calculate the regression between a 2d map and a 1d timeseries
i.e. regress v onto T_sfc_Aus
Input: cube_map, cube_ts (time coord only), name1, name2
Output: reg_cube, cor_cube
"""
numstats = 5 # number of stats returned for linregress
linreg_map = np.zeros(cube1.shape[-2::])
cor_map = np.zeros(cube1.shape[-2::])
pval_map = np.zeros(cube1.shape[-2::])
# cor_map = np.zeros(copy_cube.shape)
# print('Linreg/Cor map for '+name1+' and '+name2)
regress_ts = cube2.data
for nlat, lat in enumerate(cube1.coord('latitude')):
for nlon, lon in enumerate(cube1.coord('longitude')):
# get the sfc temp timeseries at each lat, lon.
var1 = cube1.extract(iris.Constraint(latitude=lat.points[0],longitude=lon.points[0])).data
# var2 = cube2.extract(iris.Constraint(latitude=lat.points[0],longitude=lon.points[0])).data
linreg = stats.linregress(regress_ts,var1)
linreg_map[nlat,nlon] = linreg[0]
cor_map[nlat,nlon] = linreg[2]
pval_map[nlat,nlon] = linreg[3]
if cube1.ndim==4:
reg_cube = cube1[0,0,::].copy()
elif cube1.ndim==3:
reg_cube = cube1[0,::].copy()
reg_cube.data[:] = linreg_map
reg_cube.long_name = 'Lin Regression '+ name1 +' '+ name2
reg_cube.units = 'no_unit'
reg_cube.attributes['title'] = 'Lin Regression '+ name1 +' '+ name2
reg_cube.attributes['name'] = 'reg'
try:
reg_cube.remove_coord('surface')
except:
pass
try:
reg_cube.remove_coord('time')
except:
pass
# iris.save(reg_cube,ncfile_path+'lreg.4ysl.'+name1+'.'+name2+'.nc')
if cube1.ndim==4:
cor_cube = cube1[0,0,::].copy()
elif cube1.ndim==3:
cor_cube = cube1[0,::].copy()
cor_cube.data[:] = cor_map
cor_cube.long_name = 'Correlation '+ name1 +' '+ name2
cor_cube.units = 'no_unit'
cor_cube.attributes['title'] = 'Correlation '+ name1 +' '+ name2
cor_cube.attributes['name'] = 'r_val'
try:
cor_cube.remove_coord('surface')
except:
pass
try:
cor_cube.remove_coord('time')
except:
pass
# iris.save(cor_cube,ncfile_path+'cor.4ysl.'+name1+'.'+name2+'.nc')
if cube1.ndim==4:
pval_cube = cube1[0,0,::].copy()
elif cube1.ndim==3:
pval_cube = cube1[0,::].copy()
pval_cube.data[:] = pval_map
pval_cube.long_name = 'Pval '+ name1 +' '+ name2
pval_cube.units = 'no_unit'
pval_cube.attributes['title'] = 'Pval '+ name1 +' '+ name2
pval_cube.attributes['name'] = 'p_val'
try:
pval_cube.remove_coord('surface')
except:
pass
try:
pval_cube.remove_coord('time')
except:
pass
# iris.save(pval_cube,ncfile_path+'pval.4ysl.'+name1+'.'+name2+'.nc')
return reg_cube, cor_cube, pval_cube