-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
106 lines (93 loc) · 4.06 KB
/
plot.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
import pygrib
from mpl_toolkits.basemap import Basemap, cm
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import numpy as np
import os
import time
utc = -5
def plotmaxT(filepath):
grbs = pygrib.open('rawfile/' + filepath)
grb = grbs.select(name='Maximum temperature')[0]
maxt = grb.values.T
lats, lons = grb.latlons()
maxt, lats, lons = grb.data(lat1=-90,lat2=90,lon1=0,lon2=359.75)
maxt2=[]
for i in range(maxt.shape[0]):
maxt2.append(maxt[maxt.shape[0]-i-1])
maxt = np.array(maxt2)
#print(maxt)
# create figure and axes instances
fig = plt.figure(figsize=(11,7))
ax = fig.add_axes([0.1,0.1,0.8,0.8])
'''
m = Basemap(llcrnrlon=0., llcrnrlat=-90., urcrnrlon=359.75, urcrnrlat=90., \
rsphere=(6378137.00, 6356752.3142), \
resolution='l', projection='merc', \
lat_0=0.25, lon_0=0.25, lat_ts=20.)
'''
m = Basemap(llcrnrlon=0., llcrnrlat=-90., urcrnrlon=359.75, urcrnrlat=90.)
#m = Basemap(projection='robin',lon_0=0,resolution='c')
# draw coastlines, state and country boundaries, edge of map.
m.drawcoastlines()
m.drawstates()
m.drawcountries()
# draw parallels.
parallels = np.arange(-90.,90.,20.)
m.drawparallels(parallels,labels=[1,0,0,0],fontsize=10)
# draw meridians
meridians = np.arange(0.,360.,20.)
m.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10)
ny = maxt.shape[0]; nx = maxt.shape[1]
lons, lats = m.makegrid(nx, ny) # get lat/lons of ny by nx evenly space grid.
x, y = m(lons, lats) # compute map proj coordinates.
# draw filled contours.
clevs = [-40,-36,-32,-28,-24,-20,-16,-12,-8,-4,0,4,8,12,16,20,24,28,32,36,40]
cs = m.contourf(x,y,maxt - 273.15,clevs,cmap=cm.GMT_haxby)
# add colorbar.
cbar = m.colorbar(cs,location='bottom',pad="5%")
cbar.set_label('°C')
# add title
plt.title('example plot: ' + filepath + 'z global 2m maximum Temprature (°C)')
plt.savefig('product/' + filepath + '.png')
plt.clf()
plt.close(fig)
# 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 rawfile')
os.system('mkdir product')
print('[' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + utc * 60 * 60)) + ']' + 'Create rawfile 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...')
path = 'rawfile/'
files= os.listdir(path)
print(files)
for file in files:
if file[0:3] == 'gfs':
try:
plotmaxT(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()
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()