-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotrain.py
206 lines (185 loc) · 8.49 KB
/
plotrain.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
import sys
from mpl_toolkits.basemap import Basemap, cm, shiftgrid
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import os
import time
import pygrib
import color
from area import *
if __name__ == "__main__":
ra=color.rain
fr=color.freezing
sn=color.sonw
utc = 0
# plot the diagram of 10m wind + 2m T + MSLPz
def plotRain(file, areatype):
# set boundary through areatype
boundary = ''
tmpstr = 'boundary=' + areatype
ldict = locals()
exec(tmpstr, globals(), ldict)
boundary = ldict['boundary']
#print(boundary)
#read in files
grbs = pygrib.open('rawfile/' + file)
# extract data from grib file
Temperature = grbs.select(name='2 metre temperature')[0]
Precipitation = grbs.select(name='Total Precipitation')[0]
subP = Precipitation.values
rain = grbs.select(name='Categorical rain')[0]
subR = rain.values
del rain
snow = grbs.select(name='Categorical snow')[0]
subS = snow.values
del snow
freezing = grbs.select(name='Categorical freezing rain')[0]
subF = freezing.values
del freezing
ice = grbs.select(name='Categorical ice pellets')[0]
subI = ice.values
del ice
# define longitude and latitude
lats, lons = Precipitation.latlons()
lats = (lats.T)[0]
lons = lons[0]
del Precipitation
# define the initial forecast hour
analysistime = Temperature.analDate
fcit = analysistime.timetuple() # time.struct_time
formatfcit = time.strftime('%Hz %m %d %Y', fcit) # formatted initial time
timestampfcit = time.mktime(fcit) # timestamp of initial time
fcst = Temperature.forecastTime # integer
formatvalid = time.strftime('%Hz %m %d %Y', time.localtime(timestampfcit + fcst * 60 * 60)) # formatted validtime
#calculate
rain1 = subP*(subR+subS+subF+subI)
snow1 = subP*(subS+subF+subI)
freezing1 = subP*(subF+subI)
del subP, subR, subS, subF, subI
nrain=np.ma.array(rain1,mask=(rain1==0))
del rain1
nsnow=np.ma.array(snow1,mask=(snow1==0))
del snow1
nfreezingice=np.ma.array(freezing1,mask=(freezing1==0))
del freezing1
# generatre basemap
m = Basemap(llcrnrlon=boundary[0], llcrnrlat=boundary[1], urcrnrlon=boundary[2], urcrnrlat=boundary[3],
projection='lcc', lat_0=boundary[4], lon_0=boundary[5], resolution='l', area_thresh=100)
lon, lat = np.meshgrid(lons, lats)
x, y = m(lon, lat)
fig = plt.figure(figsize=(10,7), dpi=150)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['left'].set_color('none')
ax.spines['bottom'].set_color('none')
ax.spines['top'].set_color('none')
# generate legend
y1 = mpl.colors.LinearSegmentedColormap('my_colormap',ra,256)
y2 = mpl.colors.LinearSegmentedColormap('my_colormap',sn,256)
y3 = mpl.colors.LinearSegmentedColormap('my_colormap',fr,256)
norm=mpl.colors.Normalize(0, 750)
print('contourf')
#c=plt.contourf(x, y, rt, 750, cmap=my_cmap, norm=norm)
plt.contourf(x, y, nrain, 75, cmap=y1, norm=norm)
plt.contourf(x, y, nsnow, 75, cmap=y2, norm=norm)
plt.contourf(x, y, nfreezingice, 75, cmap=y3, norm=norm)
print('STOP')
#, alpha=0.6
plt.title('GFS 6-hour Averaged Precip Rate\nlnit:' + formatfcit + ' Forecast Hour[' + str(fcst) + '] valid at ' + formatvalid + '\n@Myyd & Louis-He',
loc='left', fontsize=11)
m.drawparallels(np.arange(-90., 90., 10), labels=[1,0,0,0], fontsize=8, linewidth=0.5,color='dimgrey',dashes=[1,1])
m.drawmeridians(np.arange(0., 180., 10), labels=[0,0,0,1], fontsize=8, linewidth=0.5,color='dimgrey',dashes=[1,1])
m.drawcoastlines(linewidth=0.5)
m.drawstates(linewidth=0.4,color='dimgrey')
#m.readshapefile('/mnt/c/Users/10678/Desktop/GFS/shp/cnhimap', 'states', drawbounds=True, linewidth=0.5, color='black')
ax2 = fig.add_axes([0.85, 0.13, 0.012, 0.23])
clevs1=[0,0.1,10,25,50,100,250,400,750]
cmap1=mpl.colors.ListedColormap([[1, 1, 1], [144/255, 238/255, 144/255],
[34/255, 139/255, 34/255], [0, 191/255, 1],
[0, 0, 1],[1, 0, 1],
[205/255, 18/255, 118/255],[104/255, 39/255, 139/255]])
norm1=mpl.colors.BoundaryNorm(clevs1, cmap1.N)
cbar1=mpl.colorbar.ColorbarBase(ax2, cmap=cmap1, spacing='uniform', norm=norm1, ticks=clevs1,
orientation='vertical', drawedges=False)
cbar1.ax.set_ylabel('rain(mm)', size=4)
cbar1.ax.tick_params(labelsize=4)
clevs2=[0,0.1,10,25,50,100,250,750]
ax3 = fig.add_axes([0.85, 0.38, 0.012, 0.23])
cmap2=mpl.colors.ListedColormap([[1, 1, 1], [253/255, 216/255, 213/255],
[251/255, 174/255, 185/255], [247/255, 109/255, 163/255],
[211/255, 47/255, 146/255],[146/255, 1/255, 122/255],
[81/255, 0, 108/255]])
norm2=mpl.colors.BoundaryNorm(clevs2, cmap2.N)
cbar2=mpl.colorbar.ColorbarBase(ax3, cmap=cmap2, spacing='uniform', norm=norm2, ticks=clevs2,
orientation='vertical', drawedges=False)
cbar2.ax.set_ylabel('freezing/ice(mm)', size=4)
cbar2.ax.tick_params(labelsize=4)
clevs3=[0,0.1,2.5,5,10,20,30,750]
ax4 = fig.add_axes([0.85, 0.64, 0.012, 0.23])
cmap3=mpl.colors.ListedColormap([[1, 1, 1], [234/255, 234/255, 234/255],
[200/255, 200/255, 200/255], [154/255, 154/255, 154/255],
[108/255, 108/255, 108/255],[58/255, 58/255, 58/255],
[6/255, 6/255, 6/255]])
norm3=mpl.colors.BoundaryNorm(clevs3, cmap3.N)
cbar3=mpl.colorbar.ColorbarBase(ax4, cmap=cmap3, spacing='uniform', norm=norm3, ticks=clevs3,
orientation='vertical', drawedges=False)
cbar3.ax.set_ylabel('snow(mm)', size=4)
cbar3.ax.tick_params(labelsize=4)
#Temperature(℃)
#GFS 10m Wind and 2m Air Temperature\nlnit:00z Nov 04 2017 Forecast Hour[36] valid at 12z Sun,Nov 05 2017 6-hour #ERA Interim 850hpa Wind speed and Precipitation & 500hpa Geopotential Height#Streamlines
plt.savefig('product/RAIN/' + areatype + file + '.png', bbox_inches='tight')
# delete plot for memory
del fig
plt.cla
plt.clf()
plt.close(0)
del m, lon, lat, lons, lats, y1, y2, y3, norm, norm1, norm2, norm3, cbar1, cbar2, cbar3, ax, ax2, ax3, ax4, x, y, analysistime, fcit, formatfcit, timestampfcit, fcst, formatvalid
nargs=len(sys.argv)
skip=False
for i in range(1,nargs):
if not skip:
arg=sys.argv[i]
#print ("INFO: processing",arg)
if arg == "--path":
if i != nargs-1:
file = sys.argv[i+1]
skip=True
elif arg == "--area":
if i != nargs-1:
pic=sys.argv[i+1]
skip=True
else:
print ("ERR: unknown arg:",arg)
else:
skip=False
path = 'rawfile/' + file
if file[0:3] == 'gfs':
try:
plotRain(file, areatype=pic)
print('[Compele Plotting] File:' + file)
f = open('sysreport/plotreport.txt', 'a+')
f.write('[' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + utc * 60 * 60)) + ']' + '\t' +
file + ' Rain PLOT SUCCESS\n')
f.close()
f = open('sysreport/running.txt', 'a+')
f.write('[' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + utc * 60 * 60)) + ']' + '\t' +
file + ' Rain PLOT SUCCESS\n')
f.close()
del f
except:
print('[ERR:unknown] File:' + file)
f = open('sysreport/plotreport.txt', 'a+')
f.write('[' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + utc * 60 * 60)) + ']' + '\t' +
file + ' Rain PLOT FAILED! PLEASE CHECK\n')
f.close()
f = open('sysreport/errreport.txt', 'a+')
f.write('[' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + utc * 60 * 60)) + ']' + '\t' +
file + ' Rain PLOT FAILED! PLEASE CHECK!\n')
f.close()
f = open('sysreport/running.txt', 'a+')
f.write('[' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + utc * 60 * 60)) + ']' + '\t' +
file + ' Rain PLOT FAILED! PLEASE CHECK!\n')
f.close()
del f