]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navdb.cxx
James Turner:
[flightgear.git] / src / Navaids / navdb.cxx
1 // navdb.cxx -- top level navaids management routines
2 //
3 // Written by Curtis Olson, started May 2004.
4 //
5 // Copyright (C) 2004  Curtis L. Olson - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <simgear/compiler.h>
28
29 #include <string>
30
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/math/sg_geodesy.hxx>
33
34 #include <Airports/runways.hxx>
35 #include <Airports/simple.hxx>
36 #include <Main/globals.hxx>
37
38 #include "navrecord.hxx"
39 #include "navdb.hxx"
40
41 using std::string;
42
43
44 // load and initialize the navigational databases
45 bool fgNavDBInit( FGAirportList *airports,
46                   FGNavList *navlist, FGNavList *loclist, FGNavList *gslist,
47                   FGNavList *dmelist, FGNavList *mkrlist, 
48                   FGNavList *tacanlist, FGNavList *carrierlist,
49                   FGTACANList *channellist)
50 {
51     SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaid Databases");
52     // SG_LOG(SG_GENERAL, SG_INFO, "  VOR/NDB");
53     // SGPath p_nav( globals->get_fg_root() );
54     // p_nav.append( "Navaids/default.nav" );
55     // navlist->init( p_nav );
56
57     // SG_LOG(SG_GENERAL, SG_INFO, "  ILS and Marker Beacons");
58     // beacons->init();
59     // SGPath p_ils( globals->get_fg_root() );
60     // p_ils.append( "Navaids/default.ils" );
61     // ilslist->init( p_ils );
62
63
64     SGPath path( globals->get_fg_root() );
65     path.append( "Navaids/nav.dat" );
66
67     sg_gzifstream in( path.str() );
68     if ( !in.is_open() ) {
69         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
70         exit(-1);
71     }
72
73     // skip first two lines
74     in >> skipeol;
75     in >> skipeol;
76
77     while ( ! in.eof() ) {
78         FGNavRecord *r = new FGNavRecord;
79         in >> (*r);
80         if ( r->get_type() > 95 ) {
81             delete r;
82             break;
83         }
84
85         /*cout << "id = " << r->get_ident() << endl;
86         cout << " type = " << r->get_type() << endl;
87         cout << " lon = " << r->get_lon() << endl;
88         cout << " lat = " << r->get_lat() << endl;
89         cout << " elev = " <<r->get_elev_ft() << endl;
90         cout << " freq = " << r->get_freq() << endl;
91         cout << " range = " << r->get_range() << endl;
92         cout << " name = " << r->get_name() << endl << endl; */
93
94         // fudge elevation to the field elevation if it's not specified
95         if ( fabs(r->get_elev_ft()) < 0.01 && r->get_apt_id().length() ) {
96             // cout << r->get_type() << " " << r->get_apt_id() << " zero elev"
97             //      << endl;
98             const FGAirport* a = airports->search( r->get_apt_id() );
99             if ( a ) {
100                 r->set_elev_ft( a->getElevation() );
101                 // cout << "  setting to " << a.elevation << endl;
102             }
103         }
104
105         if ( r->get_type() == 2 || r->get_type() == 3 ) {
106             // NDB=2, VOR=3
107             navlist->add( r );
108         } else if ( r->get_type() == 4 || r->get_type() == 5 ) {
109             // ILS=4, LOC(only)=5
110             loclist->add( r );
111         } else if ( r->get_type() == 6 ) {
112             // GS=6
113             gslist->add( r );
114         } else if ( r->get_type() == 7 || r->get_type() == 8
115                     || r->get_type() == 9 )
116         {
117             // Marker Beacon = 7,8,9
118             mkrlist->add( r );
119         } else if ( r->get_type() == 12 || r->get_type() == 13) {
120             // DME with ILS=12; standalone DME=13
121             string str1( r->get_name() );
122             string::size_type loc1= str1.find( "TACAN", 0 );
123             string::size_type loc2 = str1.find( "VORTAC", 0 );
124                        
125             if( loc1 != string::npos || loc2 != string::npos ){
126                  //cout << " name = " << r->get_name() ;
127                  //cout << " freq = " << r->get_freq() ;
128                  tacanlist->add( r );
129                  }
130                 
131             dmelist->add( r );
132             
133         }
134                 
135         in >> skipcomment;
136     }
137
138 // load the carrier navaids file
139     
140     string file, name;
141     path = "";
142     path = globals->get_fg_root() ;
143     path.append( "Navaids/carrier_nav.dat" );
144     
145     file = path.str();
146     SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
147     
148     sg_gzifstream incarrier( path.str() );
149     
150     if ( !incarrier.is_open() ) {
151         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
152         exit(-1);
153     }
154     
155     // skip first two lines
156     //incarrier >> skipeol;
157     //incarrier >> skipeol;
158     
159     while ( ! incarrier.eof() ) {
160         FGNavRecord *r = new FGNavRecord;
161         incarrier >> (*r);
162         carrierlist->add ( r );
163         /*cout << " carrier lon: "<<  r->get_lon() ;
164         cout << " carrier lat: "<<  r->get_lat() ;
165         cout << " freq: " << r->get_freq() ;
166         cout << " carrier name: "<<  r->get_name() << endl;*/
167                 
168         //if ( r->get_type() > 95 ) {
169         //   break;}
170     } // end while
171
172 // end loading the carrier navaids file
173
174 // load the channel/freqency file
175     string channel, freq;
176     path="";
177     path = globals->get_fg_root();
178     path.append( "Navaids/TACAN_freq.dat" );
179     
180     SG_LOG( SG_GENERAL, SG_INFO, "opening file: " << path.str() );
181         
182     sg_gzifstream inchannel( path.str() );
183     
184     if ( !inchannel.is_open() ) {
185         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
186         exit(-1);
187     }
188     
189     // skip first line
190     inchannel >> skipeol;
191     while ( ! inchannel.eof() ) {
192         FGTACANRecord *r = new FGTACANRecord;
193         inchannel >> (*r);
194         channellist->add ( r );
195         //cout << "channel = " << r->get_channel() ;
196         //cout << " freq = " << r->get_freq() << endl;
197         
198     } // end while
199
200  
201  // end ReadChanFile
202
203
204     return true;
205 }
206
207
208 // Given a localizer record and it's corresponding runway record,
209 // adjust the localizer position so it is in perfect alignment with
210 // the runway.
211 static void update_loc_position( FGNavRecord *loc, FGRunway *rwy,
212                                  double threshold )
213 {
214     double hdg = rwy->_heading;
215     hdg += 180.0;
216     if ( hdg > 360.0 ) {
217         hdg -= 360.0;
218     }
219
220     // calculate runway threshold point
221     double thresh_lat, thresh_lon, return_az;
222     geo_direct_wgs_84 ( 0.0, rwy->_lat, rwy->_lon, hdg,
223                         rwy->_length/2.0 * SG_FEET_TO_METER,
224                         &thresh_lat, &thresh_lon, &return_az );
225     // cout << "Threshold = " << thresh_lat << "," << thresh_lon << endl;
226
227     // calculate distance from threshold to localizer
228     double az1, az2, dist_m;
229     geo_inverse_wgs_84( 0.0, loc->get_lat(), loc->get_lon(),
230                         thresh_lat, thresh_lon,
231                         &az1, &az2, &dist_m );
232     // cout << "Distance = " << dist_m << endl;
233
234     // back project that distance along the runway center line
235     double nloc_lat, nloc_lon;
236     geo_direct_wgs_84 ( 0.0, thresh_lat, thresh_lon, hdg + 180.0, 
237                         dist_m, &nloc_lat, &nloc_lon, &return_az );
238     // printf("New localizer = %.6f %.6f\n", nloc_lat, nloc_lon );
239
240     // sanity check, how far have we moved the localizer?
241     geo_inverse_wgs_84( 0.0, loc->get_lat(), loc->get_lon(),
242                         nloc_lat, nloc_lon,
243                         &az1, &az2, &dist_m );
244     // cout << "Distance moved = " << dist_m << endl;
245
246     // cout << "orig heading = " << loc->get_multiuse() << endl;
247     // cout << "new heading = " << rwy->_heading << endl;
248
249     double hdg_diff = loc->get_multiuse() - rwy->_heading;
250
251     // clamp to [-180.0 ... 180.0]
252     if ( hdg_diff < -180.0 ) {
253         hdg_diff += 360.0;
254     } else if ( hdg_diff > 180.0 ) {
255         hdg_diff -= 360.0;
256     }
257
258     if ( fabs(hdg_diff) <= threshold ) {
259         loc->set_lat( nloc_lat );
260         loc->set_lon( nloc_lon );
261         loc->set_multiuse( rwy->_heading );
262     }
263 }
264
265
266 // This routines traverses the localizer list and attempts to match
267 // each entry with it's corresponding runway.  When it is successful,
268 // it then "moves" the localizer and updates it's heading so it
269 // *perfectly* aligns with the runway, but is still the same distance
270 // from the runway threshold.
271 void fgNavDBAlignLOCwithRunway( FGRunwayList *runways, FGNavList *loclist,
272                                 double threshold ) {
273     nav_map_type navmap = loclist->get_navaids();
274
275     nav_map_const_iterator freq = navmap.begin();
276     while ( freq != navmap.end() ) {
277         nav_list_type locs = freq->second;
278         nav_list_const_iterator loc = locs.begin();
279         while ( loc != locs.end() ) {
280             string name = (*loc)->get_name();
281             string::size_type pos1 = name.find(" ");
282             string id = name.substr(0, pos1);
283             name = name.substr(pos1+1);
284             string::size_type pos2 = name.find(" ");
285             string rwy = name.substr(0, pos2);
286
287             FGRunway r;
288             if ( runways->search(id, rwy, &r) ) {
289                 update_loc_position( (*loc), &r, threshold );
290             }
291             ++loc;
292         }
293         ++freq;
294     }
295 }
296