Debian Codegraph @

src/ATC/ATISEncoder.cxx

raw symbols SCIP
1 /*
2 Encode an ATIS into spoken words
3 Copyright (C) 2014 Torsten Dreyer
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20 #include "ATISEncoder.hxx"
21 #include <Airports/dynamics.hxx>
22 #include <Main/globals.hxx>
23 #include <Main/locale.hxx>
24 #include <simgear/structure/exception.hxx>
25 #include <simgear/props/props_io.hxx>
26
27 #include <map>
28 #include <vector>
29
30 using std::string;
31 using std::vector;
32 using simgear::PropertyList;
33
34 static string NO_ATIS("nil");
35 static string EMPTY("");
36 #define SPACE append(1,' ')
37
38 std::string ATCSpeech::getSpokenDigit( int i )
39 {
40 string key = "n" + std::to_string(i);
41 return globals->get_locale()->getLocalizedString(key.c_str(), "atc", "" );
42 }
43
44 string ATCSpeech::getSpokenNumber( string number )
45 {
46 string result;
47 for( string::iterator it = number.begin(); it != number.end(); ++it ) {
48 if( !result.empty() )
49 result.SPACE;
50 result.append( getSpokenDigit( (*it) - '0' ));
51 }
52 return result;
53 }
54
55 string ATCSpeech::getSpokenNumber( int number, bool leadingZero, int digits )
56 {
57 string_list spokenDigits;
58 bool negative = false;
59 if( number < 0 ) {
60 negative = true;
61 number = -number;
62 }
63
64 int n = 0;
65 while( number > 0 ) {
66 spokenDigits.push_back( getSpokenDigit(number%10) );
67 number /= 10;
68 n++;
69 }
70
71 if( digits > 0 ) {
72 while( n++ < digits ) {
73 spokenDigits.push_back( getSpokenDigit(0) );
74 }
75 }
76
77 string result;
78 if( negative ) {
79 result.append( globals->get_locale()->getLocalizedString("minus", "atc", "minus" ) );
80 }
81
82 while( !spokenDigits.empty() ) {
83 if( !result.empty() )
84 result.SPACE;
85
86 result.append( spokenDigits.back() );
87 spokenDigits.pop_back();
88 }
89
90 return result;
91 }
92
93 string ATCSpeech::getSpokenAltitude( int altitude )
94 {
95 string result;
96 int thousands = altitude / 1000;
97 int hundreds = (altitude % 1000) / 100;
98
99 if( thousands > 0 ) {
100 result.append( getSpokenNumber(thousands) );
101 result.SPACE;
102 result.append( getSpokenDigit(1000) );
103 result.SPACE;
104 }
105 if( hundreds > 0 )
106 result.append( getSpokenNumber(hundreds) )
107 .SPACE
108 .append( getSpokenDigit(100) );
109
110 return result;
111 }
112
113 ATISEncoder::ATISEncoder()
114 {
115 handlerMap.insert( std::make_pair( "text", &ATISEncoder::processTextToken ));
116 handlerMap.insert( std::make_pair( "token", &ATISEncoder::processTokenToken ));
117 handlerMap.insert( std::make_pair( "if", &ATISEncoder::processIfToken ));
118 handlerMap.insert( std::make_pair( "section", &ATISEncoder::processTokens ));
119
120 handlerMap.insert( std::make_pair( "id", &ATISEncoder::getAtisId ));
121 handlerMap.insert( std::make_pair( "airport-name", &ATISEncoder::getAirportName ));
122 handlerMap.insert( std::make_pair( "time", &ATISEncoder::getTime ));
123 handlerMap.insert( std::make_pair( "approach-type", &ATISEncoder::getApproachType ));
124 handlerMap.insert( std::make_pair( "rwy-land", &ATISEncoder::getLandingRunway ));
125 handlerMap.insert( std::make_pair( "rwy-to", &ATISEncoder::getTakeoffRunway ));
126 handlerMap.insert( std::make_pair( "transition-level", &ATISEncoder::getTransitionLevel ));
127 handlerMap.insert( std::make_pair( "wind-dir", &ATISEncoder::getWindDirection ));
128 handlerMap.insert( std::make_pair( "wind-from", &ATISEncoder::getWindMinDirection ));
129 handlerMap.insert( std::make_pair( "wind-to", &ATISEncoder::getWindMaxDirection ));
130 handlerMap.insert( std::make_pair( "wind-speed-kn", &ATISEncoder::getWindspeedKnots ));
131 handlerMap.insert( std::make_pair( "gusts", &ATISEncoder::getGustsKnots ));
132 handlerMap.insert( std::make_pair( "visibility-metric", &ATISEncoder::getVisibilityMetric ));
133 handlerMap.insert( std::make_pair( "visibility-miles", &ATISEncoder::getVisibilityMiles ));
134 handlerMap.insert( std::make_pair( "phenomena", &ATISEncoder::getPhenomena ));
135 handlerMap.insert( std::make_pair( "clouds", &ATISEncoder::getClouds ));
136 handlerMap.insert( std::make_pair( "clouds-brief", &ATISEncoder::getCloudsBrief ));
137 handlerMap.insert( std::make_pair( "cavok", &ATISEncoder::getCavok ));
138 handlerMap.insert( std::make_pair( "temperature-deg", &ATISEncoder::getTemperatureDeg ));
139 handlerMap.insert( std::make_pair( "dewpoint-deg", &ATISEncoder::getDewpointDeg ));
140 handlerMap.insert( std::make_pair( "qnh", &ATISEncoder::getQnh ));
141 handlerMap.insert( std::make_pair( "inhg", &ATISEncoder::getInhg ));
142 handlerMap.insert( std::make_pair( "inhg-integer", &ATISEncoder::getInhgInteger ));
143 handlerMap.insert( std::make_pair( "inhg-fraction", &ATISEncoder::getInhgFraction ));
144 handlerMap.insert( std::make_pair( "trend", &ATISEncoder::getTrend ));
145 }
146
147 ATISEncoder::~ATISEncoder()
148 {
149 }
150
151 SGPropertyNode_ptr findAtisTemplate( const std::string & stationId, SGPropertyNode_ptr atisSchemaNode )
152 {
153 using simgear::strutils::starts_with;
154 SGPropertyNode_ptr atisTemplate;
155
156 PropertyList schemaNodes = atisSchemaNode->getChildren("atis-schema");
157 for( PropertyList::iterator asit = schemaNodes.begin(); asit != schemaNodes.end(); ++asit ) {
158 SGPropertyNode_ptr ppp = (*asit)->getNode("station-starts-with", false );
159 atisTemplate = (*asit)->getNode("atis", false );
160 if( !atisTemplate.valid() ) continue; // no <atis> node - ignore entry
161
162 PropertyList startsWithNodes = (*asit)->getChildren("station-starts-with");
163 for( PropertyList::iterator swit = startsWithNodes.begin(); swit != startsWithNodes.end(); ++swit ) {
164
165 if( starts_with( stationId, (*swit)->getStringValue() ) ) {
166 return atisTemplate;
167 }
168 }
169
170 }
171
172 return atisTemplate;
173 }
174
175 string ATISEncoder::encodeATIS( ATISInformationProvider * atisInformation )
176 {
177 using simgear::strutils::lowercase;
178
179 if( !atisInformation->isValid() ) return NO_ATIS;
180
181 airport = FGAirport::getByIdent( atisInformation->airportId() );
182 if( !airport.valid() ) {
183 SG_LOG( SG_ATC, SG_WARN, "ATISEncoder: unknown airport id " << atisInformation->airportId() );
184 return NO_ATIS;
185 }
186
187 _atis = atisInformation;
188
189 // lazily load the schema file on the first call
190 if( !atisSchemaNode.valid() ) {
191 atisSchemaNode = new SGPropertyNode();
192 try
193 {
194 SGPath path = globals->resolve_maybe_aircraft_path("ATC/atis.xml");
195 readProperties( path, atisSchemaNode );
196 }
197 catch (const sg_exception& e)
198 {
199 SG_LOG( SG_ATC, SG_ALERT, "ATISEncoder: Failed to load atis schema definition: " << e.getMessage());
200 return NO_ATIS;
201 }
202 }
203
204 string stationId = lowercase( airport->ident() );
205
206 SGPropertyNode_ptr atisTemplate = findAtisTemplate( stationId, atisSchemaNode );;
207 if( !atisTemplate.valid() ) {
208 SG_LOG(SG_ATC, SG_WARN, "no matching atis template for station " << stationId );
209 return NO_ATIS; // no template for this station!?
210 }
211
212 return processTokens( atisTemplate );
213 }
214
215 string ATISEncoder::processTokens( SGPropertyNode_ptr node )
216 {
217 string result;
218 if( node.valid() ) {
219 for( int i = 0; i < node->nChildren(); i++ ) {
220 result.append(processToken( node->getChild(i) ));
221 }
222 }
223 return result;
224 }
225
226 string ATISEncoder::processToken( SGPropertyNode_ptr token )
227 {
228 HandlerMap::iterator it = handlerMap.find( token->getNameString());
229 if( it == handlerMap.end() ) {
230 SG_LOG(SG_ATC, SG_WARN, "ATISEncoder: unknown token: " << token->getNameString() );
231 return EMPTY;
232 }
233 handler_t h = it->second;
234 return (this->*h)( token );
235 }
236
237 string ATISEncoder::processTextToken( SGPropertyNode_ptr token )
238 {
239 return token->getStringValue();
240 }
241
242 string ATISEncoder::processTokenToken( SGPropertyNode_ptr token )
243 {
244 HandlerMap::iterator it = handlerMap.find( token->getStringValue());
245 if( it == handlerMap.end() ) {
246 SG_LOG(SG_ATC, SG_WARN, "ATISEncoder: unknown token: " << token->getStringValue() );
247 return EMPTY;
248 }
249 handler_t h = it->second;
250 return (this->*h)( token );
251 }
252
253 string ATISEncoder::processIfToken( SGPropertyNode_ptr token )
254 {
255 using namespace simgear::strutils;
256
257 SGPropertyNode_ptr n;
258
259 if( (n = token->getChild("empty", false )).valid() ) {
260 return checkEmptyCondition( n, true) ?
261 processTokens(token->getChild("then",false)) :
262 processTokens(token->getChild("else",false));
263 }
264
265 if( (n = token->getChild("not-empty", false )).valid() ) {
266 return checkEmptyCondition( n, false) ?
267 processTokens(token->getChild("then",false)) :
268 processTokens(token->getChild("else",false));
269 }
270
271 if( (n = token->getChild("contains", false )).valid() ) {
272 return checkCondition( n, true, &contains, "contains") ?
273 processTokens(token->getChild("then",false)) :
274 processTokens(token->getChild("else",false));
275 }
276
277 if( (n = token->getChild("not-contains", false )).valid() ) {
278 return checkCondition( n, false, &contains, "not-contains") ?
279 processTokens(token->getChild("then",false)) :
280 processTokens(token->getChild("else",false));
281 }
282
283 if( (n = token->getChild("ends-with", false )).valid() ) {
284 return checkCondition( n, true, &ends_with, "ends-with") ?
285 processTokens(token->getChild("then",false)) :
286 processTokens(token->getChild("else",false));
287 }
288
289 if( (n = token->getChild("not-ends-with", false )).valid() ) {
290 return checkCondition( n, false, &ends_with, "not-ends-with") ?
291 processTokens(token->getChild("then",false)) :
292 processTokens(token->getChild("else",false));
293 }
294
295 if( (n = token->getChild("equals", false )).valid() ) {
296 return checkCondition( n, true, &equals, "equals") ?
297 processTokens(token->getChild("then",false)) :
298 processTokens(token->getChild("else",false));
299 }
300
301 if( (n = token->getChild("not-equals", false )).valid() ) {
302 return checkCondition( n, false, &equals, "not-equals") ?
303 processTokens(token->getChild("then",false)) :
304 processTokens(token->getChild("else",false));
305 }
306
307 if( (n = token->getChild("starts-with", false )).valid() ) {
308 return checkCondition( n, true, &starts_with, "starts-with") ?
309 processTokens(token->getChild("then",false)) :
310 processTokens(token->getChild("else",false));
311 }
312
313 if( (n = token->getChild("not-starts-with", false )).valid() ) {
314 return checkCondition( n, false, &starts_with, "not-starts-with") ?
315 processTokens(token->getChild("then",false)) :
316 processTokens(token->getChild("else",false));
317 }
318
319 SG_LOG(SG_ATC, SG_WARN, "ATISEncoder: no valid token found for <if> element" );
320
321 return EMPTY;
322 }
323
324 bool ATISEncoder::checkEmptyCondition( SGPropertyNode_ptr node, bool isEmpty )
325 {
326 SGPropertyNode_ptr n1 = node->getNode( "token", false );
327 if( !n1.valid() ) {
328 SG_LOG(SG_ATC, SG_WARN, "missing <token> node for (not)-empty" );
329 return false;
330 }
331 return processToken( n1 ).empty() == isEmpty;
332 }
333
334 bool ATISEncoder::checkCondition( SGPropertyNode_ptr node, bool notInverted,
335 bool (*fp)(const string &, const string &), const string &name )
336 {
337 using namespace simgear::strutils;
338
339 SGPropertyNode_ptr n1 = node->getNode( "token", 0, false );
340 SGPropertyNode_ptr n2 = node->getNode( "token", 1, false );
341
342 if( n1.valid() && n2.valid() ) {
343 bool comp = fp( processToken( n1 ), processToken( n2 ) );
344 return comp == notInverted;
345 }
346
347 if( n1.valid() && !n2.valid() ) {
348 SGPropertyNode_ptr t1 = node->getNode( "text", 0, false );
349 if( t1.valid() ) {
350 string n1s = lowercase( strip( processToken( n1 ) ) );
351 string t1s = lowercase( strip( processTextToken( t1 ) ) );
352 return fp( n1s, t1s ) == notInverted;
353 }
354 SG_LOG(SG_ATC, SG_WARN, "missing <token> or <text> node for " << name);
355 return false;
356 }
357
358 SG_LOG(SG_ATC, SG_WARN, "missing <token> node for " << name);
359 return false;
360 }
361
362 string ATISEncoder::getAtisId( SGPropertyNode_ptr )
363 {
364 FGAirportDynamics * dynamics = airport->getDynamics();
365 if( NULL != dynamics ) {
366 dynamics->updateAtisSequence( 30*60, false );
367 return dynamics->getAtisSequence();
368 }
369 return EMPTY;
370 }
371
372 string ATISEncoder::getAirportName( SGPropertyNode_ptr )
373 {
374 return airport->getName();
375 }
376
377 string ATISEncoder::getTime( SGPropertyNode_ptr )
378 {
379 return getSpokenNumber( _atis->getTime() % (100*100), true, 4 );
380 }
381
382 static inline FGRunwayRef findBestRunwayForWind( FGAirportRef airport, int windDeg, int windKt )
383 {
384 struct FGAirport::FindBestRunwayForHeadingParams p;
385 // TODO: ramp down the heading weight with wind speed
386 p.ilsWeight = 4;
387 return airport->findBestRunwayForHeading( windDeg, &p );
388 }
389
390 string ATISEncoder::getApproachType( SGPropertyNode_ptr )
391 {
392 FGRunwayRef runway = findBestRunwayForWind( airport, _atis->getWindDeg(), _atis->getWindSpeedKt() );
393 if( runway.valid() ) {
394 if( NULL != runway->ILS() ) return globals->get_locale()->getLocalizedString("ils", "atc", "ils" );
395 // TODO: any chance to find other approach types? localizer-dme, vor-dme, vor, ndb?
396 }
397
398 return globals->get_locale()->getLocalizedString("visual", "atc", "visual" );
399 }
400
401 string ATISEncoder::getLandingRunway( SGPropertyNode_ptr )
402 {
403 FGRunwayRef runway = findBestRunwayForWind( airport, _atis->getWindDeg(), _atis->getWindSpeedKt() );
404 if( runway.valid() ) {
405 string runwayIdent = runway->ident();
406 if(runwayIdent != "NN") {
407 return getSpokenNumber(runwayIdent);
408 }
409 }
410 return EMPTY;
411 }
412
413 string ATISEncoder::getTakeoffRunway( SGPropertyNode_ptr p )
414 {
415 // TODO: if the airport has more than one runway, probably pick another one?
416 return getLandingRunway( p );
417 }
418
419 string ATISEncoder::getTransitionLevel(SGPropertyNode_ptr)
420 {
421 double hPa = _atis->getQnh();
422
423 /* Transition level is the flight level above which aircraft must use standard pressure and below
424 * which airport pressure settings must be used.
425 * Following definitions are taken from German ATIS:
426 * QNH <= 977 hPa: TRL 80
427 * QNH <= 1013 hPa: TRL 70
428 * QNH > 1013 hPa: TRL 60
429 * (maybe differs slightly for other countries...)
430 */
431 int tl;
432 if (hPa <= 978) {
433 tl = 80;
434 } else if (hPa <= 1013) {
435 tl = 70;
436 } else if (hPa <= 1046) {
437 tl = 60;
438 } else {
439 tl = 50;
440 }
441
442 // add an offset to the transition level for high altitude airports (just guessing here,
443 // seems reasonable)
444 int e = int(airport->getElevation() / 1000.0);
445 if (e >= 3) {
446 // TL steps in 10(00)ft
447 tl += (e - 2) * 10;
448 }
449
450 return getSpokenNumber(tl);
451 }
452
453 string ATISEncoder::getWindDirection( SGPropertyNode_ptr )
454 {
455 string variable = globals->get_locale()->getLocalizedString("variable", "atc", "variable" );
456
457 bool vrb = _atis->getWindMinDeg() == 0 && _atis->getWindMaxDeg() == 359;
458 return vrb ? variable : getSpokenNumber( _atis->getWindDeg(), true, 3 );
459 }
460
461 string ATISEncoder::getWindMinDirection( SGPropertyNode_ptr )
462 {
463 return getSpokenNumber( _atis->getWindMinDeg(), true, 3 );
464 }
465
466 string ATISEncoder::getWindMaxDirection( SGPropertyNode_ptr )
467 {
468 return getSpokenNumber( _atis->getWindMaxDeg(), true, 3 );
469 }
470
471 string ATISEncoder::getWindspeedKnots( SGPropertyNode_ptr )
472 {
473 return getSpokenNumber( _atis->getWindSpeedKt() );
474 }
475
476 string ATISEncoder::getGustsKnots( SGPropertyNode_ptr )
477 {
478 int g = _atis->getGustsKt();
479 return g > 0 ? getSpokenNumber( g ) : EMPTY;
480 }
481
482 string ATISEncoder::getCavok( SGPropertyNode_ptr )
483 {
484 string CAVOK = globals->get_locale()->getLocalizedString("cavok", "atc", "cavok" );
485
486 return _atis->isCavok() ? CAVOK : EMPTY;
487 }
488
489 string ATISEncoder::getVisibilityMetric( SGPropertyNode_ptr )
490 {
491 string m = globals->get_locale()->getLocalizedString("meters", "atc", "meters" );
492 string km = globals->get_locale()->getLocalizedString("kilometers", "atc", "kilometers" );
493 string or_more = globals->get_locale()->getLocalizedString("ormore", "atc", "or more" );
494
495 int v = _atis->getVisibilityMeters();
496 string reply;
497 if( v < 5000 ) return reply.append( getSpokenAltitude( v ) ).SPACE.append( m );
498 if( v >= 9999 ) return reply.append( getSpokenNumber(10) ).SPACE.append( km ).SPACE.append(or_more);
499 return reply.append( getSpokenNumber( v/1000 ).SPACE.append( km ) );
500 }
501
502 string ATISEncoder::getVisibilityMiles( SGPropertyNode_ptr )
503 {
504 string feet = globals->get_locale()->getLocalizedString("feet", "atc", "feet" );
505
506 int v = _atis->getVisibilityMeters();
507 int vft = int( v * SG_METER_TO_FEET / 100 + 0.5 ) * 100; // Rounded to 100 ft
508 int vsm = int( v * SG_METER_TO_SM + 0.5 );
509
510 string reply;
511 if( vsm < 1 ) return reply.append( getSpokenAltitude( vft ) ).SPACE.append( feet );
512 if( vsm >= 10 ) return reply.append( getSpokenNumber(10) );
513 return reply.append( getSpokenNumber( vsm ) );
514 }
515
516 string ATISEncoder::getPhenomena( SGPropertyNode_ptr )
517 {
518 return _atis->getPhenomena();
519 }
520
521 string ATISEncoder::getClouds( SGPropertyNode_ptr )
522 {
523 string FEET = globals->get_locale()->getLocalizedString("feet", "atc", "feet" );
524 string reply;
525
526 ATISInformationProvider::CloudEntries cloudEntries = _atis->getClouds();
527
528 for (ATISInformationProvider::CloudEntries::iterator it = cloudEntries.begin(); it != cloudEntries.end(); ++it) {
529 if (!reply.empty())
530 reply.SPACE;
531 reply.append( it->second ).SPACE.append( getSpokenAltitude(it->first).SPACE.append( FEET ) );
532 }
533
534 return reply;
535 }
536
537 string ATISEncoder::getCloudsBrief( SGPropertyNode_ptr )
538 {
539 string reply;
540
541 ATISInformationProvider::CloudEntries cloudEntries = _atis->getClouds();
542
543 for (ATISInformationProvider::CloudEntries::iterator it = cloudEntries.begin(); it != cloudEntries.end(); ++it) {
544 if (!reply.empty())
545 reply.append(",").SPACE;
546 reply.append( it->second ).SPACE.append( getSpokenAltitude(it->first) );
547 }
548
549 return reply;
550 }
551
552 string ATISEncoder::getTemperatureDeg( SGPropertyNode_ptr )
553 {
554 return getSpokenNumber( _atis->getTemperatureDeg() );
555 }
556
557 string ATISEncoder::getDewpointDeg( SGPropertyNode_ptr )
558 {
559 return getSpokenNumber( _atis->getDewpointDeg() );
560 }
561
562 string ATISEncoder::getQnh( SGPropertyNode_ptr )
563 {
564 return getSpokenNumber( _atis->getQnh() );
565 }
566
567 string ATISEncoder::getInhgInteger( SGPropertyNode_ptr )
568 {
569 double qnh = _atis->getQnhInHg();
570 return getSpokenNumber( (int)qnh, true, 2 );
571 }
572
573 string ATISEncoder::getInhgFraction( SGPropertyNode_ptr )
574 {
575 double qnh = _atis->getQnhInHg();
576 int f = int(100 * (qnh - int(qnh)) + 0.5);
577 return getSpokenNumber( f, true, 2 );
578 }
579
580 string ATISEncoder::getInhg( SGPropertyNode_ptr node)
581 {
582 string DECIMAL = globals->get_locale()->getLocalizedString("dp", "atc", "decimal" );
583 return getInhgInteger(node)
584 .SPACE.append(DECIMAL).SPACE
585 .append(getInhgFraction(node));
586 }
587
588 string ATISEncoder::getTrend( SGPropertyNode_ptr )
589 {
590 return _atis->getTrend();
591 }
592