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( fgGetString("/sim/presets/airport-id") ),
323 update_interval_sec( 60.0 ),
328 FGMetarEnvironmentCtrl::~FGMetarEnvironmentCtrl ()
335 // use a "command" to set station temp at station elevation
336 static void set_temp_at_altitude( float temp_degc, float altitude_ft ) {
338 SGPropertyNode *node = args.getNode("temp-degc", 0, true);
339 node->setFloatValue( temp_degc );
340 node = args.getNode("altitude-ft", 0, true);
341 node->setFloatValue( altitude_ft );
342 globals->get_commands()->execute("set-outside-air-temp-degc", &args);
346 static void set_dewpoint_at_altitude( float dewpoint_degc, float altitude_ft ) {
348 SGPropertyNode *node = args.getNode("dewpoint-degc", 0, true);
349 node->setFloatValue( dewpoint_degc );
350 node = args.getNode("altitude-ft", 0, true);
351 node->setFloatValue( altitude_ft );
352 globals->get_commands()->execute("set-dewpoint-temp-degc", &args);
357 FGMetarEnvironmentCtrl::update_env_config ()
359 fgSetupWind( fgGetDouble("/environment/metar/base-wind-range-from"),
360 fgGetDouble("/environment/metar/base-wind-range-to"),
361 fgGetDouble("/environment/metar/base-wind-speed-kt"),
362 fgGetDouble("/environment/metar/gust-wind-speed-kt") );
364 fgDefaultWeatherValue( "visibility-m",
365 fgGetDouble("/environment/metar/min-visibility-m") );
366 set_temp_at_altitude( fgGetDouble("/environment/metar/temperature-degc"),
367 station_elevation_ft );
368 set_dewpoint_at_altitude( fgGetDouble("/environment/metar/dewpoint-degc"),
369 station_elevation_ft );
370 fgDefaultWeatherValue( "pressure-sea-level-inhg",
371 fgGetDouble("/environment/metar/pressure-inhg") );
375 FGMetarEnvironmentCtrl::init ()
377 const SGPropertyNode *longitude
378 = fgGetNode( "/position/longitude-deg", true );
379 const SGPropertyNode *latitude
380 = fgGetNode( "/position/latitude-deg", true );
382 bool found_metar = false;
383 while ( !found_metar ) {
384 FGAirport a = globals->get_airports()
385 ->search( longitude->getDoubleValue(),
386 latitude->getDoubleValue(),
388 if ( fetch_data( a.id ) ) {
389 cout << "closest station w/ metar = " << a.id << endl;
396 // mark as no metar so it doesn't show up in subsequent
398 cout << "no metar at metar = " << a.id << endl;
399 globals->get_airports()->no_metar( a.id );
405 FGMetarEnvironmentCtrl::reinit ()
415 FGMetarEnvironmentCtrl::update(double delta_time_sec)
417 const SGPropertyNode *longitude
418 = fgGetNode( "/position/longitude-deg", true );
419 const SGPropertyNode *latitude
420 = fgGetNode( "/position/latitude-deg", true );
421 elapsed += delta_time_sec;
422 if ( elapsed > update_interval_sec ) {
423 FGAirport a = globals->get_airports()
424 ->search( longitude->getDoubleValue(),
425 latitude->getDoubleValue(),
427 if ( fetch_data( a.id ) ) {
428 cout << "closest station w/ metar = " << a.id << endl;
434 // mark as no metar so it doesn't show up in subsequent
436 cout << "no metar at metar = " << a.id << endl;
437 globals->get_airports()->no_metar( a.id );
440 env->update(delta_time_sec);
444 FGMetarEnvironmentCtrl::setEnvironment (FGEnvironment * environment)
446 env->setEnvironment(environment);
450 FGMetarEnvironmentCtrl::fetch_data (const string &icao)
456 if ((icao == "") && (_icao == "")) {
457 _icao = fgGetString("/sim/presets/airport-id");
459 } else if (icao != "") {
463 // fetch station elevation if exists
464 FGAirport a = globals->get_airports()->search( _icao );
465 station_elevation_ft = a.elevation;
467 // fetch current metar data
470 m = new SGMetar( _icao.c_str() );
471 } catch (const sg_io_exception& e) {
472 SG_LOG( SG_GENERAL, SG_WARN, "Error fetching live weather data: "
473 << e.getFormattedMessage().c_str() );
477 d = m->getMinVisibility().getVisibility_m();
478 d = (d != SGMetarNaN) ? d : 10000;
479 fgSetDouble("/environment/metar/min-visibility-m", d);
481 dt = m->getMaxVisibility().getVisibility_m();
482 d = (dt != SGMetarNaN) ? dt : d;
483 fgSetDouble("/environment/metar/max-visibility-m", d);
485 SGMetarVisibility *dirvis = m->getDirVisibility();
486 for (i = 0; i < 8; i++, dirvis++) {
487 const char *min = "/environment/metar/visibility[%d]/min-m";
488 const char *max = "/environment/metar/visibility[%d]/max-m";
491 d = dirvis->getVisibility_m();
492 d = (d != SGMetarNaN) ? d : 10000;
494 snprintf(s, 128, min, i);
496 snprintf(s, 128, max, i);
502 fgSetInt("/environment/metar/base-wind-range-from",
503 m->getWindRangeFrom() );
504 fgSetInt("/environment/metar/base-wind-range-to",
505 m->getWindRangeTo() );
507 fgSetInt("/environment/metar/base-wind-range-from", i);
508 fgSetInt("/environment/metar/base-wind-range-to", i);
510 fgSetDouble("/environment/metar/base-wind-speed-kt",
511 m->getWindSpeed_kt() );
513 d = m->getGustSpeed_kt();
514 d = (d != SGMetarNaN) ? d : 0.0;
515 fgSetDouble("/environment/metar/gust-wind-speed-kt", d);
517 d = m->getTemperature_C();
518 if (d != SGMetarNaN) {
519 dt = m->getDewpoint_C();
520 dt = (dt != SGMetarNaN) ? dt : 0.0;
521 fgSetDouble("/environment/metar/dewpoint-degc", dt);
522 fgSetDouble("/environment/metar/rel-humidity-norm",
523 m->getRelHumidity() );
525 d = (d != SGMetarNaN) ? d : 15.0;
526 fgSetDouble("/environment/metar/temperature-degc", d);
528 d = m->getPressure_inHg();
529 d = (d != SGMetarNaN) ? d : 30.0;
530 fgSetDouble("/environment/metar/pressure-inhg", d);
532 vector<SGMetarCloud> cv = m->getClouds();
533 vector<SGMetarCloud>::iterator cloud;
535 const char *cl = "/environment/clouds/layer[%i]";
536 for (i = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, i++) {
537 const char *coverage_string[5] =
538 { "clear", "few", "scattered", "broken", "overcast" };
539 const double thickness[5] = { 0, 65, 600,750, 1000};
542 snprintf(s, 128, cl, i);
543 strncat(s, "/coverage", 128);
544 q = cloud->getCoverage();
545 q = (q != -1 ) ? q : 0;
546 fgSetString(s, coverage_string[q] );
548 snprintf(s, 128, cl, i);
549 strncat(s, "/elevation-ft", 128);
550 d = cloud->getAltitude_ft();
551 d = (d != SGMetarNaN) ? d : -9999;
552 fgSetDouble(s, d + station_elevation_ft);
554 snprintf(s, 128, cl, i);
555 strncat(s, "/thickness-ft", 128);
556 fgSetDouble(s, thickness[q]);
558 snprintf(s, 128, cl, i);
559 strncat(s, "/span-m", 128);
560 fgSetDouble(s, 40000.0);
562 for (; i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
563 snprintf(s, 128, cl, i);
564 strncat(s, "/coverage", 128);
565 fgSetString(s, "clear");
567 snprintf(s, 128, cl, i);
568 strncat(s, "/elevation-ft", 128);
569 fgSetDouble(s, -9999);
571 snprintf(s, 128, cl, i);
572 strncat(s, "/thickness-ft", 128);
575 snprintf(s, 128, cl, i);
576 strncat(s, "/span-m", 128);
577 fgSetDouble(s, 40000.0);
586 // end of environment_ctrl.cxx