]> git.mxchange.org Git - flightgear.git/blob - src/WeatherCM/FGWeatherUtils.h
Continuing work on ssg-ifying the sky dome.
[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 17.01.2000 Christian Mayer      Added conversion routines make it easier for
41                                 JSBsim to use the weather database.
42 *****************************************************************************/
43
44 /****************************************************************************/
45 /* SENTRY                                                                   */
46 /****************************************************************************/
47 #ifndef FGWeatherUtils_H
48 #define FGWeatherUtils_H
49
50 /****************************************************************************/
51 /* INCLUDES                                                                 */
52 /****************************************************************************/
53 #include <math.h>
54                 
55 /****************************************************************************/
56 /* DEFINES                                                                  */
57 /****************************************************************************/
58
59 /****************************************************************************/
60 /*assuming as given:                                                        */
61 /*                                                                          */
62 /* t:       temperature in °C                                               */
63 /* p:       preasure in mbar                                                */
64 /* //abs_hum: absoloute humidity in g/m^3,                                  */
65 /* act_vp:  actual vapor pressure pascal                                    */
66 /*                                                                          */
67 /* Calculated vaues:                                                        */
68 /* //max_hum: maximum of humidity in g/m^3,                                 */
69 /* sat_vp:  saturated vapor pressure in pascal                              */
70 /* rel_hum: relative humidity in %                                          */
71 /* dp:      dew point in °C                                                 */
72 /* wb:      approximate wetbulp in °C                                       */
73 /*                                                                          */
74 /* NOTE: Pascal is the SI unit for preasure and is defined as Pa = N/m^2    */
75 /*       1 mbar = 1 hPa = 100 Pa                                            */
76 /* NOTE: °C isn't a SI unit, so I should use °K instead. But as all         */
77 /*       formulas are given in °C and the weather database only uses        */
78 /*       'normal' temperatures, I've kept it in °C                          */
79 /****************************************************************************/
80
81 #define SAT_VP_CONST1 610.483125
82 #define SAT_VP_CONST2 7.444072452
83 #define SAT_VP_CONST3 235.3120919
84
85 inline WeatherPrecision sat_vp(const WeatherPrecision temp)
86 {
87     //old:
88     //return 6.112 * pow( 10, (7.5*dp)/(237.7+dp) );    //in mbar
89
90     //new:
91     //advantages: return the result as SI unit pascal and the constants
92     //are choosen that the correct results are returned for 0°C, 20°C and
93     //100°C. By 100°C I'm now returning a preasure of 1013.25 hPa
94     return SAT_VP_CONST1 * pow( 10, (SAT_VP_CONST2*temp)/(SAT_VP_CONST3+temp) );    //in pascal
95 }
96
97 inline WeatherPrecision rel_hum(const WeatherPrecision act_vp, const WeatherPrecision sat_vp)
98 {
99     return (act_vp / sat_vp) * 100;     //in %
100 }
101
102 inline WeatherPrecision dp(const WeatherPrecision sat_vp)
103 {
104     return (SAT_VP_CONST3*log10(sat_vp/SAT_VP_CONST1))/(SAT_VP_CONST2-log10(sat_vp/SAT_VP_CONST1)); //in °C 
105 }
106
107 inline WeatherPrecision wb(const WeatherPrecision t, const WeatherPrecision p, const WeatherPrecision dp)
108 {
109     WeatherPrecision e = sat_vp(dp); 
110     WeatherPrecision tcur, tcvp, peq, diff;
111     WeatherPrecision tmin, tmax;
112
113     if (t > dp)
114     {
115         tmax = t;
116         tmin = dp;
117     }
118     else
119     {
120         tmax = dp;
121         tmin = t;
122     }
123
124     while (true) 
125     { 
126         tcur=(tmax+tmin)/2;
127         tcvp=sat_vp(tcur);
128         
129         peq = 0.000660*(1+0.00155*tcur)*p*(t-tcur);
130         
131         diff = peq-tcvp+e;
132         
133         if (fabs(diff) < 0.01) 
134             return tcur;        //in °C
135         
136         if (diff < 0) 
137             tmax=tcur; 
138         else 
139             tmin=tcur; 
140     }; 
141
142 }
143
144 inline WeatherPrecision Celsius             (const WeatherPrecision celsius)
145 {
146     return celsius + 273.16;                            //Kelvin
147 }
148
149 inline WeatherPrecision Fahrenheit          (const WeatherPrecision fahrenheit)
150 {
151     return (fahrenheit * 9.0 / 5.0) + 32.0 + 273.16;    //Kelvin
152 }
153
154 inline WeatherPrecision Kelvin2Celsius      (const WeatherPrecision kelvin)
155 {
156     return kelvin - 273.16;                             //Celsius
157 }
158
159 inline WeatherPrecision Kelvin2Fahrenheit   (const WeatherPrecision kelvin)
160 {
161     return ((kelvin - 273.16) * 9.0 / 5.0) + 32.0;      //Fahrenheit
162 }
163
164 inline WeatherPrecision Celsius2Fahrenheit  (const WeatherPrecision celsius)
165 {
166     return (celsius * 9.0 / 5.0) + 32.0;                //Fahrenheit
167 }
168
169 inline WeatherPrecision Fahrenheit2Celsius  (const WeatherPrecision fahrenheit)
170 {
171     return (fahrenheit - 32.0) * 5.0 / 9.0;             //Celsius
172 }
173
174 inline WeatherPrecision Torr2Pascal         (const WeatherPrecision torr)
175 {
176     return (101325.0/760.0)*torr;                       //Pascal
177 }
178
179 inline WeatherPrecision Rankine2Kelvin      (const WeatherPrecision Rankine)
180 {
181     return (5.0 / 9.0) * Rankine;                       //Kelvin
182 }
183
184 inline WeatherPrecision JSBsim2SIdensity    (const WeatherPrecision JSBsim)
185 {
186     return JSBsim / 0.0019403203;                       //kg / cubic metres
187 }
188
189 inline WeatherPrecision psf2Pascal          (const WeatherPrecision psf)
190 {
191     return psf / 0.0020885434;                          //lbs / square foot (used in JSBsim)
192 }
193
194 inline WeatherPrecision Kelvin2Rankine      (const WeatherPrecision kelvin)
195 {
196     return (9.0 / 5.0) * kelvin;                        //Rankine (used in JSBsim)
197 }
198
199 inline WeatherPrecision SIdensity2JSBsim    (const WeatherPrecision SIdensity)
200 {
201     return 0.0019403203 * SIdensity;                    //slug / cubic feet (used in JSBsim)
202 }
203
204 inline WeatherPrecision Pascal2psf          (const WeatherPrecision Pascal)
205 {
206     return 0.0020885434 * Pascal;                       //lbs / square feet (used in JSBsim)
207 }
208
209 /****************************************************************************/
210 /* CLASS DECLARATION                                                        */
211 /****************************************************************************/
212
213 /****************************************************************************/
214 #endif /*FGWeatherUtils_H*/