]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_mgr.cxx
new command line option --metar="some metar"
[flightgear.git] / src / Environment / environment_mgr.cxx
1 // environment-mgr.cxx -- manager for natural environment information.
2 //
3 // Written by David Megginson, started February 2002.
4 //
5 // Copyright (C) 2002  David Megginson - david@megginson.com
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
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>
32
33 #include <Main/main.hxx>
34 #include <Main/fg_props.hxx>
35 #include <Aircraft/aircraft.hxx>
36 #include <FDM/flight.hxx>
37
38 #include "environment.hxx"
39 #include "environment_mgr.hxx"
40 #include "environment_ctrl.hxx"
41 #include "fgclouds.hxx"
42 #include "precipitation_mgr.hxx"
43
44 class SGSky;
45 extern SGSky *thesky;
46
47
48
49 FGEnvironmentMgr::FGEnvironmentMgr ()
50   : _environment(new FGEnvironment)
51 {
52
53   _controller = new FGInterpolateEnvironmentCtrl;
54   _controller->setEnvironment(_environment);
55   set_subsystem("controller", _controller, 0.5);
56
57   fgClouds = new FGClouds();
58
59   _metarcontroller = new FGMetarCtrl(_controller);
60   set_subsystem("metarcontroller", _metarcontroller, 0.25 );
61
62   _metarfetcher = new FGMetarFetcher();
63   set_subsystem("metarfetcher", _metarfetcher, 1.0 );
64
65   _precipitationManager = new FGPrecipitationMgr;
66   set_subsystem("precipitation", _precipitationManager);
67 }
68
69 FGEnvironmentMgr::~FGEnvironmentMgr ()
70 {
71   remove_subsystem("precipitation");
72   delete _precipitationManager;
73
74   remove_subsystem("metarcontroller");
75   delete _metarfetcher;
76
77   remove_subsystem("metarfetcher");
78   delete _metarcontroller;
79
80   delete fgClouds;
81
82   remove_subsystem("controller");
83   delete _controller;
84
85   delete _environment;
86 }
87
88 void
89 FGEnvironmentMgr::init ()
90 {
91   SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
92   SGSubsystemGroup::init();
93   _update_fdm();
94 }
95
96 void
97 FGEnvironmentMgr::reinit ()
98 {
99   SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
100   SGSubsystemGroup::reinit();
101   _update_fdm();
102 }
103
104 void
105 FGEnvironmentMgr::bind ()
106 {
107   SGSubsystemGroup::bind();
108   fgTie("/environment/visibility-m", _environment,
109         &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
110   fgSetArchivable("/environment/visibility-m");
111   fgTie("/environment/effective-visibility-m", thesky,
112        &SGSky::get_visibility );
113   fgTie("/environment/temperature-sea-level-degc", _environment,
114         &FGEnvironment::get_temperature_sea_level_degc,
115         &FGEnvironment::set_temperature_sea_level_degc);
116   fgSetArchivable("/environment/temperature-sea-level-degc");
117   fgTie("/environment/temperature-degc", _environment,
118         &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
119   fgTie("/environment/temperature-degf", _environment,
120         &FGEnvironment::get_temperature_degf); // FIXME: read-only for now
121   fgTie("/environment/dewpoint-sea-level-degc", _environment,
122         &FGEnvironment::get_dewpoint_sea_level_degc,
123         &FGEnvironment::set_dewpoint_sea_level_degc);
124   fgSetArchivable("/environment/dewpoint-sea-level-degc");
125   fgTie("/environment/dewpoint-degc", _environment,
126         &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
127   fgTie("/environment/pressure-sea-level-inhg", _environment,
128         &FGEnvironment::get_pressure_sea_level_inhg,
129         &FGEnvironment::set_pressure_sea_level_inhg);
130   fgSetArchivable("/environment/pressure-sea-level-inhg");
131   fgTie("/environment/pressure-inhg", _environment,
132         &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
133   fgTie("/environment/density-slugft3", _environment,
134         &FGEnvironment::get_density_slugft3); // read-only
135   fgTie("/environment/relative-humidity", _environment,
136         &FGEnvironment::get_relative_humidity); //ro
137   fgTie("/environment/atmosphere/density-tropo-avg", _environment,
138         &FGEnvironment::get_density_tropo_avg_kgm3); //ro
139   fgTie("/environment/atmosphere/altitude-half-to-sun", _environment,
140         &FGEnvironment::get_altitude_half_to_sun_m, 
141         &FGEnvironment::set_altitude_half_to_sun_m);
142   fgTie("/environment/atmosphere/altitude-troposphere-top", _environment,
143         &FGEnvironment::get_altitude_tropo_top_m,
144         &FGEnvironment::set_altitude_tropo_top_m);
145   fgTie("/environment/wind-from-heading-deg", _environment,
146         &FGEnvironment::get_wind_from_heading_deg,
147         &FGEnvironment::set_wind_from_heading_deg);
148   fgTie("/environment/wind-speed-kt", _environment,
149         &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
150   fgTie("/environment/wind-from-north-fps", _environment,
151         &FGEnvironment::get_wind_from_north_fps,
152         &FGEnvironment::set_wind_from_north_fps);
153   fgSetArchivable("/environment/wind-from-north-fps");
154   fgTie("/environment/wind-from-east-fps", _environment,
155         &FGEnvironment::get_wind_from_east_fps,
156         &FGEnvironment::set_wind_from_east_fps);
157   fgSetArchivable("/environment/wind-from-east-fps");
158   fgTie("/environment/wind-from-down-fps", _environment,
159         &FGEnvironment::get_wind_from_down_fps,
160         &FGEnvironment::set_wind_from_down_fps);
161   fgSetArchivable("/environment/wind-from-down-fps");
162
163   fgTie("/environment/thermal-lift-fps", _environment,
164         &FGEnvironment::get_thermal_lift_fps,
165         &FGEnvironment::set_thermal_lift_fps);
166   fgSetArchivable("/environment/thermal-lift-fps");
167   fgTie("/environment/ridge-lift-fps", _environment,
168         &FGEnvironment::get_ridge_lift_fps,
169         &FGEnvironment::set_ridge_lift_fps);
170   fgSetArchivable("/environment/ridge-lift-fps");
171
172   fgTie("/environment/turbulence/magnitude-norm", _environment,
173         &FGEnvironment::get_turbulence_magnitude_norm,
174         &FGEnvironment::set_turbulence_magnitude_norm);
175   fgSetArchivable("/environment/turbulence/magnitude-norm");
176   fgTie("/environment/turbulence/rate-hz", _environment,
177         &FGEnvironment::get_turbulence_rate_hz,
178         &FGEnvironment::set_turbulence_rate_hz);
179   fgSetArchivable("/environment/turbulence/rate-hz");
180
181   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
182     char buf[128];
183     sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
184     fgTie(buf, this, i,
185           &FGEnvironmentMgr::get_cloud_layer_span_m,
186           &FGEnvironmentMgr::set_cloud_layer_span_m);
187     fgSetArchivable(buf);
188     sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
189     fgTie(buf, this, i,
190           &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
191           &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
192     fgSetArchivable(buf);
193     sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
194     fgTie(buf, this, i,
195           &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
196           &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
197     fgSetArchivable(buf);
198     sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
199     fgTie(buf, this, i,
200           &FGEnvironmentMgr::get_cloud_layer_transition_ft,
201           &FGEnvironmentMgr::set_cloud_layer_transition_ft);
202     fgSetArchivable(buf);
203     sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
204     fgTie(buf, this, i,
205           &FGEnvironmentMgr::get_cloud_layer_coverage,
206           &FGEnvironmentMgr::set_cloud_layer_coverage);
207     fgSetArchivable(buf);
208   }
209
210   fgTie("/environment/metar/data", _metarcontroller,
211           &FGMetarCtrl::get_metar, &FGMetarCtrl::set_metar );
212
213   fgTie("/sim/rendering/clouds3d-enable", fgClouds,
214           &FGClouds::get_3dClouds,
215           &FGClouds::set_3dClouds);
216   fgTie("/sim/rendering/clouds3d-density", thesky,
217           &SGSky::get_3dCloudDensity,
218           &SGSky::set_3dCloudDensity);
219   fgTie("/sim/rendering/clouds3d-vis-range", thesky,
220         &SGSky::get_3dCloudVisRange,
221         &SGSky::set_3dCloudVisRange);
222   
223   fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
224           &SGEnviro::get_precipitation_enable_state, 
225           &SGEnviro::set_precipitation_enable_state);
226   fgTie("/environment/rebuild-layers", fgClouds,
227       &FGClouds::get_update_event,
228       &FGClouds::set_update_event);
229   fgTie("/sim/rendering/lightning-enable", &sgEnviro,
230       &SGEnviro::get_lightning_enable_state,
231       &SGEnviro::set_lightning_enable_state);
232   fgTie("/environment/turbulence/use-cloud-turbulence", &sgEnviro,
233       &SGEnviro::get_turbulence_enable_state,
234       &SGEnviro::set_turbulence_enable_state);
235   sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
236 }
237
238 void
239 FGEnvironmentMgr::unbind ()
240 {
241   fgUntie("/environment/visibility-m");
242   fgUntie("/environment/effective-visibility-m");
243   fgUntie("/environment/temperature-sea-level-degc");
244   fgUntie("/environment/temperature-degc");
245   fgUntie("/environment/dewpoint-sea-level-degc");
246   fgUntie("/environment/dewpoint-degc");
247   fgUntie("/environment/pressure-sea-level-inhg");
248   fgUntie("/environment/pressure-inhg");
249   fgUntie("/environment/density-inhg");
250   fgUntie("/environment/relative-humidity");
251   fgUntie("/environment/atmosphere/density-tropo-avg");
252   fgUntie("/environment/wind-from-north-fps");
253   fgUntie("/environment/wind-from-east-fps");
254   fgUntie("/environment/wind-from-down-fps");
255
256   fgUntie("/environment/thermal-lift-fps");
257   fgUntie("/environment/ridge-lift-fps");
258
259   fgUntie("/environment/atmosphere/altitude-half-to-sun");
260   fgUntie("/environment/atmosphere/altitude-troposphere-top");
261   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
262     char buf[128];
263     sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
264     fgUntie(buf);
265     sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
266     fgUntie(buf);
267     sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
268     fgUntie(buf);
269     sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
270     fgUntie(buf);
271     sprintf(buf, "/environment/clouds/layer[%d]/type", i);
272     fgUntie(buf);
273   }
274   fgUntie("/sim/rendering/clouds3d-enable");
275   fgUntie("/sim/rendering/clouds3d-vis-range");
276   fgUntie("/sim/rendering/clouds3d-density");
277   fgUntie("/sim/rendering/precipitation-enable");
278   fgUntie("/environment/rebuild-layers");
279   fgUntie("/environment/weather-scenario");
280   fgUntie("/sim/rendering/lightning-enable");
281   fgUntie("/environment/turbulence/use-cloud-turbulence");
282   SGSubsystemGroup::unbind();
283 }
284
285 void
286 FGEnvironmentMgr::update (double dt)
287 {
288   SGSubsystemGroup::update(dt);
289
290                                 // FIXME: the FDMs should update themselves
291   current_aircraft.fdm_state
292     ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
293                                    _environment->get_wind_from_east_fps(),
294                                    _environment->get_wind_from_down_fps());
295   _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
296   osg::Vec3 windVec(-_environment->get_wind_from_north_fps(),
297                     -_environment->get_wind_from_east_fps(),
298                     _environment->get_wind_from_down_fps());
299   simgear::Particles::setWindVector(windVec * SG_FEET_TO_METER);
300
301   _update_fdm();
302 }
303
304 FGEnvironment
305 FGEnvironmentMgr::getEnvironment () const
306 {
307   return *_environment;
308 }
309
310 FGEnvironment
311 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
312 {
313                                 // Always returns the same environment
314                                 // for now; we'll make it interesting
315                                 // later.
316   FGEnvironment env = *_environment;
317   env.set_elevation_ft(alt);
318   return env;
319 }
320
321 FGEnvironment
322 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
323 {
324   // Always returns the same environment
325   // for now; we'll make it interesting
326   // later.
327   FGEnvironment env = *_environment;
328   env.set_elevation_ft(aPos.getElevationFt());
329   return env;
330
331 }
332
333 void
334 FGEnvironmentMgr::_update_fdm () const
335 {
336   //
337   // Pass atmosphere on to FDM
338   // FIXME: have FDMs read properties directly.
339   //
340   if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
341                                 // convert from Rankine to Celsius
342     cur_fdm_state
343       ->set_Static_temperature((9.0/5.0)
344                                * (_environment->get_temperature_degc() + 273.15));
345                                 // convert from inHG to PSF
346     cur_fdm_state
347       ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
348                                 // keep in slugs/ft^3
349     cur_fdm_state
350       ->set_Density(_environment->get_density_slugft3());
351   }
352 }
353
354 double
355 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
356 {
357   return thesky->get_cloud_layer(index)->getSpan_m();
358 }
359
360 void
361 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
362 {
363   thesky->get_cloud_layer(index)->setSpan_m(span_m);
364 }
365
366 double
367 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
368 {
369   return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
370 }
371
372 void
373 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
374 {
375   FGEnvironment env = *_environment;
376   env.set_elevation_ft(elevation_ft);
377
378   thesky->get_cloud_layer(index)
379     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
380
381   thesky->get_cloud_layer(index)
382     ->setSpeed(env.get_wind_speed_kt() * 0.5151);       // 1 kt = 0.5151 m/s
383
384   thesky->get_cloud_layer(index)
385     ->setDirection(env.get_wind_from_heading_deg());
386 }
387
388 double
389 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
390 {
391   return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
392 }
393
394 void
395 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
396 {
397   thesky->get_cloud_layer(index)
398     ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
399 }
400
401 double
402 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
403 {
404   return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
405 }
406
407 void
408 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
409                                                  double transition_ft)
410 {
411   thesky->get_cloud_layer(index)
412     ->setTransition_m(transition_ft * SG_FEET_TO_METER);
413 }
414
415 const char *
416 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
417 {
418   switch (thesky->get_cloud_layer(index)->getCoverage()) {
419   case SGCloudLayer::SG_CLOUD_OVERCAST:
420     return "overcast";
421   case SGCloudLayer::SG_CLOUD_BROKEN:
422     return "broken";
423   case SGCloudLayer::SG_CLOUD_SCATTERED:
424     return "scattered";
425   case SGCloudLayer::SG_CLOUD_FEW:
426     return "few";
427   case SGCloudLayer::SG_CLOUD_CIRRUS:
428     return "cirrus";
429   case SGCloudLayer::SG_CLOUD_CLEAR:
430     return "clear";
431   default:
432     return "unknown";
433   }
434 }
435
436 void
437 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
438                                             const char * coverage_name)
439 {
440   SGCloudLayer::Coverage coverage;
441   if (!strcmp(coverage_name, "overcast"))
442     coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
443   else if (!strcmp(coverage_name, "broken"))
444     coverage = SGCloudLayer::SG_CLOUD_BROKEN;
445   else if (!strcmp(coverage_name, "scattered"))
446     coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
447   else if (!strcmp(coverage_name, "few"))
448     coverage = SGCloudLayer::SG_CLOUD_FEW;
449   else if (!strcmp(coverage_name, "cirrus"))
450     coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
451   else if (!strcmp(coverage_name, "clear"))
452     coverage = SGCloudLayer::SG_CLOUD_CLEAR;
453   else {
454     SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
455     coverage = SGCloudLayer::SG_CLOUD_CLEAR;
456   }
457   thesky->get_cloud_layer(index)->setCoverage(coverage);
458 }
459
460
461
462 // end of environment-mgr.cxx