]> git.mxchange.org Git - flightgear.git/blob - scripts/atis/list-airports.pl
Original ATIS voice generation scripts by John Denker
[flightgear.git] / scripts / atis / list-airports.pl
1 #! /usr/bin/perl -w
2
3 sub usage {
4   print <<EoF;
5 Read the apt.dat (or apt.dat.gz) file.
6 Remap airport names to remove ugly abbreviations.
7 Print airport names, one per line.
8
9 Remapping is done by reference to the atis_remap.hxx file.
10
11 Typical usage:
12   FG_ROOT=whatever ATIS_ONLY=yes ./list-airports.pl | words_per_line.sh > atis.list
13 EoF
14 }
15
16 use strict;
17 use Symbol;
18 my $noparen = 1;
19
20   my $verbose = 0;
21   my $apt_name = '';
22   my $lat;
23   my $lon;
24   my $atis;
25   my $country = '';
26   my $elev;
27   my $tower;
28   my $bldgs;
29   my $apt_id;
30   my $shapefile;
31   my $namer = 'NAME';
32   my $skipping = 0;
33   my $tot_apts = 0;
34
35   my %states = ();
36   my %short_country = ();
37
38
39   my $fgroot = $ENV{'FG_ROOT'} || '.';
40   my $atis_only = $ENV{'ATIS_ONLY'} || 0;
41
42 sub process_apt {
43   if ($atis_only && ! $atis) {
44     return;
45   }
46   my $str .= $apt_name;
47
48   $str =~ s' *$'';              ## remove trailing spaces
49   if ($noparen) {
50     $str =~ s/[(][^)]*[)]?//g;
51   }
52   print "$str\n";
53   $tot_apts++;
54 }
55
56 my %remap = ();
57
58 sub get_remap {
59
60 # Note: in this context, GKI probably stands for Gereja Kristen Indonesia
61 # I guess the church builds lots of airports.
62
63   my $mapfn = "$fgroot/../fgs/src/ATCDCL/atis_remap.hxx";
64   my $mapch = Symbol::gensym;
65   if (!open($mapch, '<', $mapfn)) {
66     print STDERR "Could not open abbreviation file '$mapfn'\n";
67     print STDERR "Maybe you need to set FG_ROOT\n";
68     exit(1);
69   }
70   while (my $line = <$mapch>) {
71     chomp $line;
72     if ($line =~ s/[ \t]*REMAP[(]//) {
73       $line =~ s/[)][ \t]*$//;
74       my @stuff = split(',', $line, 2);
75       my $from = $stuff[0];
76       my $to = $stuff[1];
77       $to =~ s/^[ \t]*//;
78       if ($to eq 'NIL') {
79         $to = '';
80       }
81       $remap{$from} = $to;
82     }
83   }
84 }
85
86 main: {
87
88   get_remap;
89
90   my $delim = '-';
91   my $incmd = 'zcat /games/sport/fgd/Airports/apt.dat.gz';
92   my $inch = Symbol::gensym;
93   open ($inch, '-|', $incmd)
94         || die "Couldn't open pipe from '$incmd'\n";
95
96
97   my $junk = <$inch>;
98   $junk = <$inch>;
99   liner: while (my $line = <$inch>) {
100     chomp $line;
101     my @stuff = split(' ', $line);
102     my $type = shift @stuff || 0;
103 ###    print "..$type ... $line ...\n";
104
105     if ($type == 1) {
106 ## Here if new airport.
107 ##
108 ## First, print results of previous work, i.e. airport
109 ## stanzas already seen ... since the apt.dat file
110 ## doesn't have a clear way of signaling the end of a stanza.
111       if ($apt_name) {
112         process_apt();
113       }
114       $apt_name = '';
115       $atis = '';
116       $lat = 0;
117       $lon = 0;
118       $country = '';
119
120       $elev = shift @stuff;
121       $tower = shift @stuff;
122       $bldgs = shift @stuff;
123       $apt_id = shift @stuff;
124       my $name = join $delim, @stuff;
125
126       for my $from (keys %remap) {
127         my $to = $remap{$from};
128         $name =~ s/\b$from\b/$to/gi;
129       }
130
131 ## option for plain words, not hyphenated phrases
132       if (1) {
133         $name =~ s/$delim/ /g;
134       }
135
136       $apt_name = "$name";
137     }
138
139     if ($type == 10) {
140       $lat = $stuff[0];
141       $lon = $stuff[1];
142     }
143
144     if ($type == 50) {
145       $atis = join(' ', @stuff);
146     }
147   }
148   process_apt();          ## flush out the very last one
149   print STDERR "Total airports: $tot_apts\n";
150 }