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
|
2006-02-21 01:16:04 +00:00
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-05-07 00:03:54 +00:00
|
|
|
|
//
|
|
|
|
|
// $Id$
|
|
|
|
|
|
2006-02-18 13:58:09 +00:00
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
# include "config.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2002-05-07 00:03:54 +00:00
|
|
|
|
#include <stdlib.h>
|
2007-08-03 12:06:17 +00:00
|
|
|
|
#include <math.h>
|
2003-06-10 12:03:07 +00:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2007-08-03 12:06:17 +00:00
|
|
|
|
#include <simgear/debug/logstream.hxx>
|
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"
|
|
|
|
|
|
2008-07-27 16:25:13 +00:00
|
|
|
|
using std::sort;
|
2003-06-09 08:44:59 +00:00
|
|
|
|
|
2007-10-05 21:54:52 +00:00
|
|
|
|
class metar_filter : public FGAirportSearchFilter {
|
2007-10-11 07:53:17 +00:00
|
|
|
|
virtual bool pass(FGAirport *a) { return a->getMetar(); }
|
2007-10-05 21:54:52 +00:00
|
|
|
|
} metar_only;
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-08-15 15:22:44 +00:00
|
|
|
|
sort(table.begin(), table.end(), bucket::lessThan);
|
2003-06-08 14:47:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-15 15:22:44 +00:00
|
|
|
|
bool
|
|
|
|
|
FGInterpolateEnvironmentCtrl::bucket::lessThan(bucket *a, bucket *b)
|
|
|
|
|
{
|
|
|
|
|
return (a->altitude_ft) < (b->altitude_ft);
|
|
|
|
|
}
|
2003-06-08 14:47:03 +00:00
|
|
|
|
|
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-28 19:52:17 +00:00
|
|
|
|
_icao( "" ),
|
2007-08-03 12:06:17 +00:00
|
|
|
|
metar_loaded( false ),
|
2004-02-26 18:21:11 +00:00
|
|
|
|
search_interval_sec( 60.0 ), // 1 minute
|
|
|
|
|
same_station_interval_sec( 900.0 ), // 15 minutes
|
|
|
|
|
search_elapsed( 9999.0 ),
|
|
|
|
|
fetch_elapsed( 9999.0 ),
|
2007-08-03 12:06:17 +00:00
|
|
|
|
last_apt( 0 ),
|
2004-02-26 10:23:48 +00:00
|
|
|
|
proxy_host( fgGetNode("/sim/presets/proxy/host", true) ),
|
|
|
|
|
proxy_port( fgGetNode("/sim/presets/proxy/port", true) ),
|
2004-09-10 07:54:45 +00:00
|
|
|
|
proxy_auth( fgGetNode("/sim/presets/proxy/authentication", true) ),
|
2005-02-22 18:12:32 +00:00
|
|
|
|
metar_max_age( fgGetNode("/environment/params/metar-max-age-min", true) ),
|
2007-08-03 12:06:17 +00:00
|
|
|
|
|
|
|
|
|
// Interpolation constant definitions.
|
|
|
|
|
EnvironmentUpdatePeriodSec( 0.2 ),
|
|
|
|
|
MaxWindChangeKtsSec( 0.2 ),
|
|
|
|
|
MaxVisChangePercentSec( 0.05 ),
|
|
|
|
|
MaxPressureChangeInHgSec( 0.0033 ),
|
|
|
|
|
MaxCloudAltitudeChangeFtSec( 20.0 ),
|
|
|
|
|
MaxCloudThicknessChangeFtSec( 50.0 ),
|
|
|
|
|
MaxCloudInterpolationHeightFt( 5000.0 ),
|
2008-12-06 23:03:12 +00:00
|
|
|
|
MaxCloudInterpolationDeltaFt( 4000.0 ),
|
2007-08-03 12:06:17 +00:00
|
|
|
|
|
2004-12-22 23:57:07 +00:00
|
|
|
|
_error_count( 0 ),
|
2005-11-14 17:18:27 +00:00
|
|
|
|
_stale_count( 0 ),
|
2005-09-18 09:49:26 +00:00
|
|
|
|
_dt( 0.0 ),
|
2007-08-03 12:06:17 +00:00
|
|
|
|
_error_dt( 0.0 )
|
2004-02-21 12:56:16 +00:00
|
|
|
|
{
|
2005-11-22 17:02:31 +00:00
|
|
|
|
#if defined(ENABLE_THREADS)
|
2004-02-25 15:31:01 +00:00
|
|
|
|
thread = new MetarThread(this);
|
2008-06-12 08:24:39 +00:00
|
|
|
|
thread->setProcessorAffinity(1);
|
|
|
|
|
thread->start();
|
2004-02-25 15:31:01 +00:00
|
|
|
|
#endif // ENABLE_THREADS
|
2004-02-21 12:56:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FGMetarEnvironmentCtrl::~FGMetarEnvironmentCtrl ()
|
|
|
|
|
{
|
2005-11-22 17:02:31 +00:00
|
|
|
|
#if defined(ENABLE_THREADS)
|
2006-03-07 10:26:25 +00:00
|
|
|
|
thread_stop();
|
2004-02-25 15:31:01 +00:00
|
|
|
|
#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
|
|
|
|
{
|
2007-08-03 12:06:17 +00:00
|
|
|
|
double dir_from;
|
|
|
|
|
double dir_to;
|
|
|
|
|
double speed;
|
|
|
|
|
double gust;
|
|
|
|
|
double vis;
|
|
|
|
|
double pressure;
|
|
|
|
|
double temp;
|
|
|
|
|
double dewpoint;
|
2008-12-06 23:03:12 +00:00
|
|
|
|
|
|
|
|
|
// If we aren't in the METAR scenario, don't attempt to interpolate.
|
2008-12-13 06:08:22 +00:00
|
|
|
|
if (strcmp(fgGetString("/environment/weather-scenario", "METAR"), "METAR")) return;
|
2007-08-03 12:06:17 +00:00
|
|
|
|
|
|
|
|
|
if (metar_loaded) {
|
|
|
|
|
// Generate interpolated values between the METAR and the current
|
|
|
|
|
// configuration.
|
|
|
|
|
|
|
|
|
|
// Pick up the METAR wind values and convert them into a vector.
|
|
|
|
|
double metar[2];
|
|
|
|
|
double metar_speed = fgGetDouble("/environment/metar/base-wind-speed-kt");
|
|
|
|
|
double metar_heading = fgGetDouble("/environment/metar/base-wind-range-from");
|
|
|
|
|
|
|
|
|
|
metar[0] = metar_speed * sin((metar_heading / 180.0) * M_PI);
|
|
|
|
|
metar[1] = metar_speed * cos((metar_heading / 180.0) * M_PI);
|
|
|
|
|
|
|
|
|
|
// Convert the current wind values and convert them into a vector
|
|
|
|
|
double current[2];
|
|
|
|
|
double current_speed =
|
|
|
|
|
fgGetDouble("/environment/config/boundary/entry/wind-speed-kt");
|
|
|
|
|
double current_heading = fgGetDouble(
|
|
|
|
|
"/environment/config/boundary/entry/wind-from-heading-deg");
|
|
|
|
|
|
|
|
|
|
current[0] = current_speed * sin((current_heading / 180.0) * M_PI);
|
|
|
|
|
current[1] = current_speed * cos((current_heading / 180.0) * M_PI);
|
|
|
|
|
|
|
|
|
|
// Determine the maximum component-wise value that the wind can change.
|
|
|
|
|
// First we determine the fraction in the X and Y component, then
|
|
|
|
|
// factor by the maximum wind change.
|
|
|
|
|
double x = fabs(current[0] - metar[0]);
|
|
|
|
|
double y = fabs(current[1] - metar[1]);
|
|
|
|
|
double dx = x / (x + y);
|
|
|
|
|
double dy = 1 - dx;
|
|
|
|
|
|
|
|
|
|
double maxdx = dx * MaxWindChangeKtsSec;
|
|
|
|
|
double maxdy = dy * MaxWindChangeKtsSec;
|
|
|
|
|
|
|
|
|
|
// Interpolate each component separately.
|
|
|
|
|
current[0] = interpolate_val(current[0], metar[0], maxdx);
|
|
|
|
|
current[1] = interpolate_val(current[1], metar[1], maxdy);
|
|
|
|
|
|
|
|
|
|
// Now convert back to polar coordinates.
|
|
|
|
|
if ((current[0] == 0.0) && (current[1] == 0.0)) {
|
|
|
|
|
// Special case where there is no wind (otherwise atan2 barfs)
|
|
|
|
|
speed = 0.0;
|
|
|
|
|
dir_from = current_heading;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// Some real wind to convert back from. Work out the speed
|
|
|
|
|
// and direction value in degrees.
|
|
|
|
|
speed = sqrt((current[0] * current[0]) + (current[1] * current[1]));
|
|
|
|
|
dir_from = (atan2(current[0], current[1]) * 180.0 / M_PI);
|
|
|
|
|
|
|
|
|
|
// Normalize the direction.
|
|
|
|
|
if (dir_from < 0.0)
|
|
|
|
|
dir_from += 360.0;
|
|
|
|
|
|
|
|
|
|
SG_LOG( SG_GENERAL, SG_DEBUG, "Wind : " << dir_from << "@" << speed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now handle the visibility. We convert both visibility values
|
|
|
|
|
// to X-values, then interpolate from there, then back to real values.
|
|
|
|
|
// The length_scale is fixed to 1000m, so the visibility changes by
|
|
|
|
|
// by MaxVisChangePercentSec or 1000m X MaxVisChangePercentSec,
|
|
|
|
|
// whichever is more.
|
|
|
|
|
double currentvis =
|
|
|
|
|
fgGetDouble("/environment/config/boundary/entry/visibility-m");
|
|
|
|
|
double metarvis = fgGetDouble("/environment/metar/min-visibility-m");
|
|
|
|
|
double currentxval = log(1000.0 + currentvis);
|
|
|
|
|
double metarxval = log(1000.0 + metarvis);
|
|
|
|
|
|
|
|
|
|
currentxval = interpolate_val(currentxval, metarxval, MaxVisChangePercentSec);
|
|
|
|
|
|
|
|
|
|
// Now convert back from an X-value to a straightforward visibility.
|
|
|
|
|
vis = exp(currentxval) - 1000.0;
|
|
|
|
|
|
|
|
|
|
pressure = interpolate_prop(
|
|
|
|
|
"/environment/config/boundary/entry/pressure-sea-level-inhg",
|
|
|
|
|
"/environment/metar/pressure-inhg",
|
|
|
|
|
MaxPressureChangeInHgSec);
|
|
|
|
|
|
|
|
|
|
dir_to = fgGetDouble("/environment/metar/base-wind-range-to");
|
|
|
|
|
gust = fgGetDouble("/environment/metar/gust-wind-speed-kt");
|
|
|
|
|
temp = fgGetDouble("/environment/metar/temperature-degc");
|
|
|
|
|
dewpoint = fgGetDouble("/environment/metar/dewpoint-degc");
|
|
|
|
|
|
|
|
|
|
// Set the cloud layers by interpolating over the METAR versions.
|
|
|
|
|
SGPropertyNode * clouds = fgGetNode("/environment/metar/clouds");
|
|
|
|
|
|
|
|
|
|
vector<SGPropertyNode_ptr> layers = clouds->getChildren("layer");
|
|
|
|
|
vector<SGPropertyNode_ptr>::const_iterator layer;
|
|
|
|
|
vector<SGPropertyNode_ptr>::const_iterator layers_end = layers.end();
|
|
|
|
|
|
|
|
|
|
const char *cl = "/environment/clouds/layer[%i]";
|
|
|
|
|
double aircraft_alt = fgGetDouble("/position/altitude-ft");
|
|
|
|
|
char s[128];
|
|
|
|
|
int i;
|
2008-12-19 07:42:13 +00:00
|
|
|
|
bool rebuild_clouds = false;
|
2007-08-03 12:06:17 +00:00
|
|
|
|
|
|
|
|
|
for (i = 0, layer = layers.begin(); layer != layers_end; ++layer, i++) {
|
|
|
|
|
double currentval;
|
|
|
|
|
double requiredval;
|
|
|
|
|
|
|
|
|
|
// In the case of clouds, we want to avoid writing if nothing has
|
|
|
|
|
// changed, as these properties are tied to the renderer and will
|
|
|
|
|
// cause the clouds to be updated, reseting the texture locations.
|
|
|
|
|
|
|
|
|
|
// We don't interpolate the coverage values as no-matter how we
|
|
|
|
|
// do it, it will be quite a sudden change of texture. Better to
|
|
|
|
|
// have a single change than four or five.
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/coverage", 128);
|
|
|
|
|
const char* coverage = (*layer)->getStringValue("coverage", "clear");
|
2008-12-19 07:42:13 +00:00
|
|
|
|
if (strncmp(fgGetString(s), coverage, 128) != 0) {
|
2007-08-03 12:06:17 +00:00
|
|
|
|
fgSetString(s, coverage);
|
2008-12-19 07:42:13 +00:00
|
|
|
|
rebuild_clouds = true;
|
|
|
|
|
}
|
2007-08-03 12:06:17 +00:00
|
|
|
|
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/elevation-ft", 128);
|
|
|
|
|
double current_alt = fgGetDouble(s);
|
|
|
|
|
double required_alt = (*layer)->getDoubleValue("elevation-ft");
|
|
|
|
|
|
2008-12-06 23:03:12 +00:00
|
|
|
|
if (current_alt < -9000 || required_alt < -9000 ||
|
|
|
|
|
fabs(aircraft_alt - required_alt) > MaxCloudInterpolationHeightFt ||
|
|
|
|
|
fabs(current_alt - required_alt) > MaxCloudInterpolationDeltaFt) {
|
|
|
|
|
// We don't interpolate any layers that are
|
|
|
|
|
// - too far above us to be visible
|
|
|
|
|
// - too far below us to be visible
|
|
|
|
|
// - with too large a difference to make interpolation sensible
|
|
|
|
|
// - to or from -9999 (used as a placeholder)
|
|
|
|
|
// - any values that are too high above us,
|
2007-08-03 12:06:17 +00:00
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/elevation-ft", 128);
|
|
|
|
|
if (current_alt != required_alt)
|
|
|
|
|
fgSetDouble(s, required_alt);
|
|
|
|
|
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/thickness-ft", 128);
|
|
|
|
|
if (fgGetDouble(s) != (*layer)->getDoubleValue("thickness-ft"))
|
|
|
|
|
fgSetDouble(s, (*layer)->getDoubleValue("thickness-ft"));
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// Interpolate the other values in the usual way
|
|
|
|
|
if (current_alt != required_alt) {
|
|
|
|
|
current_alt = interpolate_val(current_alt,
|
|
|
|
|
required_alt,
|
|
|
|
|
MaxCloudAltitudeChangeFtSec);
|
|
|
|
|
fgSetDouble(s, current_alt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/thickness-ft", 128);
|
|
|
|
|
currentval = fgGetDouble(s);
|
|
|
|
|
requiredval = (*layer)->getDoubleValue("thickness-ft");
|
|
|
|
|
|
|
|
|
|
if (currentval != requiredval) {
|
|
|
|
|
currentval = interpolate_val(currentval,
|
|
|
|
|
requiredval,
|
|
|
|
|
MaxCloudThicknessChangeFtSec);
|
|
|
|
|
fgSetDouble(s, currentval);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-19 07:42:13 +00:00
|
|
|
|
|
|
|
|
|
if (rebuild_clouds) {
|
|
|
|
|
// Force an update of the 3D clouds
|
|
|
|
|
fgSetDouble("/environment/rebuild-layers", 1.0);
|
|
|
|
|
}
|
2007-08-03 12:06:17 +00:00
|
|
|
|
} else {
|
|
|
|
|
// We haven't already loaded a METAR, so apply it immediately.
|
|
|
|
|
dir_from = fgGetDouble("/environment/metar/base-wind-range-from");
|
|
|
|
|
dir_to = fgGetDouble("/environment/metar/base-wind-range-to");
|
|
|
|
|
speed = fgGetDouble("/environment/metar/base-wind-speed-kt");
|
|
|
|
|
gust = fgGetDouble("/environment/metar/gust-wind-speed-kt");
|
|
|
|
|
vis = fgGetDouble("/environment/metar/min-visibility-m");
|
|
|
|
|
pressure = fgGetDouble("/environment/metar/pressure-inhg");
|
|
|
|
|
temp = fgGetDouble("/environment/metar/temperature-degc");
|
|
|
|
|
dewpoint = fgGetDouble("/environment/metar/dewpoint-degc");
|
|
|
|
|
|
|
|
|
|
// Set the cloud layers by copying over the METAR versions.
|
2008-12-18 11:49:33 +00:00
|
|
|
|
SGPropertyNode * clouds = fgGetNode("/environment/metar/clouds", true);
|
2007-08-03 12:06:17 +00:00
|
|
|
|
|
|
|
|
|
vector<SGPropertyNode_ptr> layers = clouds->getChildren("layer");
|
|
|
|
|
vector<SGPropertyNode_ptr>::const_iterator layer;
|
|
|
|
|
vector<SGPropertyNode_ptr>::const_iterator layers_end = layers.end();
|
|
|
|
|
|
|
|
|
|
const char *cl = "/environment/clouds/layer[%i]";
|
|
|
|
|
char s[128];
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0, layer = layers.begin(); layer != layers_end; ++layer, i++) {
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/coverage", 128);
|
|
|
|
|
fgSetString(s, (*layer)->getStringValue("coverage", "clear"));
|
|
|
|
|
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/elevation-ft", 128);
|
|
|
|
|
fgSetDouble(s, (*layer)->getDoubleValue("elevation-ft"));
|
|
|
|
|
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/thickness-ft", 128);
|
|
|
|
|
fgSetDouble(s, (*layer)->getDoubleValue("thickness-ft"));
|
|
|
|
|
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/span-m", 128);
|
|
|
|
|
fgSetDouble(s, 40000.0);
|
|
|
|
|
}
|
2008-12-17 19:51:18 +00:00
|
|
|
|
|
|
|
|
|
// Force an update of the 3D clouds
|
|
|
|
|
fgSetDouble("/environment/rebuild-layers", 1.0);
|
2007-08-03 12:06:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fgSetupWind(dir_from, dir_to, speed, gust);
|
|
|
|
|
fgDefaultWeatherValue("visibility-m", vis);
|
|
|
|
|
set_temp_at_altitude(temp, station_elevation_ft);
|
|
|
|
|
set_dewpoint_at_altitude(dewpoint, station_elevation_ft);
|
|
|
|
|
fgDefaultWeatherValue("pressure-sea-level-inhg", pressure);
|
|
|
|
|
|
|
|
|
|
// We've now successfully loaded a METAR into the configuration
|
|
|
|
|
metar_loaded = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double FGMetarEnvironmentCtrl::interpolate_prop(const char * currentname,
|
|
|
|
|
const char * requiredname,
|
|
|
|
|
double dt)
|
|
|
|
|
{
|
|
|
|
|
double currentval = fgGetDouble(currentname);
|
|
|
|
|
double requiredval = fgGetDouble(requiredname);
|
|
|
|
|
return interpolate_val(currentval, requiredval, dt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double FGMetarEnvironmentCtrl::interpolate_val(double currentval,
|
|
|
|
|
double requiredval,
|
|
|
|
|
double dt)
|
|
|
|
|
{
|
|
|
|
|
double dval = EnvironmentUpdatePeriodSec * dt;
|
|
|
|
|
|
|
|
|
|
if (fabs(currentval - requiredval) < dval) return requiredval;
|
|
|
|
|
if (currentval < requiredval) return (currentval + dval);
|
|
|
|
|
if (currentval > requiredval) return (currentval - dval);
|
|
|
|
|
return requiredval;
|
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 );
|
|
|
|
|
|
2007-08-03 12:06:17 +00:00
|
|
|
|
metar_loaded = false;
|
2004-02-23 01:39:12 +00:00
|
|
|
|
bool found_metar = false;
|
2005-09-18 09:49:26 +00:00
|
|
|
|
long max_age = metar_max_age->getLongValue();
|
|
|
|
|
// Don't check max age during init so that we don't loop over a lot
|
|
|
|
|
// of airports metar if there is a problem.
|
|
|
|
|
// The update() calls will find a correct metar if things went wrong here
|
2005-11-14 17:18:27 +00:00
|
|
|
|
metar_max_age->setLongValue(0);
|
2004-08-21 11:43:48 +00:00
|
|
|
|
|
|
|
|
|
while ( !found_metar && (_error_count < 3) ) {
|
2005-09-20 20:26:57 +00:00
|
|
|
|
const FGAirport* a = globals->get_airports()
|
|
|
|
|
->search( longitude->getDoubleValue(),
|
|
|
|
|
latitude->getDoubleValue(),
|
2008-08-15 18:48:11 +00:00
|
|
|
|
360.0,
|
2007-10-05 21:54:52 +00:00
|
|
|
|
metar_only );
|
2005-09-20 20:26:57 +00:00
|
|
|
|
if ( a ) {
|
|
|
|
|
FGMetarResult result = fetch_data( a->getId() );
|
|
|
|
|
if ( result.m != NULL ) {
|
|
|
|
|
SG_LOG( SG_GENERAL, SG_INFO, "closest station w/ metar = "
|
|
|
|
|
<< a->getId());
|
2005-12-29 13:58:21 +00:00
|
|
|
|
last_apt = a;
|
2005-09-20 20:26:57 +00:00
|
|
|
|
_icao = a->getId();
|
|
|
|
|
search_elapsed = 0.0;
|
|
|
|
|
fetch_elapsed = 0.0;
|
|
|
|
|
update_metar_properties( result.m );
|
|
|
|
|
update_env_config();
|
|
|
|
|
env->init();
|
|
|
|
|
found_metar = true;
|
|
|
|
|
} else {
|
|
|
|
|
// mark as no metar so it doesn't show up in subsequent
|
|
|
|
|
// searches.
|
|
|
|
|
SG_LOG( SG_GENERAL, SG_INFO, "no metar at metar = "
|
|
|
|
|
<< a->getId() );
|
|
|
|
|
globals->get_airports()->no_metar( a->getId() );
|
|
|
|
|
}
|
2004-02-23 01:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2005-09-18 09:49:26 +00:00
|
|
|
|
metar_max_age->setLongValue(max_age);
|
2004-02-21 12:56:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGMetarEnvironmentCtrl::reinit ()
|
|
|
|
|
{
|
2004-08-21 11:43:48 +00:00
|
|
|
|
_error_count = 0;
|
|
|
|
|
_error_dt = 0.0;
|
2008-12-06 23:03:12 +00:00
|
|
|
|
metar_loaded = false;
|
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-08-21 11:43:48 +00:00
|
|
|
|
|
|
|
|
|
_dt += delta_time_sec;
|
|
|
|
|
if (_error_count >= 3)
|
|
|
|
|
return;
|
|
|
|
|
|
2004-02-28 19:52:17 +00:00
|
|
|
|
FGMetarResult result;
|
|
|
|
|
|
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 );
|
2004-02-26 18:21:11 +00:00
|
|
|
|
search_elapsed += delta_time_sec;
|
|
|
|
|
fetch_elapsed += delta_time_sec;
|
2007-08-03 12:06:17 +00:00
|
|
|
|
interpolate_elapsed += delta_time_sec;
|
2004-02-28 19:52:17 +00:00
|
|
|
|
|
|
|
|
|
// if time for a new search request, push it onto the request
|
|
|
|
|
// queue
|
2004-02-26 18:21:11 +00:00
|
|
|
|
if ( search_elapsed > search_interval_sec ) {
|
2005-09-20 20:26:57 +00:00
|
|
|
|
const FGAirport* a = globals->get_airports()
|
|
|
|
|
->search( longitude->getDoubleValue(),
|
|
|
|
|
latitude->getDoubleValue(),
|
2008-08-15 18:48:11 +00:00
|
|
|
|
360.0,
|
2007-10-05 21:54:52 +00:00
|
|
|
|
metar_only );
|
2005-09-20 20:26:57 +00:00
|
|
|
|
if ( a ) {
|
2005-12-29 13:58:21 +00:00
|
|
|
|
if ( !last_apt || last_apt->getId() != a->getId()
|
2005-09-20 20:26:57 +00:00
|
|
|
|
|| fetch_elapsed > same_station_interval_sec )
|
|
|
|
|
{
|
|
|
|
|
SG_LOG( SG_GENERAL, SG_INFO, "closest station w/ metar = "
|
|
|
|
|
<< a->getId());
|
|
|
|
|
request_queue.push( a->getId() );
|
2005-12-29 13:58:21 +00:00
|
|
|
|
last_apt = a;
|
2005-09-20 20:26:57 +00:00
|
|
|
|
_icao = a->getId();
|
|
|
|
|
search_elapsed = 0.0;
|
|
|
|
|
fetch_elapsed = 0.0;
|
|
|
|
|
} else {
|
|
|
|
|
search_elapsed = 0.0;
|
|
|
|
|
SG_LOG( SG_GENERAL, SG_INFO, "same station, waiting = "
|
|
|
|
|
<< same_station_interval_sec - fetch_elapsed );
|
|
|
|
|
}
|
2004-02-23 01:39:12 +00:00
|
|
|
|
} else {
|
2005-09-20 20:26:57 +00:00
|
|
|
|
SG_LOG( SG_GENERAL, SG_WARN,
|
|
|
|
|
"Unable to find any airports with metar" );
|
2004-02-28 19:52:17 +00:00
|
|
|
|
}
|
2007-08-03 12:06:17 +00:00
|
|
|
|
} else if ( interpolate_elapsed > EnvironmentUpdatePeriodSec ) {
|
|
|
|
|
// Interpolate the current configuration closer to the actual METAR
|
|
|
|
|
update_env_config();
|
|
|
|
|
env->reinit();
|
|
|
|
|
interpolate_elapsed = 0.0;
|
2004-02-28 19:52:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-11-22 17:02:31 +00:00
|
|
|
|
#if !defined(ENABLE_THREADS)
|
2004-02-28 19:52:17 +00:00
|
|
|
|
// No loader thread running so manually fetch the data
|
|
|
|
|
string id = "";
|
|
|
|
|
while ( !request_queue.empty() ) {
|
|
|
|
|
id = request_queue.front();
|
|
|
|
|
request_queue.pop();
|
|
|
|
|
}
|
2004-08-21 11:43:48 +00:00
|
|
|
|
|
2004-02-28 19:52:17 +00:00
|
|
|
|
if ( !id.empty() ) {
|
|
|
|
|
SG_LOG( SG_GENERAL, SG_INFO, "inline fetching = " << id );
|
|
|
|
|
result = fetch_data( id );
|
|
|
|
|
result_queue.push( result );
|
|
|
|
|
}
|
|
|
|
|
#endif // ENABLE_THREADS
|
|
|
|
|
|
|
|
|
|
// process any results from the loader.
|
|
|
|
|
while ( !result_queue.empty() ) {
|
|
|
|
|
result = result_queue.front();
|
|
|
|
|
result_queue.pop();
|
|
|
|
|
if ( result.m != NULL ) {
|
|
|
|
|
update_metar_properties( result.m );
|
|
|
|
|
delete result.m;
|
|
|
|
|
update_env_config();
|
|
|
|
|
env->reinit();
|
|
|
|
|
} else {
|
|
|
|
|
// mark as no metar so it doesn't show up in subsequent
|
|
|
|
|
// searches, and signal an immediate re-search.
|
|
|
|
|
SG_LOG( SG_GENERAL, SG_WARN,
|
|
|
|
|
"no metar at station = " << result.icao );
|
|
|
|
|
globals->get_airports()->no_metar( result.icao );
|
|
|
|
|
search_elapsed = 9999.0;
|
2004-02-23 01:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2004-02-25 15:31:01 +00:00
|
|
|
|
|
2004-02-21 14:04:40 +00:00
|
|
|
|
env->update(delta_time_sec);
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-28 19:52:17 +00:00
|
|
|
|
|
2004-02-21 14:04:40 +00:00
|
|
|
|
void
|
|
|
|
|
FGMetarEnvironmentCtrl::setEnvironment (FGEnvironment * environment)
|
|
|
|
|
{
|
|
|
|
|
env->setEnvironment(environment);
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-28 19:52:17 +00:00
|
|
|
|
FGMetarResult
|
|
|
|
|
FGMetarEnvironmentCtrl::fetch_data( const string &icao )
|
2004-02-21 12:56:16 +00:00
|
|
|
|
{
|
2004-02-28 19:52:17 +00:00
|
|
|
|
FGMetarResult result;
|
|
|
|
|
result.icao = icao;
|
2004-02-21 15:00:49 +00:00
|
|
|
|
|
2004-08-21 11:43:48 +00:00
|
|
|
|
// if the last error was more than three seconds ago,
|
|
|
|
|
// then pretent nothing happened.
|
|
|
|
|
if (_error_dt < 3) {
|
|
|
|
|
_error_dt += _dt;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
_error_dt = 0.0;
|
|
|
|
|
_error_count = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-22 14:21:37 +00:00
|
|
|
|
// fetch station elevation if exists
|
2005-09-20 20:26:57 +00:00
|
|
|
|
const FGAirport* a = globals->get_airports()->search( icao );
|
|
|
|
|
if ( a ) {
|
|
|
|
|
station_elevation_ft = a->getElevation();
|
|
|
|
|
}
|
2004-02-22 14:21:37 +00:00
|
|
|
|
|
|
|
|
|
// fetch current metar data
|
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();
|
2005-01-20 09:28:45 +00:00
|
|
|
|
result.m = new FGMetar( icao, host, port, auth);
|
|
|
|
|
|
2005-02-22 18:12:32 +00:00
|
|
|
|
long max_age = metar_max_age->getLongValue();
|
2005-11-14 17:18:27 +00:00
|
|
|
|
long age = result.m->getAge_min();
|
|
|
|
|
if (max_age && age > max_age) {
|
|
|
|
|
SG_LOG( SG_GENERAL, SG_WARN, "METAR data too old (" << age << " min).");
|
2005-01-20 09:28:45 +00:00
|
|
|
|
delete result.m;
|
|
|
|
|
result.m = NULL;
|
2005-11-14 17:18:27 +00:00
|
|
|
|
|
|
|
|
|
if (++_stale_count > 10) {
|
|
|
|
|
_error_count = 1000;
|
2005-11-14 21:59:13 +00:00
|
|
|
|
throw sg_io_exception("More than 10 stale METAR messages in a row."
|
|
|
|
|
" Check your system time!");
|
2005-11-14 17:18:27 +00:00
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
_stale_count = 0;
|
2004-08-21 11:43:48 +00:00
|
|
|
|
|
2004-02-28 19:52:17 +00:00
|
|
|
|
} catch (const sg_io_exception& e) {
|
|
|
|
|
SG_LOG( SG_GENERAL, SG_WARN, "Error fetching live weather data: "
|
|
|
|
|
<< e.getFormattedMessage().c_str() );
|
2005-11-22 17:02:31 +00:00
|
|
|
|
#if defined(ENABLE_THREADS)
|
2004-08-21 11:43:48 +00:00
|
|
|
|
if (_error_count++ >= 3) {
|
|
|
|
|
SG_LOG( SG_GENERAL, SG_WARN, "Stop fetching data permanently.");
|
2006-03-07 10:26:25 +00:00
|
|
|
|
thread_stop();
|
2004-08-21 11:43:48 +00:00
|
|
|
|
}
|
2004-09-10 07:54:45 +00:00
|
|
|
|
#endif
|
2004-08-21 11:43:48 +00:00
|
|
|
|
|
2004-02-28 19:52:17 +00:00
|
|
|
|
result.m = NULL;
|
|
|
|
|
}
|
2004-02-26 10:23:48 +00:00
|
|
|
|
|
2004-08-21 11:43:48 +00:00
|
|
|
|
_dt = 0;
|
|
|
|
|
|
2004-02-28 19:52:17 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
|
|
|
|
|
2004-02-28 19:52:17 +00:00
|
|
|
|
void
|
2005-10-25 13:49:55 +00:00
|
|
|
|
FGMetarEnvironmentCtrl::update_metar_properties( const FGMetar *m )
|
2004-02-28 19:52:17 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
2005-01-20 09:28:45 +00:00
|
|
|
|
double d;
|
2004-02-28 19:52:17 +00:00
|
|
|
|
char s[128];
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
2005-05-22 08:08:22 +00:00
|
|
|
|
fgSetString("/environment/metar/real-metar", m->getData());
|
|
|
|
|
// don't update with real weather when we use a custom weather scenario
|
2005-09-18 09:49:26 +00:00
|
|
|
|
const char *current_scenario = fgGetString("/environment/weather-scenario", "METAR");
|
|
|
|
|
if( strcmp(current_scenario, "METAR") && strcmp(current_scenario, "none"))
|
2005-05-22 08:08:22 +00:00
|
|
|
|
return;
|
|
|
|
|
fgSetString("/environment/metar/last-metar", m->getData());
|
2005-01-20 09:28:45 +00:00
|
|
|
|
fgSetString("/environment/metar/station-id", m->getId());
|
|
|
|
|
fgSetDouble("/environment/metar/min-visibility-m",
|
|
|
|
|
m->getMinVisibility().getVisibility_m() );
|
|
|
|
|
fgSetDouble("/environment/metar/max-visibility-m",
|
|
|
|
|
m->getMaxVisibility().getVisibility_m() );
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
2005-10-25 13:49:55 +00:00
|
|
|
|
const SGMetarVisibility *dirvis = m->getDirVisibility();
|
2004-02-28 19:52:17 +00:00
|
|
|
|
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";
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
2004-02-28 19:52:17 +00:00
|
|
|
|
d = dirvis->getVisibility_m();
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
2004-02-28 19:52:17 +00:00
|
|
|
|
snprintf(s, 128, min, i);
|
|
|
|
|
fgSetDouble(s, d);
|
|
|
|
|
snprintf(s, 128, max, i);
|
|
|
|
|
fgSetDouble(s, d);
|
|
|
|
|
}
|
2004-02-23 01:39:12 +00:00
|
|
|
|
|
2005-01-20 09:28:45 +00:00
|
|
|
|
fgSetInt("/environment/metar/base-wind-range-from",
|
|
|
|
|
m->getWindRangeFrom() );
|
|
|
|
|
fgSetInt("/environment/metar/base-wind-range-to",
|
|
|
|
|
m->getWindRangeTo() );
|
2004-02-28 19:52:17 +00:00
|
|
|
|
fgSetDouble("/environment/metar/base-wind-speed-kt",
|
|
|
|
|
m->getWindSpeed_kt() );
|
2005-01-20 09:28:45 +00:00
|
|
|
|
fgSetDouble("/environment/metar/gust-wind-speed-kt",
|
|
|
|
|
m->getGustSpeed_kt() );
|
|
|
|
|
fgSetDouble("/environment/metar/temperature-degc",
|
|
|
|
|
m->getTemperature_C() );
|
|
|
|
|
fgSetDouble("/environment/metar/dewpoint-degc",
|
|
|
|
|
m->getDewpoint_C() );
|
|
|
|
|
fgSetDouble("/environment/metar/rel-humidity-norm",
|
|
|
|
|
m->getRelHumidity() );
|
|
|
|
|
fgSetDouble("/environment/metar/pressure-inhg",
|
|
|
|
|
m->getPressure_inHg() );
|
2004-02-28 19:52:17 +00:00
|
|
|
|
|
|
|
|
|
vector<SGMetarCloud> cv = m->getClouds();
|
2005-10-26 09:03:49 +00:00
|
|
|
|
vector<SGMetarCloud>::const_iterator cloud;
|
2004-02-28 19:52:17 +00:00
|
|
|
|
|
2007-08-03 12:06:17 +00:00
|
|
|
|
const char *cl = "/environment/metar/clouds/layer[%i]";
|
2004-02-28 19:52:17 +00:00
|
|
|
|
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();
|
|
|
|
|
fgSetString(s, coverage_string[q] );
|
|
|
|
|
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/elevation-ft", 128);
|
2005-01-20 09:28:45 +00:00
|
|
|
|
fgSetDouble(s, cloud->getAltitude_ft() + station_elevation_ft);
|
2004-02-28 19:52:17 +00:00
|
|
|
|
|
|
|
|
|
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);
|
2004-02-26 10:23:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-02-28 19:52:17 +00:00
|
|
|
|
for (; i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
|
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/coverage", 128);
|
|
|
|
|
fgSetString(s, "clear");
|
2004-02-26 10:23:48 +00:00
|
|
|
|
|
2004-02-28 19:52:17 +00:00
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/elevation-ft", 128);
|
|
|
|
|
fgSetDouble(s, -9999);
|
2004-02-26 10:23:48 +00:00
|
|
|
|
|
2004-02-28 19:52:17 +00:00
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/thickness-ft", 128);
|
|
|
|
|
fgSetDouble(s, 0);
|
2004-02-25 15:31:01 +00:00
|
|
|
|
|
2004-02-28 19:52:17 +00:00
|
|
|
|
snprintf(s, 128, cl, i);
|
|
|
|
|
strncat(s, "/span-m", 128);
|
|
|
|
|
fgSetDouble(s, 40000.0);
|
|
|
|
|
}
|
2005-01-20 09:28:45 +00:00
|
|
|
|
|
|
|
|
|
fgSetDouble("/environment/metar/rain-norm", m->getRain());
|
|
|
|
|
fgSetDouble("/environment/metar/hail-norm", m->getHail());
|
|
|
|
|
fgSetDouble("/environment/metar/snow-norm", m->getSnow());
|
|
|
|
|
fgSetBool("/environment/metar/snow-cover", m->getSnowCover());
|
2004-02-21 12:56:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-02-28 19:52:17 +00:00
|
|
|
|
|
2005-11-22 17:02:31 +00:00
|
|
|
|
#if defined(ENABLE_THREADS)
|
2006-03-07 10:26:25 +00:00
|
|
|
|
void
|
|
|
|
|
FGMetarEnvironmentCtrl::thread_stop()
|
|
|
|
|
{
|
|
|
|
|
request_queue.push( string() ); // ask thread to terminate
|
|
|
|
|
thread->join();
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-25 15:31:01 +00:00
|
|
|
|
void
|
|
|
|
|
FGMetarEnvironmentCtrl::MetarThread::run()
|
|
|
|
|
{
|
|
|
|
|
while ( true )
|
|
|
|
|
{
|
2004-02-28 19:52:17 +00:00
|
|
|
|
string icao = fetcher->request_queue.pop();
|
2006-03-07 10:26:25 +00:00
|
|
|
|
if (icao.empty())
|
|
|
|
|
return;
|
2004-02-28 19:52:17 +00:00
|
|
|
|
SG_LOG( SG_GENERAL, SG_INFO, "Thread: fetch metar data = " << icao );
|
|
|
|
|
FGMetarResult result = fetcher->fetch_data( icao );
|
|
|
|
|
fetcher->result_queue.push( result );
|
2004-02-25 15:31:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif // ENABLE_THREADS
|
|
|
|
|
|
2004-02-21 12:56:16 +00:00
|
|
|
|
|
2002-05-07 00:03:54 +00:00
|
|
|
|
// end of environment_ctrl.cxx
|