-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
350 lines (323 loc) · 11.5 KB
/
utils.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
'''
utils.py
Shared aviation utility methods.
'''
import csv
from math import sin, cos, sqrt, atan2, radians, degrees
import requests
from bs4 import BeautifulSoup
DATA_DIR = "./data/"
# REMARK DECODING / EXPANSION
remarkDict = { 'opns':"operations",
'dsgnd':"designated",
'heli':"helipad",
'extdd':"extended",
'dep':"departure",
'deps':"departures",
'apch':"approach",
'apchs':"approaches",
'rsrtd':"restricted",
'auth':"authorized",
'mntn':"maintain",
'ry':"runway",
"rwy":"runway",
'ne':"northeast",
'sw':"southwest",
"se":"southeast",
"nw":"northwest",
"n":"north",
"s":"south",
"e":"east",
"w":"west",
"prmary":"primary",
"ohd":"overhead",
"fm":"from",
"byd":"beyond",
"rcmdd":"recommended",
"pat":"pattern",
"deg":"degree",
"oper":"operating",
"arpt":"airport",
"hel":"helicopters",
"mt":"mountain",
"ovr":"over",
"ctc":"contact",
"lctd":"located",
"artcc":"air route traffic control center",
"atcc":"air traffic control center",
"tsnt":"transient",
"clsd":"closed",
"pvt":"private",
"sfc":"surface",
"acft":"aircraft",
"psnl":"personnel",
"efct":"effect",
"lmtd":"limited",
"trml":"terminal",
"non-sked":"non-scheduled",
"sked":"scheduled",
"trnsp":"transport",
"rqrd":"required",
"arr":"arrival",
"intl":"international",
"emerg":"emergency",
"fac":"facility",
"facs":"facilities",
"avbl":"available",
"proc":"procedure",
"procs":"procedures",
"hrs":"hours",
"coml":"commercial",
"tfc":"traffic",
"invof":"in the vicinity of",
"mi":"mile",
"sta":"straight in approach",
"stas":"straight in approaches",
"atct":"air traffic control tower",
"lcl":"local",
"trng":"training",
"twy":"taxiway",
"twys":"taxiways",
"freq":"frequency",
"freqs":"frequencies",
"una":"unable",
"agri":"agricultural",
"excp":"except",
"pwrd":"powered",
"svcs":"services",
"svc":"service",
"mil":"military",
"maint":"maintenance",
"btn":"between",
"lgtd":"lighted",
"vcnty":"vicinity",
"nmrs":"numerous",
"turb":"turbulence",
"rstd":"restricted",
"flt":"flight",
"ops":"operations",
"gtr":"greater",
"opn":"operation",
"lgts":"lights",
"authd":"authorized",
"pwr":"power",
"prkg":"parking",
"trmls":"terminals",
"intrpd":"interrupted",
"mnm":"minimum",
"psn":"position",
"txg":"taxiing",
"haz":"hazard",
"inbd":"inbound",
"obnd":"outbound",
"sbnd":"southbound",
"wbnd":"westbound",
"nbnd":"northbound",
"ebnd":"eastbound",
"len":"length",
"offl":"official",
"bus":"business",
"btwn":"between",
"thld":"thrust hold",
"sgl":"single",
"eng":"engine",
"grvl":"gravel",
"lndg":"landing",
"cntrln":"centerline",
"hdg":"heading",
"indef":"indefinitely",
"indefly":"indefinitely",
"mag":"magnetic",
"pmtd":"permitted",
"rwys":"runways",
"rys":"runways",
"arcft":"aircraft",
"afct":"aircraft",
"degs":"degrees",
"obsc":"obscured",
"blo":"below",
"rstr":"restricted",
"drg":"during",
"nml":"normal",
"opr":"operation",
"psgrs":"passengers",
"dsnt":"designated",
"exc":"except",
"lndgs":"landings",
"tkofs":"takeoffs",
"tkof":"takeoff",
"annc":"announce",
"act":"active",
"publd":"published",
"intxn":"intersection",
"prvd":"provide",
"ots":"out of service",
"rlls":"lead-in light system",
"ident":"identification",
"aproxly":"approximately",
"dstc":"distance",
"addnl":"additional",
"ppr":"prior permission required",
"gen":"general",
"avn":"aviation",
"procd":"proceed",
"mkd":"marked",
"auzd":"authorized",
"indus":"industrial",
"instln":"installation",
"trnspn":"transportation",
"unlgt":"unlighted",
"lgt":"lighted",
"obstn":"obstruction",
"afld":"airfield",
"ldg":"landing",
"dpt":"departing",
"vsb":"visible",
"fpl":"flight plan",
"prk":"parking",
"unauthd":"unauthorized",
"mrk":"marking",
"unavbl":"unavailable",
"rqr":"requires",
"gnd":"ground",
"grd":"ground",
"unlgtd":"unlighted",
"daylt":"daylight",
"tgls":"temporary guidance leaflets",
"tgl":"temporary guidance leaflet",
"na":"not available"
}
punctDict = ['/', '.', ';', ',', ' ', ':','(',')']
def customSplit(str):
tokens=[]
cur=0
for k in range(len(str)):
if str[k] in punctDict:
if k == cur:
word = ""
else:
word = str[cur:k]
tokens.append({'word':word,'punct':str[k]})
cur=k+1
if cur < len(str):
tokens.append({'word':str[cur:],'punct':""})
return tokens
def decode_remark(rtext):
finalTxt = ""
tokens = customSplit(rtext.lower())
for token in tokens:
if token['word'] in remarkDict.keys():
word = remarkDict[token['word']]
else:
word = token['word']
finalTxt = finalTxt + word + token['punct']
return finalTxt
# RUNWAY MATERIAL DECODING
_runwayMats = [ [["CON"], "Concrete"],
[["ASP"], "Asphalt"],
[["TUR"], "Astroturf"],
[["GRV", "GRAV"], "Gravel"],
[["SAND"], "Sand"],
[["WAT"], "Water"],
[["MAT"], "Mat"],
[["GRASS"], "Grass"] ]
def runwayMaterial(txt):
surf = txt.upper()
# no common vocab for this so we have to be a bit flexible
for matPair in _runwayMats:
abbrevs = matPair[0]
material = matPair[1]
for abv in abbrevs:
if surf.find(abv) != -1:
return material
return ""
# WEB SCRAPING
class scrape:
def getUserAgent():
return {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
def getSession():
sesh = requests.Session()
sesh.headers.update(scrape.getUserAgent())
return sesh
def getSoup(sesh, url):
return BeautifulSoup(sesh.get(url).text, features="html.parser")
# SPHERICAL NAVIGATION
class globenav:
# wrap a bearing in degrees to the range 0-359
def wrap_brg(b):
deg = b
while deg < 0:
deg = 360+deg
while deg >= 360:
deg = deg-360
return deg
# true bearing from one global point to another
def brg_coord(lat1,lon1,lat2,lon2):
# source: https://www.movable-type.co.uk/scripts/latlong.html
lat1 = radians(lat1)
lon1 = radians(lon1)
lat2 = radians(lat2)
lon2 = radians(lon2)
deltaLon = lon2 - lon1
y = sin(deltaLon) * cos(lat2)
x = cos(lat1)*sin(lat2) - sin(lat1)*cos(lat2)*cos(deltaLon)
return globenav.wrap_brg(degrees(atan2(y, x)))
# distance between two global points in nautical miles
def dist_coord(lat1,lon1,lat2,lon2):
# source: https://stackoverflow.com/questions/19412462/getting-distance-between-two-points-based-on-latitude-longitude
R = 6373.0 # approximate radius of earth in km
lat1 = radians(lat1)
lon1 = radians(lon1)
lat2 = radians(lat2)
lon2 = radians(lon2)
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
return 0.539957*R * c
# CSV DATABASE QUERIES
class db:
# execute a function on each element of a CSV
def execute(csvFile, executeFunc):
with open(DATA_DIR+csvFile, newline='', encoding='utf8') as csvfile:
elements = csv.DictReader(csvfile)
for element in elements:
executeFunc(element)
# return results filtered by a query function, and optionally post-process results
def query(csvFile, queryFunc, processFunc=None):
results = []
with open(DATA_DIR+csvFile, newline='', encoding='utf8') as csvfile:
elements = csv.DictReader(csvfile)
for element in elements:
res = queryFunc(element)
if res[0]:
if processFunc != None:
processFunc(element, res[1:])
results.append(element)
return results
# return first element matching query function
def findFirst(csvFile, queryFunc):
with open(DATA_DIR+csvFile, newline='', encoding='utf8') as csvfile:
elements = csv.DictReader(csvfile)
for element in elements:
if queryFunc(element):
return element
return None
# commonly used sort key on the 'dist' field
def sortKeyMinDist(e):
return e['dist']
# SUBSTRING MATCHING
def longestSubstringFinder(string1, string2):
#source: https://stackoverflow.com/questions/18715688/find-common-substring-between-two-strings
answer = ""
len1, len2 = len(string1), len(string2)
for i in range(len1):
match = ""
for j in range(len2):
if (i + j < len1 and string1[i + j] == string2[j]):
match += string2[j]
else:
if (len(match) > len(answer)):
answer = match
match = ""
return answer