]> git.mxchange.org Git - flightgear.git/blob - src/ATC/atislist.cxx
Initial revision of ATIS module contributed by Dave Luff.
[flightgear.git] / src / ATC / atislist.cxx
1 // atislist.cxx -- navaids management class
2 //
3 // Written by Curtis Olson, started April 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 #include <simgear/debug/logstream.hxx>
27 #include <simgear/misc/sgstream.hxx>
28 #include <simgear/math/sg_geodesy.hxx>
29
30 #include "atislist.hxx"
31
32
33 FGATISList *current_atislist;
34
35
36 // Constructor
37 FGATISList::FGATISList( void ) {
38 }
39
40
41 // Destructor
42 FGATISList::~FGATISList( void ) {
43 }
44
45
46 // load the navaids and build the map
47 bool FGATISList::init( SGPath path ) {
48     FGATIS a;
49
50     atislist.erase( atislist.begin(), atislist.end() );
51
52     sg_gzifstream in( path.str() );
53     if ( !in.is_open() ) {
54         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
55         exit(-1);
56     }
57
58     // read in each line of the file
59
60     in >> skipeol;
61     in >> skipcomment;
62
63 #ifdef __MWERKS__
64
65     char c = 0;
66     while ( in.get(c) && c != '\0' && a.get_type() != '[' ) {
67         in.putback(c);
68         in >> a;
69         if ( a.get_type() != '[' ) {
70             atislist[a.get_freq()].push_back(a);
71         }
72         in >> skipcomment;
73     }
74
75 #else
76
77     double min = 100000;
78     double max = 0;
79
80     while ( ! in.eof() && a.get_type() != '[' ) {
81         in >> a;
82         //cout << "id = " << a.get_ident() << endl;
83         //cout << " type = " << a.get_type() << endl;
84         //cout << " lon = " << a.get_lon() << endl;
85         //cout << " lat = " << a.get_lat() << endl;
86         //cout << " elev = " << a.get_elev() << endl;
87         //cout << " freq = " << a.get_freq() << endl;
88         //cout << " range = " << a.get_range() << endl;
89         if ( a.get_type() != '[' ) {
90             atislist[a.get_freq()].push_back(a);
91         }
92         in >> skipcomment;
93
94         if ( a.get_type() != 'N' ) {
95             if ( a.get_freq() < min ) {
96                 min = a.get_freq();
97             }
98             if ( a.get_freq() > max ) {
99                 max = a.get_freq();
100             }
101         }
102     }
103
104     // cout << "min freq = " << min << endl;
105     // cout << "max freq = " << max << endl;
106
107 #endif
108
109     return true;
110 }
111
112
113 // query the database for the specified frequency, lon and lat are in
114 // degrees, elev is in meters
115 bool FGATISList::query( double lon, double lat, double elev, double freq,
116                        FGATIS *a )
117 {
118     atis_list_type stations = atislist[(int)(freq*100.0 + 0.5)];
119
120     atis_list_iterator current = stations.begin();
121     atis_list_iterator last = stations.end();
122
123     // double az1, az2, s;
124     Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
125     Point3D station;
126     double d;
127     for ( ; current != last ; ++current ) {
128         //cout << "testing " << current->get_ident() << endl;
129         station = Point3D(current->get_x(), current->get_y(), current->get_z());
130         //cout << "aircraft = " << aircraft << endl;
131         //cout << "station = " << station << endl;
132
133         d = aircraft.distance3Dsquared( station );
134
135         //cout << "  dist = " << sqrt(d)
136         //     << "  range = " << current->get_range() * SG_NM_TO_METER << endl;
137
138         // match up to twice the published range so we can model
139         // reduced signal strength
140         if ( d < (2 * current->get_range() * SG_NM_TO_METER 
141                   * 2 * current->get_range() * SG_NM_TO_METER ) ) {
142             //cout << "matched = " << current->get_ident() << endl;
143             *a = *current;
144             return true;
145         }
146     }
147
148     return false;
149 }