-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_qpf_verif.py
More file actions
executable file
·1293 lines (1200 loc) · 47.7 KB
/
plot_qpf_verif.py
File metadata and controls
executable file
·1293 lines (1200 loc) · 47.7 KB
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/usr/env python
import grib2io
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import cartopy.feature as cfeature
import matplotlib
import io
import matplotlib.pyplot as plt
from PIL import Image
import matplotlib.image as image
from matplotlib.gridspec import GridSpec
import numpy as np
import time,os,sys,multiprocessing
import multiprocessing.pool
from scipy import ndimage
from scipy.ndimage.filters import minimum_filter, maximum_filter
from netCDF4 import Dataset
import pyproj
import cartopy
from datetime import datetime
import rrfs_plot_utils
import dateutil.relativedelta, dateutil.parser
from subprocess import call
from matplotlib import colors
####################################
# UTILITIES
####################################
def ndate(cdate,hours):
if not isinstance(cdate, str):
if isinstance(cdate, int):
cdate=str(cdate)
else:
sys.exit('NDATE: Error - input cdate must be string or integer. Exit!')
if not isinstance(hours, int):
if isinstance(hours, str):
hours=int(hours)
else:
sys.exit('NDATE: Error - input delta hour must be a string or integer. Exit!')
indate=cdate.strip()
hh=indate[8:10]
yyyy=indate[0:4]
mm=indate[4:6]
dd=indate[6:8]
#set date/time field
parseme=(yyyy+' '+mm+' '+dd+' '+hh)
datetime_cdate=dateutil.parser.parse(parseme)
valid=datetime_cdate+dateutil.relativedelta.relativedelta(hours=+hours)
vyyyy=str(valid.year)
vm=str(valid.month).zfill(2)
vd=str(valid.day).zfill(2)
vh=str(valid.hour).zfill(2)
return vyyyy+vm+vd+vh
def get_panel_spacing(dom, type='4panel'):
if type in ['4panel']:
if dom in ['conus']:
wspace=0.1
hspace=-0.7
elif dom in ['puerto_rico']:
wspace=0.1
hspace=-0.65
elif dom in ['sf_bay_area']:
wspace=0.1
hspace=-0.6
elif dom in ['south_central']:
wspace=0.1
hspace=-0.55
elif dom in ['boston_nyc']:
wspace=0.1
hspace=-0.5
elif dom in ['north_central','south_florida']:
wspace=0.1
hspace=-0.3
elif dom in ['southwest','northeast']:
wspace=0.1
hspace=-0.1
elif dom in [
'alaska','hawaii','central','colorado','la_vegas','mid_atlantic',
'northwest','ohio_valley','southeast','seattle_portland']:
wspace=0.1
hspace=0.1
else:
wspace=0.1
hspace=0.0
elif type in ['3panel']:
if dom in ['conus']:
wspace=0.1
hspace=-0.3
elif dom in ['puerto_rico']:
wspace=0.1
hspace=-0.3
elif dom in ['sf_bay_area']:
wspace=0.1
hspace=-0.3
elif dom in ['south_central']:
wspace=0.1
hspace=-0.3
elif dom in ['boston_nyc']:
wspace=0.1
hspace=-0.3
elif dom in ['north_central','south_florida']:
wspace=0.1
hspace=-0.3
elif dom in [
'alaska','hawaii','central','colorado','la_vegas','mid_atlantic',
'northwest','ohio_valley','southeast','seattle_portland']:
wspace=0.1
hspace=-0.3
elif dom in ['alaska']:
wspace=0.1
hspace=-0.3
else:
wspace=0.1
hspace=0.0
elif type in ['2panel']:
if dom in ['conus']:
wspace=0.1
hspace=-0.7
elif dom in ['puerto_rico']:
wspace=0.1
hspace=-0.65
elif dom in ['sf_bay_area']:
wspace=0.1
hspace=-0.6
elif dom in ['south_central']:
wspace=0.1
hspace=-0.55
elif dom in ['boston_nyc']:
wspace=0.1
hspace=-0.5
elif dom in ['north_central','south_florida']:
wspace=0.1
hspace=-0.3
elif dom in ['southwest','northeast']:
wspace=0.1
hspace=-0.1
elif dom in [
'alaska','hawaii','central','colorado','la_vegas','mid_atlantic',
'northwest','ohio_valley','southeast','seattle_portland']:
wspace=0.1
hspace=0.1
else:
wspace=0.1
hspace=0.0
else:
ValueError(f"Type \"{type}\" is not a valid plot type.")
return wspace, hspace
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 convert_and_save(filename):
#### - convert and save the image - ####
plt.savefig(filename+'.png', bbox_inches='tight',dpi=150)
#os.system('convert '+filename+'.png '+filename+'.gif')
img = Image.open(filename + '.png')
img.save(filename + '.gif', format='GIF')
os.remove(filename+'.png')
def convert_and_save_2(filename):
#### - convert and save the image - ####
#### - use higher dpi for single panel plots - ####
plt.savefig(filename+'.png', bbox_inches='tight',dpi=250)
os.system('convert '+filename+'.png '+filename+'.gif')
os.remove(filename+'.png')
def extrema(mat,mode='wrap',window=10):
# From: http://matplotlib.org/basemap/users/examples.html
"""find the indices of local extrema (min and max)
in the input array."""
mn = minimum_filter(mat, size=window, mode=mode)
mx = maximum_filter(mat, size=window, mode=mode)
# (mat == mx) true if pixel is equal to the local max # (mat == mn) true if pixel is equal to the local in
# Return the indices of the maxima, minima
return np.nonzero(mat == mn), np.nonzero(mat == mx)
def plt_highs_and_lows(x,y,mat,xmin,xmax,ymin,ymax,offset,ax,transform,mode='wrap',window=10):
# From: http://matplotlib.org/basemap/users/examples.html
if isinstance(window,int) == False:
raise TypeError("The window argument to plt_highs_and_lows must be an integer.")
local_min, local_max = extrema(mat,mode,window)
xlows = x[local_min]; xhighs = x[local_max]
ylows = y[local_min]; yhighs = y[local_max]
lowvals = mat[local_min]; highvals = mat[local_max]
# plot lows as red L's, with min pressure value underneath.
xyplotted = []
# don't plot if there is already a L or H within dmin meters.
# yoffset = 0.022*(ymax-ymin)
yoffset = offset
dmin = yoffset
for x,y,p in zip(xlows, ylows, lowvals):
x_proj, y_proj = ax.projection.transform_point(x, y, ccrs.PlateCarree())
if x_proj < xmax and x_proj > xmin and y_proj < ymax and y_proj > ymin:
# dist = [np.sqrt((x-x0)**2+(y-y0)**2) for x0,y0 in xyplotted]
# if not dist or min(dist) > dmin:
ax.text(x,y,'L',fontsize=14,fontweight='bold',
ha='center',va='center',color='r',zorder=4,clip_on=True,
transform=transform)
ax.text(x,y-yoffset,repr(int(p)),fontsize=6,zorder=4,
ha='center',va='top',color='r',
bbox = dict(boxstyle="square",ec='None',fc=(1,1,1,0.5)),clip_on=True,
transform=transform)
xyplotted.append((x,y))
# plot highs as blue H's, with max pressure value underneath.
xyplotted = []
for x,y,p in zip(xhighs, yhighs, highvals):
x_proj, y_proj = ax.projection.transform_point(x, y, ccrs.PlateCarree())
if x_proj < xmax and x_proj > xmin and y_proj < ymax and y_proj > ymin:
# dist = [np.sqrt((x-x0)**2+(y-y0)**2) for x0,y0 in xyplotted]
# if not dist or min(dist) > dmin:
ax.text(x,y,'H',fontsize=14,fontweight='bold',
ha='center',va='center',color='b',zorder=4,clip_on=True,
transform=transform)
ax.text(x,y-yoffset,repr(int(p)),fontsize=6,
ha='center',va='top',color='b',zorder=4,
bbox = dict(boxstyle="square",ec='None',fc=(1,1,1,0.5)),clip_on=True,
transform=transform)
xyplotted.append((x,y))
def get_latlons_pcolormesh(msg):
# Get shifted lats and lons for plotting with pcolormesh
lats = []
lons = []
lats_shift = []
lons_shift = []
# Unshifted grid for contours and wind barbs
lat, lon = msg.grid()
lats.append(lat)
lons.append(lon)
# Shift grid for pcolormesh
lat1 = msg.latitudeFirstGridpoint
lon1 = msg.longitudeFirstGridpoint
nx = msg.nx
ny = msg.ny
dx = msg.gridlengthXDirection
dy = msg.gridlengthYDirection
pj = pyproj.Proj(msg.projParameters)
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]
# Shifted lat/lon arrays for pcolormesh
lat_shift = lats_shift[0]
lon_shift = lons_shift[0]
# Fix for Alaska
if (np.min(lon_shift) < 0) and (np.max(lon_shift) > 0):
lon_shift = np.where(lon_shift>0,lon_shift-360,lon_shift)
return lat, lon, lat_shift, lon_shift
####################################
# Color shading / Color bars
####################################
def cmap_t2m():
# Create colormap for 2-m temperature
# Modified version of the ncl_t2m colormap from Jacob's ncepy code
r=np.array([255,128,0, 70, 51, 0, 255,0, 0, 51, 255,255,255,255,255,171,128,128,36,162,255])
g=np.array([0, 0, 0, 70, 102,162,255,92,128,185,255,214,153,102,0, 0, 0, 68, 36,162,255])
b=np.array([255,128,128,255,255,255,255,0, 0, 102,0, 112,0, 0, 0, 56, 0, 68, 36,162,255])
xsize=np.arange(np.size(r))
r = r/255.
g = g/255.
b = b/255.
red = []
green = []
blue = []
for i in range(len(xsize)):
xNorm=float(i)/(float(np.size(r))-1.0)
red.append([xNorm,r[i],r[i]])
green.append([xNorm,g[i],g[i]])
blue.append([xNorm,b[i],b[i]])
colorDict = {"red":red, "green":green, "blue":blue}
cmap_t2m_coltbl = colors.LinearSegmentedColormap('CMAP_T2M_COLTBL',colorDict)
return cmap_t2m_coltbl
def cmap_q2m():
# Create colormap for dew point temperature
r=np.array([255,179,96,128,0, 0, 51, 0, 0, 0, 133,51, 70, 0, 128,128,180])
g=np.array([255,179,96,128,92,128,153,155,155,255,162,102,70, 0, 0, 0, 0])
b=np.array([255,179,96,0, 0, 0, 102,155,255,255,255,255,255,128,255,128,128])
xsize=np.arange(np.size(r))
r = r/255.
g = g/255.
b = b/255.
red = []
green = []
blue = []
for i in range(len(xsize)):
xNorm=np.float(i)/(np.float(np.size(r))-1.0)
red.append([xNorm,r[i],r[i]])
green.append([xNorm,g[i],g[i]])
blue.append([xNorm,b[i],b[i]])
colorDict = {"red":red, "green":green, "blue":blue}
cmap_q2m_coltbl = colors.LinearSegmentedColormap('CMAP_Q2M_COLTBL',colorDict)
cmap_q2m_coltbl.set_over(color='deeppink')
return cmap_q2m_coltbl
def cmap_t850():
# Create colormap for 850-mb equivalent potential temperature
r=np.array([255,128,0, 70, 51, 0, 0, 0, 51, 255,255,255,255,255,171,128,128,96,201])
g=np.array([0, 0, 0, 70, 102,162,225,92,153,255,214,153,102,0, 0, 0, 68, 96,201])
b=np.array([255,128,128,255,255,255,162,0, 102,0, 112,0, 0, 0, 56, 0, 68, 96,201])
xsize=np.arange(np.size(r))
r = r/255.
g = g/255.
b = b/255.
red = []
green = []
blue = []
for i in range(len(xsize)):
xNorm=float(i)/(float(np.size(r))-1.0)
red.append([xNorm,r[i],r[i]])
green.append([xNorm,g[i],g[i]])
blue.append([xNorm,b[i],b[i]])
colorDict = {"red":red, "green":green, "blue":blue}
cmap_t850_coltbl = colors.LinearSegmentedColormap('CMAP_T850_COLTBL',colorDict)
return cmap_t850_coltbl
def cmap_terra():
# Create colormap for terrain height
# Emerald green to light green to tan to gold to dark red to brown to light brown to white
r=np.array([0, 152,212,188,127,119,186])
g=np.array([128,201,208,148,34, 83, 186])
b=np.array([64, 152,140,0, 34, 64, 186])
xsize=np.arange(np.size(r))
r = r/255.
g = g/255.
b=np.array([255,179,96,0, 0, 0, 102,155,255,255,255,255,255,128,255,128,128])
xsize=np.arange(np.size(r))
r = r/255.
g = g/255.
b = b/255.
red = []
green = []
blue = []
for i in range(len(xsize)):
xNorm=np.float(i)/(np.float(np.size(r))-1.0)
red.append([xNorm,r[i],r[i]])
green.append([xNorm,g[i],g[i]])
blue.append([xNorm,b[i],b[i]])
colorDict = {"red":red, "green":green, "blue":blue}
cmap_q2m_coltbl = colors.LinearSegmentedColormap('CMAP_Q2M_COLTBL',colorDict)
cmap_q2m_coltbl.set_over(color='deeppink')
return cmap_q2m_coltbl
def cmap_t850():
# Create colormap for 850-mb equivalent potential temperature
r=np.array([255,128,0, 70, 51, 0, 0, 0, 51, 255,255,255,255,255,171,128,128,96,201])
g=np.array([0, 0, 0, 70, 102,162,225,92,153,255,214,153,102,0, 0, 0, 68, 96,201])
b=np.array([255,128,128,255,255,255,162,0, 102,0, 112,0, 0, 0, 56, 0, 68, 96,201])
xsize=np.arange(np.size(r))
r = r/255.
g = g/255.
b = b/255.
red = []
green = []
blue = []
for i in range(len(xsize)):
xNorm=float(i)/(float(np.size(r))-1.0)
red.append([xNorm,r[i],r[i]])
green.append([xNorm,g[i],g[i]])
blue.append([xNorm,b[i],b[i]])
colorDict = {"red":red, "green":green, "blue":blue}
cmap_t850_coltbl = colors.LinearSegmentedColormap('CMAP_T850_COLTBL',colorDict)
return cmap_t850_coltbl
def cmap_terra():
# Create colormap for terrain height
# Emerald green to light green to tan to gold to dark red to brown to light brown to white
r=np.array([0, 152,212,188,127,119,186])
g=np.array([128,201,208,148,34, 83, 186])
b=np.array([64, 152,140,0, 34, 64, 186])
xsize=np.arange(np.size(r))
r = r/255.
g = g/255.
b = b/255.
red = []
green = []
blue = []
for i in range(len(xsize)):
xNorm=float(i)/(float(np.size(r))-1.0)
red.append([xNorm,r[i],r[i]])
green.append([xNorm,g[i],g[i]])
blue.append([xNorm,b[i],b[i]])
colorDict = {"red":red, "green":green, "blue":blue}
cmap_terra_coltbl = colors.LinearSegmentedColormap('CMAP_TERRA_COLTBL',colorDict)
cmap_terra_coltbl.set_over(color='#E0EEE0')
return cmap_terra_coltbl
def ncl_perc_11Lev():
# Create colormap for snowfall
r=np.array([202,89,139,96,26,145,217,254,252,215,150])
g=np.array([202,141,239,207,152,207,239,224,141,48,0])
b=np.array([200,252,217,145,80,96,139,139,89,39,100])
xsize=np.arange(np.size(r))
r = r/255.
g = g/255.
b = b/255.
red = []
blue = []
green = []
for i in range(len(xsize)):
xNorm=np.float(i)/(np.float(np.size(r))-1.0)
red.append([xNorm,r[i],r[i]])
green.append([xNorm,g[i],g[i]])
blue.append([xNorm,b[i],b[i]])
colorDict = {"red":red, "green":green, "blue":blue}
my_coltbl = colors.LinearSegmentedColormap('NCL_PERC_11LEV_COLTBL',colorDict)
return my_coltbl
def ncl_grnd_hflux():
# Create colormap for ground heat flux
r=np.array([0,8,16,24,32,40,48,85,133,181,230,253,253,253,253,253,253,253,253,253,253,
253])
g=np.array([253,222,189,157,125,93,60,85,133,181,230,230,181,133,85,60,93,125,157,189,
224,253])
b=np.array([253,253,253,253,253,253,253,253,253,253,253,230,181,133,85,48,40,32,24,16,
8,0])
xsize=np.arange(np.size(r))
r = r/255.
g = g/255.
b = b/255.
red = []
blue = []
green = []
for i in range(len(xsize)):
xNorm=np.float(i)/(np.float(np.size(r))-1.0)
red.append([xNorm,r[i],r[i]])
green.append([xNorm,g[i],g[i]])
blue.append([xNorm,b[i],b[i]])
colorDict = {"red":red, "green":green, "blue":blue}
my_coltbl = colors.LinearSegmentedColormap('NCL_GRND_HFLUX_COLTBL',colorDict)
return my_coltbl
def domain_latlons_proj(dom):
# These are the available pre-defined domains:
#
# namerica, caribbean, alaska, hawaii, puerto rico,
# conus, northeast, mid_atlantic, southeast, ohio_valley,
# upper_midwest, north_central, central, south_central,
# northwest, southwest, colorado, boston_nyc,
# seattle_portland, sf_bay_area, la_vegas
# Latitudes and longitudes
if dom == 'namerica':
llcrnrlon = -160.0
llcrnrlat = 15.0
urcrnrlon = -55.0
urcrnrlat = 65.0
cen_lat = 35.4
cen_lon = -105.0
xextent = -3700000
yextent = -2500000
offset = 1
elif dom == 'caribbean':
llcrnrlon = -89.0
llcrnrlat = 15.1
urcrnrlon = -60.5
urcrnrlat = 29.5
cen_lat = 25.0
cen_lon = -82.0
xextent=-300000
yextent=-725000
offset=0.25
elif dom == 'alaska':
llcrnrlon = -167.5
llcrnrlat = 50.5
urcrnrlon = -135.8
urcrnrlat = 72.5
cen_lat = 60.0
cen_lon = -150.0
lat_ts = 60.0
xextent=-850000
yextent=-600000
offset=1
elif dom == 'hawaii':
llcrnrlon = -162.3
llcrnrlat = 16.2
urcrnrlon = -153.1
urcrnrlat = 24.3
cen_lat = 20.4
cen_lon = -157.6
xextent=-325000
yextent=-285000
offset=0.25
elif dom == 'puerto_rico':
llcrnrlon = -76.5
llcrnrlat = 13.3
urcrnrlon = -61.0
urcrnrlat = 22.7
cen_lat = 18.4
cen_lon = -66.6
xextent=-925000
yextent=-375000
offset=0.25
elif dom == 'conus':
llcrnrlon = -125.5
llcrnrlat = 20.0
urcrnrlon = -63.5
urcrnrlat = 51.0
cen_lat = 35.4
cen_lon = -97.6
xextent=-2200000
yextent=-675000
offset=1
elif dom == 'northeast':
llcrnrlon = -80.0
llcrnrlat = 40.0
urcrnrlon = -66.5
urcrnrlat = 48.0
cen_lat = 44.0
cen_lon = -76.0
xextent=-175000
yextent=-282791
offset=0.25
elif dom == 'mid_atlantic':
llcrnrlon = -82.0
llcrnrlat = 36.5
urcrnrlon = -73.0
urcrnrlat = 42.5
cen_lat = 36.5
cen_lon = -79.0
xextent=-123114
yextent=125850
offset=0.25
elif dom == 'southeast':
llcrnrlon = -92.0
llcrnrlat = 24.0
urcrnrlon = -75.0
urcrnrlat = 37.0
cen_lat = 30.5
cen_lon = -89.0
xextent=-12438
yextent=-448648
offset=0.25
elif dom == 'south_florida':
llcrnrlon = -84.0
llcrnrlat = 23.0
urcrnrlon = -77
urcrnrlat = 28.0
cen_lat = 24.0
cen_lon = -81.0
xextent=-206000
yextent=-6000
offset=0.25
elif dom == 'ohio_valley':
llcrnrlon = -91.5
llcrnrlat = 34.5
urcrnrlon = -80.0
urcrnrlat = 43.0
cen_lat = 38.75
cen_lon = -88.0
xextent=-131129
yextent=-299910
offset=0.25
elif dom == 'upper_midwest':
llcrnrlon = -97.5
llcrnrlat = 40.0
urcrnrlon = -82.0
urcrnrlat = 49.5
cen_lat = 44.75
cen_lon = -92.0
xextent=-230258
yextent=-316762
offset=0.25
elif dom == 'north_central':
llcrnrlon = -111.5
llcrnrlat = 39.0
urcrnrlon = -94.0
urcrnrlat = 49.5
cen_lat = 44.25
cen_lon = -103.0
xextent=-490381
yextent=-336700
offset=0.25
elif dom == 'central':
llcrnrlon = -103.5
llcrnrlat = 32.0
urcrnrlon = -89.0
urcrnrlat = 42.0
cen_lat = 37.0
cen_lon = -99.0
xextent=-220257
yextent=-337668
offset=0.25
elif dom == 'south_central':
llcrnrlon = -109.0
llcrnrlat = 25.0
urcrnrlon = -88.5
urcrnrlat = 37.5
cen_lat = 31.25
cen_lon = -101.0
xextent=-529631
yextent=-407090
offset=0.25
elif dom == 'northwest':
llcrnrlon = -125.0
llcrnrlat = 40.0
urcrnrlon = -110.0
urcrnrlat = 50.0
cen_lat = 45.0
cen_lon = -116.0
xextent=-540000
yextent=-333623
offset=0.25
elif dom == 'southwest':
llcrnrlon = -125.0
llcrnrlat = 31.0
urcrnrlon = -108.5
urcrnrlat = 42.5
cen_lat = 36.75
cen_lon = -116.0
xextent=-593059
yextent=-377213
offset=0.25
elif dom == 'colorado':
llcrnrlon = -110.0
llcrnrlat = 35.0
urcrnrlon = -101.0
urcrnrlat = 42.0
cen_lat = 38.5
cen_lon = -106.0
xextent=-224751
yextent=-238851
offset=0.25
elif dom == 'boston_nyc':
llcrnrlon = -75.5
llcrnrlat = 40.0
urcrnrlon = -69.5
urcrnrlat = 43.0
cen_lat = 41.5
cen_lon = -76.0
xextent=112182
yextent=-99031
offset=0.25
elif dom == 'seattle_portland':
llcrnrlon = -126.0
llcrnrlat = 44.5
urcrnrlon = -118.0
urcrnrlat = 49.5
cen_lat = 47.0
cen_lon = -121.0
xextent=-275000
yextent=-180000
offset=0.25
elif dom == 'sf_bay_area':
llcrnrlon = -123.5
llcrnrlat = 37.25
urcrnrlon = -121.0
urcrnrlat = 38.5
cen_lat = 48.25
cen_lon = -121.0
xextent=-185364
yextent=-1193027
offset=0.25
elif dom == 'la_vegas':
llcrnrlon = -121.0
llcrnrlat = 32.0
urcrnrlon = -114.0
urcrnrlat = 37.0
cen_lat = 34.5
cen_lon = -114.0
xextent=-540000
yextent=-173241
offset=0.25
# Projection settings
if dom == 'namerica':
extent = [-176.,0.,0.5,45.]
myproj = ccrs.Orthographic(central_longitude=-114, central_latitude=54.0, globe=None)
elif dom == 'alaska':
extent = [llcrnrlon, urcrnrlon, llcrnrlat, urcrnrlat]
myproj = ccrs.Stereographic(central_longitude=cen_lon, central_latitude=cen_lat,
true_scale_latitude=None,false_easting=0.0,false_northing=0.0,globe=None)
elif dom == 'conus':
extent = [llcrnrlon-1, urcrnrlon-6, llcrnrlat, urcrnrlat+1]
myproj=ccrs.LambertConformal(central_longitude=cen_lon, central_latitude=cen_lat,
false_easting=0.0, false_northing=0.0, secant_latitudes=None,
standard_parallels=None, globe=None)
else:
extent = [llcrnrlon, urcrnrlon, llcrnrlat, urcrnrlat]
myproj=ccrs.LambertConformal(central_longitude=cen_lon, central_latitude=cen_lat,
false_easting=0.0, false_northing=0.0, secant_latitudes=None,
standard_parallels=None, globe=None)
return xextent, yextent, offset, extent, myproj
#-------------------------------------------------------#
# 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)
ymdh_model = str(sys.argv[2])
ymd_model = ymdh_model[0:8]
year_model = int(ymdh_model[0:4])
month_model = int(ymdh_model[4:6])
day_model = int(ymdh_model[6:8])
hour_model = int(ymdh_model[8:10])
cyc_model = str(hour_model).zfill(2)
print(year_model, month_model, day_model, hour_model)
fhr = int(sys.argv[3])
fhour = str(fhr).zfill(2)
fhourm24 = str(fhr-24).zfill(2)
print('fhour '+fhour)
# Forecast valid date/time
itime = ymd_model
vtime_start = ndate(ymdh_model,int(fhr-24))
vtime_start = str(vtime_start[0:8])
vtime_end = ymd
# Define the directory paths to the output files
STAGE_DIR = '/lfs/h2/emc/stmp/'+os.environ['USER']+'/rrfs_verif'
PARM_DIR = os.path.join(os.environ['HOMEDIR'],'parm')
COMccpa = os.environ['COMobs']
DCOMmrms = os.environ['COMobs']
HRRR_DIR = os.path.join(os.environ['COMfcst'],'hrrr.'+ymd_model)
NAM_DIR = os.path.join(os.environ['COMfcst'],'nam.'+ymd_model)
RRFS_DIR = os.path.join(
'/','lfs','h2','emc','ptmp',os.environ['USER'],'rrfs','na','prod',
'rrfs.'+ymd_model, cyc_model
)
CCPA_DIR = os.path.join(STAGE_DIR)
MRMS_DIR = os.path.join(STAGE_DIR)
# Set up working directories
if not os.path.exists(os.path.join(CCPA_DIR, 'tmp')):
if not os.path.exists(CCPA_DIR):
os.makedirs(CCPA_DIR)
os.makedirs(os.path.join(CCPA_DIR, 'tmp'))
os.makedirs(os.path.join(CCPA_DIR, 'logs'))
if not os.path.exists(os.path.join(MRMS_DIR, 'tmp')):
if not os.path.exists(MRMS_DIR):
os.makedirs(MRMS_DIR)
os.makedirs(os.path.join(MRMS_DIR, 'tmp'))
os.makedirs(os.path.join(MRMS_DIR, 'logs'))
# Specify plotting domains
domains = ['conus','alaska','hawaii','puerto_rico','boston_nyc','central','colorado','la_vegas','mid_atlantic','north_central','northeast','northwest','ohio_valley','south_central','southeast','south_florida','sf_bay_area','seattle_portland','southwest','upper_midwest']
# Paths to image files
user = str(sys.argv[4])
im = image.imread('/lfs/h2/emc/vpppg/noscrub/'+user+'/RRFS_eval_retro_graphics/noaa.png')
#-------------------------------------------------------#
# Make Python process pools non-daemonic
class NoDaemonProcess(multiprocessing.Process):
# make 'daemon' attribute always return False
@property
def daemon(self):
return False
@daemon.setter
def daemon(self, value):
pass
class NoDaemonContext(type(multiprocessing.get_context())):
Process = NoDaemonProcess
# 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):
def __init__(self, *args, **kwargs):
kwargs['context'] = NoDaemonContext()
super(MyPool, self).__init__(*args, **kwargs)
#-------------------------------------------------------#
def main():
# Number of processes must coincide with the number of domains to plot
pool = MyPool(len(domains))
pool.map(vars_figure,domains)
#-------------------------------------------------------#
def vars_figure(domain):
global dom
dom = domain
print(('Working on '+dom))
global lat,lon,lat_shift,lon_shift,fig,axes,ax1,ax2,ax3,ax4,keep_ax_lst_1,keep_ax_lst_2,keep_ax_lst_3,xextent,yextent,offset,extent,myproj,transform
# Define the input files
if dom == 'alaska':
dom1a_string = 'ak.'
dom1a_string2 = dom
dom1b_string = 'awp242'
dom2_string = 'alaska'
dom3_string = 'ak'
dom5_string = 'ak'
elif dom == 'puerto_rico':
dom1a_string = ''
dom1a_string2 = dom
dom1b_string = 'awp237'
dom2_string = 'prico'
dom3_string = 'pr'
dom5_string = 'pr'
elif dom == 'hawaii':
dom1a_string = ''
dom1a_string2 = dom
dom1b_string = 'awiphi'
dom2_string = 'hawaii'
dom3_string = 'hi'
dom5_string = 'hi'
else:
dom1a_string = ''
dom1a_string2 = 'conus'
dom1b_string = 'awip12'
dom2_string = 'conus'
dom3_string = 'conus'
dom5_string = 'conus'
if dom in ['alaska', 'puerto_rico', 'hawaii']:
use_ccpa = False
else:
use_ccpa = True
fhour_03 = str(fhr - 21).zfill(2)
fhour_06 = str(fhr - 18).zfill(2)
fhour_09 = str(fhr - 15).zfill(2)
fhour_12 = str(fhr - 12).zfill(2)
fhour_15 = str(fhr - 9).zfill(2)
fhour_18 = str(fhr - 6).zfill(2)
fhour_21 = str(fhr - 3).zfill(2)
plot_nodata_text = [False, False, False, False, False, False]
fname1a = HRRR_DIR+f'/{dom1a_string2}/hrrr.t'+cyc_model+'z.wrfprsf'+fhour+f'.{dom1a_string}grib2'
fname1a_fm24 = HRRR_DIR+f'/{dom1a_string2}/hrrr.t'+cyc_model+'z.wrfprsf'+fhourm24+f'.{dom1a_string}grib2'
fname1b_03 = NAM_DIR+'/nam.t'+cyc_model+f'z.{dom1b_string}'+fhour_03+'.tm00.grib2'
fname1b_06 = NAM_DIR+'/nam.t'+cyc_model+f'z.{dom1b_string}'+fhour_06+'.tm00.grib2'
fname1b_09 = NAM_DIR+'/nam.t'+cyc_model+f'z.{dom1b_string}'+fhour_09+'.tm00.grib2'
fname1b_12 = NAM_DIR+'/nam.t'+cyc_model+f'z.{dom1b_string}'+fhour_12+'.tm00.grib2'
fname1b_15 = NAM_DIR+'/nam.t'+cyc_model+f'z.{dom1b_string}'+fhour_15+'.tm00.grib2'
fname1b_18 = NAM_DIR+'/nam.t'+cyc_model+f'z.{dom1b_string}'+fhour_18+'.tm00.grib2'
fname1b_21 = NAM_DIR+'/nam.t'+cyc_model+f'z.{dom1b_string}'+fhour_21+'.tm00.grib2'
fname1b_24 = NAM_DIR+'/nam.t'+cyc_model+f'z.{dom1b_string}'+fhour+'.tm00.grib2'
fname2_03 = NAM_DIR+'/nam.t'+cyc_model+f'z.{dom2_string}nest.hiresf'+fhour_03+'.tm00.grib2'
fname2_06 = NAM_DIR+'/nam.t'+cyc_model+f'z.{dom2_string}nest.hiresf'+fhour_06+'.tm00.grib2'
fname2_09 = NAM_DIR+'/nam.t'+cyc_model+f'z.{dom2_string}nest.hiresf'+fhour_09+'.tm00.grib2'
fname2_12 = NAM_DIR+'/nam.t'+cyc_model+f'z.{dom2_string}nest.hiresf'+fhour_12+'.tm00.grib2'
fname2_15 = NAM_DIR+'/nam.t'+cyc_model+f'z.{dom2_string}nest.hiresf'+fhour_15+'.tm00.grib2'
fname2_18 = NAM_DIR+'/nam.t'+cyc_model+f'z.{dom2_string}nest.hiresf'+fhour_18+'.tm00.grib2'
fname2_21 = NAM_DIR+'/nam.t'+cyc_model+f'z.{dom2_string}nest.hiresf'+fhour_21+'.tm00.grib2'
fname2_24 = NAM_DIR+'/nam.t'+cyc_model+f'z.{dom2_string}nest.hiresf'+fhour+'.tm00.grib2'
fname3 = RRFS_DIR+'/rrfs.t'+cyc_model+'z.prslev.f0'+fhour+f'.{dom3_string}.grib2'
fname3_fm24 = RRFS_DIR+'/rrfs.t'+cyc_model+'z.prslev.f0'+fhourm24+f'.{dom3_string}.grib2'
if use_ccpa:
fname5 = CCPA_DIR+f'/ccpa.{ymd}/ccpa.t'+cyc+f'z.a24h.{dom5_string}.nc'
else:
fname5 = MRMS_DIR+f'/mrms.{ymd}/mrms.t'+cyc+f'z.a24h.{dom}.nc'
for fname in [
fname1a, fname1a_fm24
]:
if not os.path.exists(fname):
plot_nodata_text[0] = True
break
if not plot_nodata_text[0]:
data1a = grib2io.open(fname1a)
data1a_fm24 = grib2io.open(fname1a_fm24)
for fname1b in [
fname1b_03, fname1b_06, fname1b_09, fname1b_12,
fname1b_15, fname1b_18, fname1b_21, fname1b_24
]:
if not os.path.exists(fname1b):
plot_nodata_text[1] = True
break
if not plot_nodata_text[1]:
data1b_03 = grib2io.open(fname1b_03)
data1b_06 = grib2io.open(fname1b_06)
data1b_09 = grib2io.open(fname1b_09)
data1b_12 = grib2io.open(fname1b_12)
data1b_15 = grib2io.open(fname1b_15)
data1b_18 = grib2io.open(fname1b_18)
data1b_21 = grib2io.open(fname1b_21)
data1b_24 = grib2io.open(fname1b_24)
for fname2 in [
fname2_03, fname2_06, fname2_09, fname2_12,
fname2_15, fname2_18, fname2_21, fname2_24]:
if not os.path.exists(fname2):
plot_nodata_text[2] = True
break
if not plot_nodata_text[2]:
data2_03 = grib2io.open(fname2_03)
data2_06 = grib2io.open(fname2_06)
data2_09 = grib2io.open(fname2_09)
data2_12 = grib2io.open(fname2_12)
data2_15 = grib2io.open(fname2_15)
data2_18 = grib2io.open(fname2_18)
data2_21 = grib2io.open(fname2_21)
data2_24 = grib2io.open(fname2_24)
for fname in [
fname3, fname3_fm24
]:
if not os.path.exists(fname):
plot_nodata_text[3] = True
print(
f"WARNING: No RRFS file found for VDATE={cyc}Z {ymd} at F{fhour}: {fname}"
)
break
if not plot_nodata_text[3]:
print(f"File exists: {fname3}. Attempting to open!")
try:
data3 = grib2io.open(fname3)
except KeyError:
plot_nodata_text[3] = True
print(f"File exists: {fname3_fm24}. Attempting to open!")
try:
data3_fm24 = grib2io.open(fname3_fm24)
except KeyError:
plot_nodata_text[3] = True
if os.path.exists(fname5):
data5 = Dataset(fname5,'r')
else:
plot_nodata_text[5] = True
# Get the lats and lons
if not plot_nodata_text[0]:
msg = data1a.select(shortName='HGT', level='500 mb')[0] # msg is a Grib2Message object
lat1a,lon1a,lat1a_shift,lon1a_shift = get_latlons_pcolormesh(msg)
if not plot_nodata_text[1]:
msg = data1b_24.select(shortName='HGT', level='surface')[0] # msg is a Grib2Message object
lat1b,lon1b,lat1b_shift,lon1b_shift = get_latlons_pcolormesh(msg)
if not plot_nodata_text[2]:
msg = data2_24.select(shortName='HGT', level='500 mb')[0] # msg is a Grib2Message object
lat2,lon2,lat2_shift,lon2_shift = get_latlons_pcolormesh(msg)
if not plot_nodata_text[3]:
msg = data3.select(shortName='HGT', level='500 mb')[0] # msg is a Grib2Message object
lat3,lon3,lat3_shift,lon3_shift = get_latlons_pcolormesh(msg)
# CCPA/MRMS
if not plot_nodata_text[5]:
if dom in ['alaska','hawaii','puerto_rico']:
lat5 = data5.variables['lat'][:]
lon5 = data5.variables['lon'][:]
lon5, lat5 = np.meshgrid(lon5, lat5)
else:
lat5 = data5.variables['lat'][:,:]
lon5 = data5.variables['lon'][:,:]
###################################################
# Read in all variables and calculate differences #
###################################################
t1a = time.perf_counter()
global qpf_1a,qpf_1b,qpf_2,qpf_3,qpf_5
# Total Precipitation
if not plot_nodata_text[0]:
qpf_1a_f = data1a.select(shortName='APCP',timeRangeOfStatisticalProcess=fhr)[0].data * 0.0393701
qpf_1a_fm24 = data1a_fm24.select(shortName='APCP',timeRangeOfStatisticalProcess=(fhr-24))[0].data * 0.0393701
qpf_1a = qpf_1a_f - qpf_1a_fm24
if not plot_nodata_text[1]:
qpf_1b_03 = data1b_03.select(shortName='APCP',timeRangeOfStatisticalProcess=3)[0].data * 0.0393701
qpf_1b_06 = data1b_06.select(shortName='APCP',timeRangeOfStatisticalProcess=3)[0].data * 0.0393701