]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment.cxx
Merge branch 'vivian/cullsetting'
[flightgear.git] / src / Environment / environment.cxx
1 // environment.cxx -- routines to model the natural environment
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
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <math.h>
29
30 #include <boost/tuple/tuple.hpp>
31
32 #include <simgear/constants.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/math/interpolater.hxx>
35 #include <simgear/props/props.hxx>
36 #include <simgear/environment/visual_enviro.hxx>
37
38 #include <Main/fg_props.hxx>
39 #include <signal.h>
40
41 #include "environment.hxx"
42 #include "atmosphere.hxx"
43
44 \f
45 ////////////////////////////////////////////////////////////////////////
46 // Atmosphere model.
47 ////////////////////////////////////////////////////////////////////////
48
49 #ifdef USING_TABLES
50
51 // Calculated based on the ISA standard day, as found at e.g.
52 // http://www.av8n.com/physics/altimetry.htm
53
54 // Each line of data has 3 elements:
55 //   Elevation (ft), 
56 //   temperature factor (dimensionless ratio of absolute temp), 
57 //   pressure factor (dimensionless ratio)
58 static double atmosphere_data[][3] = {
59  {  -3000.00,   1.021,  1.1133 },
60  {      0.00,   1.000,  1.0000 },
61  {   2952.76,   0.980,  0.8978 },
62  {   5905.51,   0.959,  0.8042 },
63  {   8858.27,   0.939,  0.7187 },
64  {  11811.02,   0.919,  0.6407 },
65  {  14763.78,   0.898,  0.5697 },
66  {  17716.54,   0.878,  0.5052 },
67  {  20669.29,   0.858,  0.4468 },
68  {  23622.05,   0.838,  0.3940 },
69  {  26574.80,   0.817,  0.3463 },
70  {  29527.56,   0.797,  0.3034 },
71  {  32480.31,   0.777,  0.2649 },
72  {  35433.07,   0.756,  0.2305 },
73  {  38385.83,   0.752,  0.2000 },
74  {  41338.58,   0.752,  0.1736 },
75  {  44291.34,   0.752,  0.1506 },
76  {  47244.09,   0.752,  0.1307 },
77  {  50196.85,   0.752,  0.1134 },
78  {  53149.61,   0.752,  0.0984 },
79  {  56102.36,   0.752,  0.0854 },
80  {  59055.12,   0.752,  0.0741 },
81  {  62007.87,   0.752,  0.0643 },
82  {  65000.00,   0.752,  0.0557 },
83  {  68000.00,   0.754,  0.0482 },
84  {  71000.00,   0.758,  0.0418 },
85  {  74000.00,   0.761,  0.0362 },
86  {  77000.00,   0.764,  0.0314 },
87  {  80000.00,   0.767,  0.0273 },
88  {  83000.00,   0.770,  0.0237 },
89  {  86000.00,   0.773,  0.0206 },
90  {  89000.00,   0.777,  0.0179 },
91  {  92000.00,   0.780,  0.0156 },
92  {  95000.00,   0.783,  0.0135 },
93  {  98000.00,   0.786,  0.0118 },
94  { 101000.00,   0.789,  0.0103 },
95  { -1, -1, -1 }
96 };
97
98 static SGInterpTable * _temperature_degc_table = 0;
99 static SGInterpTable * _pressure_inhg_table = 0;
100
101 static void
102 _setup_tables ()
103 {
104   if (_temperature_degc_table != 0)
105       return;
106
107   _temperature_degc_table = new SGInterpTable;
108   _pressure_inhg_table = new SGInterpTable;
109
110   for (int i = 0; atmosphere_data[i][0] != -1; i++) {
111     _temperature_degc_table->addEntry(atmosphere_data[i][0],
112                                       atmosphere_data[i][1]);
113     _pressure_inhg_table->addEntry(atmosphere_data[i][0],
114                                    atmosphere_data[i][2]);
115   }
116 }
117 #endif
118
119 \f
120 ////////////////////////////////////////////////////////////////////////
121 // Implementation of FGEnvironment.
122 ////////////////////////////////////////////////////////////////////////
123
124 void FGEnvironment::_init()
125 {
126     elevation_ft = 0;
127     visibility_m = 32000;
128     temperature_sea_level_degc = 15;
129     temperature_degc = 15;
130     dewpoint_sea_level_degc = 5; // guess
131     dewpoint_degc = 5;
132     pressure_sea_level_inhg = 29.92;
133     pressure_inhg = 29.92;
134     turbulence_magnitude_norm = 0;
135     turbulence_rate_hz = 1;
136     wind_from_heading_deg = 0;
137     wind_speed_kt = 0;
138     wind_from_north_fps = 0;
139     wind_from_east_fps = 0;
140     wind_from_down_fps = 0;
141     thermal_lift_fps = 0;
142     ridge_lift_fps= 0;
143     local_weather_lift_fps=0;
144     altitude_half_to_sun_m = 1000;
145     altitude_tropo_top_m = 10000;
146 #ifdef USING_TABLES
147     _setup_tables();
148 #endif
149     _recalc_density();
150     _recalc_relative_humidity();
151     live_update = true;
152 }
153
154 FGEnvironment::FGEnvironment()
155 {
156     _init();
157 }
158
159 FGEnvironment::FGEnvironment (const FGEnvironment &env)
160 {
161     _init();
162     copy(env);
163 }
164
165 FGEnvironment::~FGEnvironment()
166 {
167 }
168
169 void
170 FGEnvironment::copy (const FGEnvironment &env)
171 {
172     elevation_ft = env.elevation_ft;
173     visibility_m = env.visibility_m;
174     temperature_sea_level_degc = env.temperature_sea_level_degc;
175     temperature_degc = env.temperature_degc;
176     dewpoint_sea_level_degc = env.dewpoint_sea_level_degc;
177     dewpoint_degc = env.dewpoint_degc;
178     pressure_sea_level_inhg = env.pressure_sea_level_inhg;
179     wind_from_heading_deg = env.wind_from_heading_deg;
180     wind_speed_kt = env.wind_speed_kt;
181     wind_from_north_fps = env.wind_from_north_fps;
182     wind_from_east_fps = env.wind_from_east_fps;
183     wind_from_down_fps = env.wind_from_down_fps;
184     thermal_lift_fps = env.thermal_lift_fps;
185     ridge_lift_fps= env.ridge_lift_fps;
186     local_weather_lift_fps = env.local_weather_lift_fps;
187     turbulence_magnitude_norm = env.turbulence_magnitude_norm;
188     turbulence_rate_hz = env.turbulence_rate_hz;
189 }
190
191 static inline bool
192 maybe_copy_value (FGEnvironment * env, const SGPropertyNode * node,
193                   const char * name, void (FGEnvironment::*setter)(double))
194 {
195     const SGPropertyNode * child = node->getNode(name);
196                                 // fragile: depends on not being typed
197                                 // as a number
198     if (child != 0 && child->hasValue() &&
199         child->getStringValue()[0] != '\0') {
200         (env->*setter)(child->getDoubleValue());
201         return true;
202     } else {
203         return false;
204     }
205 }
206
207 void
208 FGEnvironment::read (const SGPropertyNode * node)
209 {
210     bool live_update = set_live_update( false );
211     maybe_copy_value(this, node, "visibility-m",
212                      &FGEnvironment::set_visibility_m);
213
214     if (!maybe_copy_value(this, node, "temperature-sea-level-degc",
215                           &FGEnvironment::set_temperature_sea_level_degc))
216         maybe_copy_value(this, node, "temperature-degc",
217                          &FGEnvironment::set_temperature_degc);
218
219     if (!maybe_copy_value(this, node, "dewpoint-sea-level-degc",
220                           &FGEnvironment::set_dewpoint_sea_level_degc))
221         maybe_copy_value(this, node, "dewpoint-degc",
222                          &FGEnvironment::set_dewpoint_degc);
223
224     if (!maybe_copy_value(this, node, "pressure-sea-level-inhg",
225                           &FGEnvironment::set_pressure_sea_level_inhg))
226         maybe_copy_value(this, node, "pressure-inhg",
227                          &FGEnvironment::set_pressure_inhg);
228
229     maybe_copy_value(this, node, "wind-from-heading-deg",
230                      &FGEnvironment::set_wind_from_heading_deg);
231
232     maybe_copy_value(this, node, "wind-speed-kt",
233                      &FGEnvironment::set_wind_speed_kt);
234
235     maybe_copy_value(this, node, "elevation-ft",
236                      &FGEnvironment::set_elevation_ft);
237
238     maybe_copy_value(this, node, "turbulence/magnitude-norm",
239                      &FGEnvironment::set_turbulence_magnitude_norm);
240
241     maybe_copy_value(this, node, "turbulence/rate-hz",
242                      &FGEnvironment::set_turbulence_rate_hz);
243
244     // calculate derived properties here to avoid duplicate expensive computations
245     _recalc_ne();
246     _recalc_alt_pt();
247     _recalc_alt_dewpoint();
248     _recalc_density();
249     _recalc_relative_humidity();
250
251     set_live_update(live_update);
252 }
253
254
255 double
256 FGEnvironment::get_visibility_m () const
257 {
258   return visibility_m;
259 }
260
261 double
262 FGEnvironment::get_temperature_sea_level_degc () const
263 {
264   return temperature_sea_level_degc;
265 }
266
267 double
268 FGEnvironment::get_temperature_degc () const
269 {
270   return temperature_degc;
271 }
272
273 double
274 FGEnvironment::get_temperature_degf () const
275 {
276   return (temperature_degc * 9.0 / 5.0) + 32.0;
277 }
278
279 double
280 FGEnvironment::get_dewpoint_sea_level_degc () const
281 {
282   return dewpoint_sea_level_degc;
283 }
284
285 double
286 FGEnvironment::get_dewpoint_degc () const
287 {
288   return dewpoint_degc;
289 }
290
291 double
292 FGEnvironment::get_pressure_sea_level_inhg () const
293 {
294   return pressure_sea_level_inhg;
295 }
296
297 double
298 FGEnvironment::get_pressure_inhg () const
299 {
300   return pressure_inhg;
301 }
302
303 double
304 FGEnvironment::get_density_slugft3 () const
305 {
306   return density_slugft3;
307 }
308
309 double
310 FGEnvironment::get_relative_humidity () const
311 {
312   return relative_humidity;
313 }
314
315 double
316 FGEnvironment::get_density_tropo_avg_kgm3 () const
317 {
318   return density_tropo_avg_kgm3;
319 }
320
321 double
322 FGEnvironment::get_altitude_half_to_sun_m () const
323 {
324   return altitude_half_to_sun_m;
325 }
326
327 double
328 FGEnvironment::get_altitude_tropo_top_m () const
329 {
330   return altitude_tropo_top_m;
331 }
332
333 double
334 FGEnvironment::get_wind_from_heading_deg () const
335 {
336   return wind_from_heading_deg;
337 }
338
339 double
340 FGEnvironment::get_wind_speed_kt () const
341 {
342   return wind_speed_kt;
343 }
344
345 double
346 FGEnvironment::get_wind_from_north_fps () const
347 {
348   return wind_from_north_fps;
349 }
350
351 double
352 FGEnvironment::get_wind_from_east_fps () const
353 {
354   return wind_from_east_fps;
355 }
356
357 double
358 FGEnvironment::get_wind_from_down_fps () const
359 {
360   return wind_from_down_fps;
361 }
362
363 double
364 FGEnvironment::get_thermal_lift_fps () const
365 {
366   return thermal_lift_fps;
367 }
368
369 double
370 FGEnvironment::get_ridge_lift_fps () const
371 {
372   return ridge_lift_fps;
373 }
374
375 double
376 FGEnvironment::get_local_weather_lift_fps () const
377 {
378   return local_weather_lift_fps;
379 }
380
381 double
382 FGEnvironment::get_turbulence_magnitude_norm () const
383 {
384   if( sgEnviro.get_turbulence_enable_state() )
385     if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
386       return sgEnviro.get_cloud_turbulence();
387   return turbulence_magnitude_norm;
388 }
389
390 double
391 FGEnvironment::get_turbulence_rate_hz () const
392 {
393   return turbulence_rate_hz;
394 }
395
396 double
397 FGEnvironment::get_elevation_ft () const
398 {
399   return elevation_ft;
400 }
401
402 void
403 FGEnvironment::set_visibility_m (double v)
404 {
405   visibility_m = v;
406 }
407
408 void
409 FGEnvironment::set_temperature_sea_level_degc (double t)
410 {
411   temperature_sea_level_degc = t;
412   if (dewpoint_sea_level_degc > t)
413       dewpoint_sea_level_degc = t;
414   if( live_update ) {
415     _recalc_alt_pt();
416     _recalc_density();
417   }
418 }
419
420 void
421 FGEnvironment::set_temperature_degc (double t)
422 {
423   temperature_degc = t;
424   if( live_update ) {
425     _recalc_sl_temperature();
426     _recalc_sl_pressure();
427     _recalc_alt_pt();
428     _recalc_density();
429     _recalc_relative_humidity();
430   }
431 }
432
433 void
434 FGEnvironment::set_dewpoint_sea_level_degc (double t)
435 {
436   dewpoint_sea_level_degc = t;
437   if (temperature_sea_level_degc < t)
438       temperature_sea_level_degc = t;
439   if( live_update ) {
440     _recalc_alt_dewpoint();
441     _recalc_density();
442   }
443 }
444
445 void
446 FGEnvironment::set_dewpoint_degc (double t)
447 {
448   dewpoint_degc = t;
449   if( live_update ) {
450     _recalc_sl_dewpoint();
451     _recalc_density();
452     _recalc_relative_humidity();
453   }
454 }
455
456 void
457 FGEnvironment::set_pressure_sea_level_inhg (double p)
458 {
459   pressure_sea_level_inhg = p;
460   if( live_update ) {
461     _recalc_alt_pt();
462     _recalc_density();
463   }
464 }
465
466 void
467 FGEnvironment::set_pressure_inhg (double p)
468 {
469   pressure_inhg = p;
470   if( live_update ) {
471     _recalc_sl_pressure();
472     _recalc_density();
473   }
474 }
475
476 void
477 FGEnvironment::set_wind_from_heading_deg (double h)
478 {
479   wind_from_heading_deg = h;
480   if( live_update ) {
481     _recalc_ne();
482   }
483 }
484
485 void
486 FGEnvironment::set_wind_speed_kt (double s)
487 {
488   wind_speed_kt = s;
489   if( live_update ) {
490     _recalc_ne();
491   }
492 }
493
494 void
495 FGEnvironment::set_wind_from_north_fps (double n)
496 {
497   wind_from_north_fps = n;
498   if( live_update ) {
499     _recalc_hdgspd();
500   }
501 }
502
503 void
504 FGEnvironment::set_wind_from_east_fps (double e)
505 {
506   wind_from_east_fps = e;
507   if( live_update ) {
508     _recalc_hdgspd();
509   }
510 }
511
512 void
513 FGEnvironment::set_wind_from_down_fps (double d)
514 {
515   wind_from_down_fps = d;
516   if( live_update ) {
517     _recalc_hdgspd();
518   }
519 }
520
521 void
522 FGEnvironment::set_thermal_lift_fps (double th)
523 {
524   thermal_lift_fps = th;
525   if( live_update ) {
526     _recalc_updraft();
527   }
528 }
529
530 void
531 FGEnvironment::set_ridge_lift_fps (double ri)
532 {
533   ridge_lift_fps = ri;
534   if( live_update ) {
535     _recalc_updraft();
536   }
537 }
538
539 void
540 FGEnvironment::set_local_weather_lift_fps (double lwl)
541 {
542   local_weather_lift_fps = lwl;
543   if( live_update ) {
544     _recalc_updraft();
545   }
546 }
547
548 void
549 FGEnvironment::set_turbulence_magnitude_norm (double t)
550 {
551   turbulence_magnitude_norm = t;
552 }
553
554 void
555 FGEnvironment::set_turbulence_rate_hz (double r)
556 {
557   turbulence_rate_hz = r;
558 }
559
560 void
561 FGEnvironment::set_elevation_ft (double e)
562 {
563   elevation_ft = e;
564   if( live_update ) {
565     _recalc_alt_pt();
566     _recalc_alt_dewpoint();
567     _recalc_density();
568     _recalc_relative_humidity();
569   }
570 }
571
572 void
573 FGEnvironment::set_altitude_half_to_sun_m (double alt)
574 {
575   altitude_half_to_sun_m = alt;
576   if( live_update ) {
577     _recalc_density_tropo_avg_kgm3();
578   }
579 }
580
581 void
582 FGEnvironment::set_altitude_tropo_top_m (double alt)
583 {
584   altitude_tropo_top_m = alt;
585   if( live_update ) {
586     _recalc_density_tropo_avg_kgm3();
587   }
588 }
589
590
591 void
592 FGEnvironment::_recalc_hdgspd ()
593 {
594   double angle_rad;
595
596   if (wind_from_east_fps == 0) {
597     angle_rad = (wind_from_north_fps >= 0 ? SGD_PI_2 : -SGD_PI_2);
598   } else {
599     angle_rad = atan(wind_from_north_fps/wind_from_east_fps);
600   }
601   wind_from_heading_deg = angle_rad * SGD_RADIANS_TO_DEGREES;
602   if (wind_from_east_fps >= 0)
603     wind_from_heading_deg = 90 - wind_from_heading_deg;
604   else
605     wind_from_heading_deg = 270 - wind_from_heading_deg;
606
607 #if 0
608   // FIXME: Windspeed can become negative with these formulas.
609   //        which can cause problems for animations that rely
610   //        on the windspeed property.
611   if (angle_rad == 0)
612     wind_speed_kt = fabs(wind_from_east_fps
613                          * SG_METER_TO_NM * SG_FEET_TO_METER * 3600);
614   else
615     wind_speed_kt = (wind_from_north_fps / sin(angle_rad))
616       * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
617 #else
618   wind_speed_kt = sqrt(wind_from_north_fps * wind_from_north_fps +
619                        wind_from_east_fps * wind_from_east_fps) 
620                   * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
621 #endif
622 }
623
624 void
625 FGEnvironment::_recalc_ne ()
626 {
627   double speed_fps =
628     wind_speed_kt * SG_NM_TO_METER * SG_METER_TO_FEET * (1.0/3600);
629
630   wind_from_north_fps = speed_fps *
631     cos(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
632   wind_from_east_fps = speed_fps *
633     sin(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
634 }
635
636 void
637 FGEnvironment::_recalc_updraft ()
638 {
639   wind_from_down_fps = thermal_lift_fps + ridge_lift_fps + local_weather_lift_fps ;
640 }
641
642 // Intended to help with the interpretation of METAR data,
643 // not for random in-flight outside-air temperatures.
644 void
645 FGEnvironment::_recalc_sl_temperature ()
646 {
647
648 #if 0
649   {
650     SG_LOG(SG_GENERAL, SG_DEBUG, "recalc_sl_temperature: using "
651       << temperature_degc << " @ " << elevation_ft << " :: " << this);
652   }
653 #endif
654
655   if (elevation_ft >= ISA_def[1].height) {
656     SG_LOG(SG_GENERAL, SG_ALERT, "recalc_sl_temperature: "
657         << "valid only in troposphere, not " << elevation_ft);
658     return;
659   }
660
661 // Clamp: temperature of the stratosphere, in degrees C:
662   double t_strato = ISA_def[1].temp - atmodel::freezing;
663   if (temperature_degc < t_strato) temperature_sea_level_degc = t_strato;
664   else temperature_sea_level_degc = 
665       temperature_degc + elevation_ft * atmodel::foot * ISA_def[0].lapse;
666
667 // Alternative implemenation:
668 //  else temperature_sea_level_inhg = T_layer(0., elevation_ft * foot,
669 //      pressure_inhg * inHg, temperature_degc + freezing, ISA_def[0].lapse) - freezing;
670 }
671
672 void
673 FGEnvironment::_recalc_sl_dewpoint ()
674 {
675                                 // 0.2degC/1000ft
676                                 // FIXME: this will work only for low
677                                 // elevations
678   dewpoint_sea_level_degc = dewpoint_degc + (elevation_ft * .0002);
679   if (dewpoint_sea_level_degc > temperature_sea_level_degc)
680     dewpoint_sea_level_degc = temperature_sea_level_degc;
681 }
682
683 void
684 FGEnvironment::_recalc_alt_dewpoint ()
685 {
686                                 // 0.2degC/1000ft
687                                 // FIXME: this will work only for low
688                                 // elevations
689   dewpoint_degc = dewpoint_sea_level_degc + (elevation_ft * .0002);
690   if (dewpoint_degc > temperature_degc)
691     dewpoint_degc = temperature_degc;
692 }
693
694 void
695 FGEnvironment::_recalc_sl_pressure ()
696 {
697   using namespace atmodel;
698 #if 0
699   {
700     SG_LOG(SG_GENERAL, SG_ALERT, "recalc_sl_pressure: using "
701       << pressure_inhg << " and "
702       << temperature_degc << " @ " << elevation_ft << " :: " << this);
703   }
704 #endif
705   pressure_sea_level_inhg = P_layer(0., elevation_ft * foot,
706       pressure_inhg * inHg, temperature_degc + freezing, ISA_def[0].lapse) / inHg;
707 }
708
709 // This gets called at frame rate, to account for the aircraft's
710 // changing altitude. 
711 // Called by set_elevation_ft() which is called by FGEnvironmentMgr::update
712
713 void
714 FGEnvironment::_recalc_alt_pt ()
715 {
716   using namespace atmodel;
717 #if 0
718   {
719     static int count(0);
720     if (++count % 1000 == 0) {
721       SG_LOG(SG_GENERAL, SG_ALERT, 
722            "recalc_alt_pt for: " << elevation_ft
723         << "  using "  << pressure_sea_level_inhg 
724         << "  and "  << temperature_sea_level_degc
725         << " :: " << this
726         << "  # " << count);
727     }
728   }
729 #endif
730   double press, temp;
731   boost::tie(press, temp) = PT_vs_hpt(elevation_ft * foot, 
732         pressure_sea_level_inhg * inHg, temperature_sea_level_degc + freezing);
733   temperature_degc = temp - freezing;
734   pressure_inhg = press / inHg;
735 }
736
737 void
738 FGEnvironment::_recalc_density ()
739 {
740   double pressure_psf = pressure_inhg * 70.7487;
741   
742   // adjust for humidity
743   // calculations taken from USA Today (oops!) at
744   // http://www.usatoday.com/weather/basics/density-calculations.htm
745   double temperature_degk = temperature_degc + 273.15;
746   double pressure_mb = pressure_inhg * 33.86;
747   double vapor_pressure_mb =
748     6.11 * pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
749   double virtual_temperature_degk = temperature_degk / (1 - (vapor_pressure_mb / pressure_mb) * (1.0 - 0.622));
750   double virtual_temperature_degr = virtual_temperature_degk * 1.8;
751
752   density_slugft3 = pressure_psf / (virtual_temperature_degr * 1718);
753   _recalc_density_tropo_avg_kgm3();
754 }
755
756 // This is used to calculate the average density on the path 
757 // of sunlight to the observer for calculating sun-color
758 void
759 FGEnvironment::_recalc_density_tropo_avg_kgm3 ()
760 {
761   double pressure_mb = pressure_inhg * 33.86;
762   double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / (237.7 + dewpoint_degc)));
763
764   double virtual_temp = (temperature_degc + 273.15) / (1 - 0.379 * (vaporpressure/pressure_mb));
765
766   double density_half = (100 * pressure_mb * exp(-altitude_half_to_sun_m / 8000))
767       / (287.05 * virtual_temp);
768   double density_tropo = (100 * pressure_mb * exp((-1 * altitude_tropo_top_m) / 8000))
769       / ( 287.05 * virtual_temp);
770
771   density_tropo_avg_kgm3 = ((density_slugft3 * 515.379) + density_half + density_tropo) / 3;
772 }
773
774 void
775 FGEnvironment::_recalc_relative_humidity ()
776 {
777 /*
778   double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / ( 237.7 + dewpoint_degc)));
779   double sat_vaporpressure = 6.11 * pow(10.0, ((7.5 * temperature_degc)
780       / ( 237.7 + temperature_degc)) );
781   relative_humidity = 100 * vaporpressure / sat_vaporpressure ;
782
783   with a little algebra, this gets the same result and spares two multiplications and one pow()
784 */
785   double a = (7.5 * dewpoint_degc)    / ( 237.7 + dewpoint_degc);
786   double b = (7.5 * temperature_degc) / ( 237.7 + temperature_degc);
787   relative_humidity = 100 * pow(10.0,a-b); 
788 }
789
790 bool
791 FGEnvironment::set_live_update( bool _live_update )
792 {
793   bool b = live_update;
794   live_update = _live_update;
795   return b;
796 }
797
798
799 ////////////////////////////////////////////////////////////////////////
800 // Functions.
801 ////////////////////////////////////////////////////////////////////////
802
803 static inline double
804 do_interp (double a, double b, double fraction)
805 {
806     double retval = (a + ((b - a) * fraction));
807     return retval;
808 }
809
810 static inline double
811 do_interp_deg (double a, double b, double fraction)
812 {
813     a = fmod(a, 360);
814     b = fmod(b, 360);
815     if (fabs(b-a) > 180) {
816         if (a < b)
817             a += 360;
818         else
819             b += 360;
820     }
821     return fmod(do_interp(a, b, fraction), 360);
822 }
823
824 void
825 interpolate (const FGEnvironment * env1, const FGEnvironment * env2,
826              double fraction, FGEnvironment * result)
827 {
828     // don't calculate each internal property every time we set a single value
829     // we trigger that at the end of the interpolation process
830     bool live_update = result->set_live_update( false );
831
832     result->set_visibility_m
833         (do_interp(env1->get_visibility_m(),
834                    env2->get_visibility_m(),
835                    fraction));
836
837     result->set_temperature_sea_level_degc
838         (do_interp(env1->get_temperature_sea_level_degc(),
839                    env2->get_temperature_sea_level_degc(),
840                    fraction));
841
842     result->set_dewpoint_sea_level_degc
843         (do_interp(env1->get_dewpoint_sea_level_degc(),
844                    env2->get_dewpoint_sea_level_degc(),
845                    fraction));
846
847     result->set_pressure_sea_level_inhg
848         (do_interp(env1->get_pressure_sea_level_inhg(),
849                    env2->get_pressure_sea_level_inhg(),
850                    fraction));
851
852     result->set_wind_from_heading_deg
853         (do_interp_deg(env1->get_wind_from_heading_deg(),
854                        env2->get_wind_from_heading_deg(),
855                        fraction));
856
857     result->set_wind_speed_kt
858         (do_interp(env1->get_wind_speed_kt(),
859                    env2->get_wind_speed_kt(),
860                    fraction));
861
862     result->set_elevation_ft
863         (do_interp(env1->get_elevation_ft(),
864                    env2->get_elevation_ft(),
865                    fraction));
866
867     result->set_turbulence_magnitude_norm
868         (do_interp(env1->get_turbulence_magnitude_norm(),
869                    env2->get_turbulence_magnitude_norm(),
870                    fraction));
871
872     result->set_turbulence_rate_hz
873         (do_interp(env1->get_turbulence_rate_hz(),
874                    env2->get_turbulence_rate_hz(),
875                    fraction));
876
877     // calculate derived properties here to avoid duplicate expensive computations
878     result->_recalc_ne();
879     result->_recalc_alt_pt();
880     result->_recalc_alt_dewpoint();
881     result->_recalc_density();
882     result->_recalc_relative_humidity();
883
884     result->set_live_update(live_update);
885 }
886
887 // end of environment.cxx