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