-
Notifications
You must be signed in to change notification settings - Fork 0
/
pm_submit_queries.py
285 lines (268 loc) · 12.5 KB
/
pm_submit_queries.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#import sys
import os
os.chdir("//ahmct-065/teams/PMRF/Amir/Codes")
import pm_odom_query
import pandas as pd
import numpy as np
import math
from time import sleep
from socket import gethostbyname, gaierror
##Travis function to query from the pm_odom_query file
def odom_from_pm(cty, rtnum, rtsfx, pmpfx, pmval, alignment):
odoms = pm_odom_query.getodomfrompm_caltrans(cty, rtnum, rtsfx, pmpfx, pmval, alignment)
if (len(odoms) != 1):
return None
return odoms[0]
## Funtion to convert numpy types to python standard types
#def convert_to_python_types(obj):
# if isinstance(obj, np.generic):
# return np.asscalar(obj)
#set of postmile prefix, route suffix, and postmile suffix
pmpfx_lst=['R', 'M', 'C', 'D', 'G', 'H', 'L', 'N', 'S', 'T']
rtsfx_lst=['U', 'S']
align_lst=['R', 'L']
os.chdir("//ahmct-065/teams/PMRF/Amir/bin")
##################################################################################################################################################
##################################################################################################################################################
########################################################### LEMO_WorkOrder pm to odom ############################################################
##read the csv files
##NOTE: switch the reading files when converting pm to odom for the end mark
from_df=pd.read_csv("./fromPM.csv")
to_df=pd.read_csv("./toPM.csv")
##replace missing values with None
from_df=from_df.where((pd.notnull(from_df)), None)
to_df=to_df.where((pd.notnull(to_df)), None)
for i in range(0, from_df.shape[0]):
for pmsfx in align_lst:
cty=from_df.at[i, 'County']
if (cty is None):
continue
rtnum=from_df.at[i, 'rID']
if (rtnum is None):
continue
else:
rtnum=int(from_df.at[i, 'rID'])
rtsfx=from_df.at[i, 'rSuffix']
pmpfx=from_df.at[i, 'PMprefix']
pmval=from_df.at[i, 'PM']
if (pmval is None):
continue
else:
pmval=round(float(from_df.at[i, 'PM']), 3)
##submit with original data
try:
from_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, rtsfx, pmpfx, pmval, pmsfx)
sleep(0.05)
except (gaierror, TimeoutError, ConnectionResetError, OSError):
sleep(60)
from_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, rtsfx, pmpfx, pmval, pmsfx)
##if no match were returned with original data, submit with no route suffix, and no pm prefix
if (math.isnan(from_df.at[i, pmsfx])):
try:
from_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, None, None, pmval, pmsfx)
sleep(0.05)
except (gaierror, TimeoutError, ConnectionResetError, OSError):
sleep(60)
from_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, None, None, pmval, pmsfx)
##if no match were returned, submit all permutations of pm prefix
if (math.isnan(from_df.at[i, pmsfx])):
for pfx in pmpfx_lst:
try:
from_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, rtsfx, pfx, pmval, pmsfx)
sleep(0.05)
except (gaierror, TimeoutError, ConnectionResetError, OSError):
sleep(60)
from_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, rtsfx, pfx, pmval, pmsfx)
if (math.isnan(from_df.at[i, pmsfx])):
continue
else:
##exit on first match
break
##if no match were returned, it might be the case that the end or begin county are mistaken
if (math.isnan(from_df.at[i, pmsfx])):
if((from_df.at[i, 'County']!=to_df.at[i, 'County']) and (to_df.at[i, 'County'] is not None)):
try:
from_df.at[i, pmsfx]=odom_from_pm(to_df.at[i, 'County'], rtnum, rtsfx, pmpfx, pmval, pmsfx)
sleep(0.05)
except (gaierror, TimeoutError, ConnectionResetError, OSError):
sleep(60)
from_df.at[i, pmsfx]=odom_from_pm(to_df.at[i, 'County'], rtnum, rtsfx, pmpfx, pmval, pmsfx)
from_df.to_csv("LEMO-to.csv", sep=",")
##################################################################################################################################################
##################################################################################################################################################
################################################################# LCS pm to odom #################################################################
##read the csv files
##NOTE: change the order of reading when converting pm to odom for the end mark
from_df=pd.read_csv("./EndLane.csv")
to_df=pd.read_csv("./BeginLane.csv")
##county for 980 which is entirely in Alameda is missing
#to_df.loc[to_df['rID']==980, 'County']='ALA'
#replace missing values with None
from_df=from_df.where((pd.notnull(from_df)), None)
to_df=to_df.where((pd.notnull(to_df)), None)
for i in range(0, from_df.shape[0]):
for pmsfx in align_lst:
cty=from_df.at[i, 'County']
if (cty is None):
continue
rtnum=from_df.at[i, 'rID']
if (rtnum is None):
continue
else:
rtnum=int(from_df.at[i, 'rID'])
rtsfx=None
pmpfx=None
pmval=from_df.at[i, 'PM']
if (pmval is None):
continue
else:
pmval=round(float(from_df.at[i, 'PM']), 3)
#submit with original data
try:
from_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, rtsfx, pmpfx, pmval, pmsfx)
sleep(0.05)
except (gaierror, TimeoutError, ConnectionResetError, OSError):
sleep(60)
from_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, rtsfx, pmpfx, pmval, pmsfx)
#if no match were returned, submit all permutations of pm prefix
if (math.isnan(from_df.at[i, pmsfx])):
for pfx in pmpfx_lst:
try:
from_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, rtsfx, pfx, pmval, pmsfx)
sleep(0.05)
except (gaierror, TimeoutError, ConnectionResetError, OSError):
sleep(60)
from_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, rtsfx, pfx, pmval, pmsfx)
if (math.isnan(from_df.at[i, pmsfx])):
continue
else:
#exit on first match
break
#if no match were returned, it might be the case that the end or begin county are mistaken
if (math.isnan(from_df.at[i, pmsfx])):
if((from_df.at[i, 'County']!=to_df.at[i, 'County']) and (to_df.at[i, 'County'] is not None)):
try:
from_df.at[i, pmsfx]=odom_from_pm(to_df.at[i, 'County'], rtnum, rtsfx, pmpfx, pmval, pmsfx)
sleep(0.05)
except (gaierror, TimeoutError, ConnectionResetError, OSError):
sleep(60)
from_df.at[i, pmsfx]=odom_from_pm(to_df.at[i, 'County'], rtnum, rtsfx, pmpfx, pmval, pmsfx)
from_df.to_csv("LCS-to.csv", sep=",")
##################################################################################################################################################
##################################################################################################################################################
################################################################# AADT pm to odom ################################################################
##read the csv files
aadt_df=pd.read_csv("./pm_aadt.csv")
##replace missing values with None
aadt_df=aadt_df.where((pd.notnull(aadt_df)), None)
for i in range(0, aadt_df.shape[0]):
for pmsfx in align_lst:
cty=aadt_df.at[i, 'County']
if (cty is None):
continue
rtnum=aadt_df.at[i, 'Route']
if (rtnum is None):
continue
else:
rtnum=int(aadt_df.at[i, 'Route'])
rtsfx=aadt_df.at[i, 'rtsfx']
pmpfx=aadt_df.at[i, 'pmpfx']
pmval=aadt_df.at[i, 'Postmile']
if (pmval is None):
continue
#submit with original data
aadt_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, rtsfx, pmpfx, pmval, pmsfx)
sleep(0.05)
#if no match were returned, submit all permutations of pm prefix
if (math.isnan(aadt_df.at[i, pmsfx])):
for pfx in pmpfx_lst:
aadt_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, rtsfx, pfx, pmval, pmsfx)
sleep(0.05)
if (math.isnan(aadt_df.at[i, pmsfx])):
continue
else:
#exit on first match
break
aadt_df.to_csv("aadt_odom.csv", sep=",")
##################################################################################################################################################
##################################################################################################################################################
################################################################ Truck pm to odom ################################################################
##read the csv files
truck_df=pd.read_csv("./pm_truck.csv")
##replace missing values with None
truck_df=truck_df.where((pd.notnull(truck_df)), None)
for i in range(0, truck_df.shape[0]):
for pmsfx in align_lst:
cty=truck_df.at[i, 'County']
if (cty is None):
continue
rtnum=truck_df.at[i, 'Route']
if (rtnum is None):
continue
else:
rtnum=int(truck_df.at[i, 'Route'])
rtsfx=truck_df.at[i, 'rtsfx']
pmpfx=truck_df.at[i, 'pmpfx']
pmval=truck_df.at[i, 'Postmile']
if (pmval is None):
continue
else:
pmval=float(pmval)
#submit with original data
truck_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, rtsfx, pmpfx, pmval, pmsfx)
sleep(0.05)
#if no match were returned, submit all permutations of pm prefix
if (math.isnan(truck_df.at[i, pmsfx])):
for pfx in pmpfx_lst:
truck_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, rtsfx, pfx, pmval, pmsfx)
sleep(0.05)
if (math.isnan(truck_df.at[i, pmsfx])):
continue
else:
#exit on first match
break
truck_df.to_csv("truck_odom.csv", sep=",")
##################################################################################################################################################
##################################################################################################################################################
################################################################# chp pm to odom #################################################################
#read the csv file
os.chdir("//ahmct-065/teams/PMRF/Amir/bin")
chp_df=pd.read_csv("./chp.to.odom.csv")
chp_df['ROUTE_SUFFIX']=chp_df['ROUTE_SUFFIX'].where((chp_df['ROUTE_SUFFIX']!='-'), None)
chp_df['POSTMILE_PREFIX']=chp_df['POSTMILE_PREFIX'].where((chp_df['POSTMILE_PREFIX']!='-'), None)
chp_df=chp_df.where((pd.notnull(chp_df)), None)
for i in range(0, chp_df.shape[0]):
for pmsfx in align_lst:
cty=chp_df.at[i, 'CALTRANS_COUNTY']
if (cty is None):
continue
rtnum=chp_df.at[i, 'STATE_ROUTE']
if (rtnum is None):
continue
else:
rtnum=int(chp_df.at[i, 'STATE_ROUTE'])
rtsfx=chp_df.at[i, 'ROUTE_SUFFIX']
pmpfx=chp_df.at[i, 'POSTMILE_PREFIX']
pmval=chp_df.at[i, 'POSTMILE']
if (pmval is None):
continue
else:
pmval=float(pmval)
#submit with original data
chp_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, rtsfx, pmpfx, pmval, pmsfx)
sleep(0.05)
#if no match were returned, submit all permutations of pm prefix
if (math.isnan(chp_df.at[i, pmsfx])):
for pfx in pmpfx_lst:
try:
chp_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, rtsfx, pfx, pmval, pmsfx)
sleep(0.05)
except (gaierror, TimeoutError, ConnectionResetError, OSError):
sleep(60.0)
chp_df.at[i, pmsfx]=odom_from_pm(cty, rtnum, rtsfx, pfx, pmval, pmsfx)
if (math.isnan(chp_df.at[i, pmsfx])):
continue
else:
#exit on first match
break
chp_df.to_csv("chp_odom-2013.csv", sep=",")