-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathping.py
executable file
·117 lines (111 loc) · 5.88 KB
/
ping.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
#!/usr/bin/env python
# import stuff
import urllib, re, calendar, time
# misc prep
now = time.gmtime() # set year to this year; needed for baseTime
baseTime = calendar.timegm((int(time.strftime("%Y", now)),1,1,0,0,0,0,0,0))
nowTime = calendar.timegm((int(time.strftime("%Y", now)), int(time.strftime("%m", now)), int(time.strftime("%d", now)), int(time.strftime("%H", now)), int(time.strftime("%M", now)),0,0, 0,0))
ptypeLst = ["Hail","Test","None","Rain","Drizzle","Freezing Rain","Freezing Drizzle","Rain/Snow","Ice Pellets/Snow","Rain/Ice Pellets","Snow","Wet Snow","Ice Pellets","Graupel"]
### Step 1: Process the raw data ###############################################
# open PING homepage
raw = urllib.urlopen("http://www.nssl.noaa.gov/projects/ping/display/ping.php")
# remove top junk
daynum, timenum, lat, lon, ptype = [], [], [], [], []
rawtimenum = []
hailmag = []
print("::: File downloaded, now processing PING data")
for line in raw:
if line[:2] == 'pr': # ptype report
match = re.search('\[(.+?)\]\=\[(.+?)\,(.+?)\,(.+?)\,(.+?)\]',line)
if match:
daynum.append(match.group(1))
rawtimenum.append(int(match.group(2)))
timenum.append(time.strftime("%m/%d/%Y %H:%M UTC", time.gmtime(baseTime + (int(match.group(2))*60))))
lat.append(match.group(3))
lon.append(match.group(4))
ptype.append(match.group(5))
hailmag.append('-999') # placeholder
elif line[:2] == 'hr': # hail report
match = re.search('\[(.+?)\]\=\[(.+?)\,(.+?)\,(.+?)\,(.+?)\]',line)
if match:
daynum.append(match.group(1))
rawtimenum.append(int(match.group(2)))
timenum.append(time.strftime("%m/%d/%Y %H:%M UTC", time.gmtime(baseTime + (int(match.group(2))*60))))
lat.append(match.group(3))
lon.append(match.group(4))
hailmag.append(match.group(5))
ptype.append('0')
print("::: Done processing PING data, now creating placefiles")
# post-processing to grab latest x hours
minTime = min(rawtimenum)
maxTime = max(rawtimenum)
nowMinutes = (nowTime - baseTime) / 60
times = {}
#times[15] = nowMinutes - 15
#times[30] = nowMinutes - 30
times[600] = nowMinutes - 600
### Step 2: Create the placefile ##################################################
for t in times.keys():
# open placefile to write
f = open('ping-' + str(t) + 'min.txt', 'w')
# write basic header stuff for placefile
f.write('Title: Latest ' + str(t) + 'min PING reports\n')
f.write('Refresh: 5\n') # refresh time in minutes
f.write('Color: 255 255 255\n') # default color to be used
f.write('IconFile: 1, 15, 15, 8, 8, "pingIcons.png"\n')
# fileNum, width, height, hotX, hotY, fileName
f.write('Font: 1, 11, 1, "Courier New"\n') # whatever
message = "; Created by Joe Moore \n; Generated at " + time.strftime("%x %X %Z") + "\n; Found " + str(len(daynum)) + " total reports\n; Made with Python!\n; Public Domain"
f.write(message + '\n\n')
reports = 0
# Now let's write the actual data!
for i in range(len(daynum)):
if rawtimenum[i] > times[t]:
reports += 1
f.write('Object: ' + lat[i] + ', ' + lon[i] + '\n')
# Everything else must have a tab! I think!
f.write('Threshold: 999\n') # Display at any zoom level (?)
if ptype[i] == '0': # if hail
f.write(' Icon: 0, 0, 000, 1, ' + str(int(ptype[i])+1) + ', "Time: ' + timenum[i] + '\\nPtype: ' + ptypeLst[int(ptype[i])] + '\\nHail Size: ' + str(float(hailmag[i])/4.) + ' inches"\n')
f.write(' Text: 0, -15, 1, "' + str(float(hailmag[i])/4.) + '" "\n')
else: # else, any other ptype
f.write(' Icon: 0, 0, 000, 1, ' + str(int(ptype[i])+1) + ', "Time: ' + timenum[i] + '\\nPtype: ' + ptypeLst[int(ptype[i])] + '"\n')
f.write('End:\n\n')
f.write('; Plotted ' + str(reports) + ' reports')
f.close()
log = open(str(t) + 'ALLreportCount.txt', 'a')
log.write(time.strftime("%x %X %Z") + ' ' + str(reports) + '\n')
log.close()
print("::: Done writing ALL type Placefiles")
# Now create hail-only placefiles
for t in times.keys():
# open placefile to write
h = open('ping-hail-' + str(t) + 'min.txt', 'w')
# write basic header stuff for placefile
h.write('Title: Latest ' + str(t) + 'min PING Hail reports\n')
h.write('Refresh: 5\n') # refresh time in minutes
h.write('Color: 255 255 255\n') # default color to be used
h.write('IconFile: 1, 15, 15, 8, 8, "pingIcons.png"\n')
# fileNum, width, height, hotX, hotY, fileName
h.write('Font: 1, 11, 1, "Courier New"\n') # whatever
message = "; Created by Joe Moore \n; Generated at " + time.strftime("%x %X %Z") + "\n; Found " + str(len(daynum)) + " total reports\n; Made with Python!\n; Public Domain"
h.write(message + '\n\n')
reports = 0
# Now let's write the actual data!
for i in range(len(daynum)):
if rawtimenum[i] > times[t] and ptype[i] == '0':
reports += 1
h.write('Object: ' + lat[i] + ', ' + lon[i] + '\n')
h.write('Threshold: 999\n') # Display at any zoom level (?)
h.write(' Icon: 0, 0, 000, 1, ' + str(int(ptype[i])+1) + ', "Time: ' + timenum[i] + '\\nHail Size: ' + str(float(hailmag[i])/4.) + ' inches"\n')
h.write(' Text: 0, -15, 1, "' + str(float(hailmag[i])/4.) + '" "\n')
# or, for hail size on the ball
#h.write(' Text: 0, 0, 1, "' + str(float(hailmag[i])/4.) + '" "\n')
h.write('End:\n\n')
h.write('; Plotted ' + str(reports) + ' reports')
h.close()
log = open(str(t) + 'HAILreportCount.txt', 'a')
log.write(time.strftime("%x %X %Z") + ' ' + str(reports) + '\n')
log.close()
print("::: Done writing HAIL ONLY Placefiles")
print("::: All done!")