-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestCDOvspython.py
312 lines (252 loc) · 10.3 KB
/
testCDOvspython.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# @@@@ did not get far with this. continue tomorrow. use new NC reading funcs
#
import numpy as np # for array handling
import scipy as sp # scientific python
import matplotlib.pyplot as plt # for basic plotting
from netCDF4 import Dataset
import cccmautils as cutl
import cccmaNC as cnc
import cdo as cdo; cdo = cdo.Cdo()
import os
import cccmacmaps
# cdo bindings leave a lot of temp files
os.system('rm -rf /tmp/cdoPy*')
plt.close('all')
cutl = reload(cutl)
cnc = reload(cnc)
model = 'CanAM4'
ftype = 'ts' # timeseries
#ftype = 'climo' # 12-month climatology
timeavg = 'ANN'
# # # ########### set Simulations #############
# Control run
casename = 'kemctl1'
timstr = '001-061'
#timstr = '001-111'
styr = 2 # skip year 1
enyr = 61
# Pert run
casenamep1 = 'kem1pert1' # 2002-2012 sic and sit
casenamep2 = 'kem1pert2' # 2002-2012 sic, sit, adjusted sst
casenamep3 = 'kem1pert3' # 2002-2012 sic, adjusted sst. control sit
timstrp = '001-061'
#timstrp = '001-111'
styrp = 2 # skip year 1
enyrp = 61
#cmap = 'RdBu_r'
cmap = 'blue2red_20'
cmapclimo = 'Spectral_r'
conv = 1 # conversion factor to convert units, etc
# # # ######## set Field info ###############
field = 't'
ncfield = 'TEMP'
units = 'K' # @@
cminc = 200
cmaxc = 310
cmin = -.5
cmax = .5
cminm = -.8 # for monthly
cmaxm = .8 # for monthly
cminsc = -2.5 # as Screen et al paper
cmaxsc = 2.5 # as Screen et al paper
# # # ########## Read NC data ###############
plat = platform.system()
if plat == 'Darwin': # means I'm on my mac
basepath = '/Users/kelly/CCCma/CanSISE/RUNS/'
subdir = '/'
else: # on linux workstation in Vic
basepath = '/home/rkm/work/DATA/' + model + '/'
subdir = '/ts/'
fnamec = basepath + casename + subdir + casename + '_' + field + '_' + timstr + '_' + ftype + '.nc'
fnamep1 = basepath + casenamep1 + subdir + casenamep1 + '_' + field + '_' + timstrp + '_' + ftype + '.nc'
fnamep2 = basepath + casenamep2 + subdir + casenamep2 + '_' + field + '_' + timstrp + '_' + ftype + '.nc'
fnamep3 = basepath + casenamep3 + subdir + casenamep3 + '_' + field + '_' + timstrp + '_' + ftype + '.nc'
################################ VANILLA ######################################
ncfilec = Dataset(fnamec,'r') # control
#fldc = ncfilec.variables[ncfield][(styr-1)*12:(enyr*12+1),:,:,:]*conv
fldc = ncfilec.variables[ncfield][...]*conv
ncfilec.close()
# super-most vanilla
fldctmvan = np.average(fldc,0)
fldctmzmvan = np.average(fldctmvan,2)
# vanilla but remove extra lon
fldctmvanNOLON = fldctmvan[:,:,0:-1]
fldctmzmvanNOLON = np.average(fldctmvanNOLON,2)
# vanilla but time-weight
fldczmvan = np.average(fldc,3)
fldczmvanann = cutl.annualize_monthlyts(fldczmvan)
fldczmvananntm = np.average(fldczmvanann,0)
# vanilla but remove extra lon and time-weight
fldcNOLON = fldc[:,:,:,0:-1]
fldczmvanNOLON = np.average(fldcNOLON,3)
fldczmvanannNOLON = cutl.annualize_monthlyts(fldczmvanNOLON)
fldczmvananntmNOLON = np.average(fldczmvanannNOLON,0)
################################# PY/NC ########################################
lat = cnc.getNCvar(fnamec,'lat')
lev = cnc.getNCvar(fnamec,'plev')
#fldczm = cnc.getNCvar(fnamec,ncfield,calc='zm',timechunk=((styr-1)*12,enyr*12+1) )*conv
fldczm = cnc.getNCvar(fnamec,ncfield,calc='zm')*conv
#fldpzm = cnc.getNCvar(fnamep2,ncfield,calc='zm',timechunk=((styr-1)*12,enyr*12+1) )*conv
fldczmtm = np.mean(cutl.annualize_monthlyts(fldczm),0)
fldpzmtm = np.mean(cutl.annualize_monthlyts(fldpzm),0)
fldczmtm2 = np.average(fldczm,0)
fldpzmtm2 = np.average(fldpzm,0) # not month-weighted
################################# CDO ###########################################
## @@ test out cdo bindings and compare
# xm_fgco2 = cdo.zonmean( input = ifile ,returnMaArray ='fgco2' )
# OR: select out specific dates, time-mean then zonal mean
# xm_fgco2 = cdo.zonmean( input = cdo.timmean( input = cdo.seldate('1990-01-01,2005-12-31', input=ifile ) ) ,returnMaArray ='fgco2')
# this works: fldcselcdo = cdo.seldate('0002-01-01,0061-12-31', input = fnamec, returnArray = ncfield )
#fldctmcdo = np.squeeze(cdo.timmean(input = cdo.seldate('0002-01-01,0061-12-31', input = fnamec), returnArray = ncfield ))
# @@@@ move these to functions so can call os.system()
## fldczmcdo = np.squeeze(
## cdo.zonmean( input =
## cdo.timmean(input =
## cdo.seldate('0002-01-01,0061-12-31', input = fnamec ) ),
## returnMaArray = ncfield))
fldczmcdo = np.squeeze(
cdo.zonmean( input =
cdo.timmean(input = fnamec),returnMaArray = ncfield))
os.system('rm -rf /tmp/cdoPy*')
## fldpzmcdo = np.squeeze(
## cdo.zonmean( input =
## cdo.timmean(input =
## cdo.seldate('0002-01-01,0061-12-31', input = fnamep2 ) ),
## returnMaArray = ncfield))
lats,levs = np.meshgrid(lat,lev)
plotfld = fldczmtm - fldczmcdo
fig = plt.figure()
ax = fig.add_subplot(111)
plt.pcolormesh(lats,levs/100,plotfld,cmap= plt.cm.get_cmap(cmap),shading='gouraud',vmin=-.12,vmax=.12)
ax.set_xlim(-90,90)
ax.set_ylim(10,1000)
ax.invert_yaxis()
ax.set_yscale('log')
ax.set_yticks([1000,800, 500, 300, 100, 10])
ax.set_yticklabels((1000,800,500,300,100,10))
ax.set_title('Py/NC - CDO ' + ncfield + ' (' + units + ')')
cbar = plt.colorbar()
plotfld = fldczmtm2 - fldczmcdo
fig = plt.figure()
ax = fig.add_subplot(111)
plt.pcolormesh(lats,levs/100,plotfld,cmap= plt.cm.get_cmap(cmap),shading='gouraud',vmin=-.12,vmax=.12)
ax.set_xlim(-90,90)
ax.set_ylim(10,1000)
ax.invert_yaxis()
ax.set_yscale('log')
ax.set_yticks([1000,800, 500, 300, 100, 10])
ax.set_yticklabels((1000,800,500,300,100,10))
ax.set_title('Py/NC2 (unwgted) - CDO ' + ncfield + ' (' + units + ')')
cbar = plt.colorbar()
plotfld = fldctmzmvan - fldczmcdo
# this is the closest comparison. they are effectively the same.
fig = plt.figure()
ax = fig.add_subplot(111)
plt.pcolormesh(lats,levs/100,plotfld,cmap= plt.cm.get_cmap(cmap),shading='gouraud',vmin=-.12,vmax=.12)
ax.set_xlim(-90,90)
ax.set_ylim(10,1000)
ax.invert_yaxis()
ax.set_yscale('log')
ax.set_yticks([1000,800, 500, 300, 100, 10])
ax.set_yticklabels((1000,800,500,300,100,10))
ax.set_title('Most vanilla - CDO ' + ncfield + ' (' + units + ')')
cbar = plt.colorbar()
plotfld = fldczmvananntm - fldczmcdo
# time-weighting appears to matter quite a lot, unless annualize() is wrong...
# there are still other differences here
fig = plt.figure()
ax = fig.add_subplot(111)
plt.pcolormesh(lats,levs/100,plotfld,cmap= plt.cm.get_cmap(cmap),shading='gouraud',vmin=-.12,vmax=.12)
ax.set_xlim(-90,90)
ax.set_ylim(10,1000)
ax.invert_yaxis()
ax.set_yscale('log')
ax.set_yticks([1000,800, 500, 300, 100, 10])
ax.set_yticklabels((1000,800,500,300,100,10))
ax.set_title('Most vanilla (time-wgted) - CDO ' + ncfield + ' (' + units + ')')
cbar = plt.colorbar()
plotfld = fldczmvananntm - fldctmzmvan
# time-weighting appears to matter quite a lot, unless annualize() is wrong...
fig = plt.figure()
ax = fig.add_subplot(111)
plt.pcolormesh(lats,levs/100,plotfld,cmap= plt.cm.get_cmap(cmap),shading='gouraud',vmin=-.12,vmax=.12)
ax.set_xlim(-90,90)
ax.set_ylim(10,1000)
ax.invert_yaxis()
ax.set_yscale('log')
ax.set_yticks([1000,800, 500, 300, 100, 10])
ax.set_yticklabels((1000,800,500,300,100,10))
ax.set_title('Most vanilla (time-wgted) - Most vanilla ' + ncfield + ' (' + units + ')')
cbar = plt.colorbar()
plotfld = fldczmvananntm - fldczmtm
# differences here indicate that the "timechunk" isn't working as I think in getNCvar() --> nope, it's the extra longitude!
fig = plt.figure()
ax = fig.add_subplot(111)
plt.pcolormesh(lats,levs/100,plotfld,cmap= plt.cm.get_cmap(cmap),shading='gouraud',vmin=-.12,vmax=.12)
ax.set_xlim(-90,90)
ax.set_ylim(10,1000)
ax.invert_yaxis()
ax.set_yscale('log')
ax.set_yticks([1000,800, 500, 300, 100, 10])
ax.set_yticklabels((1000,800,500,300,100,10))
ax.set_title('Most vanilla (time-wgted) - Py/NC (tim-wgted)' + ncfield + ' (' + units + ')')
cbar = plt.colorbar()
# checking now whether extra longitude makes a noticeable difference. it does.
plotfld = fldctmzmvan - fldctmzmvanNOLON
fig = plt.figure()
ax = fig.add_subplot(111)
plt.pcolormesh(lats,levs/100,plotfld,cmap= plt.cm.get_cmap(cmap),shading='gouraud',vmin=-.12,vmax=.12)
ax.set_xlim(-90,90)
ax.set_ylim(10,1000)
ax.invert_yaxis()
ax.set_yscale('log')
ax.set_yticks([1000,800, 500, 300, 100, 10])
ax.set_yticklabels((1000,800,500,300,100,10))
ax.set_title('Most vanilla - Most vanilla NOLON' + ncfield + ' (' + units + ')')
cbar = plt.colorbar()
plotfld = fldczmvananntmNOLON - fldctmzmvan
fig = plt.figure()
ax = fig.add_subplot(111)
plt.pcolormesh(lats,levs/100,plotfld,cmap= plt.cm.get_cmap(cmap),shading='gouraud',vmin=-.12,vmax=.12)
ax.set_xlim(-90,90)
ax.set_ylim(10,1000)
ax.invert_yaxis()
ax.set_yscale('log')
ax.set_yticks([1000,800, 500, 300, 100, 10])
ax.set_yticklabels((1000,800,500,300,100,10))
ax.set_title('Most vanilla - Most vanilla (tim-wgted NOLON)' + ncfield + ' (' + units + ')')
cbar = plt.colorbar()
"""
# Get the data
ncfilec = Dataset(fnamec,'r') # control
ncfilep1 = Dataset(fnamep1,'r') # pert1
ncfilep2 = Dataset(fnamep2,'r') # pert2
ncfilep3 = Dataset(fnamep3,'r') # pert3
lat = ncfilec.variables['lat'][:]
lon = ncfilec.variables['lon'][:]
lev = ncfilec.variables['plev'][:]
if ftype == 'ts':
fldc = ncfilec.variables[ncfield][(styr-1)*12:(enyr*12+1),:,:,:]*conv # time start year to end
#fldp1 = ncfilep1.variables[ncfield][(styrp-1)*12:(enyrp*12+1),:,:,:]*conv # time start year to end
fldp2 = ncfilep2.variables[ncfield][(styrp-1)*12:(enyrp*12+1),:,:,:]*conv # time start year to end
#fldp3 = ncfilep3.variables[ncfield][(styrp-1)*12:(enyrp*12+1),:,:,:]*conv # time start year to end
else:
fldc = ncfilec.variables[ncfield][...]*conv
fldp1 = ncfilep1.variables[ncfield][...]*conv
fldp2 = ncfilep2.variables[ncfield][...]*conv # time start year to end
fldp3 = ncfilep3.variables[ncfield][...]*conv # time start year to end
nt,nlev,nlat,nlon = fldc.shape # if nt == 12 then it's a climo
# ############# set which perturbation run! ######
casenamep = casenamep2
fldp = fldp2
# ###############################################
# take zonal mean first
# for zonal mean, remove the wraparound lon at the end
fldc = np.squeeze(fldc[:,:,:,0:-1])
fldp = np.squeeze(fldp[:,:,:,0:-1])
fldczm = np.mean(fldc,3)
fldpzm = np.mean(fldp,3)
# seasonalize
seasfldczm = cutl.seasonalize_monthlyts(fldczm,timeavg)
seasfldpzm = cutl.seasonalize_monthlyts(fldpzm,timeavg)
"""