2002-05-07 00:03:54 +00:00
|
|
|
|
// environment_ctrl.cxx -- manager for natural environment information.
|
|
|
|
|
//
|
|
|
|
|
// Written by David Megginson, started February 2002.
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2002 David Megginson - david@megginson.com
|
|
|
|
|
//
|
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
// General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
//
|
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
|
#include <simgear/debug/logstream.hxx>
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2003-06-10 12:03:07 +00:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2004-02-22 14:21:37 +00:00
|
|
|
|
#include <simgear/structure/commands.hxx>
|
2004-02-21 15:00:49 +00:00
|
|
|
|
#include <simgear/structure/exception.hxx>
|
|
|
|
|
|
2004-02-22 14:21:37 +00:00
|
|
|
|
#include <Airports/simple.hxx>
|
2002-05-07 00:03:54 +00:00
|
|
|
|
#include <Main/fg_props.hxx>
|
2004-02-22 02:06:05 +00:00
|
|
|
|
#include <Main/util.hxx>
|
2002-05-07 00:03:54 +00:00
|
|
|
|
|
2004-02-21 12:56:16 +00:00
|
|
|
|
#include "environment_mgr.hxx"
|
2002-05-07 00:03:54 +00:00
|
|
|
|
#include "environment_ctrl.hxx"
|
|
|
|
|
|
2003-06-09 08:44:59 +00:00
|
|
|
|
SG_USING_STD(sort);
|
|
|
|
|
|
2002-05-07 00:03:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Implementation of FGEnvironmentCtrl abstract base class.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
FGEnvironmentCtrl::FGEnvironmentCtrl ()
|
|
|
|
|
: _environment(0),
|
|
|
|
|
_lon_deg(0),
|
|
|
|
|
_lat_deg(0),
|
|
|
|
|
_elev_ft(0)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FGEnvironmentCtrl::~FGEnvironmentCtrl ()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGEnvironmentCtrl::setEnvironment (FGEnvironment * environment)
|
|
|
|
|
{
|
|
|
|
|
_environment = environment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGEnvironmentCtrl::setLongitudeDeg (double lon_deg)
|
|
|
|
|
{
|
|
|
|
|
_lon_deg = lon_deg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGEnvironmentCtrl::setLatitudeDeg (double lat_deg)
|
|
|
|
|
{
|
|
|
|
|
_lat_deg = lat_deg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGEnvironmentCtrl::setElevationFt (double elev_ft)
|
|
|
|
|
{
|
|
|
|
|
_elev_ft = elev_ft;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGEnvironmentCtrl::setPosition (double lon_deg, double lat_deg, double elev_ft)
|
|
|
|
|
{
|
|
|
|
|
_lon_deg = lon_deg;
|
|
|
|
|
_lat_deg = lat_deg;
|
|
|
|
|
_elev_ft = elev_ft;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Implementation of FGUserDefEnvironmentCtrl.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
FGUserDefEnvironmentCtrl::FGUserDefEnvironmentCtrl ()
|
|
|
|
|
: _base_wind_speed_node(0),
|
|
|
|
|
_gust_wind_speed_node(0),
|
|
|
|
|
_current_wind_speed_kt(0),
|
|
|
|
|
_delta_wind_speed_kt(0)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FGUserDefEnvironmentCtrl::~FGUserDefEnvironmentCtrl ()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGUserDefEnvironmentCtrl::init ()
|
|
|
|
|
{
|
|
|
|
|
// Fill in some defaults.
|
|
|
|
|
if (!fgHasNode("/environment/params/base-wind-speed-kt"))
|
|
|
|
|
fgSetDouble("/environment/params/base-wind-speed-kt",
|
|
|
|
|
fgGetDouble("/environment/wind-speed-kt"));
|
|
|
|
|
if (!fgHasNode("/environment/params/gust-wind-speed-kt"))
|
|
|
|
|
fgSetDouble("/environment/params/gust-wind-speed-kt",
|
|
|
|
|
fgGetDouble("/environment/params/base-wind-speed-kt"));
|
|
|
|
|
|
|
|
|
|
_base_wind_speed_node =
|
|
|
|
|
fgGetNode("/environment/params/base-wind-speed-kt", true);
|
|
|
|
|
_gust_wind_speed_node =
|
|
|
|
|
fgGetNode("/environment/params/gust-wind-speed-kt", true);
|
|
|
|
|
|
|
|
|
|
_current_wind_speed_kt = _base_wind_speed_node->getDoubleValue();
|
|
|
|
|
_delta_wind_speed_kt = 0.1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2002-05-11 16:28:50 +00:00
|
|
|
|
FGUserDefEnvironmentCtrl::update (double dt)
|
2002-05-07 00:03:54 +00:00
|
|
|
|
{
|
|
|
|
|
double base_wind_speed = _base_wind_speed_node->getDoubleValue();
|
|
|
|
|
double gust_wind_speed = _gust_wind_speed_node->getDoubleValue();
|
|
|
|
|
|
2003-01-20 13:02:18 +00:00
|
|
|
|
if (gust_wind_speed < base_wind_speed) {
|
|
|
|
|
gust_wind_speed = base_wind_speed;
|
|
|
|
|
_gust_wind_speed_node->setDoubleValue(gust_wind_speed);
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-07 00:03:54 +00:00
|
|
|
|
if (base_wind_speed == gust_wind_speed) {
|
|
|
|
|
_current_wind_speed_kt = base_wind_speed;
|
|
|
|
|
} else {
|
|
|
|
|
int rn = rand() % 128;
|
|
|
|
|
int sign = (_delta_wind_speed_kt < 0 ? -1 : 1);
|
|
|
|
|
double gust = _current_wind_speed_kt - base_wind_speed;
|
|
|
|
|
double incr = gust / 50;
|
|
|
|
|
|
|
|
|
|
if (rn == 0)
|
|
|
|
|
_delta_wind_speed_kt = - _delta_wind_speed_kt;
|
|
|
|
|
else if (rn < 4)
|
|
|
|
|
_delta_wind_speed_kt -= incr * sign;
|
|
|
|
|
else if (rn < 16)
|
|
|
|
|
_delta_wind_speed_kt += incr * sign;
|
|
|
|
|
|
|
|
|
|
_current_wind_speed_kt += _delta_wind_speed_kt;
|
|
|
|
|
|
|
|
|
|
if (_current_wind_speed_kt < base_wind_speed) {
|
|
|
|
|
_current_wind_speed_kt = base_wind_speed;
|
|
|
|
|
_delta_wind_speed_kt = 0.01;
|
|
|
|
|
} else if (_current_wind_speed_kt > gust_wind_speed) {
|
|
|
|
|
_current_wind_speed_kt = gust_wind_speed;
|
|
|
|
|
_delta_wind_speed_kt = -0.01;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_environment != 0)
|
|
|
|
|
_environment->set_wind_speed_kt(_current_wind_speed_kt);
|
|
|
|
|
}
|
|
|
|
|
|
2003-06-08 14:47:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Implementation of FGInterpolateEnvironmentCtrl.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
FGInterpolateEnvironmentCtrl::FGInterpolateEnvironmentCtrl ()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FGInterpolateEnvironmentCtrl::~FGInterpolateEnvironmentCtrl ()
|
|
|
|
|
{
|
2003-10-16 12:53:10 +00:00
|
|
|
|
unsigned int i;
|
2003-06-09 08:44:59 +00:00
|
|
|
|
for (i = 0; i < _boundary_table.size(); i++)
|
2003-06-08 14:47:03 +00:00
|
|
|
|
delete _boundary_table[i];
|
2003-06-09 08:44:59 +00:00
|
|
|
|
for (i = 0; i < _aloft_table.size(); i++)
|
2003-06-08 14:47:03 +00:00
|
|
|
|
delete _aloft_table[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGInterpolateEnvironmentCtrl::init ()
|
|
|
|
|
{
|
|
|
|
|
read_table(fgGetNode("/environment/config/boundary", true),
|
|
|
|
|
_boundary_table);
|
|
|
|
|
read_table(fgGetNode("/environment/config/aloft", true),
|
|
|
|
|
_aloft_table);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGInterpolateEnvironmentCtrl::reinit ()
|
|
|
|
|
{
|
2003-10-16 12:53:10 +00:00
|
|
|
|
unsigned int i;
|
2003-06-09 08:44:59 +00:00
|
|
|
|
for (i = 0; i < _boundary_table.size(); i++)
|
2003-06-08 14:47:03 +00:00
|
|
|
|
delete _boundary_table[i];
|
2003-06-09 08:44:59 +00:00
|
|
|
|
for (i = 0; i < _aloft_table.size(); i++)
|
2003-06-08 14:47:03 +00:00
|
|
|
|
delete _aloft_table[i];
|
|
|
|
|
_boundary_table.clear();
|
|
|
|
|
_aloft_table.clear();
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGInterpolateEnvironmentCtrl::read_table (const SGPropertyNode * node,
|
|
|
|
|
vector<bucket *> &table)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < node->nChildren(); i++) {
|
|
|
|
|
const SGPropertyNode * child = node->getChild(i);
|
2003-07-31 01:43:57 +00:00
|
|
|
|
if ( strcmp(child->getName(), "entry") == 0
|
|
|
|
|
&& child->getStringValue("elevation-ft", "")[0] != '\0'
|
|
|
|
|
&& ( child->getDoubleValue("elevation-ft") > 0.1 || i == 0 ) )
|
|
|
|
|
{
|
2003-06-08 14:47:03 +00:00
|
|
|
|
bucket * b = new bucket;
|
|
|
|
|
if (i > 0)
|
|
|
|
|
b->environment.copy(table[i-1]->environment);
|
|
|
|
|
b->environment.read(child);
|
|
|
|
|
b->altitude_ft = b->environment.get_elevation_ft();
|
|
|
|
|
table.push_back(b);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sort(table.begin(), table.end());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGInterpolateEnvironmentCtrl::update (double delta_time_sec)
|
|
|
|
|
{
|
|
|
|
|
// FIXME
|
|
|
|
|
double altitude_ft = fgGetDouble("/position/altitude-ft");
|
|
|
|
|
double altitude_agl_ft = fgGetDouble("/position/altitude-agl-ft");
|
|
|
|
|
double boundary_transition =
|
|
|
|
|
fgGetDouble("/environment/config/boundary-transition-ft", 500);
|
|
|
|
|
|
2003-10-16 12:53:10 +00:00
|
|
|
|
// double ground_elevation_ft = altitude_ft - altitude_agl_ft;
|
2003-06-08 14:47:03 +00:00
|
|
|
|
|
|
|
|
|
int length = _boundary_table.size();
|
|
|
|
|
|
|
|
|
|
if (length > 0) {
|
|
|
|
|
// boundary table
|
|
|
|
|
double boundary_limit = _boundary_table[length-1]->altitude_ft;
|
|
|
|
|
if (boundary_limit >= altitude_agl_ft) {
|
|
|
|
|
do_interpolate(_boundary_table, altitude_agl_ft,
|
|
|
|
|
_environment);
|
|
|
|
|
return;
|
|
|
|
|
} else if ((boundary_limit + boundary_transition) >= altitude_agl_ft) {
|
|
|
|
|
// both tables
|
|
|
|
|
do_interpolate(_boundary_table, altitude_agl_ft, &env1);
|
|
|
|
|
do_interpolate(_aloft_table, altitude_ft, &env2);
|
|
|
|
|
double fraction =
|
|
|
|
|
(altitude_agl_ft - boundary_limit) / boundary_transition;
|
|
|
|
|
interpolate(&env1, &env2, fraction, _environment);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// aloft table
|
|
|
|
|
do_interpolate(_aloft_table, altitude_ft, _environment);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGInterpolateEnvironmentCtrl::do_interpolate (vector<bucket *> &table,
|
|
|
|
|
double altitude_ft,
|
|
|
|
|
FGEnvironment * environment)
|
|
|
|
|
{
|
|
|
|
|
int length = table.size();
|
|
|
|
|
if (length == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Boundary conditions
|
|
|
|
|
if ((length == 1) || (table[0]->altitude_ft >= altitude_ft)) {
|
|
|
|
|
environment->copy(table[0]->environment);
|
|
|
|
|
return;
|
|
|
|
|
} else if (table[length-1]->altitude_ft <= altitude_ft) {
|
|
|
|
|
environment->copy(table[length-1]->environment);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Search the interpolation table
|
|
|
|
|
for (int i = 0; i < length - 1; i++) {
|
|
|
|
|
if ((i == length - 1) || (table[i]->altitude_ft <= altitude_ft)) {
|
|
|
|
|
FGEnvironment * env1 = &(table[i]->environment);
|
|
|
|
|
FGEnvironment * env2 = &(table[i+1]->environment);
|
|
|
|
|
double fraction;
|
|
|
|
|
if (table[i]->altitude_ft == table[i+1]->altitude_ft)
|
|
|
|
|
fraction = 1.0;
|
|
|
|
|
else
|
|
|
|
|
fraction =
|
|
|
|
|
((altitude_ft - table[i]->altitude_ft) /
|
|
|
|
|
(table[i+1]->altitude_ft - table[i]->altitude_ft));
|
|
|
|
|
interpolate(env1, env2, fraction, environment);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
FGInterpolateEnvironmentCtrl::bucket::operator< (const bucket &b) const
|
|
|
|
|
{
|
|
|
|
|
return (altitude_ft < b.altitude_ft);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Implementation of FGMetarEnvironmentCtrl.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
FGMetarEnvironmentCtrl::FGMetarEnvironmentCtrl ()
|
2004-02-21 14:04:40 +00:00
|
|
|
|
: env( new FGInterpolateEnvironmentCtrl ),
|
2004-02-23 01:39:12 +00:00
|
|
|
|
_icao( fgGetString("/sim/presets/airport-id") ),
|
2004-02-26 10:23:48 +00:00
|
|
|
|
proxy_host( fgGetNode("/sim/presets/proxy/host", true) ),
|
|
|
|
|
proxy_port( fgGetNode("/sim/presets/proxy/port", true) ),
|
|
|
|
|
proxy_auth( fgGetNode("/sim/presets/proxy/authentication", true) ),
|
2004-02-23 01:39:12 +00:00
|
|
|
|
update_interval_sec( 60.0 ),
|
|
|
|
|
elapsed( 60.0 )
|
2004-02-21 12:56:16 +00:00
|
|
|
|
{
|
2004-02-25 15:31:01 +00:00
|
|
|
|
#ifdef ENABLE_THREADS
|
|
|
|
|
thread = new MetarThread(this);
|
|
|
|
|
thread->start();
|
|
|
|
|
#endif // ENABLE_THREADS
|
2004-02-21 12:56:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FGMetarEnvironmentCtrl::~FGMetarEnvironmentCtrl ()
|
|
|
|
|
{
|
2004-02-25 15:31:01 +00:00
|
|
|
|
#ifdef ENABLE_THREADS
|
|
|
|
|
thread->cancel();
|
|
|
|
|
thread->join();
|
|
|
|
|
#endif // ENABLE_THREADS
|
|
|
|
|
|
2004-02-21 14:04:40 +00:00
|
|
|
|
delete env;
|
|
|
|
|
env = NULL;
|
2004-02-21 12:56:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-22 14:21:37 +00:00
|
|
|
|
// use a "command" to set station temp at station elevation
|
|
|
|
|
static void set_temp_at_altitude( float temp_degc, float altitude_ft ) {
|
|
|
|
|
SGPropertyNode args;
|
|
|
|
|
SGPropertyNode *node = args.getNode("temp-degc", 0, true);
|
|
|
|
|
node->setFloatValue( temp_degc );
|
|
|
|
|
node = args.getNode("altitude-ft", 0, true);
|
|
|
|
|
node->setFloatValue( altitude_ft );
|
|
|
|
|
globals->get_commands()->execute("set-outside-air-temp-degc", &args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void set_dewpoint_at_altitude( float dewpoint_degc, float altitude_ft ) {
|
|
|
|
|
SGPropertyNode args;
|
|
|
|
|
SGPropertyNode *node = args.getNode("dewpoint-degc", 0, true);
|
|
|
|
|
node->setFloatValue( dewpoint_degc );
|
|
|
|
|
node = args.getNode("altitude-ft", 0, true);
|
|
|
|
|
node->setFloatValue( altitude_ft );
|
|
|
|
|
globals->get_commands()->execute("set-dewpoint-temp-degc", &args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-21 12:56:16 +00:00
|
|
|
|
void
|
2004-02-23 01:39:12 +00:00
|
|
|
|
FGMetarEnvironmentCtrl::update_env_config ()
|
2004-02-21 12:56:16 +00:00
|
|
|
|
{
|
2004-02-21 14:04:40 +00:00
|
|
|
|
fgSetupWind( fgGetDouble("/environment/metar/base-wind-range-from"),
|
|
|
|
|
fgGetDouble("/environment/metar/base-wind-range-to"),
|
|
|
|
|
fgGetDouble("/environment/metar/base-wind-speed-kt"),
|
|
|
|
|
fgGetDouble("/environment/metar/gust-wind-speed-kt") );
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
2004-02-22 02:06:05 +00:00
|
|
|
|
fgDefaultWeatherValue( "visibility-m",
|
|
|
|
|
fgGetDouble("/environment/metar/min-visibility-m") );
|
2004-02-22 14:21:37 +00:00
|
|
|
|
set_temp_at_altitude( fgGetDouble("/environment/metar/temperature-degc"),
|
|
|
|
|
station_elevation_ft );
|
|
|
|
|
set_dewpoint_at_altitude( fgGetDouble("/environment/metar/dewpoint-degc"),
|
|
|
|
|
station_elevation_ft );
|
2004-02-22 02:06:05 +00:00
|
|
|
|
fgDefaultWeatherValue( "pressure-sea-level-inhg",
|
|
|
|
|
fgGetDouble("/environment/metar/pressure-inhg") );
|
2004-02-23 01:39:12 +00:00
|
|
|
|
}
|
2004-02-21 14:04:40 +00:00
|
|
|
|
|
2004-02-23 01:39:12 +00:00
|
|
|
|
void
|
|
|
|
|
FGMetarEnvironmentCtrl::init ()
|
|
|
|
|
{
|
|
|
|
|
const SGPropertyNode *longitude
|
|
|
|
|
= fgGetNode( "/position/longitude-deg", true );
|
|
|
|
|
const SGPropertyNode *latitude
|
|
|
|
|
= fgGetNode( "/position/latitude-deg", true );
|
|
|
|
|
|
|
|
|
|
bool found_metar = false;
|
|
|
|
|
while ( !found_metar ) {
|
|
|
|
|
FGAirport a = globals->get_airports()
|
|
|
|
|
->search( longitude->getDoubleValue(),
|
|
|
|
|
latitude->getDoubleValue(),
|
|
|
|
|
true );
|
|
|
|
|
if ( fetch_data( a.id ) ) {
|
|
|
|
|
cout << "closest station w/ metar = " << a.id << endl;
|
|
|
|
|
_icao = a.id;
|
|
|
|
|
elapsed = 0.0;
|
|
|
|
|
update_env_config();
|
|
|
|
|
env->init();
|
|
|
|
|
found_metar = true;
|
|
|
|
|
} else {
|
|
|
|
|
// mark as no metar so it doesn't show up in subsequent
|
|
|
|
|
// searches.
|
|
|
|
|
cout << "no metar at metar = " << a.id << endl;
|
|
|
|
|
globals->get_airports()->no_metar( a.id );
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-02-21 12:56:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGMetarEnvironmentCtrl::reinit ()
|
|
|
|
|
{
|
2004-02-21 15:20:41 +00:00
|
|
|
|
#if 0
|
2004-02-23 01:39:12 +00:00
|
|
|
|
update_env_config();
|
2004-02-21 15:20:41 +00:00
|
|
|
|
#endif
|
2004-02-21 14:04:40 +00:00
|
|
|
|
|
|
|
|
|
env->reinit();
|
2004-02-21 12:56:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2004-02-21 14:04:40 +00:00
|
|
|
|
FGMetarEnvironmentCtrl::update(double delta_time_sec)
|
|
|
|
|
{
|
2004-02-25 15:31:01 +00:00
|
|
|
|
static const SGPropertyNode *longitude
|
2004-02-23 01:39:12 +00:00
|
|
|
|
= fgGetNode( "/position/longitude-deg", true );
|
2004-02-25 15:31:01 +00:00
|
|
|
|
static const SGPropertyNode *latitude
|
2004-02-23 01:39:12 +00:00
|
|
|
|
= fgGetNode( "/position/latitude-deg", true );
|
|
|
|
|
elapsed += delta_time_sec;
|
|
|
|
|
if ( elapsed > update_interval_sec ) {
|
|
|
|
|
FGAirport a = globals->get_airports()
|
|
|
|
|
->search( longitude->getDoubleValue(),
|
|
|
|
|
latitude->getDoubleValue(),
|
|
|
|
|
true );
|
|
|
|
|
if ( fetch_data( a.id ) ) {
|
|
|
|
|
cout << "closest station w/ metar = " << a.id << endl;
|
|
|
|
|
_icao = a.id;
|
|
|
|
|
elapsed = 0.0;
|
|
|
|
|
update_env_config();
|
2004-02-23 02:22:49 +00:00
|
|
|
|
env->reinit();
|
2004-02-23 01:39:12 +00:00
|
|
|
|
} else {
|
|
|
|
|
// mark as no metar so it doesn't show up in subsequent
|
|
|
|
|
// searches.
|
|
|
|
|
cout << "no metar at metar = " << a.id << endl;
|
|
|
|
|
globals->get_airports()->no_metar( a.id );
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-02-25 15:31:01 +00:00
|
|
|
|
|
2004-02-21 14:04:40 +00:00
|
|
|
|
env->update(delta_time_sec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGMetarEnvironmentCtrl::setEnvironment (FGEnvironment * environment)
|
|
|
|
|
{
|
|
|
|
|
env->setEnvironment(environment);
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-23 01:39:12 +00:00
|
|
|
|
bool
|
|
|
|
|
FGMetarEnvironmentCtrl::fetch_data (const string &icao)
|
2004-02-21 12:56:16 +00:00
|
|
|
|
{
|
|
|
|
|
char s[128];
|
|
|
|
|
double d, dt;
|
|
|
|
|
int i;
|
|
|
|
|
|
2004-02-23 01:39:12 +00:00
|
|
|
|
if ((icao == "") && (_icao == "")) {
|
|
|
|
|
_icao = fgGetString("/sim/presets/airport-id");
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
2004-02-23 01:39:12 +00:00
|
|
|
|
} else if (icao != "") {
|
|
|
|
|
_icao = icao;
|
2004-02-21 12:56:16 +00:00
|
|
|
|
}
|
2004-02-21 15:00:49 +00:00
|
|
|
|
|
2004-02-22 14:21:37 +00:00
|
|
|
|
// fetch station elevation if exists
|
|
|
|
|
FGAirport a = globals->get_airports()->search( _icao );
|
|
|
|
|
station_elevation_ft = a.elevation;
|
|
|
|
|
|
|
|
|
|
// fetch current metar data
|
2004-02-25 15:31:01 +00:00
|
|
|
|
SGMetar *m = NULL;
|
|
|
|
|
#ifdef ENABLE_THREADS
|
|
|
|
|
|
2004-02-26 10:23:48 +00:00
|
|
|
|
// We are only interested in the latest metar report
|
|
|
|
|
// FIXME: Do we want to keep the latest valid report instead?
|
|
|
|
|
while (!metar_queue.empty()) {
|
|
|
|
|
|
2004-02-25 15:31:01 +00:00
|
|
|
|
if (m != NULL)
|
2004-02-26 10:23:48 +00:00
|
|
|
|
delete m;
|
|
|
|
|
|
|
|
|
|
m = metar_queue.pop();
|
2004-02-25 15:31:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-02-26 10:23:48 +00:00
|
|
|
|
if ( m != NULL ) {
|
2004-02-25 15:31:01 +00:00
|
|
|
|
|
|
|
|
|
#else
|
2004-02-21 15:00:49 +00:00
|
|
|
|
try {
|
2004-02-26 10:23:48 +00:00
|
|
|
|
string host = proxy_host->getStringValue();
|
|
|
|
|
string auth = proxy_auth->getStringValue();
|
|
|
|
|
string port = proxy_port->getStringValue();
|
|
|
|
|
m = new SGMetar( _icao, host, port, auth);
|
|
|
|
|
|
2004-02-25 15:31:01 +00:00
|
|
|
|
#endif // ENABLE_THREADS
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
2004-02-26 10:23:48 +00:00
|
|
|
|
d = m->getMinVisibility().getVisibility_m();
|
|
|
|
|
d = (d != SGMetarNaN) ? d : 10000;
|
|
|
|
|
fgSetDouble("/environment/metar/min-visibility-m", d);
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
2004-02-26 10:23:48 +00:00
|
|
|
|
dt = m->getMaxVisibility().getVisibility_m();
|
|
|
|
|
d = (dt != SGMetarNaN) ? dt : d;
|
|
|
|
|
fgSetDouble("/environment/metar/max-visibility-m", d);
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
2004-02-26 10:23:48 +00:00
|
|
|
|
SGMetarVisibility *dirvis = m->getDirVisibility();
|
|
|
|
|
for (i = 0; i < 8; i++, dirvis++) {
|
|
|
|
|
const char *min = "/environment/metar/visibility[%d]/min-m";
|
|
|
|
|
const char *max = "/environment/metar/visibility[%d]/max-m";
|
|
|
|
|
char s[128];
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
2004-02-26 10:23:48 +00:00
|
|
|
|
d = dirvis->getVisibility_m();
|
|
|
|
|
d = (d != SGMetarNaN) ? d : 10000;
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
2004-02-26 10:23:48 +00:00
|
|
|
|
snprintf(s, 128, min, i);
|
|
|
|
|
fgSetDouble(s, d);
|
|
|
|
|
snprintf(s, 128, max, i);
|
|
|
|
|
fgSetDouble(s, d);
|
|
|
|
|
}
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
2004-02-26 10:23:48 +00:00
|
|
|
|
i = m->getWindDir();
|
|
|
|
|
if ( i == -1 ) {
|
|
|
|
|
fgSetInt("/environment/metar/base-wind-range-from",
|
|
|
|
|
m->getWindRangeFrom() );
|
|
|
|
|
fgSetInt("/environment/metar/base-wind-range-to",
|
|
|
|
|
m->getWindRangeTo() );
|
|
|
|
|
} else {
|
|
|
|
|
fgSetInt("/environment/metar/base-wind-range-from", i);
|
|
|
|
|
fgSetInt("/environment/metar/base-wind-range-to", i);
|
|
|
|
|
}
|
|
|
|
|
fgSetDouble("/environment/metar/base-wind-speed-kt",
|
|
|
|
|
m->getWindSpeed_kt() );
|
|
|
|
|
|
|
|
|
|
d = m->getGustSpeed_kt();
|
|
|
|
|
d = (d != SGMetarNaN) ? d : 0.0;
|
|
|
|
|
fgSetDouble("/environment/metar/gust-wind-speed-kt", d);
|
|
|
|
|
|
|
|
|
|
d = m->getTemperature_C();
|
|
|
|
|
if (d != SGMetarNaN) {
|
|
|
|
|
dt = m->getDewpoint_C();
|
|
|
|
|
dt = (dt != SGMetarNaN) ? dt : 0.0;
|
|
|
|
|
fgSetDouble("/environment/metar/dewpoint-degc", dt);
|
|
|
|
|
fgSetDouble("/environment/metar/rel-humidity-norm",
|
|
|
|
|
m->getRelHumidity() );
|
|
|
|
|
}
|
|
|
|
|
d = (d != SGMetarNaN) ? d : 15.0;
|
|
|
|
|
fgSetDouble("/environment/metar/temperature-degc", d);
|
|
|
|
|
|
|
|
|
|
d = m->getPressure_inHg();
|
|
|
|
|
d = (d != SGMetarNaN) ? d : 30.0;
|
|
|
|
|
fgSetDouble("/environment/metar/pressure-inhg", d);
|
|
|
|
|
|
|
|
|
|
vector<SGMetarCloud> cv = m->getClouds();
|
|
|
|
|
vector<SGMetarCloud>::iterator cloud;
|
|
|
|
|
|
|
|
|
|
const char *cl = "/environment/clouds/layer[%i]";
|
|
|
|
|
for (i = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, i++) {
|
|
|
|
|
const char *coverage_string[5] =
|
|
|
|
|
{ "clear", "few", "scattered", "broken", "overcast" };
|
|
|
|
|
const double thickness[5] = { 0, 65, 600,750, 1000};
|
|
|
|
|
int q;
|
|
|
|
|
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/coverage", 128);
|
|
|
|
|
q = cloud->getCoverage();
|
|
|
|
|
q = (q != -1 ) ? q : 0;
|
|
|
|
|
fgSetString(s, coverage_string[q] );
|
|
|
|
|
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/elevation-ft", 128);
|
|
|
|
|
d = cloud->getAltitude_ft();
|
|
|
|
|
d = (d != SGMetarNaN) ? d : -9999;
|
|
|
|
|
fgSetDouble(s, d + station_elevation_ft);
|
|
|
|
|
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/thickness-ft", 128);
|
|
|
|
|
fgSetDouble(s, thickness[q]);
|
|
|
|
|
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/span-m", 128);
|
|
|
|
|
fgSetDouble(s, 40000.0);
|
|
|
|
|
}
|
|
|
|
|
for (; i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/coverage", 128);
|
|
|
|
|
fgSetString(s, "clear");
|
|
|
|
|
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/elevation-ft", 128);
|
|
|
|
|
fgSetDouble(s, -9999);
|
|
|
|
|
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/thickness-ft", 128);
|
|
|
|
|
fgSetDouble(s, 0);
|
|
|
|
|
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/span-m", 128);
|
|
|
|
|
fgSetDouble(s, 40000.0);
|
|
|
|
|
}
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
2004-02-26 10:23:48 +00:00
|
|
|
|
delete m;
|
2004-02-23 01:39:12 +00:00
|
|
|
|
|
2004-02-26 10:23:48 +00:00
|
|
|
|
}
|
2004-02-25 15:31:01 +00:00
|
|
|
|
#ifdef ENABLE_THREADS
|
2004-02-26 10:23:48 +00:00
|
|
|
|
|
2004-02-25 15:31:01 +00:00
|
|
|
|
mutex.lock();
|
|
|
|
|
metar_cond.signal();
|
|
|
|
|
mutex.unlock();
|
2004-02-26 10:23:48 +00:00
|
|
|
|
|
|
|
|
|
if (m == NULL)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
catch (const sg_io_exception& e) {
|
|
|
|
|
SG_LOG( SG_GENERAL, SG_WARN, "Error fetching live weather data: "
|
|
|
|
|
<< e.getFormattedMessage().c_str() );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2004-02-25 15:31:01 +00:00
|
|
|
|
#endif // ENABLE_THREADS
|
|
|
|
|
|
2004-02-23 01:39:12 +00:00
|
|
|
|
return true;
|
2004-02-21 12:56:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-02-25 15:31:01 +00:00
|
|
|
|
#ifdef ENABLE_THREADS
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
FGMetarEnvironmentCtrl::MetarThread::run()
|
|
|
|
|
{
|
|
|
|
|
SGMetar *m = NULL;
|
|
|
|
|
|
|
|
|
|
// pthread_cleanup_push( metar_cleanup_handler, fetcher );
|
|
|
|
|
while ( true )
|
|
|
|
|
{
|
|
|
|
|
set_cancel( SGThread::CANCEL_DISABLE );
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
cout << "Fetching ..." << endl;
|
|
|
|
|
// if (m != NULL) m = NULL;
|
2004-02-26 10:23:48 +00:00
|
|
|
|
string host = fetcher->proxy_host->getStringValue();
|
|
|
|
|
string auth = fetcher->proxy_auth->getStringValue();
|
|
|
|
|
string port = fetcher->proxy_port->getStringValue();
|
|
|
|
|
m = new SGMetar( fetcher->_icao, host, port, auth );
|
2004-02-25 15:31:01 +00:00
|
|
|
|
|
|
|
|
|
} catch (const sg_io_exception& e) {
|
|
|
|
|
// SG_LOG( SG_GENERAL, SG_WARN, "Error fetching live weather data: "
|
|
|
|
|
// << e.getFormattedMessage().c_str() );
|
|
|
|
|
m = NULL;
|
|
|
|
|
}
|
|
|
|
|
set_cancel( SGThread::CANCEL_DEFERRED );
|
|
|
|
|
|
|
|
|
|
fetcher->metar_queue.push( m );
|
|
|
|
|
|
|
|
|
|
// Wait for the next frame signal before we fetch the next metar data
|
|
|
|
|
fetcher->mutex.lock();
|
|
|
|
|
fetcher->metar_cond.wait( fetcher->mutex );
|
|
|
|
|
fetcher->mutex.unlock();
|
|
|
|
|
}
|
|
|
|
|
// pthread_cleanup_pop(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Ensure mutex is unlocked.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
metar_cleanup_handler( void* arg )
|
|
|
|
|
{
|
|
|
|
|
FGMetarEnvironmentCtrl* fetcher = (FGMetarEnvironmentCtrl*) arg;
|
|
|
|
|
fetcher->mutex.unlock();
|
|
|
|
|
}
|
|
|
|
|
#endif // ENABLE_THREADS
|
|
|
|
|
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
2002-05-07 00:03:54 +00:00
|
|
|
|
// end of environment_ctrl.cxx
|