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/exception.hxx>
30 #include <Main/fg_props.hxx>
32 #include "environment_mgr.hxx"
33 #include "environment_ctrl.hxx"
37 // FIXME, from options.cxx
38 extern void fgSetupWind (double min_hdg, double max_hdg, double speed, double gust);
42 ////////////////////////////////////////////////////////////////////////
43 // Implementation of FGEnvironmentCtrl abstract base class.
44 ////////////////////////////////////////////////////////////////////////
46 FGEnvironmentCtrl::FGEnvironmentCtrl ()
54 FGEnvironmentCtrl::~FGEnvironmentCtrl ()
59 FGEnvironmentCtrl::setEnvironment (FGEnvironment * environment)
61 _environment = environment;
65 FGEnvironmentCtrl::setLongitudeDeg (double lon_deg)
71 FGEnvironmentCtrl::setLatitudeDeg (double lat_deg)
77 FGEnvironmentCtrl::setElevationFt (double elev_ft)
83 FGEnvironmentCtrl::setPosition (double lon_deg, double lat_deg, double elev_ft)
92 ////////////////////////////////////////////////////////////////////////
93 // Implementation of FGUserDefEnvironmentCtrl.
94 ////////////////////////////////////////////////////////////////////////
96 FGUserDefEnvironmentCtrl::FGUserDefEnvironmentCtrl ()
97 : _base_wind_speed_node(0),
98 _gust_wind_speed_node(0),
99 _current_wind_speed_kt(0),
100 _delta_wind_speed_kt(0)
104 FGUserDefEnvironmentCtrl::~FGUserDefEnvironmentCtrl ()
109 FGUserDefEnvironmentCtrl::init ()
111 // Fill in some defaults.
112 if (!fgHasNode("/environment/params/base-wind-speed-kt"))
113 fgSetDouble("/environment/params/base-wind-speed-kt",
114 fgGetDouble("/environment/wind-speed-kt"));
115 if (!fgHasNode("/environment/params/gust-wind-speed-kt"))
116 fgSetDouble("/environment/params/gust-wind-speed-kt",
117 fgGetDouble("/environment/params/base-wind-speed-kt"));
119 _base_wind_speed_node =
120 fgGetNode("/environment/params/base-wind-speed-kt", true);
121 _gust_wind_speed_node =
122 fgGetNode("/environment/params/gust-wind-speed-kt", true);
124 _current_wind_speed_kt = _base_wind_speed_node->getDoubleValue();
125 _delta_wind_speed_kt = 0.1;
129 FGUserDefEnvironmentCtrl::update (double dt)
131 double base_wind_speed = _base_wind_speed_node->getDoubleValue();
132 double gust_wind_speed = _gust_wind_speed_node->getDoubleValue();
134 if (gust_wind_speed < base_wind_speed) {
135 gust_wind_speed = base_wind_speed;
136 _gust_wind_speed_node->setDoubleValue(gust_wind_speed);
139 if (base_wind_speed == gust_wind_speed) {
140 _current_wind_speed_kt = base_wind_speed;
142 int rn = rand() % 128;
143 int sign = (_delta_wind_speed_kt < 0 ? -1 : 1);
144 double gust = _current_wind_speed_kt - base_wind_speed;
145 double incr = gust / 50;
148 _delta_wind_speed_kt = - _delta_wind_speed_kt;
150 _delta_wind_speed_kt -= incr * sign;
152 _delta_wind_speed_kt += incr * sign;
154 _current_wind_speed_kt += _delta_wind_speed_kt;
156 if (_current_wind_speed_kt < base_wind_speed) {
157 _current_wind_speed_kt = base_wind_speed;
158 _delta_wind_speed_kt = 0.01;
159 } else if (_current_wind_speed_kt > gust_wind_speed) {
160 _current_wind_speed_kt = gust_wind_speed;
161 _delta_wind_speed_kt = -0.01;
165 if (_environment != 0)
166 _environment->set_wind_speed_kt(_current_wind_speed_kt);
171 ////////////////////////////////////////////////////////////////////////
172 // Implementation of FGInterpolateEnvironmentCtrl.
173 ////////////////////////////////////////////////////////////////////////
175 FGInterpolateEnvironmentCtrl::FGInterpolateEnvironmentCtrl ()
179 FGInterpolateEnvironmentCtrl::~FGInterpolateEnvironmentCtrl ()
182 for (i = 0; i < _boundary_table.size(); i++)
183 delete _boundary_table[i];
184 for (i = 0; i < _aloft_table.size(); i++)
185 delete _aloft_table[i];
191 FGInterpolateEnvironmentCtrl::init ()
193 read_table(fgGetNode("/environment/config/boundary", true),
195 read_table(fgGetNode("/environment/config/aloft", true),
200 FGInterpolateEnvironmentCtrl::reinit ()
203 for (i = 0; i < _boundary_table.size(); i++)
204 delete _boundary_table[i];
205 for (i = 0; i < _aloft_table.size(); i++)
206 delete _aloft_table[i];
207 _boundary_table.clear();
208 _aloft_table.clear();
213 FGInterpolateEnvironmentCtrl::read_table (const SGPropertyNode * node,
214 vector<bucket *> &table)
216 for (int i = 0; i < node->nChildren(); i++) {
217 const SGPropertyNode * child = node->getChild(i);
218 if ( strcmp(child->getName(), "entry") == 0
219 && child->getStringValue("elevation-ft", "")[0] != '\0'
220 && ( child->getDoubleValue("elevation-ft") > 0.1 || i == 0 ) )
222 bucket * b = new bucket;
224 b->environment.copy(table[i-1]->environment);
225 b->environment.read(child);
226 b->altitude_ft = b->environment.get_elevation_ft();
230 sort(table.begin(), table.end());
234 FGInterpolateEnvironmentCtrl::update (double delta_time_sec)
237 double altitude_ft = fgGetDouble("/position/altitude-ft");
238 double altitude_agl_ft = fgGetDouble("/position/altitude-agl-ft");
239 double boundary_transition =
240 fgGetDouble("/environment/config/boundary-transition-ft", 500);
242 // double ground_elevation_ft = altitude_ft - altitude_agl_ft;
244 int length = _boundary_table.size();
248 double boundary_limit = _boundary_table[length-1]->altitude_ft;
249 if (boundary_limit >= altitude_agl_ft) {
250 do_interpolate(_boundary_table, altitude_agl_ft,
253 } else if ((boundary_limit + boundary_transition) >= altitude_agl_ft) {
255 do_interpolate(_boundary_table, altitude_agl_ft, &env1);
256 do_interpolate(_aloft_table, altitude_ft, &env2);
258 (altitude_agl_ft - boundary_limit) / boundary_transition;
259 interpolate(&env1, &env2, fraction, _environment);
265 do_interpolate(_aloft_table, altitude_ft, _environment);
269 FGInterpolateEnvironmentCtrl::do_interpolate (vector<bucket *> &table,
271 FGEnvironment * environment)
273 int length = table.size();
277 // Boundary conditions
278 if ((length == 1) || (table[0]->altitude_ft >= altitude_ft)) {
279 environment->copy(table[0]->environment);
281 } else if (table[length-1]->altitude_ft <= altitude_ft) {
282 environment->copy(table[length-1]->environment);
286 // Search the interpolation table
287 for (int i = 0; i < length - 1; i++) {
288 if ((i == length - 1) || (table[i]->altitude_ft <= altitude_ft)) {
289 FGEnvironment * env1 = &(table[i]->environment);
290 FGEnvironment * env2 = &(table[i+1]->environment);
292 if (table[i]->altitude_ft == table[i+1]->altitude_ft)
296 ((altitude_ft - table[i]->altitude_ft) /
297 (table[i+1]->altitude_ft - table[i]->altitude_ft));
298 interpolate(env1, env2, fraction, environment);
306 FGInterpolateEnvironmentCtrl::bucket::operator< (const bucket &b) const
308 return (altitude_ft < b.altitude_ft);
313 ////////////////////////////////////////////////////////////////////////
314 // Implementation of FGMetarEnvironmentCtrl.
315 ////////////////////////////////////////////////////////////////////////
317 FGMetarEnvironmentCtrl::FGMetarEnvironmentCtrl ()
318 : env( new FGInterpolateEnvironmentCtrl ),
319 _icao( strdup( fgGetString("/sim/presets/airport-id") ) )
323 FGMetarEnvironmentCtrl::~FGMetarEnvironmentCtrl ()
336 FGMetarEnvironmentCtrl::init ()
344 fgSetupWind( fgGetDouble("/environment/metar/base-wind-range-from"),
345 fgGetDouble("/environment/metar/base-wind-range-to"),
346 fgGetDouble("/environment/metar/base-wind-speed-kt"),
347 fgGetDouble("/environment/metar/gust-wind-speed-kt") );
349 fgSetDouble("/environment/visibility-m",
350 fgGetDouble("/environment/metar/min-visibility-m"));
351 fgSetDouble("/environment/temperature-degc",
352 fgGetDouble("/environment/metar/temperature-degc"));
353 fgSetDouble("/environment/dewpoint-degc",
354 fgGetDouble("/environment/metar/dewpoint-degc"));
355 fgSetDouble("/environment/pressure-inhg",
356 fgGetDouble("/environment/metar/pressure-inhg"));
362 FGMetarEnvironmentCtrl::reinit ()
370 fgSetupWind( fgGetDouble("/environment/metar/base-wind-range-from"),
371 fgGetDouble("/environment/metar/base-wind-range-to"),
372 fgGetDouble("/environment/metar/base-wind-speed-kt"),
373 fgGetDouble("/environment/metar/gust-wind-speed-kt") );
375 fgSetDouble("/environment/visibility-m",
376 fgGetDouble("/environment/metar/min-visibility-m"));
377 fgSetDouble("/environment/pressure-inhg",
378 fgGetDouble("/environment/metar/pressure-inhg"));
380 // FIXME: The following seem to egt overriden?
381 fgSetDouble("/environment/temperature-degc",
382 fgGetDouble("/environment/metar/temperature-degc"));
383 fgSetDouble("/environment/dewpoint-degc",
384 fgGetDouble("/environment/metar/dewpoint-degc"));
390 FGMetarEnvironmentCtrl::update(double delta_time_sec)
392 env->update(delta_time_sec);
396 FGMetarEnvironmentCtrl::setEnvironment (FGEnvironment * environment)
398 env->setEnvironment(environment);
402 FGMetarEnvironmentCtrl::fetch_data (const char *icao)
408 if ((icao == NULL) && (_icao == NULL)) {
409 _icao = strdup( fgGetString("/sim/presets/airport-id") );
411 } else if (icao != NULL) {
415 _icao = strdup(icao);
420 m = new SGMetar(_icao);
421 } catch (const sg_io_exception& e) {
422 SG_LOG( SG_GENERAL, SG_WARN, "Error fetching live weather data: "
423 << e.getFormattedMessage().c_str() );
427 d = m->getMinVisibility().getVisibility_m();
428 d = (d != SGMetarNaN) ? d : 10000;
429 fgSetDouble("/environment/metar/min-visibility-m", d);
431 dt = m->getMaxVisibility().getVisibility_m();
432 d = (dt != SGMetarNaN) ? dt : d;
433 fgSetDouble("/environment/metar/max-visibility-m", d);
435 SGMetarVisibility *dirvis = m->getDirVisibility();
436 for (i = 0; i < 8; i++, dirvis++) {
437 const char *min = "/environment/metar/visibility[%d]/min-m";
438 const char *max = "/environment/metar/visibility[%d]/max-m";
441 d = dirvis->getVisibility_m();
442 d = (d != SGMetarNaN) ? d : 10000;
444 snprintf(s, 128, min, i);
446 snprintf(s, 128, max, i);
452 fgSetInt("/environment/metar/base-wind-range-from",
453 m->getWindRangeFrom() );
454 fgSetInt("/environment/metar/base-wind-range-to",
455 m->getWindRangeTo() );
457 fgSetInt("/environment/metar/base-wind-range-from", i);
458 fgSetInt("/environment/metar/base-wind-range-to", i);
460 fgSetDouble("/environment/metar/base-wind-speed-kt",
461 m->getWindSpeed_kt() );
463 d = m->getGustSpeed_kt();
464 d = (d != SGMetarNaN) ? d : 0.0;
465 fgSetDouble("/environment/metar/gust-wind-speed-kt", d);
467 d = m->getTemperature_C();
468 if (d != SGMetarNaN) {
469 dt = m->getDewpoint_C();
470 dt = (dt != SGMetarNaN) ? dt : 0.0;
471 fgSetDouble("/environment/metar/dewpoint-degc", dt);
472 fgSetDouble("/environment/metar/rel-humidity-norm",
473 m->getRelHumidity() );
475 d = (d != SGMetarNaN) ? d : 15.0;
476 fgSetDouble("/environment/metar/temperature-degc", d);
478 d = m->getPressure_inHg();
479 d = (d != SGMetarNaN) ? d : 30.0;
480 fgSetDouble("/environment/metar/pressure-inhg", d);
482 vector<SGMetarCloud> cv = m->getClouds();
483 vector<SGMetarCloud>::iterator cloud;
485 const char *cl = "/environment/clouds/layer[%i]";
486 for (i = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, i++) {
487 const char *coverage_string[5] =
488 { "clear", "few", "scattered", "broken", "overcast" };
489 const double thickness[5] = { 0, 65, 600,750, 1000};
492 snprintf(s, 128, cl, i);
493 strncat(s, "/coverage", 128);
494 q = cloud->getCoverage();
495 q = (q != -1 ) ? q : 0;
496 fgSetString(s, coverage_string[q] );
498 snprintf(s, 128, cl, i);
499 strncat(s, "/elevation-ft", 128);
500 d = cloud->getAltitude_ft();
501 d = (d != SGMetarNaN) ? d : -9999;
504 snprintf(s, 128, cl, i);
505 strncat(s, "/thickness-ft", 128);
506 fgSetDouble(s, thickness[q]);
508 snprintf(s, 128, cl, i);
509 strncat(s, "/span-m", 128);
510 fgSetDouble(s, 40000.0);
512 for (; i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
513 snprintf(s, 128, cl, i);
514 strncat(s, "/coverage", 128);
515 fgSetString(s, "clear");
517 snprintf(s, 128, cl, i);
518 strncat(s, "/elevation-ft", 128);
519 fgSetDouble(s, -9999);
521 snprintf(s, 128, cl, i);
522 strncat(s, "/thickness-ft", 128);
525 snprintf(s, 128, cl, i);
526 strncat(s, "/span-m", 128);
527 fgSetDouble(s, 40000.0);
534 // end of environment_ctrl.cxx