File: freq

package info (click to toggle)
flightgear 1%3A2016.4.4%2Bdfsg-3%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 48,512 kB
  • ctags: 71,727
  • sloc: cpp: 195,395; ansic: 177,152; sh: 13,661; perl: 4,475; python: 2,381; asm: 642; makefile: 398; java: 314; xml: 85
file content (302 lines) | stat: -rwxr-xr-x 6,524 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/perl -w
# $Id$
# Melchior FRANZ <mfranz#aon:at>	Public Domain
#
# Usage:	$ freq [IACO:ksfo [RANGE:15]]
#
# Examples:	$ freq
#		$ freq ksjc
#		$ freq ksjc 30
#
# The RANGE is in km and defines which NDB, VOR, VORTAC, ... to
# display. Default is 15 km.
#
# Note that the directions given for NDB, VOR, VORTAC, ... are
# always the heading from this radio facility to the airport!

use strict;
use POSIX qw(ceil floor);

my $ID = shift || "KSFO";
my $RANGE = shift || 15;		# for NDB/VOR [km]

my $FG_ROOT = $ENV{'FG_ROOT'} || "/usr/local/share/FlightGear";
my $APTFILE = "$FG_ROOT/Airports/apt.dat.gz" || die "airport file not found";
my $NAVFILE = "$FG_ROOT/Navaids/nav.dat.gz" || die "nav file not found";

$ID = uc($ID);
my $PI = 3.1415926535897932384626433832795029;
my $D2R = $PI / 180;
my $R2D = 180 / $PI;
my $ERAD = 6378138.12;
my %COLOR = (
	'NONE' => "\033[m",
	'DME'  => "\033[34;1",
	'ILS'  => "\033[33;1m",
	'TWR'  => "\033[31;1m",
	'ATIS' => "\033[32;1m",
	'NDB'  => "\033[36;1m",
	'VOR'  => "\033[35;1m",
);
my $USECOLOR = 1;

my %FREQ;

my $aptdatacnt = 0;
my $aptlat = 0;
my $aptlon = 0;

open(F, "gzip -d -c $APTFILE|") or die "can't open airport file $APTFILE";
while (<F>) {
	if (/^1\s+\S+\s+\S+\s+\S+\s+$ID\s+(.+)\s+/) {
		my $title = "$ID - $1";
		print "$title\n";
		print "=" x length($title) . "\n";

		foreach (<F>) {
			chomp;
			last if /^\s*$/;

			if (/^1.\s+(\S+)\s+(\S+)\s+/) {
				my ($lat, $lon) = ($1, $2);
				map { s/^(-?)0+/$1/ } ($lat, $lon);
				$aptlat += $lat;
				$aptlon += $lon;
				$aptdatacnt++;
			} elsif (/^(5\d+)\s+(\d+)\s+(.*)\s*/) {
				my ($id, $freq, $desc) = ($1, $2, $3);
				$freq =~ s/(..)$/.$1/;
				&addfreq($freq, $desc);
			}
		}
		last;
	}
}
close F or die "can't close airport file $APTFILE";

die "no data for $ID" unless $aptdatacnt;

# calculate mean location from all structures on the airport
$aptlat /= $aptdatacnt;
$aptlon /= $aptdatacnt;
my ($aptx, $apty, $aptz) = &ll2xyz($aptlat, $aptlon);


my @OM;
my @MM;
my @IM;
my @NDB;
my @VOR;
my @DME;
my @OTHERS;

open(F, "gzip -d -c $NAVFILE|") or die "can't open airport file $NAVFILE";
while (<F>) {
	chomp;
	if (/^2\s/) {			# NDB
		my @l = split /\s+/, $_, 9;
		map { s/^(-?)0+/$1/ } @l[1,2];
		my $dist = &coord_dist_sq(&ll2xyz($l[1], $l[2]), $aptx, $apty, $aptz);
		push @NDB, [$dist, @l];

	} elsif (/^3\s/) {		# VOR/VOR-DME/DME/VORTAC/TACAN
		my @l = split /\s+/, $_, 9;
		map { s/^(-?)0+/$1/ } @l[1,2];
		my $dist = &coord_dist_sq(&ll2xyz($l[1], $l[2]), $aptx, $apty, $aptz);
		if ($l[8] =~ /\b(VOR|VOR-DME)$/) {
			push @VOR, [$dist, @l];
		} elsif ($l[8] =~ /\bDME\b/) {
			push @DME, [$dist, @l];
		} else {
			push @OTHERS, [$dist, @l];
		}

	} elsif (/^(4|5)\s/) {		# LLZ
		my @l = split /\s+/, $_, 11;
		next unless $l[8] eq $ID;
		$l[4] =~ s/(..)$/.$1/;
		&addfreq($l[4], "LLZ " . $l[9]);

	} elsif (/^6\s/) {		# GS
		my @l = split /\s+/, $_, 11;
		next unless $l[8] eq $ID;
		$l[4] =~ s/(..)$/.$1/;
		&addfreq($l[4], "GS " . $l[9]);

	} elsif (/^7\s/) {		# OM
		my @l = split /\s+/, $_, 11;
		next unless $l[8] eq $ID;
		push @OM, $l[9];

	} elsif (/^8\s/) {		# MM
		my @l = split /\s+/, $_, 11;
		next unless $l[8] eq $ID;
		push @MM, $l[9];

	} elsif (/^9\s/) {		# IM
		my @l = split /\s+/, $_, 11;
		next unless $l[8] eq $ID;
		push @IM, $l[9];

	} elsif (/^12\s/) {		# DME (ILS)
		my @l = split /\s+/, $_, 11;
		next unless $l[8] eq $ID;
		$l[4] =~ s/(..)$/.$1/;
		&addfreq($l[4], "DME " . $l[9]);

	}
}
close F or die "can't close airport file $NAVFILE";


foreach my $freq (sort { $a <=> $b } keys %FREQ) {
	my %h;
	map { $h{$_} = 1 } @{$FREQ{$freq}};
	my @uniq = keys %h;

	my @desc;
	my %rwy;
	foreach my $d (@uniq) {
		if ($d =~ /(\S*)\s*(\d\d[LRC]?)\s*(\S*)/) {
			push @{$rwy{$2}}, ($1 . $3);
		} else {
			push @desc, $d;
		}
	}
	foreach my $r (keys %rwy) {
		push @desc, ((join "/", sort @{$rwy{$r}}) . " $r");
	}

	my $s;
	my $k = join ", ", @desc;
	if ($k =~ /\bTWR\b/) {
		$s = $COLOR{'TWR'};
	} elsif ($k =~ /\bATIS\b/) {
		$s = $COLOR{'ATIS'};
	} elsif ($k =~ /\b(GZ|LLZ)\b/) {
		$s = $COLOR{'ILS'};
	}
	$s .= sprintf "%-7s %s\033[m\n", $freq, join ", ", $k;
	print $s;
}


&printfreq(0, $COLOR{'NDB'}, @NDB);
&printfreq(1, $COLOR{'VOR'}, @VOR);
&printfreq(1, $COLOR{'DME'}, @DME);
&printfreq(1, $COLOR{'NONE'}, @OTHERS);

print "        OM " . (join ", ", sort @OM) . "\n" if @OM;
print "        MM " . (join ", ", sort @MM) . "\n" if @MM;
print "        IM " . (join ", ", sort @IM) . "\n" if @IM;

exit 0;



sub printfreq($$$)
{
	my $divfreq = shift;		# divide frequency by 100?
	my $color = shift;
	foreach (sort { @{$a}[0] <=> @{$b}[0] } @_) {
		my @l = @{$_};
		my $dist = &distance($l[0]);
		my $dir = &llll2dir($l[2], $l[3], $aptlat, $aptlon);
		my $freq = $l[5];
		$freq =~ s/(..)$/.$1/ if $divfreq;
		printf "$color%-7s %s (\"%s\")\t-->\t%s km/%s nm  @  %s (%s)$COLOR{'NONE'}\n",
				$freq, $l[9], $l[8],
				&round($dist, 0.1),		# km
				&round($dist / 1.852, 0.1),	# nm
				int $dir, &symdir($dir);
		next if $l[9] =~ /\b$ID\b/;
		last if $dist > $RANGE;
	}
}


sub addfreq($$)
{
	my ($freq, $desc) = @_;
	push @{$FREQ{$freq}}, $desc;
}


sub distance($)		# km
{
	my $t = shift;
	return $ERAD * sqrt($t) / 1000;
}


sub round($)
{
	my $i = shift;
	my $m = (shift or 1);
	$i /= $m;
	$i = $i - &floor($i) >= 0.5 ? &ceil($i) : &floor($i);
	$i *= $m;
	return $i;
}


sub llll2dir($$$$)
{
	my $latA = (shift) * $D2R;
	my $lonA = (shift) * $D2R;
	my $latB = (shift) * $D2R;
	my $lonB = (shift) * $D2R;
	my $xdist = sin($lonB - $lonA) * $ERAD * cos(($latA + $latB) / 2);
	my $ydist = sin($latB - $latA) * $ERAD;
	my $dir = atan2($xdist, $ydist) * $R2D;
	$dir += 360 if $dir < 0;
	return $dir;
}


sub ll2xyz($$)
{
	my $lat = (shift) * $D2R;
	my $lon = (shift) * $D2R;
	my $cosphi = cos $lat;
	my $di = $cosphi * cos $lon;
	my $dj = $cosphi * sin $lon;
	my $dk = sin $lat;
	return ($di, $dj, $dk);
}


sub xyz2ll($$$)
{
	my ($di, $dj, $dk) = @_;
	my $aux = $di * $di + $dj * $dj;
	my $lat = atan2($dk, sqrt $aux) * $R2D;
	my $lon = atan2($dj, $di) * $R2D;
	return ($lat, $lon);
}


sub coord_dist_sq($$$$$$)
{
	my ($xa, $ya, $za, $xb, $yb, $zb) = @_;
	my $x = $xb - $xa;
	my $y = $yb - $ya;
	my $z = $zb - $za;
	return $x * $x + $y * $y + $z * $z;
}


sub symdir($)
{
	my $dir = shift;
	my @names = ("N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
			"S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW");
	my $nnames = scalar @names;
	my $idx = int($nnames * (($dir / 360) + (0.5 / $nnames)));
	if ($idx >= $nnames) {
		$idx = 0;
	}
	return $names[$idx];
}