1 // environment-mgr.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <simgear/constants.h>
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/scene/sky/sky.hxx>
30 #include <simgear/environment/visual_enviro.hxx>
31 #include <simgear/scene/model/particles.hxx>
33 #include <Main/main.hxx>
34 #include <Main/fg_props.hxx>
35 #include <Aircraft/aircraft.hxx>
36 #include <FDM/flight.hxx>
38 #include "environment.hxx"
39 #include "environment_ctrl.hxx"
40 #include "environment_mgr.hxx"
41 #include "fgclouds.hxx"
42 #include "precipitation_mgr.hxx"
49 FGEnvironmentMgr::FGEnvironmentMgr ()
50 : _environment(new FGEnvironment)
53 if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
54 _controller = new FGMetarEnvironmentCtrl;
56 _controller = new FGInterpolateEnvironmentCtrl;
58 _controller->setEnvironment(_environment);
59 set_subsystem("controller", _controller, 0.5);
60 fgClouds = new FGClouds( _controller );
61 _precipitationManager = new FGPrecipitationMgr;
62 set_subsystem("precipitation", _precipitationManager);
65 FGEnvironmentMgr::~FGEnvironmentMgr ()
68 remove_subsystem("controller");
69 remove_subsystem("precipitation");
72 delete _precipitationManager;
76 FGEnvironmentMgr::init ()
78 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
79 SGSubsystemGroup::init();
84 FGEnvironmentMgr::reinit ()
86 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
87 SGSubsystemGroup::reinit();
92 FGEnvironmentMgr::bind ()
94 fgTie("/environment/visibility-m", _environment,
95 &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
96 fgSetArchivable("/environment/visibility-m");
97 fgTie("/environment/effective-visibility-m", thesky,
98 &SGSky::get_visibility );
99 fgTie("/environment/temperature-sea-level-degc", _environment,
100 &FGEnvironment::get_temperature_sea_level_degc,
101 &FGEnvironment::set_temperature_sea_level_degc);
102 fgSetArchivable("/environment/temperature-sea-level-degc");
103 fgTie("/environment/temperature-degc", _environment,
104 &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
105 fgTie("/environment/temperature-degf", _environment,
106 &FGEnvironment::get_temperature_degf); // FIXME: read-only for now
107 fgTie("/environment/dewpoint-sea-level-degc", _environment,
108 &FGEnvironment::get_dewpoint_sea_level_degc,
109 &FGEnvironment::set_dewpoint_sea_level_degc);
110 fgSetArchivable("/environment/dewpoint-sea-level-degc");
111 fgTie("/environment/dewpoint-degc", _environment,
112 &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
113 fgTie("/environment/pressure-sea-level-inhg", _environment,
114 &FGEnvironment::get_pressure_sea_level_inhg,
115 &FGEnvironment::set_pressure_sea_level_inhg);
116 fgSetArchivable("/environment/pressure-sea-level-inhg");
117 fgTie("/environment/pressure-inhg", _environment,
118 &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
119 fgTie("/environment/density-slugft3", _environment,
120 &FGEnvironment::get_density_slugft3); // read-only
121 fgTie("/environment/relative-humidity", _environment,
122 &FGEnvironment::get_relative_humidity); //ro
123 fgTie("/environment/atmosphere/density-tropo-avg", _environment,
124 &FGEnvironment::get_density_tropo_avg_kgm3); //ro
125 fgTie("/environment/atmosphere/altitude-half-to-sun", _environment,
126 &FGEnvironment::get_altitude_half_to_sun_m,
127 &FGEnvironment::set_altitude_half_to_sun_m);
128 fgTie("/environment/atmosphere/altitude-troposphere-top", _environment,
129 &FGEnvironment::get_altitude_tropo_top_m,
130 &FGEnvironment::set_altitude_tropo_top_m);
131 fgTie("/environment/wind-from-heading-deg", _environment,
132 &FGEnvironment::get_wind_from_heading_deg,
133 &FGEnvironment::set_wind_from_heading_deg);
134 fgTie("/environment/wind-speed-kt", _environment,
135 &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
136 fgTie("/environment/wind-from-north-fps", _environment,
137 &FGEnvironment::get_wind_from_north_fps,
138 &FGEnvironment::set_wind_from_north_fps);
139 fgSetArchivable("/environment/wind-from-north-fps");
140 fgTie("/environment/wind-from-east-fps", _environment,
141 &FGEnvironment::get_wind_from_east_fps,
142 &FGEnvironment::set_wind_from_east_fps);
143 fgSetArchivable("/environment/wind-from-east-fps");
144 fgTie("/environment/wind-from-down-fps", _environment,
145 &FGEnvironment::get_wind_from_down_fps,
146 &FGEnvironment::set_wind_from_down_fps);
147 fgSetArchivable("/environment/wind-from-down-fps");
149 fgTie("/environment/thermal-lift-fps", _environment,
150 &FGEnvironment::get_thermal_lift_fps,
151 &FGEnvironment::set_thermal_lift_fps);
152 fgSetArchivable("/environment/thermal-lift-fps");
153 fgTie("/environment/ridge-lift-fps", _environment,
154 &FGEnvironment::get_ridge_lift_fps,
155 &FGEnvironment::set_ridge_lift_fps);
156 fgSetArchivable("/environment/ridge-lift-fps");
158 fgTie("/environment/turbulence/magnitude-norm", _environment,
159 &FGEnvironment::get_turbulence_magnitude_norm,
160 &FGEnvironment::set_turbulence_magnitude_norm);
161 fgSetArchivable("/environment/turbulence/magnitude-norm");
162 fgTie("/environment/turbulence/rate-hz", _environment,
163 &FGEnvironment::get_turbulence_rate_hz,
164 &FGEnvironment::set_turbulence_rate_hz);
165 fgSetArchivable("/environment/turbulence/rate-hz");
167 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
169 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
171 &FGEnvironmentMgr::get_cloud_layer_span_m,
172 &FGEnvironmentMgr::set_cloud_layer_span_m);
173 fgSetArchivable(buf);
174 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
176 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
177 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
178 fgSetArchivable(buf);
179 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
181 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
182 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
183 fgSetArchivable(buf);
184 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
186 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
187 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
188 fgSetArchivable(buf);
189 sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
191 &FGEnvironmentMgr::get_cloud_layer_coverage,
192 &FGEnvironmentMgr::set_cloud_layer_coverage);
193 fgSetArchivable(buf);
195 fgTie("/sim/rendering/clouds3d-enable", fgClouds,
196 &FGClouds::get_3dClouds,
197 &FGClouds::set_3dClouds);
198 fgTie("/sim/rendering/clouds3d-density", thesky,
199 &SGSky::get_3dCloudDensity,
200 &SGSky::set_3dCloudDensity);
201 fgTie("/sim/rendering/clouds3d-vis-range", thesky,
202 &SGSky::get_3dCloudVisRange,
203 &SGSky::set_3dCloudVisRange);
205 fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
206 &SGEnviro::get_precipitation_enable_state,
207 &SGEnviro::set_precipitation_enable_state);
208 fgTie("/environment/rebuild-layers", fgClouds,
209 &FGClouds::get_update_event,
210 &FGClouds::set_update_event);
211 fgTie("/environment/weather-scenario", fgClouds,
212 &FGClouds::get_scenario,
213 &FGClouds::set_scenario);
214 fgTie("/sim/rendering/lightning-enable", &sgEnviro,
215 &SGEnviro::get_lightning_enable_state,
216 &SGEnviro::set_lightning_enable_state);
217 fgTie("/environment/turbulence/use-cloud-turbulence", &sgEnviro,
218 &SGEnviro::get_turbulence_enable_state,
219 &SGEnviro::set_turbulence_enable_state);
220 sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
224 FGEnvironmentMgr::unbind ()
226 fgUntie("/environment/visibility-m");
227 fgUntie("/environment/effective-visibility-m");
228 fgUntie("/environment/temperature-sea-level-degc");
229 fgUntie("/environment/temperature-degc");
230 fgUntie("/environment/dewpoint-sea-level-degc");
231 fgUntie("/environment/dewpoint-degc");
232 fgUntie("/environment/pressure-sea-level-inhg");
233 fgUntie("/environment/pressure-inhg");
234 fgUntie("/environment/density-inhg");
235 fgUntie("/environment/relative-humidity");
236 fgUntie("/environment/atmosphere/density-tropo-avg");
237 fgUntie("/environment/wind-from-north-fps");
238 fgUntie("/environment/wind-from-east-fps");
239 fgUntie("/environment/wind-from-down-fps");
241 fgUntie("/environment/thermal-lift-fps");
242 fgUntie("/environment/ridge-lift-fps");
244 fgUntie("/environment/atmosphere/altitude-half-to-sun");
245 fgUntie("/environment/atmosphere/altitude-troposphere-top");
246 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
248 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
250 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
252 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
254 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
256 sprintf(buf, "/environment/clouds/layer[%d]/type", i);
259 fgUntie("/sim/rendering/clouds3d-enable");
260 fgUntie("/sim/rendering/clouds3d-vis-range");
261 fgUntie("/sim/rendering/clouds3d-density");
262 fgUntie("/sim/rendering/precipitation-enable");
263 fgUntie("/environment/rebuild-layers");
264 fgUntie("/environment/weather-scenario");
265 fgUntie("/sim/rendering/lightning-enable");
266 fgUntie("/environment/turbulence/use-cloud-turbulence");
270 FGEnvironmentMgr::update (double dt)
272 SGSubsystemGroup::update(dt);
274 // FIXME: the FDMs should update themselves
275 current_aircraft.fdm_state
276 ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
277 _environment->get_wind_from_east_fps(),
278 _environment->get_wind_from_down_fps());
279 _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
280 osg::Vec3 windVec(-_environment->get_wind_from_north_fps(),
281 -_environment->get_wind_from_east_fps(),
282 _environment->get_wind_from_down_fps());
283 simgear::Particles::setWindVector(windVec * SG_FEET_TO_METER);
289 FGEnvironmentMgr::getEnvironment () const
291 return *_environment;
295 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
297 // Always returns the same environment
298 // for now; we'll make it interesting
300 FGEnvironment env = *_environment;
301 env.set_elevation_ft(alt);
306 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
308 // Always returns the same environment
309 // for now; we'll make it interesting
311 FGEnvironment env = *_environment;
312 env.set_elevation_ft(aPos.getElevationFt());
318 FGEnvironmentMgr::_update_fdm () const
321 // Pass atmosphere on to FDM
322 // FIXME: have FDMs read properties directly.
324 if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
325 // convert from Rankine to Celsius
327 ->set_Static_temperature((9.0/5.0)
328 * (_environment->get_temperature_degc() + 273.15));
329 // convert from inHG to PSF
331 ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
332 // keep in slugs/ft^3
334 ->set_Density(_environment->get_density_slugft3());
339 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
341 return thesky->get_cloud_layer(index)->getSpan_m();
345 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
347 thesky->get_cloud_layer(index)->setSpan_m(span_m);
351 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
353 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
357 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
359 FGEnvironment env = *_environment;
360 env.set_elevation_ft(elevation_ft);
362 thesky->get_cloud_layer(index)
363 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
365 thesky->get_cloud_layer(index)
366 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
368 thesky->get_cloud_layer(index)
369 ->setDirection(env.get_wind_from_heading_deg());
373 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
375 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
379 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
381 thesky->get_cloud_layer(index)
382 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
386 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
388 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
392 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
393 double transition_ft)
395 thesky->get_cloud_layer(index)
396 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
400 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
402 switch (thesky->get_cloud_layer(index)->getCoverage()) {
403 case SGCloudLayer::SG_CLOUD_OVERCAST:
405 case SGCloudLayer::SG_CLOUD_BROKEN:
407 case SGCloudLayer::SG_CLOUD_SCATTERED:
409 case SGCloudLayer::SG_CLOUD_FEW:
411 case SGCloudLayer::SG_CLOUD_CIRRUS:
413 case SGCloudLayer::SG_CLOUD_CLEAR:
421 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
422 const char * coverage_name)
424 SGCloudLayer::Coverage coverage;
425 if (!strcmp(coverage_name, "overcast"))
426 coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
427 else if (!strcmp(coverage_name, "broken"))
428 coverage = SGCloudLayer::SG_CLOUD_BROKEN;
429 else if (!strcmp(coverage_name, "scattered"))
430 coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
431 else if (!strcmp(coverage_name, "few"))
432 coverage = SGCloudLayer::SG_CLOUD_FEW;
433 else if (!strcmp(coverage_name, "cirrus"))
434 coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
435 else if (!strcmp(coverage_name, "clear"))
436 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
438 SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
439 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
441 thesky->get_cloud_layer(index)->setCoverage(coverage);
446 // end of environment-mgr.cxx