1 // atis.cxx - routines to generate the ATIS info string
2 // This is the implementation of the FGATIS class
4 // Written by David Luff, started October 2001.
6 // Copyright (C) 2001 David C Luff - david.luff@nottingham.ac.uk
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 ///// TODO: _Cumulative_ sky coverage.
24 ///// TODO: wind _gust_
25 ///// TODO: more-sensible encoding of voice samples
26 ///// u-law? outright synthesis?
35 #include <simgear/compiler.h>
37 #include <stdlib.h> // atoi()
38 #include <stdio.h> // sprintf
43 #include <boost/tuple/tuple.hpp>
45 #include <simgear/misc/sg_path.hxx>
47 #include <Environment/environment_mgr.hxx>
48 #include <Environment/environment.hxx>
49 #include <Environment/atmosphere.hxx>
51 #include <Main/fg_props.hxx>
52 #include <Main/globals.hxx>
53 #include <Airports/runways.hxx>
56 #include "commlist.hxx"
57 #include "ATCutils.hxx"
73 _vPtr = globals->get_ATC_mgr()->GetVoicePointer(ATIS);
74 _voiceOK = (_vPtr == NULL ? false : true);
75 if (!(_type != ATIS || _type == AWOS)) {
76 SG_LOG(SG_ATC, SG_ALERT, "ERROR - _type not ATIS or AWOS in atis.cxx");
78 fgTie("/environment/attention", this, (int_getter)0, &FGATIS::attend);
82 // http://localhost:5400/environment/attention?value=1&submit=update
85 fgUntie("/environment/attention");
89 // Nothing to see here. Move along.
93 FGATIS::attend (int attn)
97 int flag = fgGetInt("/sim/logging/atmo");
99 FGAltimeter().check_model();
100 FGAltimeter().dump_stack();
106 // Main update function - checks whether we are displaying or not the correct message.
107 void FGATIS::Update(double dt) {
108 cur_time = globals->get_time_params()->get_cur_time();
109 msg_OK = (msg_time < cur_time);
111 if (msg_OK || _display != _prev_display) {
112 cout << "ATIS Update: " << _display << " " << _prev_display
113 << " len: " << transmission.length()
114 << " oldvol: " << old_volume
115 << " dt: " << dt << endl;
121 for (map<string,int>::iterator act = active_on.begin();
122 act != active_on.end(); act++) {
123 string prop = "/instrumentation/" + act->first + "/volume";
124 volume += globals->get_props()->getDoubleValue(prop.c_str());
127 // Check if we need to update the message
128 // - basically every hour and if the weather changes significantly at the station
129 // If !_prev_display, the radio had been detuned for a while and our
130 // "transmission" variable was lost when we were de-instantiated.
131 int rslt = GenTransmission(!_prev_display, attention);
132 if (rslt || volume != old_volume) {
133 //cout << "ATIS calling ATC::render volume: " << volume << endl;
134 Render(transmission, volume, refname, true);
138 // We shouldn't be displaying
139 //cout << "ATIS.CXX - calling NoRender()..." << endl;
142 _prev_display = _display;
146 string uppercase(const string &s) {
148 for(string::iterator p = rslt.begin(); p != rslt.end(); p++){
154 // Replace all occurrences of a given word.
155 // Words in the original string must be separated by hyphens (not spaces).
156 // We check for the word as given, and for the all-caps version thereof.
157 string replace_word(const string _orig, const string _www, const string _nnn){
158 // The following are so we can match words at the beginning
159 // and end of the string.
160 string orig = "-" + _orig + "-";
161 string www = "-" + _www + "-";
162 string nnn = "-" + _nnn + "-";
165 for ( ; (where = orig.find(www, where)) != string::npos ; ) {
166 orig.replace(where, www.length(), nnn);
167 where += nnn.length();
170 www = uppercase(www);
171 for ( ; (where = orig.find(www, where)) != string::npos ; ) {
172 orig.replace(where, www.length(), nnn);
173 where += nnn.length();
175 where = orig.length();
176 return orig.substr(1, where-2);
179 // Normally the interval is 1 hour,
180 // but you can shorten it for testing.
181 const int minute(60); // measured in seconds
183 const int ATIS_interval(2*minute);
185 const int ATIS_interval(60*minute);
188 // Generate the actual broadcast ATIS transmission.
189 // Regen means regenerate the /current/ transmission.
190 // Special means generate a new transmission, with a new sequence.
191 // Returns 1 if we actually generated something.
192 int FGATIS::GenTransmission(const int regen, const int special) {
193 using namespace atmodel;
197 double tstamp = atof(fgGetString("sim/time/elapsed-sec"));
198 int interval = ATIS ? ATIS_interval : 2*minute; // AWOS updated frequently
199 int sequence = current_commlist->GetAtisSequence(ident,
200 tstamp, interval, special);
201 if (!regen && sequence > LTRS) {
202 //xx if (msg_OK) cout << "ATIS: no change: " << sequence << endl;
203 //xx msg_time = cur_time;
204 return 0; // no change since last time
209 string time_str = fgGetString("sim/time/gmt-string");
211 string phonetic_seq_string;
215 // UK CAA radiotelephony manual indicated ATIS transmissions start
216 // with "This is ..."
217 // In the US they just start with the airport name.
218 // transmission += "This_is ";
220 // SG_LOG(SG_ATC, SG_ALERT, "ATIS: facility name: " << name);
222 // Note that at this point, multi-word facility names
223 // will sometimes contain hyphens, not spaces.
224 // Force the issue, just to be safe:
227 for(string::iterator p = name2.begin(); p != name2.end(); p++){
228 if (*p == ' ') *p = '-';
232 // FIXME: This would be more flexible and more extensible
233 // if the mappings were taken from an XML file, not hard-coded.
236 // Remap some abbreviations that occur in apt.dat, to
237 // make things nicer for the text-to-speech system:
238 name2 = replace_word(name2, "Intl", "International");
239 name2 = replace_word(name2, "Rgnl", "Regional");
240 name2 = replace_word(name2, "Co", "County");
241 name2 = replace_word(name2, "Muni", "Municipal");
242 name2 = replace_word(name2, "Mem", "Memorial");
243 name2 = replace_word(name2, "Fld", "Field");
244 name2 = replace_word(name2, "AFB", "Air-Force-Base");
245 name2 = replace_word(name2, "AAF", "Army-Air-Field");
246 name2 = replace_word(name2, "MCAS", "Marine-Corps-Air-Station");
247 transmission += name2 + " ";
248 if (_type == ATIS /* as opposed to AWOS */) {
249 transmission += "airport_information ";
250 phonetic_seq_string = GetPhoneticLetter(sequence); // Add the sequence letter
251 transmission += phonetic_seq_string + BRK;
253 transmission += "Automated_weather_observation ";
254 // Warning - this is fragile if the time string format changes
255 hours = time_str.substr(0,2).c_str();
256 mins = time_str.substr(3,2).c_str();
257 // speak each digit separately:
258 transmission += ConvertNumToSpokenDigits(hours + mins);
259 transmission += " zulu weather" + BRK;
261 transmission += "Wind: ";
263 double wind_speed = fgGetDouble("/environment/config/boundary/entry[0]/wind-speed-kt");
264 double wind_dir = fgGetDouble("/environment/config/boundary/entry[0]/wind-from-heading-deg");
265 while (wind_dir <= 0) wind_dir += 360;
266 // The following isn't as bad a kludge as it might seem.
267 // It combines the magvar at the /aircraft/ location with
268 // the wind direction in the environment/config array.
269 // But if the aircraft is close enough to the station to
270 // be receiving the ATIS signal, this should be a good-enough
271 // approximation. For more-distant aircraft, the wind_dir
272 // shouldn't be corrected anyway.
273 // The less-kludgy approach would be to use the magvar associated
274 // with the station, but that is not tabulated in the stationweather
275 // structure as it stands, and computing it would be expensive.
276 // Also note that as it stands, there is only one environment in
277 // the entire FG universe, so the aircraft environment is the same
278 // as the station environment anyway.
279 wind_dir -= fgGetDouble("/environment/magnetic-variation-deg"); // wind_dir now magnetic
280 if (wind_speed == 0) {
281 // Force west-facing rwys to be used in no-wind situations
282 // which is consistent with Flightgear's initial setup:
284 transmission += " light_and_variable";
286 // FIXME: get gust factor in somehow
287 snprintf(buf, bs, "%03.0f", 5*SGMiscd::round(wind_dir/5));
288 transmission += ConvertNumToSpokenDigits(buf);
290 snprintf(buf, bs, "%1.0f", wind_speed);
291 transmission += " at " + ConvertNumToSpokenDigits(buf) + BRK;
297 for (int layer = 0; layer <= 4; layer++) {
298 snprintf(buf, bs, "/environment/clouds/layer[%i]/coverage", layer);
299 string coverage = fgGetString(buf);
300 if (coverage == "clear") continue;
301 snprintf(buf, bs, "/environment/clouds/layer[%i]/thickness-ft", layer);
302 if (fgGetDouble(buf) == 0) continue;
303 snprintf(buf, bs, "/environment/clouds/layer[%i]/elevation-ft", layer);
304 double ceiling = int(fgGetDouble(buf) - _geod.getElevationFt());
305 if (ceiling > 12000) continue;
306 if (coverage == "scattered") {
307 if (!did_some) transmission += " Sky_condition: ";
309 } else /* must be a ceiling */ if (!did_ceiling) {
310 transmission += " Ceiling: ";
316 int cig00 = int(SGMiscd::round(ceiling/100)); // hundreds of feet
318 int cig000 = cig00/10;
319 cig00 -= cig000*10; // just the hundreds digit
321 snprintf(buf, bs, "%i", cig000);
322 transmission += ConvertNumToSpokenDigits(buf);
323 transmission += " thousand ";
326 snprintf(buf, bs, "%i", cig00);
327 transmission += ConvertNumToSpokenDigits(buf);
328 transmission += " hundred ";
331 // Should this be "sky obscured?"
332 transmission += " zero "; // not "zero hundred"
334 transmission += coverage + BRK;
337 transmission += "Temperature: ";
338 double Tsl = fgGetDouble("/environment/temperature-sea-level-degc");
339 int temp = int(SGMiscd::round(FGAtmo().fake_T_vs_a_us(_geod.getElevationFt(), Tsl)));
341 transmission += "minus ";
343 snprintf(buf, bs, "%i", abs(temp));
344 transmission += ConvertNumToSpokenDigits(buf);
345 transmission += " dewpoint ";
346 double dpsl = fgGetDouble("/environment/dewpoint-sea-level-degc");
347 temp = int(SGMiscd::round(FGAtmo().fake_dp_vs_a_us(dpsl, _geod.getElevationFt())));
349 transmission += "minus ";
351 snprintf(buf, bs, "%i", abs(temp));
352 transmission += ConvertNumToSpokenDigits(buf) + BRK;
355 transmission += "Visibility: ";
356 double visibility = fgGetDouble("/environment/config/boundary/entry[0]/visibility-m");
357 visibility /= atmodel::sm; // convert to statute miles
358 if (visibility < 0.25) {
359 transmission += "less than one quarter";
360 } else if (visibility < 0.5) {
361 transmission += "one quarter";
362 } else if (visibility < 0.75) {
363 transmission += "one half";
364 } else if (visibility < 1.0) {
365 transmission += "three quarters";
366 } else if (visibility >= 1.5 && visibility < 2.0) {
367 transmission += "one and one half";
370 if (visibility > 10) visibility = 10;
371 sprintf(buf, "%i", int(.5 + visibility));
372 transmission += ConvertNumToSpokenDigits(buf);
376 transmission += "Altimeter: ";
378 double Psl = fgGetDouble("/environment/pressure-sea-level-inhg");
382 tie(press, temp) = PT_vs_hpt(_geod.getElevationM(), Psl*inHg, Tsl + freezing);
384 SG_LOG(SG_ATC, SG_ALERT, "Field P: " << press << " T: " << temp);
385 SG_LOG(SG_ATC, SG_ALERT, "based on elev " << elev
389 myQNH = FGAtmo().QNH(_geod.getElevationM(), press);
391 if(ident.substr(0,2) == "EG" && fgGetBool("/sim/atc/use-millibars")) {
392 // Convert to millibars for the UK!
394 if (myQNH > 1000) myQNH -= 1000; // drop high digit
395 snprintf(buf, bs, "%03.0f", myQNH);
398 myQNH *= 100.; // shift two decimal places
399 snprintf(buf, bs, "%04.0f", myQNH);
401 transmission += ConvertNumToSpokenDigits(buf) + BRK;
403 if (_type == ATIS /* as opposed to AWOS */) {
404 const FGAirport* apt = fgFindAirportID(ident);
406 string rwy_no = apt->getActiveRunwayForUsage()->ident();
408 transmission += "Landing_and_departing_runway ";
409 transmission += ConvertRwyNumToSpokenString(rwy_no) + BRK;
412 //cout << "In atis.cxx, r.rwy_no: " << rwy_no
413 // << " wind_dir: " << wind_dir << endl;
416 transmission += "On_initial_contact_advise_you_have_information ";
417 transmission += phonetic_seq_string;
418 transmission += "... " + BRK;
421 cout << "**** ATIS active on:";
423 for (map<string,int>::iterator act = active_on.begin(); act != active_on.end(); act++){
424 string prop = "/instrumentation/" + act->first + "/atis";
425 globals->get_props()->setStringValue(prop.c_str(),
426 ("<pre>\n" + transmission + "</pre>\n").c_str());
432 cout << " ****" << endl;
433 cout << transmission << endl;
434 // Note that even if we aren't outputting the transmission
435 // on stdout, you can still see it by pointing a web browser
436 // at the property tree. The second comm radio is:
437 // http://localhost:5400/instrumentation/comm[1]
440 // Take the previous English-looking string and munge it to
441 // be relatively-more acceptable to the primitive tts system.
442 // Note that : ; and . are among the token-delimeters recognized
443 // by the tts system.
444 for (size_t where;;) {
445 where = transmission.find_first_of(":.");
446 if (where == string::npos) break;
447 transmission.replace(where, 1, " /_ ");