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