-
Notifications
You must be signed in to change notification settings - Fork 0
/
findnavs.pl
executable file
·1481 lines (1405 loc) · 46.5 KB
/
findnavs.pl
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
# NAME: findnavs.pl
# AIM: Given a lat,lon, search for navaids nearby...
# 2020/12/26 - Review in Dell03
# 2019-01-25 - Apply filters to output lists...
# 2018-10-21 - Redo some of the navs display...
# 23/08/2015 - Added to the scripts repo
# 11/10/2014 - Add -b bounding box, and find/list all navs in that bbox
# 14/02/2014 - Add -i input file - line separated command
# 12/02/2014 - Also search by NAME
# 13/01/2013 geoff mclane http://geoffair.net/mperl
use strict;
use warnings;
use File::Basename; # split path ($name,$dir,$ext) = fileparse($file [, qr/\.[^.]*/] )
use Cwd;
use Math::Trig;
my $cwd = cwd();
my $os = $^O;
my ($pgmname,$perl_dir) = fileparse($0);
my $temp_dir = $perl_dir . "temp";
my $PATH_SEP = '/';
my $CDATROOT="/media/Disk2/FG/fg22/fgdata"; # 20150716 - 3.5++
if ($os =~ /win/i) {
$PATH_SEP = "\\";
$CDATROOT='D:\FG\next\fgdata'; # 20201226 F:/fgdata"; # 20140127 - 3.1
}
unshift(@INC, $perl_dir);
require 'lib_utils.pl' or die "Unable to load 'lib_utils.pl' Check paths in \@INC...\n";
require 'fg_wsg84.pl' or die "Unable to load fg_wsg84.pl ...\n";
require "Bucket2.pm" or die "Unable to load Bucket2.pm ...\n";
require 'lib_fgio.pl' or die "Unable to load 'lib_fgio.pl' Check paths in \@INC...\n";
# log file stuff
our ($LF);
my $outfile = $temp_dir.$PATH_SEP."temp.$pgmname.txt";
open_log($outfile);
my $VERS = "0.0.8 2020-12-26";
###my $VERS = "0.0.7 2019-01-25";
###my $VERS = "0.0.6 2018-10-21";
###my $VERS = "0.0.5 2015-08-23";
###my $VERS = "0.0.4 2014-10-11";
###my $VERS = "0.0.3 2014-02-14";
###my $VERS = "0.0.2 2014-01-13";
# user variables
my $load_log = 0;
my $in_file = '';
my $verbosity = 0;
my $out_file = '';
my $m_lat = -1;
my $m_lon = -1;
my $m_icao = '';
my $max_out = 30; # 20;
my $use_xplane_dat = 0;
my $nav_name = '';
my $nav_id = '';
my @nav_list = ();
my @nav_ids = ();
my $find_vor_only = 1;
my $search_by_name = 0;
my $filter_vor_pairs = 1;
my $add_markers = 0;
my $add_json_output = 0;
my $need_only_one = 0; # accept if id equal, OR name contains find string
my $out_ils_csv = 0;
# output files
my $xgfile = $temp_dir.$PATH_SEP."temptrack.xg";
my $ilscsv_out = $temp_dir.$PATH_SEP."tempils.csv";
my ($u_min_lon,$u_min_lat,$u_max_lon,$u_max_lat);
my $u_got_bbox = 0;
my ($bgn_lon,$bgn_lat,$end_lon,$end_lat);
my $got_track = 0;
my $fudge = 0.01; # degrees
our $HOST = "localhost";
our $PORT = 5556;
our $TIMEOUT = 1;
our $DELAY = 5;
#============================================================================
# This NEEDS to be adjusted to YOUR particular default location of these files.
my $FGROOT = $CDATROOT; # 20150823 - "F:/fgdata"; # 20140110 - 2.99
###my $FGROOT ="F:/FG/fgdata"; # 20140110 - 2.99
my $APTFILE = "$FGROOT/Airports/apt.dat.gz"; # the airports data file
my $NAVFILE = "$FGROOT/Navaids/nav.dat.gz"; # the NAV, NDB, etc. data file
# add these files
my $FIXFILE = "$FGROOT/Navaids/fix.dat.gz"; # the FIX data file
my $AWYFILE = "$FGROOT/Navaids/awy.dat.gz"; # Airways data
#============================================================================
# =============================================================================
# This NEEDS to be adjusted to YOUR particular default location of these files.
my $XPROOT = "D:/FG/xplane/1000";
my $APT_FILE = "$XPROOT/apt.dat"; # the airports data file
my $NAV_FILE = "$XPROOT/earth_nav.dat"; # the NAV, NDB, etc. data file
my $FIX_FILE = "$XPROOT/earth_fix.dat"; # the FIX data file
# =============================================================================
my $g_aptdat = $APTFILE;
my $g_navdat = $NAVFILE;
my $g_fixfile = $FIXFILE;
my $g_awyfile = $AWYFILE;
my $x_aptdat = $APT_FILE;
my $x_navdat = $NAV_FILE;
# ### DEBUG ###
my $debug_on = 0;
my $def_file = 'MODESTO';
### program variables
my @warnings = ();
my @g_nav_lines = ();
my $actnav = '';
my @navlist = ();
my $actnavdat = '';
my @found_navs = ();
my $in_input = 0;
my $M2NM = 0.000539957;
my $NM2KM = 1.852;
# nav.dat.gz CODES
my $navNDB = '2';
my $navVOR = '3';
my $navILS = '4';
my $navLOC = '5';
my $navGS = '6';
my $navOM = '7';
my $navMM = '8';
my $navIM = '9';
my $navVDME = '12';
my $navNDME = '13';
my @navset = ( $navNDB, $navVOR, $navILS, $navLOC, $navGS, $navOM, $navMM, $navIM, $navVDME, $navNDME );
my @navtypes = qw( NDB VOR ILS LOC GS OM MM IM VDME NDME );
sub VERB1() { return $verbosity >= 1; }
sub VERB2() { return $verbosity >= 2; }
sub VERB5() { return $verbosity >= 5; }
sub VERB9() { return $verbosity >= 9; }
sub show_warnings($) {
my ($val) = @_;
if (@warnings) {
prt( "\nGot ".scalar @warnings." WARNINGS...\n" );
foreach my $itm (@warnings) {
prt("$itm\n");
}
prt("\n");
} else {
prt( "\nNo warnings issued.\n\n" ) if (VERB9());
}
}
sub pgm_exit($$) {
my ($val,$msg) = @_;
if (length($msg)) {
$msg .= "\n" if (!($msg =~ /\n$/));
prt($msg);
}
show_warnings($val);
close_log($outfile,$load_log);
exit($val);
}
sub prtw($) {
my ($tx) = shift;
$tx =~ s/\n$//;
prt("$tx\n");
push(@warnings,$tx);
}
sub is_valid_nav {
my ($t) = shift;
if ($t && length($t)) {
my $txt = "$t";
my $cnt = 0;
foreach my $n (@navset) {
if ($n eq $txt) {
$actnav = $navtypes[$cnt];
return 1;
}
$cnt++;
}
}
return 0;
}
sub mycmp_decend_n9 {
return -1 if (${$a}[9] < ${$b}[9]);
return 1 if (${$a}[9] > ${$b}[9]);
return 0;
}
sub load_nav_file() {
prt("\n[v9] Loading $g_navdat file ...\n") if (VERB9());
mydie("ERROR: Can NOT locate [$g_navdat]!\n") if ( !( -f $g_navdat) );
open NIF, "gzip -d -c $g_navdat|" or mydie( "ERROR: CAN NOT OPEN $g_navdat...$!...\n" );
@g_nav_lines = <NIF>;
close NIF;
prt("[v9] Got ".scalar @g_nav_lines." lines to scan...\n") if (VERB9());
$actnavdat = $g_navdat;
}
sub load_xnav_file {
prt("\nLoading $x_navdat file ...\n");
mydie("ERROR: Can NOT locate [$x_navdat]!\n") if ( !( -f $x_navdat) );
open NIF, "<$x_navdat" or mydie( "ERROR: CAN NOT OPEN $x_navdat...$!...\n" );
@g_nav_lines = <NIF>;
close NIF;
prt("[v9] Got ".scalar @g_nav_lines." lines to scan...\n") if (VERB9());
$actnavdat = $x_navdat;
}
my @fnd_list = ();
my $act_find = '';
my $act_id = '';
sub is_in_nav_list($$) {
my ($nid,$name) = @_;
$act_find = $nav_name;
$act_id = $nav_id;
# prt("Find $act_find in $nid $name\n");
return 1 if (($nid eq $act_find)&&($name =~ /$act_find/i));
if ($need_only_one) {
return 1 if (($nid eq $act_find)||($name =~ /$act_find/i));
}
my ($i,$cnt,$tstid,$tstnm);
$cnt = scalar @nav_list;
for ($i = 0; $i < $cnt; $i++) {
$tstnm = $nav_list[$i];
$tstid = $nav_ids[$i];
$act_find = $tstnm;
$act_id = $tstid;
return 1 if (($nid eq $tstid)&&($name =~ /$tstnm/i));
if ($need_only_one) {
return 1 if (($nid eq $tstid)||($name =~ /$tstnm/i));
}
}
return 0;
}
sub show_missed_names() {
my ($name,$tst,$fnd,$msg,$msg2,$id,$cnt,$i,$cmb);
$msg = '';
$msg2 = '';
$cnt = scalar @nav_list;
for ($i = 0; $i < $cnt; $i++) {
$name = $nav_list[$i];
$id = $nav_ids[$i];
$cmb = "$name:$id";
# get user requested name
$fnd = 0;
foreach $tst (@fnd_list) {
if ($cmb eq $tst) {
$fnd = 1;
last;
}
}
if ($fnd) {
# found this one
$msg2 .= ' ' if (length($msg2));
$msg2 .= $cmb;
} else {
$msg .= ' ' if (length($msg));
$msg .= $cmb;
}
}
if (length($msg)) {
prt("Note: ");
prt("Found $msg2, BUT ") if (length($msg2));
prt("did NOT find $msg\n");
} else {
#if (length($msg2)) {
# prt("Found $msg2\n");
#}
my $cnt = scalar @nav_list;
$cnt = 1 if (($cnt == 0) && length($nav_name));
prt("Appear to have found all $cnt names...\n");
}
}
sub mycmp_decend_n0 {
return -1 if (${$a}[0] < ${$b}[0]);
return 1 if (${$a}[0] > ${$b}[0]);
return 0;
}
sub order_by_distance($) {
my $ra = shift;
my $max = scalar @{$ra};
my @narr = ();
my @ind = ();
my ($i,$j,$dist,$az1,$az2,$line,@arr,$res);
my ($nlat,$nlon);
for ($i = 0; $i < $max; $i++) {
$line = ${$ra}[$i];
@arr = split(/\s+/,$line);
$nlat = $arr[1];
$nlon = $arr[2];
$res = fg_geo_inverse_wgs_84($bgn_lat,$bgn_lon,$nlat,$nlon,\$az1,\$az2,\$dist);
push(@ind,[$dist,$i]);
}
@ind = sort mycmp_decend_n0 @ind;
for ($j = 0; $j < $max; $j++) {
$i = $ind[$j][1];
$line = ${$ra}[$i];
push(@narr,$line);
}
return @narr;
}
sub in_bounding_box($$) {
my ($nlat,$nlon) = @_;
return 0 if ($nlat < $u_min_lat);
return 0 if ($nlat > $u_max_lat);
return 0 if ($nlon < $u_min_lon);
return 0 if ($nlon > $u_max_lon);
return 1;
}
sub search_nav_bbox() {
my $rnls = \@g_nav_lines;
my $nav_cnt = scalar @{$rnls};
prt("Processing $nav_cnt navaid records, get those in box $u_min_lon,$u_min_lat,$u_max_lon,$u_max_lat, fudge=$fudge.\n");
my ($ln,$lnn,$line,@arr,$nc,$typ);
my ($nlat,$nlon,$nalt,$nfrq,$nrng,$nfrq2,$nid,$name,$i,$len,$nmmx);
my @navsinbox = ();
if ($fudge > 0) {
$u_min_lon -= $fudge;
$u_min_lat -= $fudge;
$u_max_lon += $fudge;
$u_max_lat += $fudge;
}
$nmmx = 0;
for ($ln = 0; $ln < $nav_cnt; $ln++) {
$lnn = $ln + 1;
$line = ${$rnls}[$ln];
$line = trim_all($line);
$len = length($line);
next if ($line =~ /\s+Version\s+/i);
next if ($line =~ /^I/);
next if ($len == 0);
@arr = split(/\s+/,$line);
$nc = scalar @arr;
$typ = $arr[0];
last if ($typ == 99);
if ($nc < 8) {
prt("Type: [$typ] - Handle this line [$line] - count = $nc...\n");
pgm_exit(1,"ERROR: FIX ME FIRST!\n");
}
if ( is_valid_nav($typ) ) {
$nlat = $arr[1];
$nlon = $arr[2];
next if (!in_bounding_box($nlat,$nlon));
$name = '';
for ($i = 8; $i < $nc; $i++) {
$name .= ' ' if length($name);
$name .= $arr[$i];
}
$len = length($name);
$nmmx = $len if ($len > $nmmx);
push(@navsinbox,$line);
} else {
pgm_exit(1,"ERROR: FIX ME! Unknown type [$line]\n");
}
}
if ($got_track) {
@navsinbox = order_by_distance(\@navsinbox);
}
$nav_cnt = scalar @navsinbox;
prt("Found $nav_cnt navaids in bbox...\n");
my ($nav,$res,$dist,$az1,$az2,$nm);
my ($s,$lat1,$lon1,$lat2,$lon2);
my $xg = '';
my $inc = 10;
for ($ln = 0; $ln < $nav_cnt; $ln++) {
$line = $navsinbox[$ln];
@arr = split(/\s+/,$line);
$nc = scalar @arr;
# prt("$line\n");
$typ = $arr[0];
$nlat = $arr[1];
$nlon = $arr[2];
$nalt = $arr[3];
$nfrq = $arr[4];
$nrng = $arr[5];
$nfrq2 = $arr[6];
$nid = $arr[7];
$name = '';
for ($i = 8; $i < $nc; $i++) {
$name .= ' ' if length($name);
$name .= $arr[$i];
}
is_valid_nav($typ);
$nav = $actnav;
if (($typ == 2)||($typ == 3)||($typ == 12)) {
$xg .= "anno $nlon $nlat $actnav $name\n";
if ($nrng > 0) {
$s = $nrng * $NM2KM * 1000;
$az1 = 0;
$res = fg_geo_direct_wgs_84( $nlat, $nlon, $az1, $s, \$lat1, \$lon1, \$az2 );
$az1 = 180;
$res = fg_geo_direct_wgs_84( $nlat, $nlon, $az1, $s, \$lat2, \$lon2, \$az2 );
$xg .= "$lon1 $lat1\n";
$xg .= "$lon2 $lat2\n";
$xg .= "NEXT\n";
$az1 = 90;
$res = fg_geo_direct_wgs_84( $nlat, $nlon, $az1, $s, \$lat1, \$lon1, \$az2 );
$az1 = 270;
$res = fg_geo_direct_wgs_84( $nlat, $nlon, $az1, $s, \$lat2, \$lon2, \$az2 );
$xg .= "$lon1 $lat1\n";
$xg .= "$lon2 $lat2\n";
$xg .= "NEXT\n";
for ($az1 = 0; $az1 <= 360; $az1 += $inc) {
$res = fg_geo_direct_wgs_84( $nlat, $nlon, $az1, $s, \$lat1, \$lon1, \$az2 );
$xg .= "$lon1 $lat1\n";
}
$xg .= "NEXT\n";
}
}
if ($got_track) {
$res = fg_geo_inverse_wgs_84($bgn_lat,$bgn_lon,$nlat,$nlon,\$az1,\$az2,\$dist);
}
# for display
$nav .= ' ' while (length($nav) < 4);
$typ .= ' ' while (length($typ) < 2);
$nalt = ' '.$nalt while (length($nalt) < 4);
$nfrq = ' '.$nfrq while (length($nfrq) < 5);
$nrng = ' '.$nrng while (length($nrng) < 3);
$nid .= ' ' while (length($nid) < 4);
$name .= ' ' while (length($name) < $nmmx);
prt("$nav,$typ,$nlat,$nlon, $nalt, $nfrq, $nrng, $nid, $name ");
if ($got_track) {
$nm = int($M2NM * $dist * 10) / 10;
$az1 = int($az1 + 0.5);
$az2 = int($az2 + 0.5);
if (! ($nm =~ /\./) ) {
$nm .= ".0";
}
$nm = ' '.$nm while (length($nm) < 6);
prt("$nm nm, on $az1/$az2");
}
prt("\n");
}
if ($got_track) {
prt("Listed $nav_cnt navaids in distance from begin lat/lon $bgn_lat,$bgn_lon...\n");
}
if (length($xg) && length($xgfile)) {
if (open XG, ">$xgfile") {
print XG "# Navaids on route\n";
print XG "anno $bgn_lon $bgn_lat Begin of track\n";
print XG "color gray\n";
print XG $xg;
print XG "anno $end_lon $end_lat End of track\n";
print XG "color red\n";
print XG "$bgn_lon $bgn_lat # Begin of track\n";
print XG "$end_lon $end_lat # End of track\n";
print XG "NEXT\n";
close XG;
prt("Navaids XG written to $xgfile\n");
}
}
}
sub search_nav_lines() {
my $rnls = \@g_nav_lines;
my $nav_cnt = scalar @{$rnls};
my $by_name = $search_by_name;
prt("Processing $nav_cnt navaid records");
if ($by_name) {
prt(", to find $nav_name...");
} else {
if (($m_lat == -1)||($m_lon == -1)) {
pgm_exit(1,"\n, but do not have a search point!\n");
} else {
prt(", get distance to $m_lat,$m_lon...");
}
}
prt("\n");
my ($ln,$line,$len,$lnn,@arr,$typ,$nc,$i,$dist,$az1,$az2,$res);
my ($nlat,$nlon,$nalt,$nfrq,$nrng,$nfrq2,$nid,$name1,$km,$az);
my ($icao,$rwy1,$name4);
my $ilscsv = "type,lat,lon,alt,frq,rng,frq2,id,icao,rwy,name\n";
my $ilscnt = 0;
for ($ln = 0; $ln < $nav_cnt; $ln++) {
$lnn = $ln + 1;
$line = ${$rnls}[$ln];
$line = trim_all($line);
$len = length($line);
next if ($len == 0);
if ($line =~ /\d+\s+Version\s+/i) {
if ($len > 75) {
$line = substr($line,0,75);
}
prt("$line\n");
next;
}
next if ($line =~ /^I/);
# 0 1 (lat) 2 (lon) 3 4 5 6 7 8++
# 2 38.087769 -077.324919 284 396 25 0.000 APH A P Hill NDB
# 3 57.103719 009.995578 57 11670 100 1.000 AAL Aalborg VORTAC
# 0 1 (lat) 2 (lon) 3 4 5 6 7 8 9 10
# 4 39.980911 -075.877814 660 10850 18 281.662 IMQS 40N 29 ILS-cat-I
# 4 -09.458922 147.231225 128 11010 18 148.650 IWG AYPY 14L ILS-cat-I
# 5 40.034606 -079.023281 2272 10870 18 236.086 ISOZ 2G9 24 LOC
# 5 67.018506 -050.682072 165 10955 18 61.600 ISF BGSF 10 LOC
# 6 39.977294 -075.860275 655 10850 10 300281.205 --- 40N 29 GS
# 6 -09.432703 147.216444 128 11010 10 302148.785 --- AYPY 14L GS
# 7 39.960719 -075.750778 660 0 0 281.205 --- 40N 29 OM
# 7 -09.376150 147.176867 146 0 0 148.785 JSN AYPY 14L OM
# 8 -09.421875 147.208331 91 0 0 148.785 MM AYPY 14L MM
# 8 -09.461050 147.232544 146 0 0 328.777 PY AYPY 32R MM
# 9 65.609444 -018.052222 32 0 0 22.093 --- BIAR 01 IM
# 9 08.425319 004.475597 1126 0 0 49.252 IL DNIL 05 IM
# 12 -09.432703 147.216444 11 11010 18 0.000 IWG AYPY 14L DME-ILS
# 12 -09.449222 147.226589 11 10950 18 0.000 IBB AYPY 32R DME-ILS
@arr = split(/\s+/,$line);
$nc = scalar @arr;
$typ = $arr[0];
last if ($typ == 99);
if ($nc < 8) {
prt("Type: [$typ] - Handle this line [$line] - count = $nc...\n");
pgm_exit(1,"ERROR: FIX ME FIRST!\n");
}
if ( is_valid_nav($typ) ) {
$nlat = $arr[1];
$nlon = $arr[2];
$nalt = $arr[3];
$nfrq = $arr[4];
$nrng = $arr[5];
$nfrq2 = $arr[6];
$nid = $arr[7];
$name1 = '';
for ($i = 8; $i < $nc; $i++) {
$name1 .= ' ' if length($name1);
$name1 .= $arr[$i];
}
if (($typ > 3)&&($typ < 12)&&($typ != 6)) {
## if ($typ == 4) {
if ($nc < 11) {
prt("$lnn: Type: [$typ] - Handle this line [$line] - count = $nc...\n");
pgm_exit(1,"ERROR: FIX ME FIRST!\n");
}
$icao = $arr[8];
$rwy1 = $arr[9];
$name4 = join(' ', splice(@arr,10)); # Name
#prt("$nc $typ $icao $rwy1 $name4\n");
if ($out_ils_csv && ($typ == 4)) {
$ilscsv .= "$typ,$nlat,$nlon,$nalt,$nfrq,$nrng,$nfrq2,$nid,$icao,$rwy1,$name4\n";
$ilscnt++;
}
}
$az1 = 400;
$az2 = 400;
if ($by_name) {
$dist = -1; # this is by NAME, not location
if (is_in_nav_list($nid,$name1)) {
# 0=typ,1=lat, 2=lon, 3=alt, 4=frq, 5-rng, 6-frq2, 7=nid, 8=name,9=find,10=az1,11=az2
push(@found_navs, [$typ, $nlat, $nlon, $nalt, $nfrq, $nrng, $nfrq2, $nid, $name1, $act_find, $az1, $az2]);
push(@fnd_list,"$act_find:$act_id"); # keep the fact we found one for this name
}
} else {
$res = fg_geo_inverse_wgs_84 ($m_lat,$m_lon,$nlat,$nlon,\$az1,\$az2,\$dist);
$km = $dist / 1000;
$km = (int(($km + 0.05) * 10) / 10);
$az = (int(($az1 + 0.05) * 10) / 10);
}
prt( "[v5] $actnav, $typ, $nlat, $nlon, $nalt, $nfrq, $nrng, $nfrq2, $nid, $name1, $km, $az\n") if (VERB5());
# 0=typ,1=lat, 2=lon, 3=alt, 4=frq, 5-rng, 6-frq2, 7=nid,8=name,9=dist,10=az1,11=az2
push(@navlist, [$typ, $nlat, $nlon, $nalt, $nfrq, $nrng, $nfrq2, $nid, $name1, $dist, $az1, $az2]);
} else {
pgm_exit(1,"ERROR: FIX ME! Unknown type [$line]\n");
}
}
$nc = scalar @navlist;
if ($out_ils_csv) {
rename_2_old_bak($ilscsv_out);
write2file($ilscsv,$ilscsv_out);
prt("Loaded $nc navaids... from $actnavdat... written $ilscnt ils csv to $ilscsv_out\n");
} else {
prt("Loaded $nc navaids... from $actnavdat...\n");
}
}
sub is_vor_type($) {
my $t = shift;
return 1 if ($t == 3);
return 2 if ($t == 12);
return 3 if ($t == 13);
return 0;
}
sub is_ndb_type($) {
my $t = shift;
return 1 if ($t == 2);
return 0;
}
sub is_ilsdme_type($) {
my $ra = shift;
my $t = ${$ra}[0];
if ($t == 12) {
my $name = trim_all(${$ra}[8]);
if ($name =~ /ILS/) {
return 1;
}
}
return 0;
}
# VDME, 12, 37.61948333, -122.37389167, 7, 11580, 40, SFO , SAN FRANCISCO VOR-DME , 17.9, 229.4
my $hdr = 'type, # , latitude , longitude , elev, freq., rng, ID , Name , dist., brng';
my $done_hdr = 0;
my $min_lat = 90;
my $min_lon = 180;
my $max_lat = -90;
my $max_lon = -180;
my $min_alt = 9999;
my $max_alt = -9999;
sub prtnav($$$$$$$$$$$) {
my ($nav, $typ, $nlat, $nlon, $nalt, $nfrq, $nrng, $nid, $name, $km, $az) = @_;
prt("$hdr\n") if (!$done_hdr);
$done_hdr = 1;
if ($nlat > $max_lat) {
$max_lat = $nlat;
}
if ($nlat < $min_lat) {
$min_lat = $nlat;
}
if ($nlon > $max_lon) {
$max_lon = $nlon;
}
if ($nlon < $min_lon) {
$min_lon = $nlon;
}
if ($nalt > $max_alt) {
$max_alt = $nalt;
}
if ($nalt < $min_alt) {
$min_alt = $nalt;
}
$nav .= ' ' while (length($nav) < 4);
$typ .= ' ' while (length($typ) < 2);
$nalt = ' '.$nalt while (length($nalt) < 4);
$nfrq = ' '.$nfrq while (length($nfrq) < 5);
$nrng = ' '.$nrng while (length($nrng) < 3);
$nid .= ' ' while (length($nid) < 4);
$name .= ' ' while (length($name) < 24);
$km = ' '.$km while (length($km) < 5);
my $msg = "$nav, $typ, $nlat, $nlon, $nalt, $nfrq, $nrng, $nid, $name, $km, $az\n";
prt($msg);
return $msg;
}
sub prtnav2($$$$$$$$$) {
my ($nav, $typ, $nlat, $nlon, $nalt, $nfrq, $nrng, $nid, $name) = @_;
$nav .= ' ' while (length($nav) < 4);
$typ .= ' ' while (length($typ) < 2);
$nalt = ' '.$nalt while (length($nalt) < 4);
$nfrq = ' '.$nfrq while (length($nfrq) < 5);
$nrng = ' '.$nrng while (length($nrng) < 3);
$nid .= ' ' while (length($nid) < 4);
$name .= ' ' while (length($name) < 24);
my $msg = "$nav, $typ, $nlat, $nlon, $nalt, $nfrq, $nrng, $nid, $name\n";
prt($msg);
return $msg;
}
sub ind_in_array($$) {
my ($ind,$ra) = @_;
my ($i);
foreach $i (@{$ra}) {
return 1 if ($ind == $i);
}
return 0;
}
sub filter_refvor_pairs($) {
my ($rfn) = @_;
my @navs = ();
my ($ra,$ra2,$j,$j2,$typ2,$dist2,$fnd,$i2,$km);
my ($i,$typ,$dist);
my ($nfrq,$nid,$nfrq2,$nid2,$km2);
my $max = scalar @{$rfn}; ### @found_navs;
prt("Filter $max for pairs...\n") if (VERB5());
my @excu = ();
for ($i = 0; $i < $max; $i++) {
$i2 = $i + 1;
#$ra = $found_navs[$i];
$ra = ${$rfn}[$i];
$typ = ${$ra}[0];
if ($find_vor_only) {
next if (is_ndb_type($typ));
next if (is_ilsdme_type($ra));
}
next if (ind_in_array($i,\@excu));
$nfrq = ${$ra}[4];
$nid = ${$ra}[7];
$dist = ${$ra}[9];
$km = $dist / 1000; # m to km
$km = (int(($km + 0.05) * 10) / 10);
$fnd = 0;
for ($j = 0; $j < $max; $j++) {
next if ($i == $j);
next if (ind_in_array($j,\@excu));
$j2 = $j + 1;
$ra2 = ${$rfn}[$j];
$typ2 = ${$ra2}[0];
if ($find_vor_only) {
next if (is_ndb_type($typ2));
next if (is_ilsdme_type($ra2));
}
$dist2 = ${$ra2}[9];
next if (length($dist2) == 0);
$km2 = $dist2 / 1000; # m to km
$km2 = (int(($km2 + 0.05) * 10) / 10);
# prt("Comparing $dist with $dist2\n");
$nfrq2 = ${$ra2}[4];
$nid2 = ${$ra2}[7];
# ($dist eq $dist2) ||
if ( (($nid eq $nid2) && ($nfrq eq $nfrq2)) ) {
# same 'id' 'freq' - decide which
prt("$i2:$j2: Equal id $dist type $typ/$typ2 id $nid/$nid2") if (VERB9());
if (($typ == 3)&&($typ2 == 12)) {
push(@excu,$j);
push(@navs,$ra2);
$fnd = 1;
prt("Stored $typ2\n") if (VERB9());
} elsif (($typ == 12)&&($typ2 == 3)) {
push(@navs,$ra);
push(@excu,$i);
$fnd = 1;
prt("Stored $typ\n") if (VERB9());
} elsif ((($typ == 13)&&($typ2 == 3))||($typ == 3)&&($typ2 == 13)||
(($typ == $typ2)&&(($typ == 3)||($typ ==13)))) {
push(@navs,$ra);
push(@excu,$i);
$fnd = 1;
prt("Stored $typ\n") if (VERB9());
} else {
prtw("WARNING: Presently NO filter of types $typ and $typ2 - ** FIX ME **\n");
}
}
}
if ($fnd == 0) {
$km = $dist / 1000; # m to km
$km = (int(($km + 0.05) * 10) / 10);
prt("$i2: Dup. not found $typ, $nid, $nfrq, at $km km, ...\n") if (VERB5());
push(@navs,$ra); # so keep this one
}
}
$max = scalar @navs;
prt("Return $max after filtering.\n") if (VERB5());
return @navs;
}
sub filter_vor_pair() {
my ($i,$typ,$nlat,$nlon,$nalt,$nfrq,$nrng,$nfrq2,$nid,$name,$dist,$az1,$km,$az,$out,$msg,$clat,$clon,$cfrq);
my $max = scalar @found_navs;
my $rfn = \@found_navs;
if ($filter_vor_pairs) {
return filter_refvor_pairs($rfn);
} else {
prt("No filtering of $max found...\n") if (VERB5());
}
return @found_navs;
}
sub filter_dupes() {
my ($i,$ra,$max,$tst,$nfrq,$nid,$name,$typ);
$max = scalar @found_navs;
my @navs = ();
my %dupes = ();
for ($i = 0; $i < $max; $i++) {
$ra = $found_navs[$i];
$typ = ${$ra}[0];
$nfrq = ${$ra}[4];
$nid = ${$ra}[7];
$name = trim_all(${$ra}[8]);
# $tst = $nfrq.$nid.$name;
$tst = $name.$nfrq.$nid.$typ;
if (defined $dupes{$tst}) {
$typ = ${$ra}[0];
prt("Dropping: $typ $nfrq $nid $name\n");
next;
}
$dupes{$tst} = 1;
push(@navs,$ra);
}
return @navs;
}
sub show_found_navs() {
my ($i,$typ,$nlat,$nlon,$nalt,$nfrq,$nrng,$nfrq2,$nid,$name,$dist,$az1,$km,$az,$out,$msg,$clat,$clon,$cfrq);
@found_navs = filter_dupes();
$out = scalar @nav_list;
$out = 1 if (($out == 0)&&(length($nav_name)));
my $max = scalar @found_navs;
my @nav_list = filter_vor_pair();
$i = scalar @nav_list;
prt("Found $max matching $out navaids");
if ($i < $max) {
prt(", REDUCED to $i after filtering. Use -a to stop filtering ");
$max = $i;
}
prt("\n");
my $rnl = \@nav_list;
$out = 0;
$msg = '';
if ($max) {
# VOR , 3 , 37.62736111, -120.95786111, 93, 11460, 130, MOD , MODESTO VOR-DME
prt("type,cod, latitude, longitude, alt, freq, rng, id, name\n");
}
my $json = "\"navaids\":[\n";
for ($i = 0; $i < $max; $i++) {
$typ = ${$rnl}[$i][0];
if ($find_vor_only) {
next if (is_ndb_type($typ));
}
$nlat = ${$rnl}[$i][1];
$nlon = ${$rnl}[$i][2];
$nalt = ${$rnl}[$i][3];
$nfrq = ${$rnl}[$i][4];
$nrng = ${$rnl}[$i][5];
$nfrq2 = ${$rnl}[$i][6];
$nid = ${$rnl}[$i][7];
$name = ${$rnl}[$i][8];
$dist = ${$rnl}[$i][9];
$az1 = ${$rnl}[$i][10];
is_valid_nav($typ);
$msg .= prtnav2( $actnav, $typ, $nlat, $nlon, $nalt, $nfrq, $nrng, $nid, $name );
$clat = sprintf("%.8f",$nlat);
$clon = sprintf("%.8f",$nlon);
$cfrq = sprintf("%.2f", $nfrq / 100);
$json .= ",\n" if ($out);
#$json .= "{\"type\":\"$actnav\",\"lat\":$clat,\"lon\":$clon,\"alt\":$nalt,\"freq\":$cfrq,\"rng\":$nrng,\"id\":\"$nid\",\"name\":\"$name\"}";
$json .= "{\"N\":\"$dist\",\"id\":\"$nid\",\"type\":\"$actnav\",\"lat\":$clat,\"lon\":$clon,\"alt\":$nalt,\"freq\":$cfrq,\"rng\":$nrng,\"fn\":\"$name\"}";
$out++;
}
$json .= "\n]\n";
if ($out) {
if (length($out_file)) {
rename_2_old_bak($out_file);
write2file($json,$out_file);
prt("Written json to $out_file\n");
} elsif ($add_json_output) {
prt($json);
}
}
}
sub vor_pair_filter($) {
my $rnl = shift;
my $max = $max_out;
$max *= 2;
$max += 4;
my $tot = scalar @{$rnl};
if ($tot < $max) {
$max = $tot;
}
my @navs = ();
my ($i,$typ,$nfrq,$j,$typ2,$nfrq2,$rn,$rn2,$fnd,%inds);
for ($i = 0; $i < $max; $i++) {
$rn = ${$rnl}[$i];
$typ = ${$rn}[0];
if (!is_vor_type($typ)) {
push(@navs,$rn); # not a VOR
next;
}
$nfrq = ${$rn}[4];
$fnd = 0;
%inds = ();
for ($j = 0; $j < $max; $i++) {
next if ($i == $j);
$rn2 = ${$rnl}[$j];
$typ2 = ${$rn2}[0];
next if (!is_vor_type($typ2));
$nfrq2 = ${$rn2}[4];
if ($nfrq == $nfrq2) {
# decide which of these to keep
$fnd = 1;
$inds{$i} = 1;
$inds{$j} = 1;
}
}
if ($fnd) {
# decide which indexed item to keep - usually of 2, but could be more
my @arr = keys %inds;
my ($k,$icnt,$in1,$in2,$rnk);
$icnt = scalar @arr;
$rnk = $rn;
for ($k = 0; $k < $icnt - 1; $k++) {
$in1 = $arr[$k];
$in2 = $arr[$k+1];
$rn = ${$rnl}[$in1];
$rn2 = ${$rnl}[$in2];
$typ = ${$rn}[0];
$typ2 = ${$rn2}[0];
if ($typ == $typ2) {
$rnk = $rn;
} elsif (($typ == 3)&&($typ2 == 12)) {
$rnk = $rn2;
} elsif (($typ == 12)&&($typ2 == 3)) {
$rnk = $rn;
}
}
push(@navs,$rnk);
} else {
push(@navs,$rn);
}
}
return @navs;
}
sub show_nearest_navs() {
my ($i);
my @navs = sort mycmp_decend_n9 @navlist; # sort by calculated distance from lat,lon
# TODO - See below - some of this now done...
#if ($filter_vor_pairs) {
# @navs = vor_pair_filter(\@navs);
#}
my $rnl = \@navs;
my ($typ,$nlat,$nlon,$nalt,$nfrq,$nrng,$nfrq2,$nid,$name,$dist,$az1,$km,$az,$out,$ra,@arr);
my $vcnt = 0;
my $ncnt = 0;
my $icnt = 0;
my $msg = '';
my $max = $max_out;
$out = scalar @{$rnl};
if ($out < $max) {
$max = $out;
}
my @vorlist = ();
my @ndblist = ();
my @ilslist = ();
if ($max == 0) {
pgm_exit(1,"Failed to find any navs suiting criteria!\n");
}
if ($u_got_bbox) {
prt("Listing $max in bbox $u_min_lon,$u_min_lat,$u_max_lon,$u_max_lon, VOR/DME first, then NDB, then ILS...\n");
} else {
prt("Listing closest $max,...\n");
}
prt("VOR/DME ");
if ($filter_vor_pairs || $find_vor_only) {
prt("filtered");
} else {
prt("first");
}
prt(", ");
if ($find_vor_only) {
prt("no NDB/ILS - see '-a', '-A'");
} else {
prt("then NDB, and ILS");
}
prt("\n");
for ($i = 0; $i < $max; $i++) {
$ra = ${$rnl}[$i];
$typ = ${$ra}[0];
next if (!is_vor_type($typ));
push(@vorlist,$ra);
}
$vcnt = scalar @vorlist;
if ($vcnt) {
prt("VOR List $vcnt");
if ($filter_vor_pairs) {
@arr = filter_refvor_pairs(\@vorlist);
$i = scalar @arr;
if ($i < $vcnt) {
prt("/$i");
@vorlist = @arr;
$vcnt = $i;
}
}
prt(", ");
}
for ($i = 0; $i < $max; $i++) {
$ra = ${$rnl}[$i];
$typ = ${$ra}[0];
next if (!is_ndb_type($typ));
push(@ndblist,$ra);
}
$ncnt = scalar @ndblist;
if ($ncnt) {