Howto:Fetch live aloft data

From FlightGear wiki
Jump to navigation Jump to search

This HOWTO describes how to setup live setting of wind aloft. This is the wind speed, direction and temperature for different altitudes. FlightGear's feature of setting current weather by using the --enable-real-weather-fetch uses METAR which only reports ground weather.

The aloft feature currently only works if you have a source distribution of FlightGear or the CVS version since you have to apply a tiny patch to the sourcecode (just a single line). It only works if you have bash, awk, wget and netcat on your machine. This is usually the case if you are running linux and should be no sweat for OS/X but might require some setup if you have some less feature rich operating system.

General description

The wind aloft data is brought to you by the nice people of Jeppesen(R). Under http://www.jeppesen.com/weather a Text Weather service is provided free of charge. We use this service to fetch the aloft forecast and use these data as input for FlightGear.

When enabling the --real-weather-fetch option, FlightGear searches for the nearest airport to your present position that has a METAR service. If it finds one, it propagates the environmental settings and the station id of the airport the METAR was fetched for. A tiny add-on sends this station-id to a listening daemon program called aloftd.

The aloftd waits for input from FlightGear and interprets it as station ids. If the station id changes, it retrieves the aloft forecast from Jeppesen(R), parses the data, connects to the FlightGear telnet server and sets the aloft data in the property tree.

What to do

The Protocol Extension

First, we need to tell FlightGear how to broadcast the station ID of the actual METAR-station. Copy and paste the following code into a file named metarstationid.xml and save it to your data/Protocol directory.

<?xml version="1.0"?>
<PropertyList>
 <comment>
  <![CDATA[
    Usage: 
    fgfs --generic=socket,out,1,localhost,5500,udp,metarstationid
  ]]>
 </comment>
 <generic>
  <output>
   <line_separator>newline</line_separator>
   <var_separator>,</var_separator>
   <chunk>
    <name>metar-station-id</name>
    <format>%s</format>
    <type>string</type>
    <node>/environment/metar/station-id</node>
   </chunk>
  </output>
 </generic>
</PropertyList>

The FlightGear Patch

Unfortunately we have to patch FlightGear. The current implementation of FGMetarEnvironmentCtrl sets up the aloft data by just guessing some values and it does so after every reception of a new METAR. As a bad hack, just comment the line

fgSetupWind(dir_from, dir_to, speed, gust);

so it reads

// fgSetupWind(dir_from, dir_to, speed, gust);

in the file src/Environment/environment_ctrl.cxx. It should be around line 620. Perform a make for FlightGear.

EDIT: This is no longer needed if you use FlightGear CVS. Just set the property

/environment/params/metar-updates-winds-aloft

to false. This can be done from the command line like

--prop://environment/params/metar-updates-winds-aloft=0

The aloftd

Copy and paste the following code to a file named aloftd:

#!/bin/bash
#KSFO FD DATA BASED ON 190600Z.
#       3000    6000    9000   12000   15000   18000   21000   24000
#18Z  1516P07 1723P03 1732M01 1738M06 1941M11 2045M16 2148M23 2151M30
#00Z  1814P09 1819P02 1826M03 1932M08 2039M12 2246M16 2254M23 2161M30
#06Z  1713P08 1915P02 2118M03 2223M09 2229M13 2236M18 2245M25 2253M32
#12Z  1609P07 2011P03 2313M03 2420M08 2430M13 2441M19 2450M26 2458M33

#set
#/environment/config/aloft/entry[i]/elevation-ft
#/environment/config/aloft/entry[i]/wind-from-heading-deg
#/environment/config/aloft/entry[i]/wind-speed-kt
#/environment/config/aloft/entry[i]/temperature-degc
#
#guess
#/environment/config/aloft/entry[i]/dewpoint-degc

LISTEN_PORT=5123
TELNET_PORT=2323

METAR_STATION_ID="XXXX"

# FL060-390
#WX_TYPE="fd"

# FL030-240
WX_TYPE="fdl"

# FL120-500
#WX_TYPE="fdh"

URL="http://www.jetplan.com/jeppesen/weatherServlet?query=999&notamFilterName=&"

netcat -u -l -p ${LISTEN_PORT} | \
while read line; do
 if [ "$line" == "$METAR_STATION_ID" ]; then
   continue
 fi
 METAR_STATION_ID="$line"

 if [ "$METAR_STATION_ID" == "XXXX" ]; then
   continue
 fi

 echo "new ID: $METAR_STATION_ID" >&2
 echo "${URL}wxType=${WX_TYPE}&wxStation=${METAR_STATION_ID}" >&2
 wget --quiet --output-document=- "${URL}wxType=${WX_TYPE}&wxStation=${METAR_STATION_ID}" | awk '
   BEGIN {
     state=0;
     count=0;
     delete altitudes;
     printf( "data\r\n" );
   }
   /FD DATA BASED ON/ {
     state = 1;
     count = 0;
   }

   /^[0-9][0-9]Z/ {
     if( state == 1 )
       state = 2;
   }

   {
     if( state == 1 ) {
       if( count == 1 ) {
         for( i = 1; i <= NF; i++ ) {
           altitudes[i] = $i;
         }
       }
     }
     if( state == 2 ) {
       for( i = 2; i <= NF; i++ ) {
         altitude = altitudes[i-1];
         token = $i;
         wind_dir = substr( token, 1, 2 ) * 10;
         wind_speed = substr( token, 3, 2 );
         sign = substr( token, 5, 1 );
         temp = substr( token, 6, 2 );
         if( sign == "M" )
           temp = temp * -1;
         printf( "set /environment/config/aloft/entry[%d]/elevation-ft %d\r\n", i-2, altitude );
         printf( "set /environment/config/aloft/entry[%d]/wind-from-heading-deg %d\r\n", i-2, wind_dir );
         printf( "set /environment/config/aloft/entry[%d]/wind-speed-kt %d\r\n", i-2, wind_speed );
         printf( "set /environment/config/aloft/entry[%d]/temperature-degc %d\r\n", i-2, temp );
       }
       state = 3;
     }
     count++;
   }
   END {
    printf( "quit\r\n" );
   }
 ' | netcat localhost ${TELNET_PORT}
done

Save the file to any location and make the file executable.

Running FlightGear

Now start your FlightGear with the following additional options:

--generic=socket,out,0.5,localhost,5123,udp,metarstationid --telnet=2323

Open another console window and start the created script aloftd. You should see some output telling you which airport it is fetching data for.