]> git.mxchange.org Git - flightgear.git/blob - src/ATC/atis.cxx
Moved JSBSim.hxx to src/FDM/JSBSim/
[flightgear.git] / src / ATC / atis.cxx
1 // atis.cxx - routines to generate the ATIS info string
2 //
3 // Written by David Luff, started October 2001.
4 //
5 // Copyright (C) 2001  David C Luff - david.luff@nottingham.ac.uk
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 #include <simgear/compiler.h>
27
28 #include <stdlib.h>     // atoi()
29 #include <stdio.h>      // sprintf
30 #include <string>
31 SG_USING_STD(string);
32
33 #include STL_IOSTREAM
34 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
35 SG_USING_STD(cout);
36 #endif
37
38 //#include <simgear/debug/logstream.hxx>
39 //#include <simgear/misc/sgstream.hxx>
40 #include <simgear/misc/sg_path.hxx>
41
42 //#ifndef FG_NEW_ENVIRONMENT
43 //sorry, that works only with the new weather system
44 #include <WeatherCM/FGLocalWeatherDatabase.h>
45 //#else
46 //#  include <Environment/environment.hxx>
47 //#endif
48
49 #include <Main/fg_props.hxx>
50 #include <Airports/runways.hxx>
51
52 #include "atis.hxx"
53 #include "atislist.hxx"
54
55 string GetPhoneticIdent(int i) {
56 // TODO - Check i is between 1 and 26 and wrap if necessary
57     switch(i) {
58     case 1 : return("Alpha");
59     case 2 : return("Bravo");
60     case 3 : return("Charlie");
61     case 4 : return("Delta");
62     case 5 : return("Echo");
63     case 6 : return("Foxtrot");
64     case 7 : return("Golf");
65     case 8 : return("Hotel");
66     case 9 : return("Indigo");
67     case 10 : return("Juliet");
68     case 11 : return("Kilo");
69     case 12 : return("Lima");
70     case 13 : return("Mike");
71     case 14 : return("November");
72     case 15 : return("Oscar");
73     case 16 : return("Papa");
74     case 17 : return("Quebec");
75     case 18 : return("Romeo");
76     case 19 : return("Sierra");
77     case 20 : return("Tango");
78     case 21 : return("Uniform");
79     case 22 : return("Victor");
80     case 23 : return("Whiskey");
81     case 24 : return("X-ray");
82     case 25 : return("Yankee");
83     case 26 : return("Zulu");
84     }
85     // We shouldn't get here
86     return("Error");
87 }
88
89 // Constructor
90 FGATIS::FGATIS() {
91 }
92
93 // Destructor
94 FGATIS::~FGATIS() {
95 }
96
97 string FGATIS::get_transmission() {
98 //void FGATIS::get_transmission() {
99 #if !defined(FG_NEW_ENVIRONMENT)
100
101     string transmission = "";
102     double visibility;
103     char buf[10];
104     int phonetic_id;
105     string phonetic_id_string;
106     string time_str = fgGetString("sim/time/gmt-string");
107     int hours;
108     // int minutes;
109     sgVec3 position = { lat, lon, elev };
110     FGPhysicalProperty stationweather = WeatherDatabase->get(position);
111
112 // Only update every so-many loops - FIXME - possibly register this with the event scheduler
113 // Ack this doesn't work since the static counter is shared between all instances of FGATIS
114 // OK, for now the radiostack is handling only calling this every-so-often but that is not really 
115 // a proper solution since the atis knows when the transmission is going to change not the radio.
116     //static int i=0;
117     //if(i == 0) {
118         transmission = "";
119
120         // Start with the transmitted station name.
121         transmission += name;
122         // Add "Information"
123         transmission += " Information";
124
125         //cout << "In atis.cxx, time_str = " << time_str << '\n';
126         // Add the recording identifier
127         // For now we will assume we only transmit every hour
128         hours = atoi((time_str.substr(1,2)).c_str());   //Warning - this is fragile if the 
129                                                         //time string format changes
130         //cout << "In atis.cxx, hours = " << hours << endl;
131         phonetic_id = current_atislist->GetCallSign(ident, hours, 0);
132         phonetic_id_string = GetPhoneticIdent(phonetic_id);
133         transmission += " ";
134         transmission += phonetic_id_string;
135
136         // Output the recording time. - we'll just output the last whole hour for now.
137         // FIXME - this only gets GMT time but that appears to be all the clock outputs for now
138         //cout << "in atis.cxx, time = " << time_str << endl;
139         transmission = transmission + "  Weather " + time_str.substr(0,3) + "00 hours Zulu";
140
141         // Get the temperature
142         // (Hardwire it for now since the global property returns the temperature at the current altitude
143         //temperature = fgGetDouble("/environment/weather/temperature-K");
144         sprintf(buf, "%i", int(stationweather.Temperature - 273.15));
145         transmission += "  Temperature ";
146         transmission += buf;
147         transmission += " degrees Celsius";
148
149         // Get the pressure / altimeter
150         // pressure is: stationweather.AirPressure in Pascal
151
152         // Get the visibility
153         visibility = fgGetDouble("/environment/visibility-m");
154         sprintf(buf, "%i", int(visibility/1600));
155         transmission += "  Visibility ";
156         transmission += buf;
157         transmission += " miles";
158
159         // Get the cloudbase
160         if(fgGetBool("/environment/clouds/status")) {
161             double cloudbase = fgGetDouble("/environment/clouds/altitude-ft");
162             // For some reason the altitude returned doesn't seem to correspond to the actual cloud altitude.
163             char buf3[10];
164             // cout << "cloudbase = " << cloudbase << endl;
165             sprintf(buf3, "%i", int(cloudbase));
166             transmission = transmission + "  Cloudbase " + buf3 + " feet";
167         }
168
169         // Based on the airport-id and wind get the active runway
170         //FGRunway *r;
171         SGPath path( globals->get_fg_root() );
172         path.append( "Airports" );
173         path.append( "runways.mk4" );
174         FGRunways runways( path.c_str() );
175
176         //Set the heading to into the wind
177         double wind_x = stationweather.Wind[0];
178         double wind_y = stationweather.Wind[1];
179
180         double speed = sqrt( wind_x*wind_x + wind_y*wind_y ) * SG_METER_TO_NM / (60.0*60.0);
181         double hdg;
182
183         //If no wind use 270degrees
184         if(speed == 0) {
185             hdg = 270;
186             transmission += "  Winds light and variable";
187         } else {
188             // //normalize the wind to get the direction
189             //wind_x /= speed; wind_y /= speed;
190
191             hdg = - atan2 ( wind_x, wind_y ) * SG_RADIANS_TO_DEGREES ;
192             if (hdg < 0.0)
193               hdg += 360.0;
194
195             //add a description of the wind to the transmission
196             char buf2[72];
197             sprintf(buf2, "%s %i %s %i %s", "  Winds ", int(speed), " knots from ", int(hdg), " degrees");
198             transmission += buf2;
199         }
200
201         string rwy_no = runways.search(ident, int(hdg));
202         if(rwy_no != (string)"NN") {
203             transmission += "  Landing and departing runway ";
204             transmission += rwy_no;
205             //cout << "in atis.cxx, r.rwy_no = " << rwy_no << " r.id = " << r->id << " r.heading = " << r->heading << endl;
206         }
207
208         // Anything else?
209
210         // TODO - unhardwire the identifier
211         transmission += "  Advise controller on initial contact you have ";
212         transmission += phonetic_id_string;
213
214     //}
215
216 //    i++;
217 //    if(i == 600) {
218 //      i=0;
219 //    }
220
221     return(transmission);
222 #else
223     return "Station unavailable (not supported by FG_NEW_ENVIRONMENT)";
224 #endif // FG_NEW_ENVIRONMENT
225 }