1 // atislist.cxx -- ATIS data management class
3 // Written by David Luff, started October 2001.
4 // Based on navlist.cxx by Curtis Olson, started April 2000.
6 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/misc/sgstream.hxx>
29 #include <simgear/math/sg_geodesy.hxx>
30 #include <simgear/math/sg_random.h>
32 #include "atislist.hxx"
35 FGATISList *current_atislist;
39 FGATISList::FGATISList( void ) {
44 FGATISList::~FGATISList( void ) {
48 // load the navaids and build the map
49 bool FGATISList::init( SGPath path ) {
51 atislist.erase( atislist.begin(), atislist.end() );
53 sg_gzifstream in( path.str() );
54 if ( !in.is_open() ) {
55 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
59 // read in each line of the file
65 while ( in.get(c) && c != '\0' ) {
73 if ( a.get_type() == '[' ) {
77 /* cout << "id = " << a.GetIdent() << endl;
78 cout << " type = " << a.get_type() << endl;
79 cout << " lon = " << a.get_lon() << endl;
80 cout << " lat = " << a.get_lat() << endl;
81 cout << " elev = " << a.get_elev() << endl;
82 cout << " freq = " << a.get_freq() << endl;
83 cout << " range = " << a.get_range() << endl << endl; */
85 atislist[a.get_freq()].push_back(a);
94 // query the database for the specified frequency, lon and lat are in
95 // degrees, elev is in meters
96 bool FGATISList::query( double lon, double lat, double elev, double freq,
99 lon *= SGD_DEGREES_TO_RADIANS;
100 lat *= SGD_DEGREES_TO_RADIANS;
102 atis_list_type stations = atislist[(int)(freq*100.0 + 0.5)];
104 atis_list_iterator current = stations.begin();
105 atis_list_iterator last = stations.end();
107 // double az1, az2, s;
108 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
111 for ( ; current != last ; ++current ) {
112 //cout << "testing " << current->get_ident() << endl;
113 station = Point3D(current->get_x(), current->get_y(), current->get_z());
114 //cout << "aircraft = " << aircraft << endl;
115 //cout << "station = " << station << endl;
117 d = aircraft.distance3Dsquared( station );
119 //cout << " dist = " << sqrt(d)
120 // << " range = " << current->get_range() * SG_NM_TO_METER << endl;
122 // match up to twice the published range so we can model
123 // reduced signal strength
124 if ( d < (2 * current->get_range() * SG_NM_TO_METER
125 * 2 * current->get_range() * SG_NM_TO_METER ) ) {
126 //cout << "matched = " << current->get_ident() << endl;
136 int FGATISList::GetCallSign( string apt_id, int hours, int mins )
138 atis_transmission_type tran;
140 if(atislog.find(apt_id) == atislog.end()) {
141 // This station has not transmitted yet - return a random identifier
142 // and add the transmission to the log
146 tran.callsign = int(sg_random() * 25) + 1; // This *should* give a random int between 1 and 26
147 //atislog[apt_id].push_back(tran);
148 atislog[apt_id] = tran;
150 // This station has transmitted - calculate the appropriate identifier
151 // and add the transmission to the log if it has changed
152 tran = atislog[apt_id];
153 // This next bit assumes that no-one comes back to the same ATIS station
154 // after running FlightGear for more than 24 hours !!
155 if((tran.hours == hours) && (tran.mins == mins)) {
156 return(tran.callsign);
158 if(tran.hours == hours) {
159 // The minutes must have changed
163 if(hours < tran.hours) {
166 tran.callsign += (hours - tran.hours);
168 // Assume transmissions were made on every hour
174 // Wrap if we've exceeded Zulu
175 if(tran.callsign > 26) {
178 // And write the new transmission to the log
179 atislog[apt_id] = tran;
182 return(tran.callsign);