]> git.mxchange.org Git - flightgear.git/blob - src/ATC/atis.cxx
Bugfix. The engine thrust is recalculated based on the current N1 value
[flightgear.git] / src / ATC / atis.cxx
1 // atis.cxx - routines to generate the ATIS info string
2 // This is the implementation of the FGATIS class
3 //
4 // Written by David Luff, started October 2001.
5 //
6 // Copyright (C) 2001  David C Luff - david.luff@nottingham.ac.uk
7 //
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.
12 //
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.
17 //
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.
21
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <simgear/compiler.h>
28
29 #include <stdlib.h>     // atoi()
30 #include <stdio.h>      // sprintf
31 #include <string>
32 SG_USING_STD(string);
33
34 #include STL_IOSTREAM
35 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
36 SG_USING_STD(cout);
37 #endif
38
39 //#include <simgear/debug/logstream.hxx>
40 //#include <simgear/misc/sgstream.hxx>
41 #include <simgear/misc/sg_path.hxx>
42
43 #ifdef FG_WEATHERCM
44 # include <WeatherCM/FGLocalWeatherDatabase.h>
45 #else
46 # include <Environment/environment_mgr.hxx>
47 # include <Environment/environment.hxx>
48 #endif
49
50 #include <Main/fg_props.hxx>
51 #include <Main/globals.hxx>
52 #include <Airports/runways.hxx>
53
54 #include "atis.hxx"
55 #include "atislist.hxx"
56 #include "ATCdisplay.hxx"
57
58 string GetPhoneticIdent(int i) {
59 // TODO - Check i is between 1 and 26 and wrap if necessary
60     switch(i) {
61     case 1 : return("Alpha");
62     case 2 : return("Bravo");
63     case 3 : return("Charlie");
64     case 4 : return("Delta");
65     case 5 : return("Echo");
66     case 6 : return("Foxtrot");
67     case 7 : return("Golf");
68     case 8 : return("Hotel");
69     case 9 : return("Indigo");
70     case 10 : return("Juliet");
71     case 11 : return("Kilo");
72     case 12 : return("Lima");
73     case 13 : return("Mike");
74     case 14 : return("November");
75     case 15 : return("Oscar");
76     case 16 : return("Papa");
77     case 17 : return("Quebec");
78     case 18 : return("Romeo");
79     case 19 : return("Sierra");
80     case 20 : return("Tango");
81     case 21 : return("Uniform");
82     case 22 : return("Victor");
83     case 23 : return("Whiskey");
84     case 24 : return("X-ray");
85     case 25 : return("Yankee");
86     case 26 : return("Zulu");
87     }
88     // We shouldn't get here
89     return("Error");
90 }
91
92 // Constructor
93 FGATIS::FGATIS()
94   : type(0),
95     lon(0.0), lat(0.0),
96     elev(0.0),
97     x(0.0), y(0.0), z(0.0),
98     freq(0),
99     range(0),
100     display(false),
101     displaying(false),
102     ident(""),
103     name(""),
104     transmission(""),
105     trans_ident(""),
106     atis_failed(false)
107 {
108 }
109
110 // Destructor
111 FGATIS::~FGATIS() {
112 }
113
114 // Main update function - checks whether we are displaying or not the correct message.
115 void FGATIS::Update() {
116     if(display) {
117         if(displaying) {
118             // Check if we need to update the message
119             // - basically every hour and if the weather changes significantly at the station
120             //globals->get_ATC_display()->ChangeRepeatingMessage(transmission);
121         } else {
122             // We need to get and display the message
123             UpdateTransmission();
124             globals->get_ATC_display()->RegisterRepeatingMessage(transmission);
125             displaying = true;
126         }
127     } else {
128         // We shouldn't be displaying
129         if(displaying) {
130             globals->get_ATC_display()->CancelRepeatingMessage();
131             displaying = false;
132         }
133     }
134 }
135
136 // Sets the actual broadcast ATIS transmission.
137 void FGATIS::UpdateTransmission() {
138     double visibility;
139     char buf[10];
140     int phonetic_id;
141     string phonetic_id_string;
142     string time_str = fgGetString("sim/time/gmt-string");
143     int hours;
144     // int minutes;
145
146 #ifdef FG_WEATHERCM
147     sgVec3 position = { lat, lon, elev };
148     FGPhysicalProperty stationweather = WeatherDatabase->get(position);
149 #else
150     FGEnvironment stationweather =
151       globals->get_environment_mgr()->getEnvironment(lat, lon, elev);
152 #endif
153
154     transmission = "";
155
156     // Start with the transmitted station name.
157     transmission += name;
158     // Add "Information"
159     transmission += " Information";
160
161     //cout << "In atis.cxx, time_str = " << time_str << '\n';
162     // Add the recording identifier
163     // For now we will assume we only transmit every hour
164     hours = atoi((time_str.substr(1,2)).c_str());       //Warning - this is fragile if the 
165                                                         //time string format changes
166     //cout << "In atis.cxx, hours = " << hours << endl;
167     phonetic_id = current_atislist->GetCallSign(ident, hours, 0);
168     phonetic_id_string = GetPhoneticIdent(phonetic_id);
169     transmission += " ";
170     transmission += phonetic_id_string;
171
172     // Output the recording time. - we'll just output the last whole hour for now.
173     // FIXME - this only gets GMT time but that appears to be all the clock outputs for now
174     //cout << "in atis.cxx, time = " << time_str << endl;
175     transmission = transmission + "  Weather " + time_str.substr(0,3) + "00 hours Zulu";
176
177     // Get the temperature
178     // (Hardwire it for now since the global property returns the temperature at the current altitude
179     //temperature = fgGetDouble("/environment/weather/temperature-K");
180 #ifdef FG_WEATHERCM
181     sprintf(buf, "%i", int(stationweather.Temperature - 273.15));
182 #else
183     sprintf(buf, "%i", int(stationweather.get_temperature_degc()));
184 #endif
185     transmission += "  Temperature ";
186     transmission += buf;
187     transmission += " degrees Celsius";
188
189         // Get the pressure / altimeter
190         // pressure is: stationweather.AirPressure in Pascal
191
192         // Get the visibility
193 #ifdef FG_WEATHERCM
194         visibility = fgGetDouble("/environment/visibility-m");
195 #else
196         visibility = stationweather.get_visibility_m();
197 #endif
198         sprintf(buf, "%i", int(visibility/1600));
199         transmission += "  Visibility ";
200         transmission += buf;
201         transmission += " miles";
202
203         // Get the cloudbase
204         // FIXME: kludge for now
205         if (!strcmp(fgGetString("/environment/clouds/layer[0]/type"),
206                     "clear")) {
207             double cloudbase =
208               fgGetDouble("/environment/clouds/layer[0]/elevation-ft");
209             // For some reason the altitude returned doesn't seem to correspond to the actual cloud altitude.
210             char buf3[10];
211             // cout << "cloudbase = " << cloudbase << endl;
212             sprintf(buf3, "%i", int(cloudbase));
213             transmission = transmission + "  Cloudbase " + buf3 + " feet";
214         }
215
216         // Based on the airport-id and wind get the active runway
217         //FGRunway *r;
218         SGPath path( globals->get_fg_root() );
219         path.append( "Airports" );
220         path.append( "runways.mk4" );
221         FGRunways runways( path.c_str() );
222
223 #ifdef FG_WEATHERCM
224         //Set the heading to into the wind
225         double wind_x = stationweather.Wind[0];
226         double wind_y = stationweather.Wind[1];
227
228         double speed = sqrt( wind_x*wind_x + wind_y*wind_y ) * SG_METER_TO_NM / (60.0*60.0);
229         double hdg;
230
231         //If no wind use 270degrees
232         if(speed == 0) {
233             hdg = 270;
234             transmission += "  Winds light and variable";
235         } else {
236             // //normalize the wind to get the direction
237             //wind_x /= speed; wind_y /= speed;
238
239             hdg = - atan2 ( wind_x, wind_y ) * SG_RADIANS_TO_DEGREES ;
240             if (hdg < 0.0)
241               hdg += 360.0;
242
243             //add a description of the wind to the transmission
244             char buf2[72];
245             sprintf(buf2, "%s %i %s %i %s", "  Winds ", int(speed), " knots from ", int(hdg), " degrees");
246             transmission += buf2;
247         }
248 #else
249         double speed = stationweather.get_wind_speed_kt();
250         double hdg = stationweather.get_wind_from_heading_deg();
251         if (speed == 0) {
252           transmission += "  Winds light and variable";
253         } else {
254                                 // FIXME: get gust factor in somehow
255             char buf2[72];
256             sprintf(buf2, "%s %i %s %i %s", "  Winds ", int(speed),
257                     " knots from ", int(hdg), " degrees");
258         }
259 #endif
260
261         string rwy_no = runways.search(ident, int(hdg));
262         if(rwy_no != (string)"NN") {
263             transmission += "  Landing and departing runway ";
264             transmission += rwy_no;
265             //cout << "in atis.cxx, r.rwy_no = " << rwy_no << " r.id = " << r->id << " r.heading = " << r->heading << endl;
266         }
267
268         // Anything else?
269
270         transmission += "  Advise controller on initial contact you have ";
271         transmission += phonetic_id_string;
272 }