-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotWTP_NA.py
172 lines (149 loc) · 7.05 KB
/
plotWTP_NA.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
import sys
import pygrib
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
if __name__ == "__main__":
ys=color.temp
utc = 0
# plot the diagram of 10m wind + 2m T + MSLPz
def plotWTP(file):
#read in files
grbs = pygrib.open('rawfile/' + file)
# extract data from grib file
Temperature = grbs.select(name='2 metre temperature')[0]
wind10m_u = grbs.select(name='10 metre U wind component')[0]
wind10m_v = grbs.select(name='10 metre V wind component')[0]
MSLP = grbs.select(name='MSLP (Eta model reduction)')[0]
# define longitude and latitude
lats, lons = Temperature.latlons()
lats = (lats.T)[0]
lons = lons[0]
# 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
# extract each data
subT = Temperature.values - 273.15
subWU = wind10m_u.values
subWV = wind10m_v.values
subMSLP = MSLP.values / 100.0
# delete unnecessary variables
del Temperature
del wind10m_u
del wind10m_v
del MSLP
del grbs
# generatre basemap
m = Basemap(llcrnrlon=-105,llcrnrlat=25,urcrnrlon=-50,urcrnrlat=55,projection='lcc',lat_0=40, lon_0=-80,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')
my_cmap = mpl.colors.LinearSegmentedColormap('my_colormap',ys,256)
norm=mpl.colors.Normalize(-60, 50)
#c=plt.contourf(x, y, rt, 750, cmap=my_cmap, norm=norm)
m.contourf(x, y, subT, 110, cmap=my_cmap, norm=norm)#,norm=norm cmaps.temp_19lev NCV_jaisnd
d=m.contour(x, y, subT, 110, colors = 'red', linewidths=0.6, levels=0)
d1=m.contour(x, y, subMSLP, 70, colors = 'whitesmoke', linewidths=0.5)#, alpha=0.6
plt.clabel(d, inline = True, fmt='%.0f℃', fontsize=8)
plt.clabel(d1, inline = True, fmt='%.0f', colors='whitesmoke', fontsize=7)#alpha=0.6,
skip=slice(None,None,5)
#m.streamplot(x, y, u, v, linewidth=0.25, density=4, color='black', arrowsize=0.4, arrowstyle='->')
m.barbs(x[skip,skip], y[skip,skip], subWU[skip,skip], subWV[skip,skip], length=3.5,
sizes=dict(emptybarb=0, spacing=0.2, height=0.5),barb_increments=dict(half=2, full=4, flag=20 ),
linewidth=0.2, color='black')
plt.title('GFS 10m Wind & 2m Air Temperature & MSLP\nlnit:' + formatfcit + ' Forecast Hour[' + str(fcst) + '] valid at ' + formatvalid + '\n@myyd & Louis-He',
loc='left', fontsize=11)
m.drawparallels(np.arange(0, 65, 10), labels=[1,0,0,0], fontsize=8, linewidth=0.5,color='dimgrey',dashes=[1,1])
m.drawmeridians(np.arange(65., 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('D:\\shp\\provinces', 'states', drawbounds=True, linewidth=0.5, color='black')
ax2 = fig.add_axes([0.88, 0.11, 0.018, 0.77])
cbar=mpl.colorbar.ColorbarBase(ax2, cmap=my_cmap, norm=norm, orientation='vertical', drawedges=False)
cbar.set_ticks(np.linspace(-60,50,23))
cbar.ax.set_ylabel('Temperature(℃)', size=8)#Temperature(℃)
cbar.ax.tick_params(labelsize=8)
#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 Temperature & 500hpa Geopotential Height#Streamlines
plt.savefig('product/WTP_NA/' + file + '.png', bbox_inches='tight')
# delete plot for memory
del fig
plt.cla
plt.clf()
plt.close(0)
del subMSLP, subWU, subWV, subT, m, lon, lat, lons, lats, my_cmap, norm, d, d1, cbar, ax, ax2, x, y, skip, analysistime, fcit, formatfcit, timestampfcit, fcst, formatvalid
# run at the beginning of the program
def initialize():
os.system('rm -rf product/')
print('['+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + utc * 60 * 60))+']'+'Erase expired product')
os.system('mkdir product')
os.system('mkdir product/WTP')
print('[' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + utc * 60 * 60)) + ']' + 'Create product folder')
f = open('/root/GFS/sysreport/plotreport.txt', 'w+')
f.close()
print('[' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + utc * 60 * 60)) + ']' + 'Create system report file...')
f = open('/root/GFS/sysreport/errreport.txt', 'w+')
f.close()
print('[' + time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime(time.time() + utc * 60 * 60)) + ']' + 'Create system error report file...')
print('[' + time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime(time.time() + utc * 60 * 60)) + ']' + 'Start to plot...')
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
else:
print ("ERR: unknown arg:",arg)
else:
skip=False
#initialize()
path = 'rawfile/' + file
if file[0:3] == 'gfs':
try:
plotWTP(file)
print('[Compele Plotting] File:' + file)
f = open('/root/GFS/sysreport/plotreport.txt', 'a+')
f.write('[' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + utc * 60 * 60)) + ']' + '\t' +
file + ' PLOT SUCCESS\n')
f.close()
f = open('/root/GFS/sysreport/running.txt', 'a+')
f.write('[' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + utc * 60 * 60)) + ']' + '\t' +
file + ' PLOT SUCCESS\n')
f.close()
del f
except:
print('[ERR:unknown] File:' + file)
f = open('/root/GFS/sysreport/plotreport.txt', 'a+')
f.write('[' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + utc * 60 * 60)) + ']' + '\t' +
file + ' PLOT FAILED! PLEASE CHECK\n')
f.close()
f = open('/root/GFS/sysreport/errreport.txt', 'a+')
f.write('[' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + utc * 60 * 60)) + ']' + '\t' +
file + ' PLOT FAILED! PLEASE CHECK!\n')
f.close()
f = open('/root/GFS/sysreport/running.txt', 'a+')
f.write('[' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + utc * 60 * 60)) + ']' + '\t' +
file + ' PLOT FAILED! PLEASE CHECK!\n')
f.close()
del f