#include <Main/fg_props.hxx>
#include <Main/util.hxx>
+#include "atmosphere.hxx"
#include "fgmetar.hxx"
#include "environment_ctrl.hxx"
void
FGInterpolateEnvironmentCtrl::reinit ()
{
-// TODO: do we really need to throw away the old tables on reinit? Better recycle
- unsigned int i;
- for (i = 0; i < _boundary_table.size(); i++)
- delete _boundary_table[i];
- for (i = 0; i < _aloft_table.size(); i++)
- 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++) {
+ double last_altitude_ft = 0.0;
+ double sort_required = false;
+ size_t i;
+
+ for (i = 0; i < (size_t)node->nChildren(); i++) {
const SGPropertyNode * child = node->getChild(i);
if ( strcmp(child->getName(), "entry") == 0
&& child->getStringValue("elevation-ft", "")[0] != '\0'
&& ( child->getDoubleValue("elevation-ft") > 0.1 || i == 0 ) )
{
- bucket * b = new bucket;
+ bucket * b;
+ if( i < table.size() ) {
+ // recycle existing bucket
+ b = table[i];
+ } else {
+ // more nodes than buckets in table, add a new one
+ b = new bucket;
+ table.push_back(b);
+ }
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);
+
+ // check, if altitudes are in ascending order
+ if( b->altitude_ft < last_altitude_ft )
+ sort_required = true;
+ last_altitude_ft = b->altitude_ft;
}
}
- sort(table.begin(), table.end(), bucket::lessThan);
+ // remove leftover buckets
+ while( table.size() > i ) {
+ bucket * b = *(table.end() - 1);
+ delete b;
+ table.pop_back();
+ }
+
+ if( sort_required )
+ sort(table.begin(), table.end(), bucket::lessThan);
}
void
do_interpolate(_boundary_table, altitude_agl_ft, _environment);
return;
} else if ((boundary_limit + boundary_transition) >= altitude_agl_ft) {
+ //TODO: this is 500ft above the top altitude of boundary layer
+ //shouldn't this be +/-250 ft off of the top altitude?
// both tables
do_interpolate(_boundary_table, altitude_agl_ft, &env1);
do_interpolate(_aloft_table, altitude_ft, &env2);
////////////////////////////////////////////////////////////////////////
FGMetarCtrl::FGMetarCtrl( SGSubsystem * environmentCtrl )
- : _environmentCtrl(environmentCtrl),
- station_elevation_ft(0.0),
+ :
metar_valid(false),
setup_winds_aloft(true),
+ wind_interpolation_required(true),
// Interpolation constant definitions.
EnvironmentUpdatePeriodSec( 0.2 ),
MaxWindChangeKtsSec( 0.2 ),
MaxCloudAltitudeChangeFtSec( 20.0 ),
MaxCloudThicknessChangeFtSec( 50.0 ),
MaxCloudInterpolationHeightFt( 5000.0 ),
- MaxCloudInterpolationDeltaFt( 4000.0 )
+ MaxCloudInterpolationDeltaFt( 4000.0 ),
+ _environmentCtrl(environmentCtrl)
{
+ windModulator = new FGBasicWindModulator();
+
metar_base_n = fgGetNode( "/environment/metar", true );
station_id_n = metar_base_n->getNode("station-id", true );
station_elevation_n = metar_base_n->getNode("station-elevation-ft", true );
base_wind_range_from_n = metar_base_n->getNode("base-wind-range-from", true );
base_wind_range_to_n = metar_base_n->getNode("base-wind-range-to", true );
base_wind_speed_n = metar_base_n->getNode("base-wind-speed-kt", true );
- base_wind_dir_n = metar_base_n->getNode("base-wind-dir-deg", true );
+ base_wind_dir_n = metar_base_n->getNode("base-wind-dir-deg", true );
gust_wind_speed_n = metar_base_n->getNode("gust-wind-speed-kt", true );
temperature_n = metar_base_n->getNode("temperature-degc", true );
dewpoint_n = metar_base_n->getNode("dewpoint-degc", true );
hail_n = metar_base_n->getNode("hail-norm", true );
snow_n = metar_base_n->getNode("snow-norm", true );
snow_cover_n = metar_base_n->getNode("snow-cover", true );
+ magnetic_variation_n = fgGetNode( "/environment/magnetic-variation-deg", true );
ground_elevation_n = fgGetNode( "/position/ground-elev-m", true );
longitude_n = fgGetNode( "/position/longitude-deg", true );
latitude_n = fgGetNode( "/position/latitude-deg", true );
void FGMetarCtrl::bind ()
{
- fgTie("/environment/metar/valid", this, &FGMetarCtrl::get_valid );
- fgTie("/environment/params/metar-updates-environment", this, &FGMetarCtrl::get_enabled, &FGMetarCtrl::set_enabled );
- fgTie("/environment/params/metar-updates-winds-aloft", this, &FGMetarCtrl::get_setup_winds_aloft, &FGMetarCtrl::set_setup_winds_aloft );
+ fgTie("/environment/metar/valid", this, &FGMetarCtrl::get_valid );
+ fgTie("/environment/params/metar-updates-environment", this, &FGMetarCtrl::get_enabled, &FGMetarCtrl::set_enabled );
+ fgTie("/environment/params/metar-updates-winds-aloft", this, &FGMetarCtrl::get_setup_winds_aloft, &FGMetarCtrl::set_setup_winds_aloft );
}
void FGMetarCtrl::unbind ()
{
- fgUntie("/environment/metar/valid");
- fgUntie("/environment/params/metar-updates-environment");
- fgUntie("/environment/params/metar-updates-winds-aloft");
+ fgUntie("/environment/metar/valid");
+ fgUntie("/environment/params/metar-updates-environment");
+ fgUntie("/environment/params/metar-updates-winds-aloft");
}
// use a "command" to set station temp at station elevation
FGMetarCtrl::init ()
{
first_update = true;
+ wind_interpolation_required = true;
}
void
init();
}
+static inline double convert_to_360( double d )
+{
+ if( d < 0.0 ) return d + 360.0;
+ if( d >= 360.0 ) return d - 360.0;
+ return d;
+}
+
+static inline double convert_to_180( double d )
+{
+ return d > 180.0 ? d - 360.0 : d;
+}
+
+// Return the sea level pressure for a metar observation, in inHg.
+// This is different from QNH because it accounts for the current
+// temperature at the observation point.
+// metarPressure in inHg
+// fieldHt in ft
+// fieldTemp in C
+
+static double reducePressureSl(double metarPressure, double fieldHt,
+ double fieldTemp)
+{
+ double elev = fieldHt * SG_FEET_TO_METER;
+ double fieldPressure
+ = FGAtmo::fieldPressure(elev, metarPressure * atmodel::inHg);
+ double slPressure = P_layer(0, elev, fieldPressure,
+ fieldTemp + atmodel::freezing,
+ atmodel::ISA::lam0);
+ return slPressure / atmodel::inHg;
+}
+
void
FGMetarCtrl::update(double dt)
{
if( dt <= 0 || !metar_valid ||!enabled)
return;
+ windModulator->update(dt);
// Interpolate the current configuration closer to the actual METAR
bool reinit_required = false;
bool layer_rebuild_required = false;
+ double station_elevation_ft = station_elevation_n->getDoubleValue();
if (first_update) {
- double dir = base_wind_dir_n->getDoubleValue();
+ double dir = base_wind_dir_n->getDoubleValue()+magnetic_variation_n->getDoubleValue();
double speed = base_wind_speed_n->getDoubleValue();
double gust = gust_wind_speed_n->getDoubleValue();
setupWind(setup_winds_aloft, dir, speed, gust);
fgDefaultWeatherValue("visibility-m", metarvis);
double metarpressure = pressure_n->getDoubleValue();
- fgDefaultWeatherValue("pressure-sea-level-inhg", metarpressure);
+ fgDefaultWeatherValue("pressure-sea-level-inhg",
+ reducePressureSl(metarpressure,
+ station_elevation_ft,
+ temperature_n->getDoubleValue()));
// We haven't already loaded a METAR, so apply it immediately.
vector<SGPropertyNode_ptr> layers = clouds_n->getChildren("layer");
layer_rebuild_required = true;
} else {
- // 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 = base_wind_speed_n->getDoubleValue();
- double metar_heading = base_wind_dir_n->getDoubleValue();
-
- metar[0] = metar_speed * sin(metar_heading * SG_DEGREES_TO_RADIANS );
- metar[1] = metar_speed * cos(metar_heading * SG_DEGREES_TO_RADIANS);
-
- // Convert the current wind values and convert them into a vector
- double current[2];
- double speed = boundary_wind_speed_n->getDoubleValue();
- double dir_from = boundary_wind_from_heading_n->getDoubleValue();;
-
- current[0] = speed * sin(dir_from * SG_DEGREES_TO_RADIANS );
- current[1] = speed * cos(dir_from * SG_DEGREES_TO_RADIANS );
-
- // 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]);
-
- // only interpolate if we have a difference
- if (x + y > 0) {
- 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;
- } 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]) * SG_RADIANS_TO_DEGREES );
-
- // Normalize the direction.
- if (dir_from < 0.0)
- dir_from += 360.0;
-
- SG_LOG( SG_GENERAL, SG_DEBUG, "Wind : " << dir_from << "@" << speed);
+ if( wind_interpolation_required ) {
+ // 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 = base_wind_speed_n->getDoubleValue();
+ double metar_heading = base_wind_dir_n->getDoubleValue()+magnetic_variation_n->getDoubleValue();
+
+ metar[0] = metar_speed * sin(metar_heading * SG_DEGREES_TO_RADIANS );
+ metar[1] = metar_speed * cos(metar_heading * SG_DEGREES_TO_RADIANS);
+
+ // Convert the current wind values and convert them into a vector
+ double current[2];
+ double speed = boundary_wind_speed_n->getDoubleValue();
+ double dir_from = boundary_wind_from_heading_n->getDoubleValue();;
+
+ current[0] = speed * sin(dir_from * SG_DEGREES_TO_RADIANS );
+ current[1] = speed * cos(dir_from * SG_DEGREES_TO_RADIANS );
+
+ // 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]);
+
+ // only interpolate if we have a difference
+ if (x + y > 0.01 ) {
+ 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 ((fabs(current[0]) > 0.1) || (fabs(current[1]) > 0.1)) {
+ // 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]) * SG_RADIANS_TO_DEGREES );
+
+ // Normalize the direction.
+ if (dir_from < 0.0)
+ dir_from += 360.0;
+
+ SG_LOG( SG_GENERAL, SG_DEBUG, "Wind : " << dir_from << "@" << speed);
+ } else {
+ // Special case where there is no wind (otherwise atan2 barfs)
+ speed = 0.0;
+ }
+ double gust = gust_wind_speed_n->getDoubleValue();
+ setupWind(setup_winds_aloft, dir_from, speed, gust);
+ reinit_required = true;
+ } else {
+ wind_interpolation_required = false;
+ }
+ } else { // if(wind_interpolation_required)
+ // interpolation of wind vector is finished, apply wind
+ // variations and gusts for the boundary layer only
+
+
+ bool wind_modulated = false;
+
+ // start with the main wind direction
+ double wind_dir = base_wind_dir_n->getDoubleValue()+magnetic_variation_n->getDoubleValue();
+ double min = convert_to_180(base_wind_range_from_n->getDoubleValue()+magnetic_variation_n->getDoubleValue());
+ double max = convert_to_180(base_wind_range_to_n->getDoubleValue()+magnetic_variation_n->getDoubleValue());
+ if( max > min ) {
+ // if variable winds configured, modulate the wind direction
+ double f = windModulator->get_direction_offset_norm();
+ wind_dir = min+(max-min)*f;
+ double old = convert_to_180(boundary_wind_from_heading_n->getDoubleValue());
+ wind_dir = convert_to_360(fgGetLowPass(old, wind_dir, dt ));
+ wind_modulated = true;
+ }
+
+ // start with main wind speed
+ double wind_speed = base_wind_speed_n->getDoubleValue();
+ max = gust_wind_speed_n->getDoubleValue();
+ if( max > wind_speed ) {
+ // if gusts are configured, modulate wind magnitude
+ double f = windModulator->get_magnitude_factor_norm();
+ wind_speed = wind_speed+(max-wind_speed)*f;
+ wind_speed = fgGetLowPass(boundary_wind_speed_n->getDoubleValue(), wind_speed, dt );
+ wind_modulated = true;
+ }
+ if( wind_modulated ) {
+ setupWind(false, wind_dir, wind_speed, max);
+ reinit_required = true;
}
- double gust = gust_wind_speed_n->getDoubleValue();
- setupWind(setup_winds_aloft, dir_from, speed, gust);
- reinit_required = true;
}
// Now handle the visibility. We convert both visibility values
double pressure = boundary_sea_level_pressure_n->getDoubleValue();
double metarpressure = pressure_n->getDoubleValue();
- if( pressure != metarpressure ) {
- pressure = interpolate_val( pressure, metarpressure, MaxPressureChangeInHgSec );
+ double newpressure = reducePressureSl(metarpressure,
+ station_elevation_ft,
+ temperature_n->getDoubleValue());
+ if( pressure != newpressure ) {
+ pressure = interpolate_val( pressure, newpressure, MaxPressureChangeInHgSec );
fgDefaultWeatherValue("pressure-sea-level-inhg", pressure);
reinit_required = true;
}
}
}
}
-
- set_temp_at_altitude(temperature_n->getDoubleValue(), station_elevation_ft);
- set_dewpoint_at_altitude(dewpoint_n->getDoubleValue(), station_elevation_ft);
+ set_temp_at_altitude(temperature_n->getDoubleValue(), station_elevation_ft);
+ set_dewpoint_at_altitude(dewpoint_n->getDoubleValue(), station_elevation_ft);
+ //TODO: check if temperature/dewpoint have changed. This requires reinit.
// Force an update of the 3D clouds
if( layer_rebuild_required )
return;
}
+ wind_interpolation_required = true;
+
min_visibility_n->setDoubleValue( m->getMinVisibility().getVisibility_m() );
max_visibility_n->setDoubleValue( m->getMaxVisibility().getVisibility_m() );
break;
}
- metar_fetcher->fetch( airport_id );
+ metar_fetcher->fetch( airport_id );
}
}
#endif
-FGMetarFetcher::FGMetarFetcher()
- :
+FGMetarFetcher::FGMetarFetcher() :
#if defined(ENABLE_THREADS)
metar_thread(NULL),
#endif
search_timer(0.0),
error_timer(0.0),
_stale_count(0),
- _error_count(0)
+ _error_count(0),
+ enabled(false)
{
longitude_n = fgGetNode( "/position/longitude-deg", true );
latitude_n = fgGetNode( "/position/latitude-deg", true );
- enable_n = fgGetNode( "/environment/params/real-world-weather-fetch", true );
+ enable_n = fgGetNode( "/environment/params/real-world-weather-fetch", true );
proxy_host_n = fgGetNode("/sim/presets/proxy/host", true);
proxy_port_n = fgGetNode("/sim/presets/proxy/port", true);
proxy_auth_n = fgGetNode("/sim/presets/proxy/authentication", true);
- max_age_n = fgGetNode("/environment/params/metar-max-age-min", true);
+ max_age_n = fgGetNode("/environment/params/metar-max-age-min", true);
output_n = fgGetNode("/environment/metar/data", true );
#if defined(ENABLE_THREADS)
_stale_count = 0;
_error_count = 0;
current_airport_id.clear();
+ /* Torsten Dreyer:
+ hack to stop startup.nas complaining if metar arrives after nasal-dir-initialized
+ is fired. Immediately fetch and wait for the METAR before continuing. This gets the
+ /environment/metar/xxx properties filled before nasal-dir is initialized.
+ Maybe the runway selection should happen here to make startup.nas obsolete?
+ */
+ const char * startup_airport = fgGetString("/sim/startup/options/airport");
+ if( *startup_airport ) {
+ FGAirport * a = FGAirport::getByIdent( startup_airport );
+ if( a ) {
+ SGGeod pos = SGGeod::fromDeg(a->getLongitude(), a->getLatitude());
+ a = FGAirport::findClosest(pos, 10000.0, &airportWithMetarFilter);
+ current_airport_id = a->getId();
+ fetch( current_airport_id );
+ }
+ }
}
void FGMetarFetcher::reinit ()
_error_count = 0;
}
- if( enable_n->getBoolValue() == false )
+ if( enable_n->getBoolValue() == false ) {
+ enabled = false;
return;
+ }
+
+ // we were just enabled, reset all timers to
+ // trigger immediate metar fetch
+ if( !enabled ) {
+ search_timer = 0.0;
+ fetch_timer = 0.0;
+ error_timer = error_timer_sec;
+ enabled = true;
+ }
FGAirport * a = NULL;
void FGMetarFetcher::fetch( const string & id )
{
+ if( enable_n->getBoolValue() == false )
+ return;
+
SGSharedPtr<FGMetar> result = NULL;
// fetch current metar data
}
} else {
_stale_count = 0;
- }
+ }
} catch (const sg_io_exception& e) {
SG_LOG( SG_GENERAL, SG_WARN, "Error fetching live weather data: " << e.getFormattedMessage().c_str() );
result = NULL;
+ // remove METAR flag from the airport
+ FGAirport * a = FGAirport::findByIdent( id );
+ if( a ) a->setMetar( false );
+ // immediately schedule a new search
+ search_timer = 0.0;
}
// write the metar to the property node, the rest is done by the methods tied to this property