-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiFly_Supp_FSL.py
513 lines (487 loc) · 22.6 KB
/
iFly_Supp_FSL.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import math
import os
import shutil
from zipfile import ZIP_DEFLATED, ZipFile
import pandas as pd
from directories import base_dir, output_dir
DF_APT = pd.read_csv(f"{base_dir}/AIRPORT.csv")
DF_RWY = pd.read_csv(f"{base_dir}/RUNWAY.csv")
DF_PRO = pd.read_csv(f"{base_dir}/AIRPORT_PROCEDURE.csv")
DF_WPT = pd.read_csv(f"{base_dir}/WAYPOINT.csv")
DF_VHF = pd.read_csv(f"{base_dir}/VHF_NAVAID.csv")
DF_NDB = pd.read_csv(f"{base_dir}/NDB_NAVAID.csv")
LOG = []
def main() -> None:
try:
# restore directories
print_debug_message("[INFO] Preparing output directories...")
shutil.rmtree(output_dir, ignore_errors=True)
os.makedirs(output_dir)
os.makedirs(os.path.join(output_dir, "Supp"), exist_ok=True)
os.makedirs(os.path.join(output_dir, "Star"), exist_ok=True)
os.makedirs(os.path.join(output_dir, "Sid"), exist_ok=True)
# convert
print_debug_message("[INFO] Converting from FSL to iFly...")
export_airport_supp()
export_airport_sid()
export_airport_star()
export_airport_app()
# pack
print_debug_message("[INFO] Making package...")
shutil.copy("Installation.txt", f"{output_dir}/Installation.txt")
with ZipFile(f"{output_dir}-CHN-PROC-FULL.zip", 'w',
compression=ZIP_DEFLATED, compresslevel=9) as zipf:
for root, dirs, files in os.walk(output_dir):
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, output_dir)
zipf.write(file_path, arcname=arcname)
print_debug_message("[INFO] Completed!")
except Exception as e:
print_debug_message(f"[ERRO] {repr(e)}")
finally:
open(f"{output_dir}/Log.txt", 'w',
newline='\r\n').write('\n'.join(LOG))
def export_airport_supp() -> None:
DF_APT['TRANSITIONS_ALT'] = DF_APT['TRANSITIONS_ALT'].fillna(9800)
DF_APT['TRANSITION_LEVEL'] = DF_APT['TRANSITION_LEVEL'].fillna(11800)
for _, row in DF_APT.iterrows():
arpt_name = row['ARPT_IDENT']
lines = ["[Speed_Transition]"]
lines.append("Speed=250")
arpt_alt = int(row['ARPT_ELEV'])
arpt_alt = 0 if arpt_alt < 5000 else arpt_alt
spd_alt = int((arpt_alt+10000)/1000)*1000
lines.append(f"Altitude={spd_alt}")
lines.append("[Transition_Altitude]")
trs_alt = int(row['TRANSITIONS_ALT'])
lines.append(f"Altitude={trs_alt}")
lines.append("[Transition_Level]")
trs_lvl = int(row['TRANSITION_LEVEL'])
lines.append(f"Altitude={trs_lvl}")
open(f"{output_dir}/Supp/{arpt_name}.supp", 'w',
newline='\r\n').write('\n'.join(lines))
print_debug_message(f"[INFO] Exported: {arpt_name}.supp")
def export_airport_sid() -> None:
arpt_list = DF_PRO.loc[(DF_PRO['SUBS_CODE'] == 'D') &
(DF_PRO['ROUTE_TYPE'].isin(
['0', '1', '2', '3', '4', '5', '6'])),
'ARPT_IDENT'].value_counts(sort=False).index.to_list()
for arpt in arpt_list: # by airport
df_arpt_proc = DF_PRO[(DF_PRO['ARPT_IDENT'] == arpt) &
# departure
(DF_PRO['SUBS_CODE'] == 'D') &
# RNAV SID
(DF_PRO['ROUTE_TYPE'].isin(['1', '2', '3', '4', '5', '6']))]
# structure: {type:{procedure:[[leg,],]}}
dict_arpt = {'main': {}, 'trans': {}}
for proc_type in ['0', '1', '2', '3', '4', '5', '6']: # force sequence
# by procedure
df_arpt_proc_type = df_arpt_proc[df_arpt_proc['ROUTE_TYPE'] == proc_type]
if not df_arpt_proc_type.shape[0]:
continue
for dict_proc in split_procedure(df_arpt_proc_type, 'SEQ_NR'):
proc_name = dict_proc['ident']
proc_conn = dict_proc['conn']
proc_legs = [extract_leg(r)
for _, r in dict_proc['legs'].iterrows()]
if proc_type in ['3', '6']: # SID trans
dict_arpt['trans'][f"{proc_conn}.{proc_name}"] = proc_legs
continue
arpt_rwys = DF_RWY.loc[DF_RWY['ARPT_IDENT'] == arpt,
'RUNWAY_IDENT'].to_list()
if pd.isna(proc_conn) or proc_conn == 'ALL':
is_extended = False
# find a previous procedure with same ident
for pn in dict_arpt['main'].keys():
if pn.split('.')[0] == proc_name:
# drop first point (IF leg)
dict_arpt['main'][pn].extend(proc_legs[1:])
is_extended = True
if is_extended:
continue
else:
proc_conn = 'RW' # eliminate na
if proc_conn[-1] == 'B':
proc_conn = proc_conn[:-1]
for rw in arpt_rwys: # rw is like "RW09L"
if proc_conn in rw:
dict_arpt['main'][f"{proc_name}.{rw[2:]}"] = proc_legs
# organize this airport
for pt in ['main', 'trans']:
if len(dict_arpt[pt]) == 0:
continue
full_lines = ["[list]"]
i = 0
for pn in sorted(dict_arpt[pt].keys()):
full_lines.append(f"Procedure.{i}={pn}")
i += 1
for pn in sorted(dict_arpt[pt].keys()): # pn:str, pl:list
k = 0
pl = dict_arpt[pt][pn]
for ls in pl: # ls:list
full_lines.append(f"[{pn}.{k}]")
full_lines.extend(ls)
k += 1
filename = f"{arpt}.sid{'trs' if pt == 'trans' else ''}"
open(f"{output_dir}/Sid/{filename}", 'w',
newline='\r\n').write('\n'.join(full_lines))
print_debug_message(f"[INFO] Exported: {filename}")
def export_airport_star() -> None:
arpt_list = DF_PRO.loc[(DF_PRO['SUBS_CODE'] == 'E') &
(DF_PRO['ROUTE_TYPE'].isin(
['3', '2', '1', '6', '5', '4'])),
'ARPT_IDENT'].value_counts(sort=False).index.to_list()
for arpt in arpt_list: # by airport
df_arpt_proc = DF_PRO[(DF_PRO['ARPT_IDENT'] == arpt) &
# arrival
(DF_PRO['SUBS_CODE'] == 'E') &
# RNAV STAR
(DF_PRO['ROUTE_TYPE'].isin(['3', '2', '1', '6', '5', '4']))]
# structure: {type:{procedure:[[leg,],]}}
dict_arpt = {'main': {}, 'trans': {}}
for proc_type in ['3', '2', '1', '6', '5', '4']: # force sequence
# by procedure
df_arpt_proc_type = df_arpt_proc[df_arpt_proc['ROUTE_TYPE'] == proc_type]
if not df_arpt_proc_type.shape[0]:
continue
for dict_proc in split_procedure(df_arpt_proc_type, 'SEQ_NR'):
proc_name = dict_proc['ident']
proc_conn = dict_proc['conn']
proc_legs = [extract_leg(r)
for _, r in dict_proc['legs'].iterrows()]
if proc_type in ['1', '4']: # STAR trans
dict_arpt['trans'][f"{proc_conn}.{proc_name}"] = proc_legs
continue
arpt_rwys = DF_RWY.loc[DF_RWY['ARPT_IDENT'] == arpt,
'RUNWAY_IDENT'].to_list()
if pd.isna(proc_conn) or proc_conn == 'ALL':
is_extended = False
# find a previous procedure with same ident
for pn in dict_arpt['main'].keys():
if pn.split('.')[0] == proc_name:
# drop first point (IF leg), reversed extend
plegs = list(proc_legs) # copy by val
plegs.extend(dict_arpt['main'][pn][1:])
dict_arpt['main'][pn] = plegs
is_extended = True
if is_extended:
continue
else:
proc_conn = 'RW' # eliminate na
if proc_conn[-1] == 'B':
proc_conn = proc_conn[:-1]
for rw in arpt_rwys: # rw is like "RW09L"
if proc_conn in rw:
dict_arpt['main'][f"{proc_name}.{rw[2:]}"] = proc_legs
# organize this airport
for pt in ['main', 'trans']:
if len(dict_arpt[pt]) == 0:
continue
full_lines = ["[list]"]
i = 0
for pn in sorted(dict_arpt[pt].keys()):
full_lines.append(f"Procedure.{i}={pn}")
i += 1
for pn in sorted(dict_arpt[pt].keys()): # pn:str, pl:list
k = 0
pl = dict_arpt[pt][pn]
for ls in pl: # ls:list
full_lines.append(f"[{pn}.{k}]")
full_lines.extend(ls)
k += 1
filename = f"{arpt}.star{'trs' if pt == 'trans' else ''}"
open(f"{output_dir}/Star/{filename}", 'w',
newline='\r\n').write('\n'.join(full_lines))
print_debug_message(f"[INFO] Exported: {filename}")
def export_airport_app() -> None:
arpt_list = DF_PRO.loc[DF_PRO['SUBS_CODE'] == 'F',
'ARPT_IDENT'].value_counts(sort=False).index.to_list()
for arpt in arpt_list: # by airport
df_arpt_proc = DF_PRO[(DF_PRO['ARPT_IDENT'] == arpt) &
(DF_PRO['SUBS_CODE'] == 'F')] # approach
# structure: {type:{procedure:[[leg,],]}}
dict_arpt = {'main': {}, 'trans': {}}
# by procedure
for dict_proc in split_procedure(df_arpt_proc, 'SEQ_NR'):
proc_name = dict_proc['ident']
proc_conn = dict_proc['conn']
proc_type = dict_proc['type']
proc_legs = [extract_leg(r)
for _, r in dict_proc['legs'].iterrows()]
if proc_type == 'A': # approach trans
dict_arpt['trans'][f"{proc_conn}.{proc_name}"] = proc_legs
else: # approach, need to parse runway ident from procedure ident
rw_ident = str(proc_name[1:3])
if rw_ident.isdigit():
if len(proc_name) > 3: # contains L/R and/or W/X/Y/Z
rw_ident += proc_name[3] if proc_name[3] in ['L', 'R'] else ""
dict_arpt['main'][f"{proc_name}.{rw_ident}"] = proc_legs
else: # in case not specified, e.g. ZYJM:CNDB
arpt_rwys = DF_RWY.loc[DF_RWY['ARPT_IDENT'] == arpt,
'RUNWAY_IDENT'].to_list()
for rw in arpt_rwys: # rw is like "RW09L"
dict_arpt['main'][f"{proc_name}.{rw[2:]}"] = proc_legs
print_debug_message(
f"[WARN] uncertain runway, added to all. {arpt}:{proc_name}:{proc_conn}")
# organize this airport
for pt in ['main', 'trans']:
if len(dict_arpt[pt]) == 0:
continue
full_lines = ["[list]"]
i = 0
for pn in sorted(dict_arpt[pt].keys()):
full_lines.append(f"Procedure.{i}={pn}")
i += 1
for pn in sorted(dict_arpt[pt].keys()): # pn:str, pl:list
k = 0
pl = dict_arpt[pt][pn]
for ls in pl: # ls:list
full_lines.append(f"[{pn}.{k}]")
full_lines.extend(ls)
k += 1
filename = f"{arpt}.app{'trs' if pt == 'trans' else ''}"
open(f"{output_dir}/Star/{filename}", 'w',
newline='\r\n').write('\n'.join(full_lines))
print_debug_message(f"[INFO] Exported: {filename}")
def split_procedure(df: pd.DataFrame, col: str) -> dict:
"""return a dict of split DataFrame, structure: [{'ident':str, 'type':str , 'conn':str, 'legs': DataFrame}]"""
df.reset_index(drop=True, inplace=True)
split_indices = df[df[col].shift(1) >= df[col]].index
split_dataframes = []
start_index = 0
for end_index in split_indices:
if end_index < 1:
continue
group_df = df.loc[start_index:end_index-1]
split_dataframes.append(group_df)
start_index = end_index
last_group_df = df.loc[start_index:]
split_dataframes.append(last_group_df)
res = []
for sd in split_dataframes:
sd.reset_index(drop=True, inplace=True)
res.append({
'ident': sd['PROC_IDENT'].iloc[0],
'type': sd['ROUTE_TYPE'].iloc[0],
'conn': sd['TRANSITION_IDENT'].iloc[0],
'legs': sd,
})
return res
def extract_leg(row: pd.Series) -> list:
"""return a list of lines (without header)"""
arpt = row['ARPT_IDENT']
leg_type = row['PATH_AND_TERMINATION']
proc_name = row['PROC_IDENT']
extracted_lines = [f"Leg={leg_type}"]
pt_name = row['FIX_IDENT']
# find Lat/Lon
if leg_type in ['PI', 'HA', 'HF', 'HM', 'AF', 'CF', 'DF', 'FC', 'FD', 'RF', 'TF', 'IF']:
if not pd.isna(pt_name):
extracted_lines.append(f"Name={pt_name}")
latitude, longitude, msg = (0, 0, "")
if row['FIX_SUBS_CODE'] == 'G': # use runway csv
rw_row = DF_RWY[(DF_RWY['ARPT_IDENT'] == arpt) &
(DF_RWY['RUNWAY_IDENT'] == pt_name)]
if rw_row.shape[0]:
latitude = rw_row['RUNWAY_LAT'].iloc[0]
longitude = rw_row['RUNWAY_LON'].iloc[0]
else:
latitude, longitude, msg = find_a_point(
pt_name, arpt, row['FIX_SECT_CODE'], row['FIX_SUBS_CODE'])
if latitude and longitude:
extracted_lines.append("Latitude=%.06f" % latitude)
extracted_lines.append("Longitude=%.06f" % longitude)
if len(msg):
print_debug_message(
f"[WARN] Lat/Lon for {arpt}:{proc_name}:{pt_name}:{msg}")
else:
print_debug_message(
f"[WARN] IDENT missing for {arpt}:{proc_name}")
# cross this point: by finding 'B/Y' in 2nd char of WAYPOINT_DESCR_CODE
pt_descr = row['WAYPOINT_DESCR_CODE']
if not pd.isna(pt_descr) and len(pt_descr) == 4 and pt_descr[1] in ['B', 'Y']:
extracted_lines.append("CrossThisPoint=1")
# heading
pt_hdg = row['MAG_COURSE']
if leg_type in ['PI', 'HA', 'HF', 'HM', 'FM', 'VM', 'CA', 'VA', 'CD', 'VD', 'CF', 'CI', 'VI', 'CR', 'VR', 'FA', 'FC', 'FD']:
if not pd.isna(pt_hdg):
extracted_lines.append("Heading=%.01f" % float(pt_hdg))
else:
print_debug_message(
f"[WARN] Heading missing for {arpt}:{proc_name}:{pt_name}")
# turn direction
pt_tdir = row['TURN_DIR']
if pt_tdir in ['L', 'R']:
extracted_lines.append(f"TurnDirection={pt_tdir}")
elif leg_type in ['PI', 'HA', 'HF', 'HM']:
print_debug_message(
f"[WARN] TurnDirection missing for {arpt}:{proc_name}:{pt_name}")
# speed
pt_spd = row['SPEED_LIMIT']
if not pd.isna(pt_spd) and len(pt_spd := pt_spd.strip()):
pt_spd_descr = row['SPEED_LIMIT_DESCR']
if pt_spd_descr == '+':
extracted_lines.append(f"Speed={pt_spd}A")
elif pt_spd_descr == '-':
extracted_lines.append(f"Speed={pt_spd}B")
else:
extracted_lines.append(f"Speed={pt_spd}")
# altitude
pt_alt1 = row['ALT_1']
if not pd.isna(pt_alt1):
pt_alt_descr = row['ALT_DESCR']
# discrepancies with ARINC-424: G/H/I-@; J-+
if pt_alt_descr in ['+', 'C', 'J', 'V']:
extracted_lines.append("Altitude=%dA" % pt_alt1)
elif pt_alt_descr in ['-', 'Y']:
extracted_lines.append("Altitude=%dB" % pt_alt1)
elif pt_alt_descr == 'B':
pt_alt2 = row['ALT_2']
extracted_lines.append("Altitude=%dA%dB" % (pt_alt2, pt_alt1))
else:
extracted_lines.append("Altitude=%d" % pt_alt1)
elif leg_type in ['CA', 'VA', 'FA']:
print_debug_message(
f"[WARN] Altitude missing for {arpt}:{proc_name}:{pt_name}")
# missed approach point: by finding 'M' in 4th char of WAYPOINT_DESCR_CODE
if not pd.isna(pt_descr) and len(pt_descr) == 4 and pt_descr[3] == 'M':
extracted_lines.append("MAP=1")
# frequency (using ident)
pt_navaid = row['RECOMMENDED_NAVAID']
if not pd.isna(pt_navaid) and len(pt_navaid := pt_navaid.strip()):
extracted_lines.append(f"Frequency={pt_navaid}")
elif leg_type in ['PI', 'AF', 'CD', 'VD', 'CR', 'VR', 'FD']:
print_debug_message(
f"[WARN] Frequency missing for {arpt}:{proc_name}:{pt_name}")
# slope
pt_angl = row['VERTICAL_ANGLE']
if not pd.isna(pt_angl):
extracted_lines.append(f"Slope={-float(pt_angl)}")
# NavBear
pt_navbear = row['THETA']
if not pd.isna(pt_navbear):
extracted_lines.append("NavBear=%.01f" % (int(pt_navbear)/10))
elif leg_type in ['PI', 'CR', 'VR']:
print_debug_message(
f"[WARN] NavBear missing for {arpt}:{proc_name}:{pt_name}")
# NavDist
if leg_type in ['CD', 'VD', 'FD']:
pt_dort = row['ROUTE_DISTANCE_HOLDING_DISTANCE_OR_TIME']
if not pd.isna(pt_dort) and len(pt_dort := pt_dort.strip()) and pt_dort.isdigit():
extracted_lines.append("NavDist=%.01f" % (int(pt_dort)/10))
else:
print_debug_message(
f"[WARN] NavDist missing for {arpt}:{proc_name}:{pt_name}")
else:
pt_navrho = row['RHO']
if not pd.isna(pt_navrho):
extracted_lines.append("NavDist=%.01f" % (int(pt_navrho)/10))
elif leg_type in ['PI', 'AF']:
print_debug_message(
f"[WARN] NavDist missing for {arpt}:{proc_name}:{pt_name}")
# dist
pt_dort = row['ROUTE_DISTANCE_HOLDING_DISTANCE_OR_TIME']
if not pd.isna(pt_dort) and len(pt_dort := pt_dort.strip()) and pt_dort[0] == 'T':
extracted_lines.append("Dist=%d" % (int(pt_dort[1:])*1000)) # time
elif not pd.isna(pt_dort) and pt_dort.isdigit(): # n miles
extracted_lines.append("Dist=%.01f" % (int(pt_dort)/10))
elif leg_type in ['PI', 'HA', 'HF', 'HM', 'FC']:
print_debug_message(
f"[WARN] Dist missing for {arpt}:{proc_name}:{pt_name}")
# Center lat/lon
if leg_type == 'RF':
pt_cfix = row['CENTER_FIX_OR_TAA_PROCEDURE_TURN_IND'].strip()
if len(pt_cfix):
latitude, longitude, msg = find_a_point(
pt_cfix, arpt, row['MULTIPLE_CODE_OR_TAA_SECTOR_SECT_CODE'], row['MULTIPLE_CODE_OR_TAA_SECTOR_SUBS_CODE'])
if latitude and longitude:
extracted_lines.append("CenterLat=%.06f" % latitude)
extracted_lines.append("CenterLon=%.06f" % longitude)
if len(msg):
print_debug_message(
f"[WARN] RF center for {arpt}:{proc_name}:{pt_cfix}:{msg}")
else:
print_debug_message(
f"[WARN] RF center missing for {arpt}:{proc_name}:{pt_name}")
return extracted_lines
def find_a_point(ident: str, airport: str, sect_code: str, subs_code: str) -> tuple:
"""returns (lat, lon, msg) tuple"""
arpt_row = DF_APT[DF_APT['ARPT_IDENT'] == airport]
if not arpt_row.shape[0]:
return (0, 0, "airport not found")
arpt_lat, arpt_lon = arpt_row.iloc[0][['ARPT_LAT', 'ARPT_LON']].to_list()
if sect_code == 'E': # enroute waypoint
p_match = DF_WPT[(DF_WPT['WAYPOINT_IDENT'] == ident) &
(DF_WPT['SECT_CODE'] == 'E')]
if p_match.shape[0]:
p_all = pd.DataFrame(
{'LAT': p_match['WAYPOINT_LAT'],
'LON': p_match['WAYPOINT_LON'],
'DIS': 1000})
else:
return (0, 0, "enroute waypoint not found")
elif sect_code == 'P' and subs_code == 'C': # terminal waypoint
p_match = DF_WPT[(DF_WPT['WAYPOINT_IDENT'] == ident) &
(DF_WPT['REGION_CODE'] == airport)]
if p_match.shape[0]:
p_all = pd.DataFrame(
{'LAT': p_match['WAYPOINT_LAT'],
'LON': p_match['WAYPOINT_LON'],
'DIS': 1000})
else:
return (0, 0, "terminal waypoint not found")
elif sect_code == 'D' and (pd.isna(subs_code) or not len(subs_code.strip())): # VOR
p_match = DF_VHF[DF_VHF['VOR_IDENT'] == ident]
if p_match.shape[0]:
p_all = pd.DataFrame(
{'LAT': p_match['VOR_LAT'],
'LON': p_match['VOR_LON'],
'DIS': 1000})
else:
return (0, 0, "VOR not found")
elif (sect_code == 'D' and subs_code == 'B') or \
(sect_code == 'P' and subs_code == 'N'): # NDB
p_match = DF_NDB[(DF_NDB['NDB_IDENT'] == ident) &
(DF_NDB['SECT_CODE'] == sect_code)]
if p_match.shape[0]:
p_all = pd.DataFrame(
{'LAT': p_match['NDB_LAT'],
'LON': p_match['NDB_LON'],
'DIS': 1000})
else:
return (0, 0, "NDB not found")
elif sect_code == 'P' and subs_code == 'G': # runway
p_match = DF_RWY[(DF_RWY['ARPT_IDENT'] == airport) &
(DF_RWY['RUNWAY_IDENT'] == ident)]
if p_match.shape[0]:
p_all = pd.DataFrame(
{'LAT': p_match['RUNWAY_LAT'],
'LON': p_match['RUNWAY_LON'],
'DIS': 1000})
else:
return (0, 0, "runway not found")
else: # unknown
return (0, 0, "unknown type point")
p_all['DIS'] = p_all[['LAT', 'LON']].apply(lambda r: calculate_distance(
r['LAT'], r['LON'], arpt_lat, arpt_lon), axis=1)
p_all.sort_values('DIS', ascending=True, inplace=True)
msg = "too far" if p_all.iloc[0, 2] >= 1000 else ""
return (p_all.iloc[0, 0], p_all.iloc[0, 1], msg)
def calculate_distance(lat1, lon1, lat2, lon2):
"""Haversine formula. Args in degrees. Returns in kilometers."""
lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2])
earth_radius = 6371
dlat = lat2 - lat1
dlon = lon2 - lon1
a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * \
math.cos(lat2) * math.sin(dlon / 2) ** 2
c = 2 * math.asin(math.sqrt(a))
distance = earth_radius * c
return distance
def print_debug_message(msg: str) -> None:
LOG.append(msg)
print(msg)
if __name__ == "__main__":
main()
input("Press Enter to exit...")