]> git.mxchange.org Git - flightgear.git/blob - src/WeatherCM/FGWeatherUtils.h
Panel code reorganization contributed by David Megginson:
[flightgear.git] / src / WeatherCM / FGWeatherUtils.h
1 /*****************************************************************************
2
3  Header:       FGWeatherUtils.h 
4  Author:       Christian Mayer
5  Date started: 28.05.99
6
7  -------- Copyright (C) 1999 Christian Mayer (fgfs@christianmayer.de) --------
8
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  details.
18
19  You should have received a copy of the GNU General Public License along with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA  02111-1307, USA.
22
23  Further information about the GNU General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25
26 FUNCTIONAL DESCRIPTION
27 ------------------------------------------------------------------------------
28 Utilities for the weather calculation like converting formulas
29
30 HISTORY
31 ------------------------------------------------------------------------------
32 02.06.1999 Christian Mayer      Created
33 08.06.1999 Christian Mayer      Changed sat_vp
34 16.06.1999 Durk Talsma          Portability for Linux
35 20.06.1999 Christian Mayer      added lots of consts
36 11.10.1999 Christian Mayer      changed set<> to map<> on Bernie Bright's 
37                                 suggestion
38 19.10.1999 Christian Mayer      change to use PLIB's sg instead of Point[2/3]D
39                                 and lots of wee code cleaning
40 *****************************************************************************/
41
42 /****************************************************************************/
43 /* SENTRY                                                                   */
44 /****************************************************************************/
45 #ifndef FGWeatherUtils_H
46 #define FGWeatherUtils_H
47
48 /****************************************************************************/
49 /* INCLUDES                                                                 */
50 /****************************************************************************/
51 #include <math.h>
52                 
53 /****************************************************************************/
54 /* DEFINES                                                                  */
55 /****************************************************************************/
56
57 /****************************************************************************/
58 /*assuming as given:                                                        */
59 /*                                                                          */
60 /* t:       temperature in °C                                               */
61 /* p:       preasure in mbar                                                */
62 /* //abs_hum: absoloute humidity in g/m^3,                                  */
63 /* act_vp:  actual vapor pressure pascal                                    */
64 /*                                                                          */
65 /* Calculated vaues:                                                        */
66 /* //max_hum: maximum of humidity in g/m^3,                                 */
67 /* sat_vp:  saturated vapor pressure in pascal                              */
68 /* rel_hum: relative humidity in %                                          */
69 /* dp:      dew point in °C                                                 */
70 /* wb:      approximate wetbulp in °C                                       */
71 /*                                                                          */
72 /* NOTE: Pascal is the SI unit for preasure and is defined as Pa = N/m^2    */
73 /*       1 mbar = 1 hPa = 100 Pa                                            */
74 /* NOTE: °C isn't a SI unit, so I should use °K instead. But as all         */
75 /*       formulas are given in °C and the weather database only uses        */
76 /*       'normal' temperatures, I've kept it in °C                          */
77 /****************************************************************************/
78
79 #define SAT_VP_CONST1 610.483125
80 #define SAT_VP_CONST2 7.444072452
81 #define SAT_VP_CONST3 235.3120919
82
83 inline WeatherPrecision sat_vp(const WeatherPrecision temp)
84 {
85     //old:
86     //return 6.112 * pow( 10, (7.5*dp)/(237.7+dp) );    //in mbar
87
88     //new:
89     //advantages: return the result as SI unit pascal and the constants
90     //are choosen that the correct results are returned for 0°C, 20°C and
91     //100°C. By 100°C I'm now returning a preasure of 1013.25 hPa
92     return SAT_VP_CONST1 * pow( 10, (SAT_VP_CONST2*temp)/(SAT_VP_CONST3+temp) );    //in pascal
93 }
94
95 inline WeatherPrecision rel_hum(const WeatherPrecision act_vp, const WeatherPrecision sat_vp)
96 {
97     return (act_vp / sat_vp) * 100;     //in %
98 }
99
100 inline WeatherPrecision dp(const WeatherPrecision sat_vp)
101 {
102     return (SAT_VP_CONST3*log10(sat_vp/SAT_VP_CONST1))/(SAT_VP_CONST2-log10(sat_vp/SAT_VP_CONST1)); //in °C 
103 }
104
105 inline WeatherPrecision wb(const WeatherPrecision t, const WeatherPrecision p, const WeatherPrecision dp)
106 {
107     WeatherPrecision e = sat_vp(dp); 
108     WeatherPrecision tcur, tcvp, peq, diff;
109     WeatherPrecision tmin, tmax;
110
111     if (t > dp)
112     {
113         tmax = t;
114         tmin = dp;
115     }
116     else
117     {
118         tmax = dp;
119         tmin = t;
120     }
121
122     while (true) 
123     { 
124         tcur=(tmax+tmin)/2;
125         tcvp=sat_vp(tcur);
126         
127         peq = 0.000660*(1+0.00155*tcur)*p*(t-tcur);
128         
129         diff = peq-tcvp+e;
130         
131         if (fabs(diff) < 0.01) 
132             return tcur;        //in °C
133         
134         if (diff < 0) 
135             tmax=tcur; 
136         else 
137             tmin=tcur; 
138     }; 
139
140 }
141
142 inline WeatherPrecision Celsius(const WeatherPrecision celsius)
143 {
144     return celsius + 273.16;                            //Kelvin
145 }
146
147 inline WeatherPrecision Fahrenheit(const WeatherPrecision fahrenheit)
148 {
149     return (fahrenheit * 9.0 / 5.0) + 32.0 + 273.16;    //Kelvin
150 }
151
152 inline WeatherPrecision Kelvin2Celsius(const WeatherPrecision kelvin)
153 {
154     return kelvin - 273.16;                             //Celsius
155 }
156
157 inline WeatherPrecision Kelvin2Fahrenheit(const WeatherPrecision kelvin)
158 {
159     return ((kelvin - 273.16) * 9.0 / 5.0) + 32.0;       //Fahrenheit
160 }
161
162 inline WeatherPrecision Celsius2Fahrenheit(const WeatherPrecision celsius)
163 {
164     return (celsius * 9.0 / 5.0) + 32.0;                 //Fahrenheit
165 }
166
167 inline WeatherPrecision Fahrenheit2Celsius(const WeatherPrecision fahrenheit)
168 {
169     return (fahrenheit - 32.0) * 5.0 / 9.0;             //Celsius
170 }
171
172 inline WeatherPrecision Torr2Pascal(const WeatherPrecision torr)
173 {
174     return (101325.0/760.0)*torr;
175 }
176
177 /****************************************************************************/
178 /* CLASS DECLARATION                                                        */
179 /****************************************************************************/
180
181 /****************************************************************************/
182 #endif /*FGWeatherUtils_H*/