]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/atis.cxx
Remove confusing default (missing) path from 2D panel code.
[flightgear.git] / src / ATCDCL / 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21
22 /////
23 ///// TODO:  _Cumulative_ sky coverage.
24 ///// TODO:  wind _gust_
25 ///// TODO:  more-sensible encoding of voice samples
26 /////       u-law?  outright synthesis?
27 /////
28
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32
33 #include "atis.hxx"
34
35 #include <simgear/compiler.h>
36
37 #include <stdlib.h> // atoi()
38 #include <stdio.h>  // sprintf
39 #include <string>
40 #include <iostream>
41
42
43 #include <boost/tuple/tuple.hpp>
44
45 #include <simgear/misc/sg_path.hxx>
46
47 #include <Environment/environment_mgr.hxx>
48 #include <Environment/environment.hxx>
49 #include <Environment/atmosphere.hxx>
50
51 #include <Main/fg_props.hxx>
52 #include <Main/globals.hxx>
53 #include <Airports/runways.hxx>
54
55
56 #include "commlist.hxx"
57 #include "ATCutils.hxx"
58 #include "ATCmgr.hxx"
59
60 using std::cout;
61 using std::cout;
62 using boost::ref;
63 using boost::tie;
64
65 FGATIS::FGATIS() :
66   old_volume(0),
67   atis_failed(false),
68   msg_OK(0),
69   attention(0),
70   _prev_display(0),
71   refname("atis")
72 {
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");
77   }
78   fgTie("/environment/attention", this, (int_getter)0, &FGATIS::attend);
79 }
80
81 // Hint:
82 // http://localhost:5400/environment/attention?value=1&submit=update
83
84 FGATIS::~FGATIS() {
85   fgUntie("/environment/attention");
86 }
87
88 void FGATIS::Init() {
89 // Nothing to see here.  Move along.
90 }
91
92 void
93 FGATIS::attend (int attn)
94 {
95   attention = attn;
96 #ifdef ATMO_TEST
97   int flag = fgGetInt("/sim/logging/atmo");
98   if (flag) {
99     FGAltimeter().check_model();
100         FGAltimeter().dump_stack();
101   }
102 #endif
103 }
104
105
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);
110 #if 0
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;
116     msg_time = cur_time;
117   }
118 #endif
119   if(_display) {
120     double volume(0);
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());
125     }
126
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);
135       old_volume = volume;
136     }
137   } else {
138 // We shouldn't be displaying
139     //cout << "ATIS.CXX - calling NoRender()..." << endl;
140     NoRender(refname);
141   }
142   _prev_display = _display;
143   attention = 0;
144 }
145
146 string uppercase(const string &s) {
147   string rslt(s);
148   for(string::iterator p = rslt.begin(); p != rslt.end(); p++){
149     *p = toupper(*p);
150   }
151   return rslt;
152 }
153
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 + "-";
163
164   size_t where(0);
165   for ( ; (where = orig.find(www, where)) != string::npos ; ) {
166     orig.replace(where, www.length(), nnn);
167     where += nnn.length();
168   }
169   
170   www = uppercase(www);
171   for ( ; (where = orig.find(www, where)) != string::npos ; ) {
172     orig.replace(where, www.length(), nnn);
173     where += nnn.length();
174   }
175   where = orig.length();
176   return orig.substr(1, where-2);
177 }
178
179 // Normally the interval is 1 hour, 
180 // but you can shorten it for testing.
181 const int minute(60);           // measured in seconds
182 #ifdef ATIS_TEST
183   const int ATIS_interval(2*minute);
184 #else
185   const int ATIS_interval(60*minute);
186 #endif
187
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;
194
195   string BRK = ".\n";
196
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
205   }
206
207   const int bs(100);
208   char buf[bs];
209   string time_str = fgGetString("sim/time/gmt-string");
210   string hours, mins;
211   string phonetic_seq_string;
212
213   transmission = "";
214
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 ";
219
220   // SG_LOG(SG_ATC, SG_ALERT, "ATIS: facility name: " << name);
221
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:
225
226   string name2 = name;
227   for(string::iterator p = name2.begin(); p != name2.end(); p++){
228     if (*p == ' ') *p = '-';
229   }
230
231 ///////////////
232 // FIXME:  This would be more flexible and more extensible
233 // if the mappings were taken from an XML file, not hard-coded.
234 ///////////////
235
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;
252   }
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;
260
261   transmission += "Wind: ";
262
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:
283       wind_dir = 270;
284       transmission += " light_and_variable";
285   } else {
286       // FIXME: get gust factor in somehow
287       snprintf(buf, bs, "%03.0f", 5*SGMiscd::round(wind_dir/5));
288       transmission += ConvertNumToSpokenDigits(buf);
289
290       snprintf(buf, bs, "%1.0f", wind_speed);
291       transmission += " at " + ConvertNumToSpokenDigits(buf) + BRK;
292   }
293
294   int did_some(0);
295   int did_ceiling(0);
296
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: ";
308       did_some++;
309     } else /* must be a ceiling */  if (!did_ceiling) {
310       transmission += "   Ceiling: ";
311       did_ceiling++;
312       did_some++;
313     } else {
314       transmission += "   ";
315     }
316     int cig00  = int(SGMiscd::round(ceiling/100));  // hundreds of feet
317     if (cig00) {
318       int cig000 = cig00/10;
319       cig00 -= cig000*10;       // just the hundreds digit
320       if (cig000) {
321     snprintf(buf, bs, "%i", cig000);
322     transmission += ConvertNumToSpokenDigits(buf);
323     transmission += " thousand ";
324       }
325       if (cig00) {
326     snprintf(buf, bs, "%i", cig00);
327     transmission += ConvertNumToSpokenDigits(buf);
328     transmission += " hundred ";
329       }
330     } else {
331       // Should this be "sky obscured?"
332       transmission += " zero ";     // not "zero hundred"
333     }
334     transmission += coverage + BRK;
335   }
336
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)));
340   if(temp < 0) {
341       transmission += "minus ";
342   }
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())));
348   if(temp < 0) {
349       transmission += "minus ";
350   }
351   snprintf(buf, bs, "%i", abs(temp));
352   transmission += ConvertNumToSpokenDigits(buf) + BRK;
353
354
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";
368   } else {
369     // integer miles
370     if (visibility > 10) visibility = 10;
371     sprintf(buf, "%i", int(.5 + visibility));
372     transmission += ConvertNumToSpokenDigits(buf);
373   }
374   transmission += BRK;
375
376   transmission += "Altimeter: ";
377   double myQNH;
378   double Psl = fgGetDouble("/environment/pressure-sea-level-inhg");
379   {
380     double press, temp;
381     
382     tie(press, temp) = PT_vs_hpt(_geod.getElevationM(), Psl*inHg, Tsl + freezing);
383 #if 0
384     SG_LOG(SG_ATC, SG_ALERT, "Field P: " << press << "  T: " << temp);
385     SG_LOG(SG_ATC, SG_ALERT, "based on elev " << elev 
386                                 << "  Psl: " << Psl
387                                 << "  Tsl: " << Tsl);
388 #endif
389     myQNH = FGAtmo().QNH(_geod.getElevationM(), press);
390   }
391   if(ident.substr(0,2) == "EG" && fgGetBool("/sim/atc/use-millibars")) {
392     // Convert to millibars for the UK!
393     myQNH /= mbar;
394     if  (myQNH > 1000) myQNH -= 1000;       // drop high digit
395     snprintf(buf, bs, "%03.0f", myQNH);
396   } else {
397     myQNH /= inHg;
398     myQNH *= 100.;                        // shift two decimal places
399     snprintf(buf, bs, "%04.0f", myQNH);
400   }
401   transmission += ConvertNumToSpokenDigits(buf) + BRK;
402
403   if (_type == ATIS /* as opposed to AWOS */) {
404       const FGAirport* apt = fgFindAirportID(ident);
405       assert(apt);
406         string rwy_no = apt->getActiveRunwayForUsage()->ident();
407       if(rwy_no != "NN") {
408         transmission += "Landing_and_departing_runway ";
409         transmission += ConvertRwyNumToSpokenString(rwy_no) + BRK;
410         if (msg_OK) {
411           msg_time = cur_time;
412           //cout << "In atis.cxx, r.rwy_no: " << rwy_no
413           //   << " wind_dir: " << wind_dir << endl;
414         }
415     }
416     transmission += "On_initial_contact_advise_you_have_information ";
417     transmission += phonetic_seq_string;
418     transmission += "... " + BRK;
419   }
420 #ifdef ATIS_TEST
421   cout << "**** ATIS active on:";
422 #endif
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());
427 #ifdef ATIS_TEST
428     cout << "  " << prop;
429 #endif
430   }
431 #ifdef ATIS_TEST
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]
438 #endif
439
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, " /_ ");
448   }
449   return 1;
450 }