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