-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvorpath.py
282 lines (239 loc) · 7.73 KB
/
vorpath.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
'''
vorroute.py
Finds the shortest VOR-to-VOR route between two airports or navaids.
'''
import sys
from rich.console import Console
from rich.table import Column, Table, box
from rich.markdown import Markdown
from rich.progress import track
from igrf.magvar import Magvar
from utils import db, globenav
console = Console()
MV = Magvar()
def distance(a,b):
aLat = float(a['latitude_deg'])
aLon = float(a['longitude_deg'])
bLat = float(b['latitude_deg'])
bLon = float(b['longitude_deg'])
return globenav.dist_coord(aLat, aLon, bLat, bLon)
# bearing from A -> B
def bearing(a,b):
aLat = float(a['latitude_deg'])
aLon = float(a['longitude_deg'])
bLat = float(b['latitude_deg'])
bLon = float(b['longitude_deg'])
midLat = (aLat + bLat) / 2
midLon = (aLon + bLon) / 2
return globenav.wrap_brg(globenav.brg_coord(aLat, aLon, bLat, bLon) - MV.declination(midLat, midLon, 0))
if len(sys.argv) > 2:
src = sys.argv[1].upper()
dst = sys.argv[2].upper()
if src == dst:
sys.exit("Your source and destination are the same!")
if len(sys.argv) > 3:
region = sys.argv[3].upper()
anyRegion = False
else:
region = ""
anyRegion = True
else:
sys.exit("You must provide two ICAO airport/navaid codes and an (optional) region code!")
possibleSources=[]
possibleDests=[]
Q = []
dist = {}
prev = {}
def matchICAOCodes(element):
ident = element['ident']
if ident == src:
possibleSources.append(element)
elif ident == dst:
possibleDests.append(element)
def matchICAOCodesAndPrep(element):
ident = element['ident']
if ident == src:
possibleSources.append(element)
elif ident == dst:
possibleDests.append(element)
element['dist'] = 99999
element['prev'] = None
Q.append(element)
db.execute('airports.csv', matchICAOCodes)
db.execute('navaids.csv', matchICAOCodesAndPrep)
if len(possibleSources) == 0 or len(possibleDests) == 0:
if len(possibleSources) == 0:
print("Can't find " + src + "!")
if len(possibleDests) == 0:
print("Can't find " + dst + "!")
sys.exit()
refSource = None
refDest = None
if len(possibleSources) > 1:
for source in possibleSources:
country = source['iso_country']
if anyRegion or region==country:
refSource = source
region = country
anyRegion = False
break
if refSource == None:
refSource = possibleSources[0]
region = possibleSources[0]['iso_country']
anyRegion = False
else:
refSource = possibleSources[0]
region = possibleSources[0]['iso_country']
anyRegion = False
def minSortdist(e):
return e['sortdist']
for dest in possibleDests:
dest['sortdist'] = distance(refSource,dest)
possibleDests.sort(key=minSortdist)
refDest = possibleDests[0]
srcName = refSource['name']
dstName = refDest['name']
srcLat = float(refSource['latitude_deg'])
srcLong = float(refSource['longitude_deg'])
dstLat = float(refDest['latitude_deg'])
dstLong = float(refDest['longitude_deg'])
refSource['dist'] = 0
if not (refSource in Q):
refSource['prev'] = None
Q.append(refSource)
if not (refDest in Q):
refDest['dist'] = 99999
refDest['prev'] = None
Q.append(refDest)
maxDist = 2*globenav.dist_coord(srcLat,srcLong,dstLat,dstLong)
midLat = (srcLat+dstLat)/2
midLon = (srcLong + dstLong)/2
milTypes = ['TACAN']
modNonMilTypes = ['VOR-DME', 'VOR']
modTypes = ['VOR-DME', 'VORTAC','VOR']
legTypes = ['NDB', 'NDB-DME']
# modern civilian by default
filterTypes = milTypes + legTypes
print("")
print("Enter number of VOR route type (or press Return for 1):\n")
print(" [1] - Modern civilian (VOR/DME only)")
print(" [2] - Legacy civilian (NDB only)")
print(" [3] - All civilian (VOR/DME & NDB)")
print(" [4] - Military (TACAN/VORTAC only)")
print(" [5] - All available ")
print("")
routeSelect = input("> ").rstrip()
if routeSelect == "2":
filterTypes = modTypes + milTypes
elif routeSelect == "3":
filterTypes = milTypes
elif routeSelect == "4":
filterTypes = legTypes + modNonMilTypes
elif routeSelect == "5":
filterTypes = []
# Eliminate unnecessary navaids
Z = []
while Q:
e = Q.pop()
eLat = float(e['latitude_deg'])
eLon = float(e['longitude_deg'])
allow = True
if globenav.dist_coord(midLat,midLon,eLat,eLon) > maxDist:
allow = False
else:
eType = e['type'].rstrip().upper()
for fType in filterTypes:
if eType == fType:
allow=False
break
if allow:
Z.append(e)
Q = Z
print("\nPlease wait... considering " + str(len(Q)) + " possible navaids.\n")
def min_dist(e):
return e['dist']
VOR_ranges = { 'LOW':25, 'MEDIUM':40, 'HIGH':130, 'UNKNOWN':25, '':25 }
NDB_ranges = { 'LOW':25, 'MEDIUM':250, 'HIGH':500, 'UNKNOWN':15, '':15 }
while len(Q) > 0:
Q.sort(key=min_dist)
u = Q.pop(0)
if u==refDest:
break
uLat = float(u['latitude_deg'])
uLon = float(u['longitude_deg'])
for v in Q:
vLat = float(v['latitude_deg'])
vLon = float(v['longitude_deg'])
nodeDist = globenav.dist_coord(uLat,uLon,vLat,vLon)
# consider v a neighbor of u if u within range of v's signal
if 'power' in v:
if v['type'] in legTypes:
nodePower = NDB_ranges[v['power']]
else:
nodePower = VOR_ranges[v['power']]
else:
if v['type'] in legTypes:
nodePower = NDB_ranges['LOW']
else:
nodePower = VOR_ranges['LOW']
if nodeDist <= nodePower:
alt = u['dist'] + nodeDist
if alt < v['dist']:
v['dist'] = alt
v['prev'] = u
S = []
u = refDest
if u['prev'] != None or u == refSource:
while not (u == None):
S.insert(0, u)
u = u['prev']
if len(S) == 0:
sys.exit("Can't find a valid route! Try a more permissive route type (e.g. \"All civilian\" or \"All available\").\n")
# remove source and destination
S.remove(S[0])
S.remove(S[len(S)-1])
naTable = Table(show_header=True, box=box.SIMPLE)
naTable.add_column("#", justify="center")
naTable.add_column("ID", justify="center")
naTable.add_column("Name", justify="left")
naTable.add_column("Distance", justify="right")
naTable.add_column("Heading", justify="right")
naTable.add_column("Type", justify="left")
naTable.add_column("Freq (mHz)", justify="right")
prevNode = refSource
totalDist = 0
naTable.add_row("1", refSource['ident'], refSource['name'], "", "", "----", "----")
for i in range(len(S)):
navaid = S[i]
if navaid['frequency_khz'] != "":
if navaid['type'] == "NDB":
freq = navaid['frequency_khz']
else:
freq = str(float(navaid['frequency_khz'])/1000)
else:
freq = ""
nDist = distance(prevNode, navaid)
nBrg = int(round(bearing(prevNode, navaid)))
naTable.add_row(str(i+2), navaid['ident'], navaid['name'], str(round(nDist,1)) + " nm", str(nBrg) + "°", navaid['type'], freq)
prevNode = navaid
totalDist += nDist
nDist = distance(prevNode, refDest)
totalDist += nDist
brgStr = ""
distStr = ""
if nDist > 0:
brgStr = str(int(round(bearing(prevNode, refDest)))) + "°"
distStr = str(round(nDist,1)) + " nm"
naTable.add_row(str(len(S)+2), refDest['ident'], refDest['name'], distStr, brgStr, "----", "----")
print("")
console.print(Markdown("# VOR-to-VOR Route from " + src + " (" + srcName + ") to " + dst + " (" + dstName + ")"))
console.print(naTable)
console.print("[b]Total distance:[/b] " + str(int(round(totalDist))) + " nm")
print("")
routeStr = refSource['ident']
print("Route string for flight plan:")
for na in S:
routeStr = routeStr + " " + na['ident']
routeStr = routeStr + " " + refDest['ident']
print(routeStr)
print("")