-
Notifications
You must be signed in to change notification settings - Fork 236
/
load_navdata_txt.py
350 lines (274 loc) · 14.6 KB
/
load_navdata_txt.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
''' Load navigation data from text files.'''
import os
import numpy as np
from bluesky import settings
from bluesky.tools.aero import ft
def load_navdata_txt():
#---------- Read nav.dat file (nav aids) ----------
wptdata = dict()
wptdata['wpid'] = [] # identifier (string)
wptdata['wplat'] = [] # latitude [deg]
wptdata['wplon'] = [] # longitude [deg]
wptdata['wptype'] = [] # Type "VOR","NDB","DME","TACAN" or "FIX"
wptdata['wpelev'] = [] # elevation [m]
wptdata['wpvar'] = [] # magnetic variation [deg]
wptdata['wpfreq'] = [] # Navaid frequency kHz(NDB) or MHz(VOR)
wptdata['wpdesc'] = [] # description
with open(os.path.join(settings.navdata_path, 'nav.dat'), 'rb') as f:
print("Reading nav.dat")
for line in f:
line = line.decode(encoding="ascii", errors="ignore").strip()
# Skip empty lines or comments
if len(line) == 0 or line[0] == "#":
continue
# Data line => Process fields of this record, separated by a comma
# Example lines:
# 2 58.61466599 125.42666626 451 522 30 0.0 A Aldan NDB
# 3 31.26894444 -085.72630556 334 11120 40 -3.0 OZR CAIRNS VOR-DME
# type lat lon elev freq ? var id desc
# 0 1 2 3 4 5 6 7 8
fields = line.split()
# Valid line starst with integers
if not fields[0].isdigit():
continue # Next line
# Get code for type of navaid
itype = int(fields[0])
# Type names
wptypedict = {2:"NDB",3:"VOR", \
4:"ILS",5:"LOC",6:"GS", \
7:"OM",8:"MM",9:"IM",
12:"DME",13:"TACAN"}
# Type code never larger than 20
if itype not in list(wptypedict.keys()):
continue # Next line
wptype = wptypedict[itype]
# Select types to read
if wptype not in ["NDB","VOR","DME","TACAN"]:
continue # Next line
wptdata["wptype"].append(wptype)
wptdata["wplat"].append(float(fields[1])) # latitude [deg]
wptdata["wplon"].append(float(fields[2])) # longitude [deg]
wptdata["wpelev"].append(float(fields[3])*ft) # elevation [ft]
if wptype=="NDB":
wptdata["wpfreq"].append(int(fields[4])) # NDB freq in kHz
elif wptype in ["VOR","DME","TACAN"]:
wptdata["wpfreq"].append(float(fields[4])/100.) # VOR freq in MHz
else:
wptdata["wpfreq"].append(0.0)
if wptype in ["VOR","NDB"]:
wptdata["wpvar"].append(float(fields[6])) # Magnetic variation in degrees
wptdata["wpid"].append(fields[7]) # Id
elif wptype in ["DME","TACAN"]:
wptdata["wpvar"].append(0.0) # Magnetic variation not given
wptdata["wpid"].append(fields[7]) # Id
else:
wptdata['wpvar'].append(0.0) # Magnetic variation in degrees
wptdata['wpid'].append(" ") # Id
# Find description
wpid = wptdata["wpid"][-1]
try:
idesc = line.index(wpid)+len(wpid)
# Description at end of line may include spaces
wptdata['wpdesc'].append(line[idesc:]) # Description
except:
wptdata['wpdesc'].append(" ") # Description
#---------- Read fix.dat file ----------
with open(os.path.join(settings.navdata_path, 'fix.dat'), 'rb') as f:
print("Reading fix.dat")
for line in f:
line = line.decode(encoding="ascii", errors="ignore").strip()
# Skip empty lines or comments
if len(line) < 3 or line[0] == "#":
continue
# Start with valid 2 digit latitude -45. or 52.
if not ((line[0]=="-" and line[3]==".") or line[2]==".") :
continue
# Data line => Process fields of this record, separated by a comma
# Example line:
# 30.580372 -094.384169 FAREL
fields = line.split()
wptdata["wptype"].append("FIX")
wptdata['wplat'].append(float(fields[0])) # latitude [deg]
wptdata['wplon'].append(float(fields[1])) # longitude [deg]
wptdata['wpid'].append(fields[2]) # Id
# Not given for fixes but fill out tables for equal length
wptdata['wpelev'].append(0.0) # elevation [ft]
wptdata['wpfreq'].append(0.0) # Fix is no navaid, so no freq
wptdata['wpvar'].append(0.0) # Magnetic variation not given
wptdata['wpdesc'].append("") # Description
# Convert lists for lat,lon to numpy-array for vectorised clipping
wptdata['wplat'] = np.array(wptdata['wplat'])
wptdata['wplon'] = np.array(wptdata['wplon'])
#---------- Read awy.dat file (airway legs) ----------
awydata = dict()
awydata['awid'] = [] # airway identifier (string)
awydata['awfromwpid'] = [] # from waypoint identifier (string)
awydata['awfromlat'] = [] # from waypoint lat [deg](float)
awydata['awfromlon'] = [] # from waypoint lon [deg](float)
awydata['awtowpid'] = [] # to waypoint identifier (string)
awydata['awtolat'] = [] # to waypoint lat [deg](float)
awydata['awtolon'] = [] # to waypoint lon [deg](float)
awydata['awndir'] = [] # number of directions (1 or 2)
awydata['awlowfl'] = [] # lowest flight level (int)
awydata['awupfl'] = [] # highest flight level (int)
with open(os.path.join(settings.navdata_path, 'awy.dat'), 'rb') as f:
print("Reading awy.dat")
for line in f:
line = line.decode(encoding="ascii", errors="ignore").strip()
# Skip empty lines or comments
if len(line) == 0 or line[0] == "#":
continue
fields = line.split()
if len(fields) < 10:
continue
# Example line
# ABAGO 56.291668 144.236667 GINOL 54.413334 142.011667 1 177 528 A218
# fromfwp fromlat fromlon towp tolat tolon ndir lowfl hghfl airwayid
# 0 1 2 3 4 5 6 7 8 9
# Second field should be float
try:
fromlat = float(fields[1])
except:
continue
awydata['awfromwpid'].append(fields[0]) # from waypoint identifier (string)
awydata['awfromlat'].append(fromlat) # from latitude [deg]
awydata['awfromlon'].append(float(fields[2])) # from longitude [deg]
awydata['awtowpid'].append(fields[3]) # to waypoint identifier (string)
awydata['awtolat'].append(float(fields[4])) # to latitude [deg]
awydata['awtolon'].append(float(fields[5])) # to longitude [deg]
awydata['awndir'].append(int(fields[6])) # number of directions (1 or 2)
awydata['awlowfl'].append(int(fields[7])) # number of directions (1 or 2)
awydata['awupfl'].append(int(fields[8])) # number of directions (1 or 2)
if fields[9].find("-")<0:
#only one airway uses this leg
awydata['awid'].append(fields[9])
else:
# More airways use this leg => copy leg with all airway ids
awids = fields[9].split("-")
for i, awid in enumerate(awids):
awydata['awid'].append(awid.strip())
if i>0:
# Repeat last entry
for key in awydata:
if key!="awid":
awydata[key].append(awydata[key][-1])
# Convert lat,lons to numpy arrays for easy clipping
awydata['awfromlat'] = np.array(awydata['awfromlat'])
awydata['awfromlon'] = np.array(awydata['awfromlon'])
awydata['awtolat'] = np.array(awydata['awtolat'])
awydata['awtolon'] = np.array(awydata['awtolon'])
#---------- Read airports.dat file ----------
aptdata = dict()
aptdata['apid'] = [] # 4 char identifier (string)
aptdata['apname'] = [] # full name
aptdata['aplat'] = [] # latitude [deg]
aptdata['aplon'] = [] # longitude [deg]
aptdata['apmaxrwy'] = [] # reference airport {string}
aptdata['aptype'] = [] # type (int, 1=large, 2=medium, 3=small)
aptdata['apco'] = [] # two char country code (string)
aptdata['apelev'] = [] # field elevation ft-> m
with open(os.path.join(settings.navdata_path, 'airports.dat'), 'rb') as f:
types = {'L': 1, 'M': 2, 'S': 3}
for line in f:
line = line.decode(encoding="ascii", errors="ignore").strip()
# Skip empty lines or comments
if len(line) == 0 or line[0] == "#":
continue
# Data line => Process fields of this record, separated by a comma
# Example line:
# EHAM, SCHIPHOL, 52.309, 4.764, Large, 12467, NL
# [id] [name] [lat] [lon] [type] [max rwy length in ft] [country code] [elevation]
# 0 1 2 3 4 5 6 7
fields = line.split(",")
# Skip airports without identifier in file and closed airports
if fields[0].strip() == "" or fields[4].strip() == 'Closed':
continue
aptdata['apid'].append(fields[0].strip()) # id, no leading or trailing spaces
aptdata['apname'].append(fields[1].strip()) # name, no leading or trailing spaces
aptdata['aplat'].append(float(fields[2])) # latitude [deg]
aptdata['aplon'].append(float(fields[3])) # longitude [deg]
aptdata['aptype'].append(types[fields[4].strip()[0]]) # large=1, medium=2, small=3
# Not all airports have rwy length (e.g. heliports)
try:
aptdata['apmaxrwy'].append(float(fields[5])*ft) # max rwy ltgh [m]
except:
aptdata['apmaxrwy'].append(0.0)
aptdata['apco'].append(fields[6].strip().lower()[:2]) # country code
# Not all airports have elevation in data
try:
aptdata['apelev'].append(float(fields[7])*ft) # apt elev [m]
except:
aptdata['apelev'].append(0.0)
aptdata['aplat'] = np.array(aptdata['aplat'])
aptdata['aplon'] = np.array(aptdata['aplon'])
aptdata['apmaxrwy'] = np.array(aptdata['apmaxrwy'])
aptdata['aptype'] = np.array(aptdata['aptype'])
aptdata['apelev'] = np.array(aptdata['apelev'])
#---------- Read FIR files ----------
firdata = dict()
firdata['fir'] = []
firdata['firlat0'] = []
firdata['firlon0'] = []
firdata['firlat1'] = []
firdata['firlon1'] = []
files = os.listdir(os.path.join(settings.navdata_path, 'fir'))
# Get fir names
for filname in files:
if ".txt" in filname:
firname = filname[:filname.index(".txt")]
firdata['fir'].append([firname, [], []])
with open(os.path.join(settings.navdata_path, 'fir/' + filname), 'rb') as f:
for line in f:
rec = line.decode(encoding="ascii", errors="ignore").upper().strip()
if len(rec) == 0:
continue
latsign = 2 * int(line[0] == "N") - 1
latdeg = float(line[1:4])
latmin = float(line[5:7])
latsec = float(line[8:14])
lat = latsign*latdeg+latmin/60.+latsec/3600.
lonsign = 2 * int(line[15] == "E") - 1
londeg = float(line[16:19])
lonmin = float(line[20:22])
lonsec = float(line[23:29])
lon = lonsign*londeg+lonmin/60.+lonsec/3600.
# For drawing create a line from last lat,lon to current lat,lon
if len(firdata['fir'][-1][1]) > 0: # skip first lat,lon
firdata['firlat0'].append(firdata['fir'][-1][1][-1])
firdata['firlon0'].append(firdata['fir'][-1][2][-1])
firdata['firlat1'].append(lat)
firdata['firlon1'].append(lon)
# Add to FIR record
firdata['fir'][-1][1].append(lat)
firdata['fir'][-1][2].append(lon)
# Convert lat/lon lines to numpy arrays
firdata['firlat0'] = np.array(firdata['firlat0'])
firdata['firlat1'] = np.array(firdata['firlat1'])
firdata['firlon0'] = np.array(firdata['firlon0'])
firdata['firlon1'] = np.array(firdata['firlon1'])
#---------- Read ICAO country codes file icao-countries.dat ----------
codata = dict()
codata['coname'] = [] # Country name
codata['cocode2'] = [] # 2 char code
codata['cocode3'] = [] # 3 char code
codata['conr'] = [] # country nr
with open(os.path.join(settings.navdata_path, 'icao-countries.dat'), 'rb') as f:
for line in f:
line = line.decode(encoding="ascii", errors="ignore").strip()
# Skip empty lines or comments
if len(line) == 0 or line[0] == "#":
continue
# Data line: comma separated values:
# full name, A2 code, A3 code, number
fields = line.split(",")
# Skip airports without identifier in file and closed airports
if fields[0].strip() == "":
continue
codata['coname'].append(fields[0].strip()) # id, no leading or trailing spaces
codata['cocode2'].append(fields[1].strip().upper()) # name, no leading or trailing spaces
codata['cocode3'].append(fields[2].strip().upper()) # latitude [deg]
try:
codata['conr'].append(int(fields[3])) # longitude [deg]
except:
codata['conr'].append(-1)
return wptdata, aptdata, awydata, firdata, codata