]> git.mxchange.org Git - flightgear.git/blob - src/ATC/atis.cxx
Fix compiler warning
[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 SG_USING_STD(cout);
36
37 //#include <simgear/debug/logstream.hxx>
38 //#include <simgear/misc/sgstream.hxx>
39 #include <simgear/misc/sg_path.hxx>
40
41 #ifdef FG_WEATHERCM
42 # include <WeatherCM/FGLocalWeatherDatabase.h>
43 #else
44 # include <Environment/environment_mgr.hxx>
45 # include <Environment/environment.hxx>
46 #endif
47
48 #include <Main/fg_props.hxx>
49 #include <Main/globals.hxx>
50 #include <Airports/runways.hxx>
51
52 #include "atis.hxx"
53 #include "atislist.hxx"
54 #include "ATCdisplay.hxx"
55 #include "ATCutils.hxx"
56 #include "ATCmgr.hxx"
57
58 // Constructor
59 FGATIS::FGATIS()
60 : type(0),
61 lon(0.0), lat(0.0),
62 elev(0.0),
63 x(0.0), y(0.0), z(0.0),
64 freq(0),
65 range(0),
66 display(false),
67 displaying(false),
68 ident(""),
69 name(""),
70 transmission(""),
71 trans_ident(""),
72 atis_failed(false)
73 {
74 }
75
76 // Destructor
77 FGATIS::~FGATIS() {
78 }
79
80 // Main update function - checks whether we are displaying or not the correct message.
81 void FGATIS::Update() {
82         if(display) {
83                 if(displaying) {
84                         // Check if we need to update the message
85                         // - basically every hour and if the weather changes significantly at the station
86                         //globals->get_ATC_display()->ChangeRepeatingMessage(transmission);
87                 } else {
88                         // We need to get and display the message
89                         UpdateTransmission();
90                         //cout << "ATIS.CXX - calling ATCMgr to render transmission..." << endl;
91                         globals->get_ATC_mgr()->Render(transmission, true);
92                         displaying = true;
93                 }
94         } else {
95                 // We shouldn't be displaying
96                 if(displaying) {
97                         //cout << "ATIS.CXX - calling NoRender()..." << endl;
98                         globals->get_ATC_mgr()->NoRender();
99                         displaying = false;
100                 }
101         }
102 }
103
104 // Sets the actual broadcast ATIS transmission.
105 void FGATIS::UpdateTransmission() {
106         double visibility;
107         char buf[10];
108         int phonetic_id;
109         string phonetic_id_string;
110         string time_str = fgGetString("sim/time/gmt-string");
111         int hours;
112         // int minutes;
113         
114         #ifdef FG_WEATHERCM
115         sgVec3 position = { lat, lon, elev };
116         FGPhysicalProperty stationweather = WeatherDatabase->get(position);
117         #else
118         FGEnvironment stationweather =
119         globals->get_environment_mgr()->getEnvironment(lat, lon, elev);
120         #endif
121         
122         transmission = "";
123         
124         // UK CAA radiotelephony manual indicated ATIS transmissions start with "This is"
125         // Not sure about rest of the world though.
126         transmission += "This_is ";
127         // transmitted station name.
128         transmission += name;
129         // Add "Information"
130         transmission += " information";
131         
132         //cout << "In atis.cxx, time_str = " << time_str << '\n';
133         // Add the recording identifier
134         // For now we will assume we only transmit every hour
135         hours = atoi((time_str.substr(1,2)).c_str());   //Warning - this is fragile if the 
136         //time string format changes
137         //cout << "In atis.cxx, hours = " << hours << endl;
138         phonetic_id = current_atislist->GetCallSign(ident, hours, 0);
139         phonetic_id_string = GetPhoneticIdent(phonetic_id);
140         transmission += " ";
141         transmission += phonetic_id_string;
142         
143         // Output the recording time. - we'll just output the last whole hour for now.
144         // FIXME - this only gets GMT time but that appears to be all the clock outputs for now
145         //cout << "in atis.cxx, time = " << time_str << endl;
146         transmission = transmission + " / Weather " + ConvertNumToSpokenDigits((time_str.substr(0,3) + "00")) + " hours zulu";
147         
148         // Get the temperature
149         // (Hardwire it for now since the global property returns the temperature at the current altitude
150         //temperature = fgGetDouble("/environment/weather/temperature-K");
151         #ifdef FG_WEATHERCM
152         sprintf(buf, "%i", int(stationweather.Temperature - 273.15));
153         #else
154         // HACK ALERT - at the moment the new environment subsystem returns bogus temperatures
155         // FIXME - take out this hack when this gets fixed upstream
156         int temp = (int)stationweather.get_temperature_degc();
157         if((temp < -50) || (temp > 60)) {
158                 temp = 15;
159         }
160         // original:
161         //sprintf(buf, "%d", int(stationweather.get_temperature_degc()));
162         // hack:
163         sprintf(buf, "%d", temp);
164         #endif
165         transmission += " / Temperature ";
166         if(temp < 0) {
167                 transmission += "minus ";
168         }
169         string tempstr1 = buf;
170         string tempstr2;
171         transmission += ConvertNumToSpokenDigits(tempstr1);
172         transmission += " degrees_Celsius";
173         
174         // Get the visibility
175         #ifdef FG_WEATHERCM
176         visibility = fgGetDouble("/environment/visibility-m");
177         #else
178         visibility = stationweather.get_visibility_m();
179         #endif
180         sprintf(buf, "%i", int(visibility/1600));
181         transmission += " / Visibility ";
182         tempstr1 = buf;
183         transmission += ConvertNumToSpokenDigits(tempstr1);
184         transmission += " miles";
185         
186         // Get the cloudbase
187         // FIXME: kludge for now
188         if (strcmp(fgGetString("/environment/clouds/layer[0]/type"), "clear")) {
189                 double cloudbase =
190                 fgGetDouble("/environment/clouds/layer[0]/elevation-ft");
191                 // For some reason the altitude returned doesn't seem to correspond to the actual cloud altitude.
192                 char buf3[10];
193                 char buf4[10];
194                 // cout << "cloudbase = " << cloudbase << endl;
195                 sprintf(buf3, "%i", int(cloudbase)/1000);
196                 sprintf(buf4, "%i", ((int)cloudbase % 1000)/100);
197                 transmission += " / Cloudbase";
198                 if(int(cloudbase)/1000) {
199                         tempstr1 = buf3;
200                         transmission = transmission + " " + ConvertNumToSpokenDigits(tempstr1) + " thousand";
201                 }
202                 if(((int)cloudbase % 1000)/100) {
203                         tempstr1 = buf4;
204                         transmission = transmission + " " + ConvertNumToSpokenDigits(tempstr1) + " hundred";
205                 }                       
206                 transmission += " feet";
207         }
208         
209         // Get the pressure / altimeter
210         
211         #ifndef FG_WEATHERCM
212         double altimeter = stationweather.get_pressure_sea_level_inhg();
213         sprintf(buf, "%.2f", altimeter);
214         transmission += " / Altimeter ";
215         tempstr1 = buf;
216         transmission += ConvertNumToSpokenDigits(tempstr1);
217         #endif
218         
219         // Based on the airport-id and wind get the active runway
220         //FGRunway *r;
221         SGPath path( globals->get_fg_root() );
222         path.append( "Airports" );
223         path.append( "runways.mk4" );
224         FGRunways runways( path.c_str() );
225         
226         #ifdef FG_WEATHERCM
227         //Set the heading to into the wind
228         double wind_x = stationweather.Wind[0];
229         double wind_y = stationweather.Wind[1];
230         
231         double speed = sqrt( wind_x*wind_x + wind_y*wind_y ) * SG_METER_TO_NM / (60.0*60.0);
232         double hdg;
233         
234         //If no wind use 270degrees
235         if(speed == 0) {
236                 hdg = 270;
237                 transmission += " / Winds_light_and_variable";
238         } else {
239                 // //normalize the wind to get the direction
240                 //wind_x /= speed; wind_y /= speed;
241                 
242                 hdg = - atan2 ( wind_x, wind_y ) * SG_RADIANS_TO_DEGREES ;
243                 if (hdg < 0.0)
244                         hdg += 360.0;
245                 
246                 //add a description of the wind to the transmission
247                 char buf5[10];
248                 char buf6[10];
249                 sprintf(buf5, "%i", int(speed));
250                 sprintf(buf6, "%i", int(hdg));
251                 tempstr1 = buf5;
252                 tempstr2 = buf6;
253                 transmission = transmission + " / Winds " + ConvertNumToSpokenDigits(tempstr1) + " knots from "
254                                             + ConvertNumToSpokenDigits(tempstr2) + " degrees";
255         }
256         #else
257         double speed = stationweather.get_wind_speed_kt();
258         double hdg = stationweather.get_wind_from_heading_deg();
259         if (speed == 0) {
260                 transmission += " / Winds_light_and_variable";
261         } else {
262                 // FIXME: get gust factor in somehow
263                 char buf5[10];
264                 char buf6[10];
265                 sprintf(buf5, "%i", int(speed));
266                 sprintf(buf6, "%i", int(hdg));
267                 tempstr1 = buf5;
268                 tempstr2 = buf6;
269                 transmission = transmission + " / Winds " + ConvertNumToSpokenDigits(tempstr1) + " knots from "
270                                             + ConvertNumToSpokenDigits(tempstr2) + " degrees";
271         }
272         #endif
273         
274         string rwy_no = runways.search(ident, int(hdg));
275         if(rwy_no != (string)"NN") {
276                 transmission += " / Landing_and_departing_runway ";
277                 transmission += ConvertRwyNumToSpokenString(atoi(rwy_no.c_str()));
278                 //cout << "in atis.cxx, r.rwy_no = " << rwy_no << " r.id = " << r->id << " r.heading = " << r->heading << endl;
279         }
280         
281         // Anything else?
282         
283         transmission += " / Advise_controller_on_initial_contact_you_have ";
284         transmission += phonetic_id_string;
285         transmission += " /// ";
286 }