1 // environment_ctrl.cxx -- manager for natural environment information.
3 // Written by David Megginson, started February 2002.
5 // Copyright (C) 2002 David Megginson - david@megginson.com
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <simgear/debug/logstream.hxx>
28 #include <simgear/structure/commands.hxx>
29 #include <simgear/structure/exception.hxx>
31 #include <Airports/simple.hxx>
32 #include <Main/fg_props.hxx>
33 #include <Main/util.hxx>
35 #include "environment_mgr.hxx"
36 #include "environment_ctrl.hxx"
40 // FIXME, from options.cxx
41 extern void fgSetupWind (double min_hdg, double max_hdg, double speed, double gust);
45 ////////////////////////////////////////////////////////////////////////
46 // Implementation of FGEnvironmentCtrl abstract base class.
47 ////////////////////////////////////////////////////////////////////////
49 FGEnvironmentCtrl::FGEnvironmentCtrl ()
57 FGEnvironmentCtrl::~FGEnvironmentCtrl ()
62 FGEnvironmentCtrl::setEnvironment (FGEnvironment * environment)
64 _environment = environment;
68 FGEnvironmentCtrl::setLongitudeDeg (double lon_deg)
74 FGEnvironmentCtrl::setLatitudeDeg (double lat_deg)
80 FGEnvironmentCtrl::setElevationFt (double elev_ft)
86 FGEnvironmentCtrl::setPosition (double lon_deg, double lat_deg, double elev_ft)
95 ////////////////////////////////////////////////////////////////////////
96 // Implementation of FGUserDefEnvironmentCtrl.
97 ////////////////////////////////////////////////////////////////////////
99 FGUserDefEnvironmentCtrl::FGUserDefEnvironmentCtrl ()
100 : _base_wind_speed_node(0),
101 _gust_wind_speed_node(0),
102 _current_wind_speed_kt(0),
103 _delta_wind_speed_kt(0)
107 FGUserDefEnvironmentCtrl::~FGUserDefEnvironmentCtrl ()
112 FGUserDefEnvironmentCtrl::init ()
114 // Fill in some defaults.
115 if (!fgHasNode("/environment/params/base-wind-speed-kt"))
116 fgSetDouble("/environment/params/base-wind-speed-kt",
117 fgGetDouble("/environment/wind-speed-kt"));
118 if (!fgHasNode("/environment/params/gust-wind-speed-kt"))
119 fgSetDouble("/environment/params/gust-wind-speed-kt",
120 fgGetDouble("/environment/params/base-wind-speed-kt"));
122 _base_wind_speed_node =
123 fgGetNode("/environment/params/base-wind-speed-kt", true);
124 _gust_wind_speed_node =
125 fgGetNode("/environment/params/gust-wind-speed-kt", true);
127 _current_wind_speed_kt = _base_wind_speed_node->getDoubleValue();
128 _delta_wind_speed_kt = 0.1;
132 FGUserDefEnvironmentCtrl::update (double dt)
134 double base_wind_speed = _base_wind_speed_node->getDoubleValue();
135 double gust_wind_speed = _gust_wind_speed_node->getDoubleValue();
137 if (gust_wind_speed < base_wind_speed) {
138 gust_wind_speed = base_wind_speed;
139 _gust_wind_speed_node->setDoubleValue(gust_wind_speed);
142 if (base_wind_speed == gust_wind_speed) {
143 _current_wind_speed_kt = base_wind_speed;
145 int rn = rand() % 128;
146 int sign = (_delta_wind_speed_kt < 0 ? -1 : 1);
147 double gust = _current_wind_speed_kt - base_wind_speed;
148 double incr = gust / 50;
151 _delta_wind_speed_kt = - _delta_wind_speed_kt;
153 _delta_wind_speed_kt -= incr * sign;
155 _delta_wind_speed_kt += incr * sign;
157 _current_wind_speed_kt += _delta_wind_speed_kt;
159 if (_current_wind_speed_kt < base_wind_speed) {
160 _current_wind_speed_kt = base_wind_speed;
161 _delta_wind_speed_kt = 0.01;
162 } else if (_current_wind_speed_kt > gust_wind_speed) {
163 _current_wind_speed_kt = gust_wind_speed;
164 _delta_wind_speed_kt = -0.01;
168 if (_environment != 0)
169 _environment->set_wind_speed_kt(_current_wind_speed_kt);
174 ////////////////////////////////////////////////////////////////////////
175 // Implementation of FGInterpolateEnvironmentCtrl.
176 ////////////////////////////////////////////////////////////////////////
178 FGInterpolateEnvironmentCtrl::FGInterpolateEnvironmentCtrl ()
182 FGInterpolateEnvironmentCtrl::~FGInterpolateEnvironmentCtrl ()
185 for (i = 0; i < _boundary_table.size(); i++)
186 delete _boundary_table[i];
187 for (i = 0; i < _aloft_table.size(); i++)
188 delete _aloft_table[i];
194 FGInterpolateEnvironmentCtrl::init ()
196 read_table(fgGetNode("/environment/config/boundary", true),
198 read_table(fgGetNode("/environment/config/aloft", true),
203 FGInterpolateEnvironmentCtrl::reinit ()
206 for (i = 0; i < _boundary_table.size(); i++)
207 delete _boundary_table[i];
208 for (i = 0; i < _aloft_table.size(); i++)
209 delete _aloft_table[i];
210 _boundary_table.clear();
211 _aloft_table.clear();
216 FGInterpolateEnvironmentCtrl::read_table (const SGPropertyNode * node,
217 vector<bucket *> &table)
219 for (int i = 0; i < node->nChildren(); i++) {
220 const SGPropertyNode * child = node->getChild(i);
221 if ( strcmp(child->getName(), "entry") == 0
222 && child->getStringValue("elevation-ft", "")[0] != '\0'
223 && ( child->getDoubleValue("elevation-ft") > 0.1 || i == 0 ) )
225 bucket * b = new bucket;
227 b->environment.copy(table[i-1]->environment);
228 b->environment.read(child);
229 b->altitude_ft = b->environment.get_elevation_ft();
233 sort(table.begin(), table.end());
237 FGInterpolateEnvironmentCtrl::update (double delta_time_sec)
240 double altitude_ft = fgGetDouble("/position/altitude-ft");
241 double altitude_agl_ft = fgGetDouble("/position/altitude-agl-ft");
242 double boundary_transition =
243 fgGetDouble("/environment/config/boundary-transition-ft", 500);
245 // double ground_elevation_ft = altitude_ft - altitude_agl_ft;
247 int length = _boundary_table.size();
251 double boundary_limit = _boundary_table[length-1]->altitude_ft;
252 if (boundary_limit >= altitude_agl_ft) {
253 do_interpolate(_boundary_table, altitude_agl_ft,
256 } else if ((boundary_limit + boundary_transition) >= altitude_agl_ft) {
258 do_interpolate(_boundary_table, altitude_agl_ft, &env1);
259 do_interpolate(_aloft_table, altitude_ft, &env2);
261 (altitude_agl_ft - boundary_limit) / boundary_transition;
262 interpolate(&env1, &env2, fraction, _environment);
268 do_interpolate(_aloft_table, altitude_ft, _environment);
272 FGInterpolateEnvironmentCtrl::do_interpolate (vector<bucket *> &table,
274 FGEnvironment * environment)
276 int length = table.size();
280 // Boundary conditions
281 if ((length == 1) || (table[0]->altitude_ft >= altitude_ft)) {
282 environment->copy(table[0]->environment);
284 } else if (table[length-1]->altitude_ft <= altitude_ft) {
285 environment->copy(table[length-1]->environment);
289 // Search the interpolation table
290 for (int i = 0; i < length - 1; i++) {
291 if ((i == length - 1) || (table[i]->altitude_ft <= altitude_ft)) {
292 FGEnvironment * env1 = &(table[i]->environment);
293 FGEnvironment * env2 = &(table[i+1]->environment);
295 if (table[i]->altitude_ft == table[i+1]->altitude_ft)
299 ((altitude_ft - table[i]->altitude_ft) /
300 (table[i+1]->altitude_ft - table[i]->altitude_ft));
301 interpolate(env1, env2, fraction, environment);
309 FGInterpolateEnvironmentCtrl::bucket::operator< (const bucket &b) const
311 return (altitude_ft < b.altitude_ft);
316 ////////////////////////////////////////////////////////////////////////
317 // Implementation of FGMetarEnvironmentCtrl.
318 ////////////////////////////////////////////////////////////////////////
320 FGMetarEnvironmentCtrl::FGMetarEnvironmentCtrl ()
321 : env( new FGInterpolateEnvironmentCtrl ),
322 _icao( strdup( fgGetString("/sim/presets/airport-id") ) )
326 FGMetarEnvironmentCtrl::~FGMetarEnvironmentCtrl ()
338 // use a "command" to set station temp at station elevation
339 static void set_temp_at_altitude( float temp_degc, float altitude_ft ) {
341 SGPropertyNode *node = args.getNode("temp-degc", 0, true);
342 node->setFloatValue( temp_degc );
343 node = args.getNode("altitude-ft", 0, true);
344 node->setFloatValue( altitude_ft );
345 globals->get_commands()->execute("set-outside-air-temp-degc", &args);
349 static void set_dewpoint_at_altitude( float dewpoint_degc, float altitude_ft ) {
351 SGPropertyNode *node = args.getNode("dewpoint-degc", 0, true);
352 node->setFloatValue( dewpoint_degc );
353 node = args.getNode("altitude-ft", 0, true);
354 node->setFloatValue( altitude_ft );
355 globals->get_commands()->execute("set-dewpoint-temp-degc", &args);
360 FGMetarEnvironmentCtrl::init ()
368 fgSetupWind( fgGetDouble("/environment/metar/base-wind-range-from"),
369 fgGetDouble("/environment/metar/base-wind-range-to"),
370 fgGetDouble("/environment/metar/base-wind-speed-kt"),
371 fgGetDouble("/environment/metar/gust-wind-speed-kt") );
373 fgDefaultWeatherValue( "visibility-m",
374 fgGetDouble("/environment/metar/min-visibility-m") );
375 set_temp_at_altitude( fgGetDouble("/environment/metar/temperature-degc"),
376 station_elevation_ft );
377 set_dewpoint_at_altitude( fgGetDouble("/environment/metar/dewpoint-degc"),
378 station_elevation_ft );
379 fgDefaultWeatherValue( "pressure-sea-level-inhg",
380 fgGetDouble("/environment/metar/pressure-inhg") );
386 FGMetarEnvironmentCtrl::reinit ()
395 fgSetupWind( fgGetDouble("/environment/metar/base-wind-range-from"),
396 fgGetDouble("/environment/metar/base-wind-range-to"),
397 fgGetDouble("/environment/metar/base-wind-speed-kt"),
398 fgGetDouble("/environment/metar/gust-wind-speed-kt") );
400 fgDefaultWeatherValue( "visibility-m",
401 fgGetDouble("/environment/metar/min-visibility-m") );
402 set_temp_at_altitude( fgGetDouble("/environment/metar/temperature-degc"),
403 station_elevation_ft );
404 set_dewpoint_at_altitude( fgGetDouble("/environment/metar/dewpoint-degc"),
405 station_elevation_ft );
406 fgDefaultWeatherValue( "pressure-sea-level-inhg",
407 fgGetDouble("/environment/metar/pressure-inhg") );
414 FGMetarEnvironmentCtrl::update(double delta_time_sec)
416 env->update(delta_time_sec);
420 FGMetarEnvironmentCtrl::setEnvironment (FGEnvironment * environment)
422 env->setEnvironment(environment);
426 FGMetarEnvironmentCtrl::fetch_data (const char *icao)
432 if ((icao == NULL) && (_icao == NULL)) {
433 _icao = strdup( fgGetString("/sim/presets/airport-id") );
435 } else if (icao != NULL) {
439 _icao = strdup(icao);
442 // fetch station elevation if exists
443 FGAirport a = globals->get_airports()->search( _icao );
444 station_elevation_ft = a.elevation;
446 // fetch current metar data
449 m = new SGMetar(_icao);
450 } catch (const sg_io_exception& e) {
451 SG_LOG( SG_GENERAL, SG_WARN, "Error fetching live weather data: "
452 << e.getFormattedMessage().c_str() );
456 d = m->getMinVisibility().getVisibility_m();
457 d = (d != SGMetarNaN) ? d : 10000;
458 fgSetDouble("/environment/metar/min-visibility-m", d);
460 dt = m->getMaxVisibility().getVisibility_m();
461 d = (dt != SGMetarNaN) ? dt : d;
462 fgSetDouble("/environment/metar/max-visibility-m", d);
464 SGMetarVisibility *dirvis = m->getDirVisibility();
465 for (i = 0; i < 8; i++, dirvis++) {
466 const char *min = "/environment/metar/visibility[%d]/min-m";
467 const char *max = "/environment/metar/visibility[%d]/max-m";
470 d = dirvis->getVisibility_m();
471 d = (d != SGMetarNaN) ? d : 10000;
473 snprintf(s, 128, min, i);
475 snprintf(s, 128, max, i);
481 fgSetInt("/environment/metar/base-wind-range-from",
482 m->getWindRangeFrom() );
483 fgSetInt("/environment/metar/base-wind-range-to",
484 m->getWindRangeTo() );
486 fgSetInt("/environment/metar/base-wind-range-from", i);
487 fgSetInt("/environment/metar/base-wind-range-to", i);
489 fgSetDouble("/environment/metar/base-wind-speed-kt",
490 m->getWindSpeed_kt() );
492 d = m->getGustSpeed_kt();
493 d = (d != SGMetarNaN) ? d : 0.0;
494 fgSetDouble("/environment/metar/gust-wind-speed-kt", d);
496 d = m->getTemperature_C();
497 if (d != SGMetarNaN) {
498 dt = m->getDewpoint_C();
499 dt = (dt != SGMetarNaN) ? dt : 0.0;
500 fgSetDouble("/environment/metar/dewpoint-degc", dt);
501 fgSetDouble("/environment/metar/rel-humidity-norm",
502 m->getRelHumidity() );
504 d = (d != SGMetarNaN) ? d : 15.0;
505 fgSetDouble("/environment/metar/temperature-degc", d);
507 d = m->getPressure_inHg();
508 d = (d != SGMetarNaN) ? d : 30.0;
509 fgSetDouble("/environment/metar/pressure-inhg", d);
511 vector<SGMetarCloud> cv = m->getClouds();
512 vector<SGMetarCloud>::iterator cloud;
514 const char *cl = "/environment/clouds/layer[%i]";
515 for (i = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, i++) {
516 const char *coverage_string[5] =
517 { "clear", "few", "scattered", "broken", "overcast" };
518 const double thickness[5] = { 0, 65, 600,750, 1000};
521 snprintf(s, 128, cl, i);
522 strncat(s, "/coverage", 128);
523 q = cloud->getCoverage();
524 q = (q != -1 ) ? q : 0;
525 fgSetString(s, coverage_string[q] );
527 snprintf(s, 128, cl, i);
528 strncat(s, "/elevation-ft", 128);
529 d = cloud->getAltitude_ft();
530 d = (d != SGMetarNaN) ? d : -9999;
531 fgSetDouble(s, d + station_elevation_ft);
533 snprintf(s, 128, cl, i);
534 strncat(s, "/thickness-ft", 128);
535 fgSetDouble(s, thickness[q]);
537 snprintf(s, 128, cl, i);
538 strncat(s, "/span-m", 128);
539 fgSetDouble(s, 40000.0);
541 for (; i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
542 snprintf(s, 128, cl, i);
543 strncat(s, "/coverage", 128);
544 fgSetString(s, "clear");
546 snprintf(s, 128, cl, i);
547 strncat(s, "/elevation-ft", 128);
548 fgSetDouble(s, -9999);
550 snprintf(s, 128, cl, i);
551 strncat(s, "/thickness-ft", 128);
554 snprintf(s, 128, cl, i);
555 strncat(s, "/span-m", 128);
556 fgSetDouble(s, 40000.0);
563 // end of environment_ctrl.cxx