-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_JULES.py
executable file
·401 lines (401 loc) · 11.8 KB
/
data_JULES.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# Python script to analyse output from a JULES run
#
# python JULES_map_netCDF_v3.py N Y 0.5 225 GRADS
#
import os
import numpy as np
from numpy import arange,dtype
#
import data_netCDF
#
# ##################################################################################################
# (1) JULES offline - get variables in dataset
# ##################################################################################################
#
def get_JULES_variables(FILE_CDF_IN,JULES_VARINFO):
#
VAR_NAMES = data_netCDF.data_netCDF_getVARNAMES(FILE_CDF_IN)
#
for iVAR in range(len(VAR_NAMES)):
VAR_NAME = VAR_NAMES[iVAR]
VAR_NAME_MOD = VAR_NAME.lower()
# print iVAR,VAR_NAMES[iVAR],JULES_VARINFO[VAR_NAME_MOD][1]
TEXT = ('%4d %-20s spin-up diagnostic: %s' \
% (iVAR,VAR_NAMES[iVAR],JULES_VARINFO[VAR_NAME_MOD][1]))
print(TEXT)
#
# Select variable
print 'Input index for variable (select those with Y for valid spin-up diagnostic): '
iVAR = int(input())
VAR_NAME = VAR_NAMES[iVAR]
#
# Assign attributes
#
NLEVELS = 10
iFLAG = 1
iSOIL = -1
VAR_NAME_MOD = VAR_NAME.lower()
PLOT_LABEL = JULES_VARINFO[VAR_NAME_MOD][5]+JULES_VARINFO[VAR_NAME_MOD][6]
PLOT_MIN = float(JULES_VARINFO[VAR_NAME_MOD][3])
PLOT_MAX = float(JULES_VARINFO[VAR_NAME_MOD][4])
PLOT_INC = (PLOT_MAX-PLOT_MIN)/NLEVELS
CLEVELS = arange(PLOT_MIN,PLOT_MAX+PLOT_INC,PLOT_INC)
#
print JULES_VARINFO[VAR_NAME_MOD][2]
#
if 'z' in JULES_VARINFO[VAR_NAME_MOD][2]:
print 'Input soil level (1-4): '
iSOIL = int(input())-1
#
return VAR_NAME,VAR_NAME_MOD,PLOT_LABEL,PLOT_MIN,PLOT_MAX,PLOT_INC,CLEVELS,iSOIL,iFLAG
#
# ##################################################################################################
# (2.1) JULES offline - single global netCDF dataset
# ##################################################################################################
#
# ##################################################################################################
# (2.2) JULES offline - multi-annual global netCDF datasets
# ##################################################################################################
#
def get_JULES_data_global_multi(NYEARS,START_YEAR,FILE_PREFIX,FILE_SUFFIX,JULES_VARINFO, \
MISS_DATA,iFLAG,DEBUG):
#
COUNT = 0
#
for iYEAR in range(NYEARS):
#
SYEAR = ('%4s' % (START_YEAR+iYEAR))
FILE_CDF_IN = FILE_PREFIX+SYEAR+FILE_SUFFIX
print ('%3d: ' % iYEAR)+FILE_CDF_IN
#
if iFLAG == 0:
VAR_NAME,VAR_NAME_MOD,PLOT_LABEL,PLOT_MIN, \
PLOT_MAX,PLOT_INC,CLEVELS,iSOIL,iFLAG= \
get_JULES_variables(FILE_CDF_IN,JULES_VARINFO)
#
# Skip if file missing
#
if os.path.exists(FILE_CDF_IN):
if os.path.getsize(FILE_CDF_IN) > 0:
#
DIMS,VAR_DATA = \
data_netCDF.data_netCDF_array_var(FILE_CDF_IN,VAR_NAME)
if DEBUG == 'Y':
print VAR_DATA.shape,VAR_DATA.min(),VAR_DATA.max()
#
if iYEAR == 0:
NTIMES = DIMS[0]*NYEARS
DIMS_MOD = [NTIMES]
for i in range(1,len(DIMS)):
DIMS_MOD.append(DIMS[i])
VAR_DATA_ALL = np.zeros(DIMS_MOD)
#
NCOUNT = DIMS[0]
#
if len(DIMS) == 2:
VAR_DATA_ALL[COUNT:COUNT+NCOUNT,:] = VAR_DATA[:,:]
elif len(DIMS) == 3:
VAR_DATA_ALL[COUNT:COUNT+NCOUNT,:,:] = VAR_DATA[:,:,:]
elif len(DIMS) == 4:
VAR_DATA_ALL[COUNT:COUNT+NCOUNT,:,:,:] = VAR_DATA[:,:,:,:]
#
else:
#
if len(DIMS) == 2:
VAR_DATA_ALL[COUNT:COUNT+NCOUNT,:] = MISS_DATA
elif len(DIMS) == 3:
VAR_DATA_ALL[COUNT:COUNT+NCOUNT,:,:] = MISS_DATA
elif len(DIMS) == 4:
VAR_DATA_ALL[COUNT:COUNT+NCOUNT,:,:,:] = MISS_DATA
#
# Increment as NCOUNT timepoints
#
COUNT += NCOUNT
#
VAR_DATA_ALL = np.squeeze(VAR_DATA_ALL)
#
# Return to calling routine
#
return VAR_DATA_ALL,VAR_NAME,VAR_NAME_MOD,PLOT_LABEL,PLOT_MIN,PLOT_MAX,PLOT_INC,CLEVELS,iSOIL
#
# ##################################################################################################
#
def get_JULES_data_global_multi2(NYEARS,START_YEAR,FILE_PREFIX,FILE_SUFFIX,VAR_NAME, \
MISS_DATA,DEBUG):
#
COUNT = 0
#
for iYEAR in range(NYEARS):
#
SYEAR = ('%4s' % (START_YEAR+iYEAR))
FILE_CDF_IN = FILE_PREFIX+SYEAR+FILE_SUFFIX
print ('%3d: ' % iYEAR)+FILE_CDF_IN
#
# Skip if file missing
#
if os.path.exists(FILE_CDF_IN):
if os.path.getsize(FILE_CDF_IN) > 0:
#
DIMS,VAR_DATA = \
data_netCDF.data_netCDF_array_var(FILE_CDF_IN,VAR_NAME)
if DEBUG == 'Y':
print VAR_DATA.shape,VAR_DATA.min(),VAR_DATA.max()
#
if iYEAR == 0:
NTIMES = DIMS[0]*NYEARS
DIMS_MOD = [NTIMES]
for i in range(1,len(DIMS)):
DIMS_MOD.append(DIMS[i])
VAR_DATA_ALL = np.zeros(DIMS_MOD)
#
NCOUNT = DIMS[0]
#
if len(DIMS) == 2:
VAR_DATA_ALL[COUNT:COUNT+NCOUNT,:] = VAR_DATA[:,:]
elif len(DIMS) == 3:
VAR_DATA_ALL[COUNT:COUNT+NCOUNT,:,:] = VAR_DATA[:,:,:]
elif len(DIMS) == 4:
VAR_DATA_ALL[COUNT:COUNT+NCOUNT,:,:,:] = VAR_DATA[:,:,:,:]
#
else:
#
if len(DIMS) == 2:
VAR_DATA_ALL[COUNT:COUNT+NCOUNT,:] = MISS_DATA
elif len(DIMS) == 3:
VAR_DATA_ALL[COUNT:COUNT+NCOUNT,:,:] = MISS_DATA
elif len(DIMS) == 4:
VAR_DATA_ALL[COUNT:COUNT+NCOUNT,:,:,:] = MISS_DATA
#
# Increment as NCOUNT timepoints
#
COUNT += NCOUNT
#
VAR_DATA_ALL = np.squeeze(VAR_DATA_ALL)
#
# Return to calling routine
#
return VAR_DATA_ALL
#
# ##################################################################################################
# (2.3) JULES offline - single multi-annual regional netCDF datasets
# ##################################################################################################
#
def get_JULES_data_regional_single(NREGIONS,NPOINTS,FILE_PREFIX,FILE_SUFFIX,JULES_VARINFO, \
MISS_DATA,iFLAG,DEFAULT,DEBUG):
#
COUNT = 0
#
for iREGION in range(NREGIONS):
#
if 'm46' in FILE_PREFIX:
FILE_CDF_IN = FILE_PREFIX+('%04d' % (iREGION+1))+FILE_SUFFIX
elif 'g03' in FILE_PREFIX:
FILE_CDF_IN = FILE_PREFIX+('%s' % iREGION)+FILE_SUFFIX
print ('%3d %6d: ' % (iREGION,COUNT))+FILE_CDF_IN
#
if iFLAG == 0:
VAR_NAME,VAR_NAME_MOD,PLOT_LABEL,PLOT_MIN, \
PLOT_MAX,PLOT_INC,CLEVELS,iSOIL,iFLAG= \
get_JULES_variables(FILE_CDF_IN,JULES_VARINFO)
#
# Skip if file missing
#
if os.path.exists(FILE_CDF_IN):
if os.path.getsize(FILE_CDF_IN) > 0:
#
DIMS,VAR_DATA = \
data_netCDF.data_netCDF_array_var(FILE_CDF_IN,VAR_NAME)
if DEBUG == 'Y' and len(VAR_DATA) == 1:
print VAR_DATA.shape,VAR_DATA.min(),VAR_DATA.max()
#
if iREGION == 0:
DIMS_MOD = []
for i in range(0,len(DIMS)-1):
DIMS_MOD.append(DIMS[i])
DIMS_MOD.append(NPOINTS)
VAR_DATA_ALL = np.zeros(DIMS_MOD)
#
if len(DIMS) == 1:
NCOUNT = DEFAULT
else:
NCOUNT = DIMS[-1]
#
if len(DIMS) == 2:
VAR_DATA_ALL[:,COUNT:COUNT+NCOUNT] = VAR_DATA[:,:]
elif len(DIMS) == 3:
VAR_DATA_ALL[:,:,COUNT:COUNT+NCOUNT] = VAR_DATA[:,:,:]
elif len(DIMS) == 4:
VAR_DATA_ALL[:,:,:,COUNT:COUNT+NCOUNT] = VAR_DATA[:,:,:,:]
#
if not os.path.exists(FILE_CDF_IN) or os.path.getsize(FILE_CDF_IN) <= 0:
#
if len(DIMS) == 2:
VAR_DATA_ALL[:,COUNT:COUNT+NCOUNT] = MISS_DATA
elif len(DIMS) == 3:
VAR_DATA_ALL[:,:,COUNT:COUNT+NCOUNT] = MISS_DATA
elif len(DIMS) == 4:
VAR_DATA_ALL[:,:,:,COUNT:COUNT+NCOUNT] = MISS_DATA
#
print iREGION,COUNT,NCOUNT,NPOINTS
#
# Increment regions
#
COUNT += NCOUNT
#
VAR_DATA_ALL = np.squeeze(VAR_DATA_ALL)
print VAR_DATA_ALL.shape
#
# Return to calling routine
#
return VAR_DATA_ALL,VAR_NAME,VAR_NAME_MOD,PLOT_LABEL,PLOT_MIN,PLOT_MAX,PLOT_INC,CLEVELS,iSOIL
#
# ##################################################################################################
#
def get_JULES_data_regional_single2(NREGIONS,NPOINTS,FILE_PREFIX,FILE_SUFFIX,VAR_NAME, \
MISS_DATA,DEFAULT,DEBUG):
#
COUNT = 0
#
for iREGION in range(NREGIONS):
#
if 'm46' in FILE_PREFIX or 'MAMM' in FILE_PREFIX:
FILE_CDF_IN = FILE_PREFIX+('%04d' % (iREGION+1))+FILE_SUFFIX
elif 'g03' in FILE_PREFIX:
FILE_CDF_IN = FILE_PREFIX+('%s' % iREGION)+FILE_SUFFIX
print ('%3d %6d: ' % (iREGION,COUNT))+FILE_CDF_IN
#
# Skip if file missing
#
if os.path.exists(FILE_CDF_IN):
if os.path.getsize(FILE_CDF_IN) > 0:
#
DIMS,VAR_DATA = \
data_netCDF.data_netCDF_array_var(FILE_CDF_IN,VAR_NAME)
if DEBUG == 'Y' and len(VAR_DATA) == 1:
print VAR_DATA.shape,VAR_DATA.min(),VAR_DATA.max()
#
if iREGION == 0:
DIMS_MOD = []
for i in range(0,len(DIMS)-1):
DIMS_MOD.append(DIMS[i])
DIMS_MOD.append(NPOINTS)
VAR_DATA_ALL = np.zeros(DIMS_MOD)
#
if len(DIMS) == 1:
NCOUNT = DEFAULT
else:
NCOUNT = DIMS[-1]
#
if len(DIMS) == 2:
VAR_DATA_ALL[:,COUNT:COUNT+NCOUNT] = VAR_DATA[:,:]
elif len(DIMS) == 3:
VAR_DATA_ALL[:,:,COUNT:COUNT+NCOUNT] = VAR_DATA[:,:,:]
elif len(DIMS) == 4:
VAR_DATA_ALL[:,:,:,COUNT:COUNT+NCOUNT] = VAR_DATA[:,:,:,:]
#
if not os.path.exists(FILE_CDF_IN) or os.path.getsize(FILE_CDF_IN) <= 0:
#
if len(DIMS) == 2:
VAR_DATA_ALL[:,COUNT:COUNT+NCOUNT] = MISS_DATA
elif len(DIMS) == 3:
VAR_DATA_ALL[:,:,COUNT:COUNT+NCOUNT] = MISS_DATA
elif len(DIMS) == 4:
VAR_DATA_ALL[:,:,:,COUNT:COUNT+NCOUNT] = MISS_DATA
#
print iREGION,COUNT,NCOUNT,NPOINTS
#
# Increment regions
#
COUNT += NCOUNT
#
VAR_DATA_ALL = np.squeeze(VAR_DATA_ALL)
print VAR_DATA_ALL.shape
#
# Return to calling routine
#
return VAR_DATA_ALL
#
# ##################################################################################################
# (3) JULES offline - regird to regular lat-lon grid
# ##################################################################################################
#
def JULES_data_regrid(VAR_DATA,NLON,NLAT,LON_DATA,LAT_DATA,LONG_START,LONG_END, \
LAT_START,LAT_END,RESOL,iSOIL,MISS_DATA,DEBUG):
#
# Assign land points to regular lat-lon grid
#
DIMS = VAR_DATA.shape
NPOINTS = DIMS[-1]
print VAR_DATA.shape,DIMS,NPOINTS
#
if iSOIL == -1:
VAR_DATA_REGRID = np.zeros((DIMS[0],NLAT,NLON))
VAR_DATA_REGRID[:,:,:] = MISS_DATA
else:
VAR_DATA_REGRID = np.zeros((DIMS[0],DIMS[1],NLAT,NLON))
VAR_DATA_REGRID[:,:,:,:] = MISS_DATA
#
for INDEX in range(NPOINTS):
#
if LON_DATA[INDEX] > LONG_END:
LON_DATA[INDEX] = LON_DATA[INDEX]-360.0
#
iLON = int((LON_DATA[INDEX]-LONG_START-RESOL/2.0)/RESOL)
iLAT = int((LAT_DATA[INDEX]-LAT_START -RESOL/2.0)/RESOL)
#
# Skip if out of range
#
if iLON >= 0 and iLON < NLON and iLAT >= 0 and iLAT < NLAT:
#
if iSOIL == -1:
VAR_DATA_REGRID[:,iLAT,iLON] = VAR_DATA[:,INDEX]
else:
VAR_DATA_REGRID[:,:,iLAT,iLON] = VAR_DATA[:,:,INDEX]
#
return VAR_DATA_REGRID
#
# Return to calling routine
#
# ##################################################################################################
#
def JULES_data_regrid_mask(VAR_DATA,NLON,NLAT,LON_DATA,LAT_DATA,LONG_START,LONG_END, \
LAT_START,LAT_END,RESOL,iSOIL,MISS_DATA,DEBUG):
#
# Assign land points to regular lat-lon grid
#
DIMS = VAR_DATA.shape
NPOINTS = DIMS[-1]
print VAR_DATA.shape,DIMS,NPOINTS
#
LAND_MASK = np.empty((NLAT,NLON))
LAND_MASK[:,:] = 1
#
if iSOIL == -1:
VAR_DATA_REGRID = np.zeros((DIMS[0],NLAT,NLON))
VAR_DATA_REGRID[:,:,:] = MISS_DATA
else:
VAR_DATA_REGRID = np.zeros((DIMS[0],DIMS[1],NLAT,NLON))
VAR_DATA_REGRID[:,:,:,:] = MISS_DATA
#
for INDEX in range(NPOINTS):
#
if LON_DATA[INDEX] > LONG_END:
LON_DATA[INDEX] = LON_DATA[INDEX]-360.0
#
iLON = int((LON_DATA[INDEX]-LONG_START-RESOL/2.0)/RESOL)
iLAT = int((LAT_DATA[INDEX]-LAT_START -RESOL/2.0)/RESOL)
#
LAND_MASK[iLAT,iLON] = 0
#
# Skip if out of range
#
if iLON >= 0 and iLON < NLON and iLAT >= 0 and iLAT < NLAT:
#
if iSOIL == -1:
VAR_DATA_REGRID[:,iLAT,iLON] = VAR_DATA[:,INDEX]
else:
VAR_DATA_REGRID[:,:,iLAT,iLON] = VAR_DATA[:,:,INDEX]
#
return VAR_DATA_REGRID,LAND_MASK
#
# Return to calling routine