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 result.append( getSpokenDigit( (*it) - '0' )).SPACE;
54 string ATCSpeech::getSpokenNumber( int number, bool leadingZero, int digits )
56 vector<const char *> spokenDigits;
59 spokenDigits.push_back( getSpokenDigit(number%10) );
65 while( n++ < digits ) {
66 spokenDigits.push_back( getSpokenDigit(0) );
71 while( false == spokenDigits.empty() ) {
72 if( false == spokenDigits.empty() )
75 result.append( spokenDigits.back() );
76 spokenDigits.pop_back();
82 string ATCSpeech::getSpokenAltitude( int altitude )
85 int thousands = altitude / 1000;
86 int hundrets = (altitude % 1000) / 100;
89 result.append( getSpokenNumber(thousands) );
91 result.append( getSpokenDigit(1000) );
95 result.append( getSpokenNumber(hundrets) )
97 .append( getSpokenDigit(100) );
102 ATISEncoder::ATISEncoder()
104 handlerMap.insert( std::make_pair( "text", &ATISEncoder::processTextToken ));
105 handlerMap.insert( std::make_pair( "token", &ATISEncoder::processTokenToken ));
106 handlerMap.insert( std::make_pair( "if", &ATISEncoder::processIfToken ));
108 handlerMap.insert( std::make_pair( "id", &ATISEncoder::getAtisId ));
109 handlerMap.insert( std::make_pair( "airport-name", &ATISEncoder::getAirportName ));
110 handlerMap.insert( std::make_pair( "time", &ATISEncoder::getTime ));
111 handlerMap.insert( std::make_pair( "approach-type", &ATISEncoder::getApproachType ));
112 handlerMap.insert( std::make_pair( "rwy-land", &ATISEncoder::getLandingRunway ));
113 handlerMap.insert( std::make_pair( "rwy-to", &ATISEncoder::getTakeoffRunway ));
114 handlerMap.insert( std::make_pair( "transition-level", &ATISEncoder::getTransitionLevel ));
115 handlerMap.insert( std::make_pair( "wind-dir", &ATISEncoder::getWindDirection ));
116 handlerMap.insert( std::make_pair( "wind-from", &ATISEncoder::getWindMinDirection ));
117 handlerMap.insert( std::make_pair( "wind-to", &ATISEncoder::getWindMaxDirection ));
118 handlerMap.insert( std::make_pair( "wind-speed-kn", &ATISEncoder::getWindspeedKnots ));
119 handlerMap.insert( std::make_pair( "gusts", &ATISEncoder::getGustsKnots ));
120 handlerMap.insert( std::make_pair( "visibility-metric", &ATISEncoder::getVisibilityMetric ));
121 handlerMap.insert( std::make_pair( "phenomena", &ATISEncoder::getPhenomena ));
122 handlerMap.insert( std::make_pair( "clouds", &ATISEncoder::getClouds ));
123 handlerMap.insert( std::make_pair( "cavok", &ATISEncoder::getCavok ));
124 handlerMap.insert( std::make_pair( "temperature-deg", &ATISEncoder::getTemperatureDeg ));
125 handlerMap.insert( std::make_pair( "dewpoint-deg", &ATISEncoder::getDewpointDeg ));
126 handlerMap.insert( std::make_pair( "qnh", &ATISEncoder::getQnh ));
127 handlerMap.insert( std::make_pair( "inhg", &ATISEncoder::getInhg ));
128 handlerMap.insert( std::make_pair( "trend", &ATISEncoder::getTrend ));
131 ATISEncoder::~ATISEncoder()
135 SGPropertyNode_ptr findAtisTemplate( const std::string & stationId, SGPropertyNode_ptr atisSchemaNode )
137 using simgear::strutils::starts_with;
138 SGPropertyNode_ptr atisTemplate;
140 PropertyList schemaNodes = atisSchemaNode->getChildren("atis-schema");
141 for( PropertyList::iterator asit = schemaNodes.begin(); asit != schemaNodes.end(); ++asit ) {
142 SGPropertyNode_ptr ppp = (*asit)->getNode("station-starts-with", false );
143 atisTemplate = (*asit)->getNode("atis", false );
144 if( false == atisTemplate.valid() ) continue; // no <atis> node - ignore entry
146 PropertyList startsWithNodes = (*asit)->getChildren("station-starts-with");
147 for( PropertyList::iterator swit = startsWithNodes.begin(); swit != startsWithNodes.end(); ++swit ) {
149 if( starts_with( stationId, (*swit)->getStringValue() ) ) {
159 string ATISEncoder::encodeATIS( ATISInformationProvider * atisInformation )
161 using simgear::strutils::lowercase;
163 if( false == atisInformation->isValid() ) return NO_ATIS;
165 airport = FGAirport::getByIdent( atisInformation->airportId() );
166 if( false == airport.valid() ) {
167 SG_LOG( SG_ATC, SG_WARN, "ATISEncoder: unknown airport id " << atisInformation->airportId() );
171 _atis = atisInformation;
173 // lazily load the schema file on the first call
174 if( false == atisSchemaNode.valid() ) {
175 atisSchemaNode = new SGPropertyNode();
178 SGPath path = globals->resolve_maybe_aircraft_path("ATC/atis.xml");
179 readProperties( path.str(), atisSchemaNode );
181 catch (const sg_exception& e)
183 SG_LOG( SG_ATC, SG_ALERT, "ATISEncoder: Failed to load atis schema definition: " << e.getMessage());
188 string stationId = lowercase( airport->ident() );
190 SGPropertyNode_ptr atisTemplate = findAtisTemplate( stationId, atisSchemaNode );;
191 if( false == atisTemplate.valid() ) {
192 SG_LOG(SG_ATC, SG_WARN, "no matching atis template for station " << stationId );
193 return NO_ATIS; // no template for this station!?
196 return processTokens( atisTemplate );
199 string ATISEncoder::processTokens( SGPropertyNode_ptr node )
203 for( int i = 0; i < node->nChildren(); i++ ) {
204 result.append(processToken( node->getChild(i) ));
210 string ATISEncoder::processToken( SGPropertyNode_ptr token )
212 HandlerMap::iterator it = handlerMap.find( token->getName());
213 if( it == handlerMap.end() ) {
214 SG_LOG(SG_ATC, SG_WARN, "ATISEncoder: unknown token: " << token->getName() );
217 handler_t h = it->second;
218 return (this->*h)( token );
221 string ATISEncoder::processTextToken( SGPropertyNode_ptr token )
223 return token->getStringValue();
226 string ATISEncoder::processTokenToken( SGPropertyNode_ptr token )
228 HandlerMap::iterator it = handlerMap.find( token->getStringValue());
229 if( it == handlerMap.end() ) {
230 SG_LOG(SG_ATC, SG_WARN, "ATISEncoder: unknown token: " << token->getStringValue() );
233 handler_t h = it->second;
234 return (this->*h)( token );
236 token->getStringValue();
239 string ATISEncoder::processIfToken( SGPropertyNode_ptr token )
241 SGPropertyNode_ptr n;
243 if( (n = token->getChild("empty", false )).valid() ) {
244 return checkEmptyCondition( n, true) ?
245 processTokens(token->getChild("then",false)) :
246 processTokens(token->getChild("else",false));
249 if( (n = token->getChild("not-empty", false )).valid() ) {
250 return checkEmptyCondition( n, false) ?
251 processTokens(token->getChild("then",false)) :
252 processTokens(token->getChild("else",false));
255 if( (n = token->getChild("equals", false )).valid() ) {
256 return checkEqualsCondition( n, true) ?
257 processTokens(token->getChild("then",false)) :
258 processTokens(token->getChild("else",false));
261 if( (n = token->getChild("not-equals", false )).valid() ) {
262 return checkEqualsCondition( n, false) ?
263 processTokens(token->getChild("then",false)) :
264 processTokens(token->getChild("else",false));
267 SG_LOG(SG_ATC, SG_WARN, "ATISEncoder: no valid token found for <if> element" );
272 bool ATISEncoder::checkEmptyCondition( SGPropertyNode_ptr node, bool isEmpty )
274 SGPropertyNode_ptr n1 = node->getNode( "token", false );
275 if( false == n1.valid() ) {
276 SG_LOG(SG_ATC, SG_WARN, "missing <token> node for (not)-empty" );
279 return processToken( n1 ).empty() == isEmpty;
282 bool ATISEncoder::checkEqualsCondition( SGPropertyNode_ptr node, bool isEqual )
284 SGPropertyNode_ptr n1 = node->getNode( "token", 0, false );
285 SGPropertyNode_ptr n2 = node->getNode( "token", 1, false );
286 if( false == n1.valid() || false == n2.valid()) {
287 SG_LOG(SG_ATC, SG_WARN, "missing <token> node for (not)-equals" );
291 bool comp = processToken( n1 ).compare( processToken( n2 ) ) == 0;
292 return comp == isEqual;
295 string ATISEncoder::getAtisId( SGPropertyNode_ptr )
297 FGAirportDynamics * dynamics = airport->getDynamics();
298 if( NULL != dynamics ) {
299 dynamics->updateAtisSequence( 30*60, false );
300 return dynamics->getAtisSequence();
305 string ATISEncoder::getAirportName( SGPropertyNode_ptr )
307 return airport->getName();
310 string ATISEncoder::getTime( SGPropertyNode_ptr )
312 return getSpokenNumber( _atis->getTime() % (100*100), true, 4 );
315 static inline FGRunwayRef findBestRunwayForWind( FGAirportRef airport, int windDeg, int windKt )
317 struct FGAirport::FindBestRunwayForHeadingParams p;
318 //TODO: ramp down the heading weight with wind speed
320 return airport->findBestRunwayForHeading( windDeg, &p );
323 string ATISEncoder::getApproachType( SGPropertyNode_ptr )
325 FGRunwayRef runway = findBestRunwayForWind( airport, _atis->getWindDeg(), _atis->getWindSpeedKt() );
326 if( runway.valid() ) {
327 if( NULL != runway->ILS() ) return globals->get_locale()->getLocalizedString("ils", "atc", "ils" );
328 //TODO: any chance to find other approach types? localizer-dme, vor-dme, vor, ndb?
331 return globals->get_locale()->getLocalizedString("visual", "atc", "visual" );
334 string ATISEncoder::getLandingRunway( SGPropertyNode_ptr )
336 FGRunwayRef runway = findBestRunwayForWind( airport, _atis->getWindDeg(), _atis->getWindSpeedKt() );
337 if( runway.valid() ) {
338 string runwayIdent = runway->ident();
339 if(runwayIdent != "NN") {
340 return getSpokenNumber(runwayIdent);
346 string ATISEncoder::getTakeoffRunway( SGPropertyNode_ptr p )
348 //TODO: if the airport has more than one runway, probably pick another one?
349 return getLandingRunway( p );
352 string ATISEncoder::getTransitionLevel( SGPropertyNode_ptr )
354 double hPa = _atis->getQnh();
356 /* Transition level is the flight level above which aircraft must use standard pressure and below
357 * which airport pressure settings must be used.
358 * Following definitions are taken from German ATIS:
359 * QNH <= 977 hPa: TRL 80
360 * QNH <= 1013 hPa: TRL 70
361 * QNH > 1013 hPa: TRL 60
362 * (maybe differs slightly for other countries...)
367 } else if( hPa > 978 && hPa <= 1013 ) {
369 } else if( hPa > 1013 && hPa <= 1046 ) {
375 // add an offset to the transition level for high altitude airports (just guessing here,
377 int e = int(airport->getElevation() / 1000.0);
379 // TL steps in 10(00)ft
383 return getSpokenNumber(tl);
386 string ATISEncoder::getWindDirection( SGPropertyNode_ptr )
388 return getSpokenNumber( _atis->getWindDeg(), true, 3 );
391 string ATISEncoder::getWindMinDirection( SGPropertyNode_ptr )
393 return getSpokenNumber( _atis->getWindMinDeg(), true, 3 );
396 string ATISEncoder::getWindMaxDirection( SGPropertyNode_ptr )
398 return getSpokenNumber( _atis->getWindMaxDeg(), true, 3 );
401 string ATISEncoder::getWindspeedKnots( SGPropertyNode_ptr )
403 return getSpokenNumber( _atis->getWindSpeedKt() );
406 string ATISEncoder::getGustsKnots( SGPropertyNode_ptr )
408 int g = _atis->getGustsKt();
409 return g > 0 ? getSpokenNumber( g ) : EMPTY;
412 string ATISEncoder::getCavok( SGPropertyNode_ptr )
414 string CAVOK = globals->get_locale()->getLocalizedString("cavok", "atc", "cavok" );
416 return _atis->isCavok() ? CAVOK : EMPTY;
419 string ATISEncoder::getVisibilityMetric( SGPropertyNode_ptr )
421 string m = globals->get_locale()->getLocalizedString("meters", "atc", "meters" );
422 string km = globals->get_locale()->getLocalizedString("kilometers", "atc", "kilometers" );
423 string or_more = globals->get_locale()->getLocalizedString("ormore", "atc", "or more" );
425 int v = _atis->getVisibilityMeters();
427 if( v < 5000 ) return reply.append( getSpokenAltitude( v ) ).SPACE.append( m );
428 if( v >= 9999 ) return reply.append( getSpokenNumber(10) ).SPACE.append( km ).SPACE.append(or_more);
429 return reply.append( getSpokenNumber( v/1000 ).SPACE.append( km ) );
432 string ATISEncoder::getPhenomena( SGPropertyNode_ptr )
434 return _atis->getPhenomena();
437 string ATISEncoder::getClouds( SGPropertyNode_ptr )
439 string FEET = globals->get_locale()->getLocalizedString("feet", "atc", "feet" );
442 ATISInformationProvider::CloudEntries cloudEntries = _atis->getClouds();
444 for( ATISInformationProvider::CloudEntries::iterator it = cloudEntries.begin(); it != cloudEntries.end(); it++ ) {
445 if( false == reply.empty() ) reply.SPACE;
446 reply.append( it->second ).SPACE.append( getSpokenAltitude(it->first).SPACE.append( FEET ) );
451 string ATISEncoder::getTemperatureDeg( SGPropertyNode_ptr )
453 return getSpokenNumber( _atis->getTemperatureDeg() );
456 string ATISEncoder::getDewpointDeg( SGPropertyNode_ptr )
458 return getSpokenNumber( _atis->getDewpointDeg() );
461 string ATISEncoder::getQnh( SGPropertyNode_ptr )
463 return getSpokenNumber( _atis->getQnh() );
466 string ATISEncoder::getInhg( SGPropertyNode_ptr )
468 string DECIMAL = globals->get_locale()->getLocalizedString("dp", "atc", "decimal" );
470 int fractpart = 1000 * ::modf( _atis->getQnh() * 100.0 / SG_INHG_TO_PA, &intpart );
475 reply.append( getSpokenNumber( (int)intpart ) )
476 .SPACE.append( DECIMAL ).SPACE
477 .append( getSpokenNumber( fractpart ) );
481 string ATISEncoder::getTrend( SGPropertyNode_ptr )
483 return _atis->getTrend();