2 Encode an ATIS into spoken words
3 Copyright (C) 2014 Torsten Dreyer
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.
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.
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.
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 #include <boost/lexical_cast.hpp>
33 using simgear::PropertyList;
35 static string NO_ATIS("nil");
36 static string EMPTY("");
37 #define SPACE append(1,' ')
39 const char * ATCSpeech::getSpokenDigit( int i )
41 string key = "n" + boost::lexical_cast<std::string>( i );
42 return globals->get_locale()->getLocalizedString(key.c_str(), "atc", "" );
45 string ATCSpeech::getSpokenNumber( string number )
48 for( string::iterator it = number.begin(); it != number.end(); ++it ) {
49 if( false == result.empty() )
51 result.append( getSpokenDigit( (*it) - '0' ));
56 string ATCSpeech::getSpokenNumber( int number, bool leadingZero, int digits )
58 vector<const char *> spokenDigits;
59 bool negative = false;
67 spokenDigits.push_back( getSpokenDigit(number%10) );
73 while( n++ < digits ) {
74 spokenDigits.push_back( getSpokenDigit(0) );
80 result.append( globals->get_locale()->getLocalizedString("minus", "atc", "minus" ) );
83 while( false == spokenDigits.empty() ) {
84 if( false == result.empty() )
87 result.append( spokenDigits.back() );
88 spokenDigits.pop_back();
94 string ATCSpeech::getSpokenAltitude( int altitude )
97 int thousands = altitude / 1000;
98 int hundrets = (altitude % 1000) / 100;
100 if( thousands > 0 ) {
101 result.append( getSpokenNumber(thousands) );
103 result.append( getSpokenDigit(1000) );
107 result.append( getSpokenNumber(hundrets) )
109 .append( getSpokenDigit(100) );
114 ATISEncoder::ATISEncoder()
116 handlerMap.insert( std::make_pair( "text", &ATISEncoder::processTextToken ));
117 handlerMap.insert( std::make_pair( "token", &ATISEncoder::processTokenToken ));
118 handlerMap.insert( std::make_pair( "if", &ATISEncoder::processIfToken ));
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( "phenomena", &ATISEncoder::getPhenomena ));
134 handlerMap.insert( std::make_pair( "clouds", &ATISEncoder::getClouds ));
135 handlerMap.insert( std::make_pair( "cavok", &ATISEncoder::getCavok ));
136 handlerMap.insert( std::make_pair( "temperature-deg", &ATISEncoder::getTemperatureDeg ));
137 handlerMap.insert( std::make_pair( "dewpoint-deg", &ATISEncoder::getDewpointDeg ));
138 handlerMap.insert( std::make_pair( "qnh", &ATISEncoder::getQnh ));
139 handlerMap.insert( std::make_pair( "inhg", &ATISEncoder::getInhg ));
140 handlerMap.insert( std::make_pair( "trend", &ATISEncoder::getTrend ));
143 ATISEncoder::~ATISEncoder()
147 SGPropertyNode_ptr findAtisTemplate( const std::string & stationId, SGPropertyNode_ptr atisSchemaNode )
149 using simgear::strutils::starts_with;
150 SGPropertyNode_ptr atisTemplate;
152 PropertyList schemaNodes = atisSchemaNode->getChildren("atis-schema");
153 for( PropertyList::iterator asit = schemaNodes.begin(); asit != schemaNodes.end(); ++asit ) {
154 SGPropertyNode_ptr ppp = (*asit)->getNode("station-starts-with", false );
155 atisTemplate = (*asit)->getNode("atis", false );
156 if( false == atisTemplate.valid() ) continue; // no <atis> node - ignore entry
158 PropertyList startsWithNodes = (*asit)->getChildren("station-starts-with");
159 for( PropertyList::iterator swit = startsWithNodes.begin(); swit != startsWithNodes.end(); ++swit ) {
161 if( starts_with( stationId, (*swit)->getStringValue() ) ) {
171 string ATISEncoder::encodeATIS( ATISInformationProvider * atisInformation )
173 using simgear::strutils::lowercase;
175 if( false == atisInformation->isValid() ) return NO_ATIS;
177 airport = FGAirport::getByIdent( atisInformation->airportId() );
178 if( false == airport.valid() ) {
179 SG_LOG( SG_ATC, SG_WARN, "ATISEncoder: unknown airport id " << atisInformation->airportId() );
183 _atis = atisInformation;
185 // lazily load the schema file on the first call
186 if( false == atisSchemaNode.valid() ) {
187 atisSchemaNode = new SGPropertyNode();
190 SGPath path = globals->resolve_maybe_aircraft_path("ATC/atis.xml");
191 readProperties( path.str(), atisSchemaNode );
193 catch (const sg_exception& e)
195 SG_LOG( SG_ATC, SG_ALERT, "ATISEncoder: Failed to load atis schema definition: " << e.getMessage());
200 string stationId = lowercase( airport->ident() );
202 SGPropertyNode_ptr atisTemplate = findAtisTemplate( stationId, atisSchemaNode );;
203 if( false == atisTemplate.valid() ) {
204 SG_LOG(SG_ATC, SG_WARN, "no matching atis template for station " << stationId );
205 return NO_ATIS; // no template for this station!?
208 return processTokens( atisTemplate );
211 string ATISEncoder::processTokens( SGPropertyNode_ptr node )
215 for( int i = 0; i < node->nChildren(); i++ ) {
216 result.append(processToken( node->getChild(i) ));
222 string ATISEncoder::processToken( SGPropertyNode_ptr token )
224 HandlerMap::iterator it = handlerMap.find( token->getName());
225 if( it == handlerMap.end() ) {
226 SG_LOG(SG_ATC, SG_WARN, "ATISEncoder: unknown token: " << token->getName() );
229 handler_t h = it->second;
230 return (this->*h)( token );
233 string ATISEncoder::processTextToken( SGPropertyNode_ptr token )
235 return token->getStringValue();
238 string ATISEncoder::processTokenToken( SGPropertyNode_ptr token )
240 HandlerMap::iterator it = handlerMap.find( token->getStringValue());
241 if( it == handlerMap.end() ) {
242 SG_LOG(SG_ATC, SG_WARN, "ATISEncoder: unknown token: " << token->getStringValue() );
245 handler_t h = it->second;
246 return (this->*h)( token );
248 token->getStringValue();
251 string ATISEncoder::processIfToken( SGPropertyNode_ptr token )
253 SGPropertyNode_ptr n;
255 if( (n = token->getChild("empty", false )).valid() ) {
256 return checkEmptyCondition( n, true) ?
257 processTokens(token->getChild("then",false)) :
258 processTokens(token->getChild("else",false));
261 if( (n = token->getChild("not-empty", false )).valid() ) {
262 return checkEmptyCondition( n, false) ?
263 processTokens(token->getChild("then",false)) :
264 processTokens(token->getChild("else",false));
267 if( (n = token->getChild("equals", false )).valid() ) {
268 return checkEqualsCondition( n, true) ?
269 processTokens(token->getChild("then",false)) :
270 processTokens(token->getChild("else",false));
273 if( (n = token->getChild("not-equals", false )).valid() ) {
274 return checkEqualsCondition( n, false) ?
275 processTokens(token->getChild("then",false)) :
276 processTokens(token->getChild("else",false));
279 SG_LOG(SG_ATC, SG_WARN, "ATISEncoder: no valid token found for <if> element" );
284 bool ATISEncoder::checkEmptyCondition( SGPropertyNode_ptr node, bool isEmpty )
286 SGPropertyNode_ptr n1 = node->getNode( "token", false );
287 if( false == n1.valid() ) {
288 SG_LOG(SG_ATC, SG_WARN, "missing <token> node for (not)-empty" );
291 return processToken( n1 ).empty() == isEmpty;
294 bool ATISEncoder::checkEqualsCondition( SGPropertyNode_ptr node, bool isEqual )
296 SGPropertyNode_ptr n1 = node->getNode( "token", 0, false );
297 SGPropertyNode_ptr n2 = node->getNode( "token", 1, false );
299 if( n1.valid() && n2.valid() ) {
300 bool comp = processToken( n1 ).compare( processToken( n2 ) ) == 0;
301 return comp == isEqual;
304 if( n1.valid() && !n2.valid() ) {
305 SGPropertyNode_ptr t = node->getNode( "text", 0, false );
307 bool comp = processToken( n1 ).compare( processTextToken( t ) ) == 0;
308 return comp == isEqual;
310 SG_LOG(SG_ATC, SG_WARN, "missing <token> or <text> node for (not)-equals");
314 SG_LOG(SG_ATC, SG_WARN, "missing <token> node for (not)-equals");
318 string ATISEncoder::getAtisId( SGPropertyNode_ptr )
320 FGAirportDynamics * dynamics = airport->getDynamics();
321 if( NULL != dynamics ) {
322 dynamics->updateAtisSequence( 30*60, false );
323 return dynamics->getAtisSequence();
328 string ATISEncoder::getAirportName( SGPropertyNode_ptr )
330 return airport->getName();
333 string ATISEncoder::getTime( SGPropertyNode_ptr )
335 return getSpokenNumber( _atis->getTime() % (100*100), true, 4 );
338 static inline FGRunwayRef findBestRunwayForWind( FGAirportRef airport, int windDeg, int windKt )
340 struct FGAirport::FindBestRunwayForHeadingParams p;
341 //TODO: ramp down the heading weight with wind speed
343 return airport->findBestRunwayForHeading( windDeg, &p );
346 string ATISEncoder::getApproachType( SGPropertyNode_ptr )
348 FGRunwayRef runway = findBestRunwayForWind( airport, _atis->getWindDeg(), _atis->getWindSpeedKt() );
349 if( runway.valid() ) {
350 if( NULL != runway->ILS() ) return globals->get_locale()->getLocalizedString("ils", "atc", "ils" );
351 //TODO: any chance to find other approach types? localizer-dme, vor-dme, vor, ndb?
354 return globals->get_locale()->getLocalizedString("visual", "atc", "visual" );
357 string ATISEncoder::getLandingRunway( SGPropertyNode_ptr )
359 FGRunwayRef runway = findBestRunwayForWind( airport, _atis->getWindDeg(), _atis->getWindSpeedKt() );
360 if( runway.valid() ) {
361 string runwayIdent = runway->ident();
362 if(runwayIdent != "NN") {
363 return getSpokenNumber(runwayIdent);
369 string ATISEncoder::getTakeoffRunway( SGPropertyNode_ptr p )
371 //TODO: if the airport has more than one runway, probably pick another one?
372 return getLandingRunway( p );
375 string ATISEncoder::getTransitionLevel( SGPropertyNode_ptr )
377 double hPa = _atis->getQnh();
379 /* Transition level is the flight level above which aircraft must use standard pressure and below
380 * which airport pressure settings must be used.
381 * Following definitions are taken from German ATIS:
382 * QNH <= 977 hPa: TRL 80
383 * QNH <= 1013 hPa: TRL 70
384 * QNH > 1013 hPa: TRL 60
385 * (maybe differs slightly for other countries...)
390 } else if( hPa > 978 && hPa <= 1013 ) {
392 } else if( hPa > 1013 && hPa <= 1046 ) {
398 // add an offset to the transition level for high altitude airports (just guessing here,
400 int e = int(airport->getElevation() / 1000.0);
402 // TL steps in 10(00)ft
406 return getSpokenNumber(tl);
409 string ATISEncoder::getWindDirection( SGPropertyNode_ptr )
411 return getSpokenNumber( _atis->getWindDeg(), true, 3 );
414 string ATISEncoder::getWindMinDirection( SGPropertyNode_ptr )
416 return getSpokenNumber( _atis->getWindMinDeg(), true, 3 );
419 string ATISEncoder::getWindMaxDirection( SGPropertyNode_ptr )
421 return getSpokenNumber( _atis->getWindMaxDeg(), true, 3 );
424 string ATISEncoder::getWindspeedKnots( SGPropertyNode_ptr )
426 return getSpokenNumber( _atis->getWindSpeedKt() );
429 string ATISEncoder::getGustsKnots( SGPropertyNode_ptr )
431 int g = _atis->getGustsKt();
432 return g > 0 ? getSpokenNumber( g ) : EMPTY;
435 string ATISEncoder::getCavok( SGPropertyNode_ptr )
437 string CAVOK = globals->get_locale()->getLocalizedString("cavok", "atc", "cavok" );
439 return _atis->isCavok() ? CAVOK : EMPTY;
442 string ATISEncoder::getVisibilityMetric( SGPropertyNode_ptr )
444 string m = globals->get_locale()->getLocalizedString("meters", "atc", "meters" );
445 string km = globals->get_locale()->getLocalizedString("kilometers", "atc", "kilometers" );
446 string or_more = globals->get_locale()->getLocalizedString("ormore", "atc", "or more" );
448 int v = _atis->getVisibilityMeters();
450 if( v < 5000 ) return reply.append( getSpokenAltitude( v ) ).SPACE.append( m );
451 if( v >= 9999 ) return reply.append( getSpokenNumber(10) ).SPACE.append( km ).SPACE.append(or_more);
452 return reply.append( getSpokenNumber( v/1000 ).SPACE.append( km ) );
455 string ATISEncoder::getPhenomena( SGPropertyNode_ptr )
457 return _atis->getPhenomena();
460 string ATISEncoder::getClouds( SGPropertyNode_ptr )
462 string FEET = globals->get_locale()->getLocalizedString("feet", "atc", "feet" );
465 ATISInformationProvider::CloudEntries cloudEntries = _atis->getClouds();
467 for( ATISInformationProvider::CloudEntries::iterator it = cloudEntries.begin(); it != cloudEntries.end(); it++ ) {
468 if( false == reply.empty() ) reply.SPACE;
469 reply.append( it->second ).SPACE.append( getSpokenAltitude(it->first).SPACE.append( FEET ) );
474 string ATISEncoder::getTemperatureDeg( SGPropertyNode_ptr )
476 return getSpokenNumber( _atis->getTemperatureDeg() );
479 string ATISEncoder::getDewpointDeg( SGPropertyNode_ptr )
481 return getSpokenNumber( _atis->getDewpointDeg() );
484 string ATISEncoder::getQnh( SGPropertyNode_ptr )
486 return getSpokenNumber( _atis->getQnh() );
489 string ATISEncoder::getInhg( SGPropertyNode_ptr )
491 string DECIMAL = globals->get_locale()->getLocalizedString("dp", "atc", "decimal" );
493 int fractpart = 1000 * ::modf( _atis->getQnh() * 100.0 / SG_INHG_TO_PA, &intpart );
498 reply.append( getSpokenNumber( (int)intpart ) )
499 .SPACE.append( DECIMAL ).SPACE
500 .append( getSpokenNumber( fractpart ) );
504 string ATISEncoder::getTrend( SGPropertyNode_ptr )
506 return _atis->getTrend();