forked from akrherz/pyWWA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
STOIA_parser.py
221 lines (191 loc) · 7.77 KB
/
STOIA_parser.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
I parse the Iowa Road Condition reports AFOS PIL: STOIA
I also generate a shapefile of the parsed data
"""
import sys
import re
import datetime
import pytz
import dbflib
import shapelib
import zipfile
import os
import logging
import shutil
import StringIO
import traceback
from pyiem import wellknowntext
import subprocess
import ConfigParser
config = ConfigParser.ConfigParser()
config.read(os.path.join(os.path.dirname(__file__), 'cfg.ini'))
import common
import psycopg2
import psycopg2.extras
POSTGIS = psycopg2.connect(database="postgis",
host=config.get('database','host'),
user=config.get('database','user'),
password=config.get('database','password'))
pcursor = POSTGIS.cursor(cursor_factory=psycopg2.extras.DictCursor)
FORMAT = "%(asctime)-15s:: %(message)s"
logging.basicConfig(filename='logs/ingestRC.log', filemode='a+', format=FORMAT)
logger=logging.getLogger()
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)
errors = StringIO.StringIO()
# Changedir to /tmp
os.chdir("/tmp")
def figureCondition(condition, conditions):
""" Find this condition within a dict of conditions """
for typ in ["Closed", "Travel Advisory", "CC Ice", "CC Snow", "CC Slush",
"CC Mixed", "MC Ice", "MC Snow", "MC Slush", "MC Mixed",
"PC Ice", "PC Snow", "PC Slush", "PC Mixed", "CC Frost",
"MC Frost", "PC Frost", "Wet", "Normal", "No Conditions Reproted"]:
if condition.find( typ.upper() ) > -1:
if (not conditions.has_key(typ.upper())):
logger.info("Unknown Condition: %s\n" % (typ,) )
pcursor.execute("SELECT max(code) as m from roads_conditions")
row = pcursor.fetchone()
if row['m'] is None:
newID = 1
else:
newID = int( row['m'] ) + 1
pcursor.execute("INSERT into roads_conditions VALUES (%s, %s) ",
(newID, typ) )
conditions[typ.upper()] = newID
return conditions[typ.upper()]
return conditions["NORMAL"]
def process(raw):
""" Actually do something """
# Load up dictionary of Possible Road Conditions
conditions = {}
condcodes = {}
pcursor.execute("SELECT label, code from roads_conditions")
for row in pcursor:
conditions[ row['label'].upper() ] = row['code']
condcodes[ int(row['code']) ] = row['label'].upper()
# Load up dictionary of roads...
roads = {}
pcursor.execute("SELECT major, minor, segid from roads_base")
for row in pcursor:
roads["%s%s" % (row['major'], row['minor'].upper())] = row['segid']
# Figure out when this report is valid
tokens = re.findall("([0-9]{1,2})([0-9][0-9]) ([AP]M) C[DS]T [A-Z][A-Z][A-Z] ([A-Z][A-Z][A-Z]) ([0-9]+) (2[0-9][0-9][0-9])\n", raw)
# tokens is like [('08', '52', 'AM', 'NOV', '23', '2004')]
hroffset = 0
if tokens[0][2] == "PM" and int(tokens[0][0]) < 12:
hroffset = 12
if tokens[0][2] == "AM" and int(tokens[0][0]) == 12:
hroffset = -12
hr = int(tokens[0][0]) + hroffset
mi = int(tokens[0][1])
mod = {"JAN": 1, "FEB": 2, "MAR": 3, "APR": 4, "MAY": 5, "JUN": 6, "JUL": 7,
"AUG": 8, "SEP": 9, "OCT":10, "NOV":11, "DEC":12}
mo = mod[ tokens[0][3] ]
dy = int(tokens[0][4])
year = int(tokens[0][5])
ts = datetime.datetime(year, mo, dy, hr, mi).replace(
tzinfo=pytz.timezone("America/Chicago"))
logger.info("PROCESSING STOIA: %s" % (ts,))
# Lets start our processing
lines = re.split("\n", raw[ raw.find("*"):])
for line in lines:
if (len(line) < 20 or line[0] == "*" or line[6] != " " or line[7] == " "):
continue
if (line[0] != " "):
major = (line[:6]).strip()
minor = (line[7:47]).strip().upper()
condition = (line[47:]).strip().upper()
#----------------------------------------
# Now we are going to do things by type!
roadCondCode = figureCondition(condition, conditions)
#print roadCondCode, condition, condcodes[roadCondCode]
towingProhibited = (condition.find("TOWING PROHIBITED") > -1)
limitedVis = (condition.find("LIMITED VIS.") > -1)
rkey = "%s%s" % (major, minor)
if not roads.has_key(rkey):
logger.info("Unknown Road: %s\n" % (rkey,) )
continue
segid = roads[rkey]
pcursor.execute("""UPDATE roads_current SET cond_code = %s, valid = %s,
towing_prohibited = %s, limited_vis = %s, raw = %s
WHERE segid = %s """ , (roadCondCode, ts,
towingProhibited, limitedVis, condition,
segid) )
# Copy the currents table over to the log... HARD CODED
pcursor.execute("INSERT into roads_%s_log SELECT * from roads_current" % (
ts.year,))
return ts
def generate_shapefile(ts):
""" Generate a shapefile of this data """
# Now we generate a shapefile....
dbf = dbflib.create("iaroad_cond")
dbf.add_field("SEGID", dbflib.FTInteger, 4, 0)
dbf.add_field("MAJOR", dbflib.FTString, 10, 0)
dbf.add_field("MINOR", dbflib.FTString, 40, 0)
dbf.add_field("US1", dbflib.FTInteger, 4, 0)
dbf.add_field("ST1", dbflib.FTInteger, 4, 0)
dbf.add_field("INT1", dbflib.FTInteger, 4, 0)
dbf.add_field("TYPE", dbflib.FTInteger, 4, 0)
dbf.add_field("VALID", dbflib.FTString, 12, 0)
dbf.add_field("COND_CODE", dbflib.FTInteger, 4, 0)
dbf.add_field("COND_TXT", dbflib.FTString, 120, 0)
dbf.add_field("BAN_TOW", dbflib.FTString, 1, 0)
dbf.add_field("LIM_VIS", dbflib.FTString, 1, 0)
shp = shapelib.create("iaroad_cond", shapelib.SHPT_ARC)
pcursor.execute("""select b.*, c.*, astext(b.geom) as bgeom from
roads_base b, roads_current c WHERE b.segid = c.segid""")
i = 0
for row in pcursor:
s = row["bgeom"]
f = wellknowntext.convert_well_known_text(s)
valid = row["valid"]
d = {}
d["SEGID"] = row["segid"]
d["MAJOR"] = row["major"]
d["MINOR"] = row["minor"]
d["US1"] = row["us1"]
d["ST1"] = row["st1"]
d["INT1"] = row["int1"]
d["TYPE"] = row["type"]
d["VALID"] = valid.strftime("%Y%m%d%H%M")
d["COND_CODE"] = row["cond_code"]
d["COND_TXT"] = row["raw"]
d["BAN_TOW"] = str(row["towing_prohibited"])[0]
d["LIM_VIS"] = str(row["limited_vis"])[0]
obj = shapelib.SHPObject(shapelib.SHPT_ARC, 1, f )
shp.write_object(-1, obj)
dbf.write_record(i, d)
del(obj)
i += 1
del(shp)
del(dbf)
z = zipfile.ZipFile("iaroad_cond.zip", 'w')
z.write("iaroad_cond.shp")
z.write("iaroad_cond.shx")
z.write("iaroad_cond.dbf")
shutil.copyfile("/mesonet/data/gis/meta/26915.prj", "iaroad_cond.prj")
z.write("iaroad_cond.prj")
z.close()
utc = ts.astimezone( pytz.timezone("UTC") )
subprocess.call("/home/ldm/bin/pqinsert -p 'zip ac %s gis/shape/26915/ia/iaroad_cond.zip GIS/iaroad_cond_%s.zip zip' iaroad_cond.zip" % (
utc.strftime("%Y%m%d%H%M"),
utc.strftime("%Y%m%d%H%M")),
shell=True )
for suffix in ['shp', 'shx', 'dbf', 'prj', 'zip']:
os.unlink("iaroad_cond.%s" % (suffix,))
if (__name__ == "__main__"):
raw = sys.stdin.read()
try:
ts = process(raw)
generate_shapefile(ts)
except:
traceback.print_exc(file=errors)
errors.seek(0)
errstr = errors.read()
if len(errstr) > 0:
logger.error( errstr )
common.email_error(errstr, raw)
pcursor.close()
POSTGIS.commit()
POSTGIS.close()