-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_lam_refc_dif.py
executable file
·478 lines (418 loc) · 14.2 KB
/
plot_lam_refc_dif.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import pygrib
import matplotlib
matplotlib.use('Agg')
import io
import matplotlib.pyplot as plt
#from PIL import Image
import matplotlib.image as image
from matplotlib.gridspec import GridSpec
import mpl_toolkits
mpl_toolkits.__path__.append('/gpfs/dell2/emc/modeling/noscrub/gwv/py/lib/python/basemap-1.2.1-py3.6-linux-x86_64.egg/mpl_toolkits/')
from mpl_toolkits.basemap import Basemap, maskoceans
import numpy as np
import time,os,sys,multiprocessing
import multiprocessing.pool
import ncepy
from scipy import ndimage
from netCDF4 import Dataset
import pyproj
#--------------Set some classes------------------------#
# Make Python process pools non-daemonic
class NoDaemonProcess(multiprocessing.Process):
# make 'daemon' attribute always return False
def _get_daemon(self):
return False
def _set_daemon(self, value):
pass
daemon = property(_get_daemon, _set_daemon)
# We sub-class multiprocessing.pool.Pool instead of multiprocessing.Pool
# because the latter is only a wrapper function, not a proper class.
class MyPool(multiprocessing.pool.Pool):
Process = NoDaemonProcess
#--------------Define some functions ------------------#
def clear_plotables(ax,keep_ax_lst,fig):
#### - step to clear off old plottables but leave the map info - ####
if len(keep_ax_lst) == 0 :
print("clear_plotables WARNING keep_ax_lst has length 0. Clearing ALL plottables including map info!")
cur_ax_children = ax.get_children()[:]
if len(cur_ax_children) > 0:
for a in cur_ax_children:
if a not in keep_ax_lst:
# if the artist isn't part of the initial set up, remove it
a.remove()
def compress_and_save(filename):
#### - compress and save the image - ####
# ram = io.StringIO()
# ram = io.BytesIO()
# plt.savefig(ram, format='png', bbox_inches='tight', dpi=150)
plt.savefig(filename, format='png', bbox_inches='tight', dpi=300)
# ram.seek(0)
# im = Image.open(ram)
# im2 = im.convert('RGB').convert('P', palette=Image.ADAPTIVE)
# im2.save(filename, format='PNG')
#-------------------------------------------------------#
# Necessary to generate figs when not running an Xserver (e.g. via PBS)
# plt.switch_backend('agg')
# Read date/time and forecast hour from command line
ymdh = str(sys.argv[1])
ymd = ymdh[0:8]
year = int(ymdh[0:4])
month = int(ymdh[4:6])
day = int(ymdh[6:8])
hour = int(ymdh[8:10])
cyc = str(hour).zfill(2)
print(year, month, day, hour)
fhr = int(sys.argv[2])
fhrm1 = fhr - 1
fhrm2 = fhr - 2
fhrm6 = fhr - 6
fhrm24 = fhr - 24
fhour = str(fhr).zfill(2)
fhour1 = str(fhrm1).zfill(2)
fhour2 = str(fhrm2).zfill(2)
fhour6 = str(fhrm6).zfill(2)
fhour24 = str(fhrm24).zfill(2)
print('fhour '+fhour)
# Define the input files
data1 = pygrib.open('/gpfs/dell6/emc/modeling/noscrub/James.A.Abeles/results.022400Z/PRSLEV.GrbF'+fhour)
data2 = pygrib.open('/gpfs/dell4/ptmp/emc.campara/fv3lam/fv3lam.'+str(ymd)+'/'+cyc+'/fv3lam.t'+cyc+'z.conus.f'+fhour+'.grib2')
# Get the lats and lons
grids = [data1, data2]
lats = []
lons = []
lats_shift = []
lons_shift = []
for data in grids:
# Unshifted grid for contours and wind barbs
lat, lon = data[1].latlons()
lats.append(lat)
lons.append(lon)
# Shift grid for pcolormesh
lat1 = data[1]['latitudeOfFirstGridPointInDegrees']
lon1 = data[1]['longitudeOfFirstGridPointInDegrees']
try:
nx = data[1]['Nx']
ny = data[1]['Ny']
except:
nx = data[1]['Ni']
ny = data[1]['Nj']
dx = data[1]['DxInMetres']
dy = data[1]['DyInMetres']
pj = pyproj.Proj(data[1].projparams)
llcrnrx, llcrnry = pj(lon1,lat1)
llcrnrx = llcrnrx - (dx/2.)
llcrnry = llcrnry - (dy/2.)
x = llcrnrx + dx*np.arange(nx)
y = llcrnry + dy*np.arange(ny)
x,y = np.meshgrid(x,y)
lon, lat = pj(x, y, inverse=True)
lats_shift.append(lat)
lons_shift.append(lon)
# Unshifted lat/lon arrays grabbed directly using latlons() method
lat = lats[0]
lon = lons[0]
lat2 = lats[1]
lon2 = lons[1]
# Shifted lat/lon arrays for pcolormesh
lat_shift = lats_shift[0]
lon_shift = lons_shift[0]
lat2_shift = lats_shift[1]
lon2_shift = lons_shift[1]
Lat0 = data1[1]['LaDInDegrees']
Lon0 = data1[1]['LoVInDegrees']
#Lon0 = 262.5
print(Lat0)
print(Lon0)
# Forecast valid date/time
itime = ymdh
vtime = ncepy.ndate(itime,int(fhr))
# Specify plotting domains
#domains = ['conus','BN','CE','CO','LA','MA','NC','NE','NW','OV','SC','SE','SF','SP','SW','UM']
domains=['conus']
###################################################
# Read in all variables and calculate differences #
###################################################
t1a = time.clock()
# Total precipitation
refc_1 = data1.select(name='Maximum/Composite radar reflectivity')[0].values
refc_2 = data2.select(name='Maximum/Composite radar reflectivity')[0].values
t2a = time.clock()
t3a = round(t2a-t1a, 3)
print(("%.3f seconds to read all messages") % t3a)
# colors for difference plots, only need to define once
difcolors = ['blue','#1874CD','dodgerblue','deepskyblue','turquoise','white','white','#EEEE00','#EEC900','darkorange','orangered','red']
difcolors2 = ['white']
difcolors3 = ['blue','dodgerblue','turquoise','white','white','#EEEE00','darkorange','red']
########################################
# START PLOTTING FOR EACH DOMAIN #
########################################
def main():
# Number of processes must coincide with the number of domains to plot
# pool = multiprocessing.Pool(len(domains))
pool = MyPool(len(domains))
pool.map(plot_all,domains)
def plot_all(domain):
global dom
dom = domain
print(('Working on '+dom))
global fig,axes,ax1,ax2,ax3,keep_ax_lst_1,keep_ax_lst_2,keep_ax_lst_3,m,x,y,x2,y2,x_shift,y_shift,x2_shift,y2_shift,xscale,yscale,im,par
fig,axes,ax1,ax2,ax3,keep_ax_lst_1,keep_ax_lst_2,keep_ax_lst_3,m,x,y,x2,y2,x_shift,y_shift,x2_shift,y2_shift,xscale,yscale,im,par = create_figure()
# Split plots into 2 sets with multiprocessing
sets = [1]
# sets = [1]
pool2 = multiprocessing.Pool(len(sets))
pool2.map(plot_sets,sets)
def create_figure():
# create figure and axes instances
fig = plt.figure()
gs = GridSpec(9,9,wspace=0.0,hspace=0.0)
ax1 = fig.add_subplot(gs[0:4,0:4])
ax2 = fig.add_subplot(gs[0:4,5:])
ax3 = fig.add_subplot(gs[5:,1:8])
axes = [ax1, ax2, ax3]
im = image.imread('/gpfs/dell2/emc/modeling/noscrub/Benjamin.Blake/python.fv3/noaa.png')
par = 1
# Map corners for each domain
if dom == 'conus':
llcrnrlon = -120.5
llcrnrlat = 21.0
urcrnrlon = -64.5
urcrnrlat = 49.0
lat_0 = 35.4
lon_0 = -97.6
xscale=0.15
yscale=0.2
elif dom == 'BN':
llcrnrlon = -75.75
llcrnrlat = 40.0
urcrnrlon = -69.5
urcrnrlat = 43.0
lat_0 = 41.0
lon_0 = -74.6
xscale=0.14
yscale=0.19
elif dom == 'CE':
llcrnrlon = -103.0
llcrnrlat = 32.5
urcrnrlon = -88.5
urcrnrlat = 41.5
lat_0 = 35.0
lon_0 = -97.0
xscale=0.15
yscale=0.18
elif dom == 'CO':
llcrnrlon = -110.5
llcrnrlat = 35.0
urcrnrlon = -100.5
urcrnrlat = 42.0
lat_0 = 38.0
lon_0 = -105.0
xscale=0.17
yscale=0.18
elif dom == 'LA':
llcrnrlon = -121.0
llcrnrlat = 32.0
urcrnrlon = -114.0
urcrnrlat = 37.0
lat_0 = 34.0
lon_0 = -114.0
xscale=0.16
yscale=0.18
elif dom == 'MA':
llcrnrlon = -82.0
llcrnrlat = 36.5
urcrnrlon = -73.5
urcrnrlat = 42.0
lat_0 = 37.5
lon_0 = -80.0
xscale=0.18
yscale=0.18
elif dom == 'NC':
llcrnrlon = -111.0
llcrnrlat = 39.0
urcrnrlon = -93.5
urcrnrlat = 49.0
lat_0 = 44.5
lon_0 = -102.0
xscale=0.16
yscale=0.18
elif dom == 'NE':
llcrnrlon = -80.0
llcrnrlat = 40.5
urcrnrlon = -66.0
urcrnrlat = 47.5
lat_0 = 42.0
lon_0 = -80.0
xscale=0.16
yscale=0.18
elif dom == 'NW':
llcrnrlon = -125.5
llcrnrlat = 40.5
urcrnrlon = -109.0
urcrnrlat = 49.5
lat_0 = 44.0
lon_0 = -116.0
xscale=0.15
yscale=0.18
elif dom == 'OV':
llcrnrlon = -91.5
llcrnrlat = 34.75
urcrnrlon = -80.0
urcrnrlat = 43.0
lat_0 = 38.0
lon_0 = -87.0
xscale=0.18
yscale=0.17
elif dom == 'SC':
llcrnrlon = -108.0
llcrnrlat = 25.0
urcrnrlon = -88.0
urcrnrlat = 37.0
lat_0 = 32.0
lon_0 = -98.0
xscale=0.14
yscale=0.18
elif dom == 'SE':
llcrnrlon = -91.5
llcrnrlat = 24.0
urcrnrlon = -74.0
urcrnrlat = 36.5
lat_0 = 34.0
lon_0 = -85.0
xscale=0.17
yscale=0.18
elif dom == 'SF':
llcrnrlon = -123.25
llcrnrlat = 37.25
urcrnrlon = -121.25
urcrnrlat = 38.5
lat_0 = 37.5
lon_0 = -121.0
xscale=0.16
yscale=0.19
elif dom == 'SP':
llcrnrlon = -125.0
llcrnrlat = 45.0
urcrnrlon = -119.5
urcrnrlat = 49.2
lat_0 = 46.0
lon_0 = -120.0
xscale=0.19
yscale=0.18
elif dom == 'SW':
llcrnrlon = -125.0
llcrnrlat = 30.0
urcrnrlon = -108.0
urcrnrlat = 42.5
lat_0 = 37.0
lon_0 = -113.0
xscale=0.17
yscale=0.18
elif dom == 'UM':
llcrnrlon = -96.75
llcrnrlat = 39.75
urcrnrlon = -81.0
urcrnrlat = 49.0
lat_0 = 44.0
lon_0 = -91.5
xscale=0.18
yscale=0.18
# Create basemap instance and set the dimensions
for ax in axes:
if dom == 'BN' or dom == 'LA' or dom == 'SF' or dom == 'SP':
m = Basemap(ax=ax,projection='gnom',lat_0=lat_0,lon_0=lon_0,\
llcrnrlat=llcrnrlat, urcrnrlat=urcrnrlat,\
llcrnrlon=llcrnrlon, urcrnrlon=urcrnrlon,\
resolution='h')
elif dom == 'conus':
m = Basemap(ax=ax,projection='gnom',lat_0=lat_0,lon_0=lon_0,\
llcrnrlat=llcrnrlat, urcrnrlat=urcrnrlat,\
llcrnrlon=llcrnrlon, urcrnrlon=urcrnrlon,\
resolution='i')
else:
m = Basemap(ax=ax,projection='gnom',lat_0=lat_0,lon_0=lon_0,\
llcrnrlat=llcrnrlat, urcrnrlat=urcrnrlat,\
llcrnrlon=llcrnrlon, urcrnrlon=urcrnrlon,\
resolution='l')
m.fillcontinents(color='LightGrey',zorder=0)
m.drawcoastlines(linewidth=0.75)
m.drawstates(linewidth=0.5)
m.drawcountries(linewidth=0.5)
## parallels = np.arange(0.,90.,10.)
## map.drawparallels(parallels,labels=[1,0,0,0],fontsize=6)
## meridians = np.arange(180.,360.,10.)
## map.drawmeridians(meridians,labels=[0,0,0,1],fontsize=6)
x,y = m(lon,lat)
x2,y2 = m(lon2,lat2)
x_shift,y_shift = m(lon_shift,lat_shift)
x2_shift,y2_shift = m(lon2_shift,lat2_shift)
# Map/figure has been set up here, save axes instances for use again later
if par == 1:
keep_ax_lst_1 = ax.get_children()[:]
elif par == 2:
keep_ax_lst_2 = ax.get_children()[:]
elif par == 3:
keep_ax_lst_3 = ax.get_children()[:]
par += 1
par = 1
return fig,axes,ax1,ax2,ax3,keep_ax_lst_1,keep_ax_lst_2,keep_ax_lst_3,m,x,y,x2,y2,x_shift,y_shift,x2_shift,y2_shift,xscale,yscale,im,par
def plot_sets(set):
# Add print to see if dom is being passed in
print(('plot_sets dom variable '+dom))
global fig,axes,ax1,ax2,ax3,keep_ax_lst_1,keep_ax_lst_2,keep_ax_lst_3,m,x,y,x2,y2,x_shift,y_shift,x2_shift,y2_shift,xscale,yscale,im,par
if set == 1:
plot_set_1()
elif set == 2:
plot_set_2()
elif set == 3:
plot_set_3()
def plot_set_1():
global fig,axes,ax1,ax2,ax3,keep_ax_lst_1,keep_ax_lst_2,keep_ax_lst_3,m,x,y,x2,y2,x_shift,y_shift,x2_shift,y2_shift,xscale,yscale,im,par
#################################
# Plot composite reflectivity
#################################
t1 = time.clock()
print(('Working on composite reflectivity for '+dom))
units = 'dBZ'
clevs = np.linspace(5,70,14)
clevsdif = [20,1000]
colorlist = ['turquoise','dodgerblue','mediumblue','lime','limegreen','green','#EEEE00','#EEC900','darkorange','red','firebrick','darkred','fuchsia']
cm = matplotlib.colors.ListedColormap(colorlist)
norm = matplotlib.colors.BoundaryNorm(clevs, cm.N)
for ax in axes:
xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()
xmax = int(round(xmax))
ymax = int(round(ymax))
if par == 1:
cs_1 = m.pcolormesh(x_shift,y_shift,refc_1,cmap=cm,vmin=5,norm=norm,ax=ax)
cs_1.cmap.set_under('white',alpha=0.)
cs_1.cmap.set_over('black')
cbar1 = m.colorbar(cs_1,ax=ax,location='bottom',pad=0.05,ticks=clevs,extend='max')
cbar1.set_label(units,fontsize=6)
cbar1.ax.tick_params(labelsize=6)
ax.text(.5,1.03,'Inline Post Composite Reflectivity ('+units+') \n initialized: '+itime+' valid: '+vtime + ' (f'+fhour+')',horizontalalignment='center',fontsize=6,transform=ax.transAxes,bbox=dict(facecolor='white',alpha=0.85,boxstyle='square,pad=0.2'))
ax.imshow(im,aspect='equal',alpha=0.5,origin='upper',extent=(0,int(round(xmax*xscale)),0,int(round(ymax*yscale))),zorder=4)
elif par == 2:
cs_2 = m.pcolormesh(x2_shift,y2_shift,refc_2,cmap=cm,vmin=5,norm=norm,ax=ax)
cs_2.cmap.set_under('white',alpha=0.)
cs_2.cmap.set_over('black')
cbar2 = m.colorbar(cs_2,ax=ax,location='bottom',pad=0.05,ticks=clevs,extend='max')
cbar2.set_label(units,fontsize=6)
cbar2.ax.tick_params(labelsize=6)
ax.text(.5,1.03,'Parallel Composite Reflectivity ('+units+') \n initialized: '+itime+' valid: '+vtime + ' (f'+fhour+')',horizontalalignment='center',fontsize=6,transform=ax.transAxes,bbox=dict(facecolor='white',alpha=0.85,boxstyle='square,pad=0.2'))
ax.imshow(im,aspect='equal',alpha=0.5,origin='upper',extent=(0,int(round(xmax*xscale)),0,int(round(ymax*yscale))),zorder=4)
elif par == 3:
csdif = m.contourf(x,y,refc_1,clevsdif,colors='red')
csdif2 = m.contourf(x2,y2,refc_2,clevsdif,colors='dodgerblue')
ax.text(.5,1.03,'Inline Post (red) and Parallel (blue) Composite Reflectivity > 20 ('+units+') \n initialized: '+itime+' valid: '+vtime + ' (f'+fhour+')',horizontalalignment='center',fontsize=5,transform=ax.transAxes,bbox=dict(facecolor='white',alpha=0.85,boxstyle='square,pad=0.2'))
ax.imshow(im,aspect='equal',alpha=0.5,origin='upper',extent=(0,int(round(xmax*xscale)),0,int(round(ymax*yscale))),zorder=4)
par += 1
par = 1
compress_and_save('comparerefc_'+dom+'_f'+fhour+'.png')
t2 = time.clock()
t3 = round(t2-t1, 3)
print(('%.3f seconds to plot composite reflectivity for: '+dom) % t3)
plt.clf()
################################################################################
main()