]> git.mxchange.org Git - flightgear.git/blob - src/WeatherCM/FGWeatherUtils.h
Updates by Christian Mayer.
[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 (vader@t-online.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 *****************************************************************************/
37
38 /****************************************************************************/
39 /* SENTRY                                                                   */
40 /****************************************************************************/
41 #ifndef FGWeatherUtils_H
42 #define FGWeatherUtils_H
43
44 /****************************************************************************/
45 /* INCLUDES                                                                 */
46 /****************************************************************************/
47 #include <math.h>
48                 
49 /****************************************************************************/
50 /* DEFINES                                                                  */
51 /****************************************************************************/
52
53 /****************************************************************************/
54 /*assuming as given:                                                        */
55 /*                                                                          */
56 /* t:       temperature in °C                                               */
57 /* p:       preasure in mbar                                                */
58 /* //abs_hum: absoloute humidity in g/m^3,                                  */
59 /* act_vp:  actual vapor pressure pascal                                    */
60 /*                                                                          */
61 /* Calculated vaues:                                                        */
62 /* //max_hum: maximum of humidity in g/m^3,                                 */
63 /* sat_vp:  saturated vapor pressure in pascal                              */
64 /* rel_hum: relative humidity in %                                          */
65 /* dp:      dew point in °C                                                 */
66 /* wb:      approximate wetbulp in °C                                       */
67 /*                                                                          */
68 /* NOTE: Pascal is the SI unit for preasure and is defined as Pa = N/m^2    */
69 /*       1 mbar = 1 hPa = 100 Pa                                            */
70 /* NOTE: °C isn't a SI unit, so I should use °K instead. But as all         */
71 /*       formulas are given in °C and the weather database only uses        */
72 /*       'normal' temperatures, I've kept it in °C                          */
73 /****************************************************************************/
74
75 #define SAT_VP_CONST1 610.483125
76 #define SAT_VP_CONST2 7.444072452
77 #define SAT_VP_CONST3 235.3120919
78
79 inline WeatherPrecition sat_vp(const WeatherPrecition& temp)
80 {
81     //old:
82     //return 6.112 * pow( 10, (7.5*dp)/(237.7+dp) );    //in mbar
83
84     //new:
85     //advantages: return the result as SI unit pascal and the constants
86     //are choosen that the correct results are returned for 0°C, 20°C and
87     //100°C. By 100°C I'm now returning a preasure of 1013.25 hPa
88     return SAT_VP_CONST1 * pow( 10, (SAT_VP_CONST2*temp)/(SAT_VP_CONST3+temp) );    //in pascal
89 }
90
91 inline WeatherPrecition rel_hum(const WeatherPrecition& act_vp, const WeatherPrecition& sat_vp)
92 {
93     return (act_vp / sat_vp) * 100;     //in %
94 }
95
96 inline WeatherPrecition dp(const WeatherPrecition& sat_vp)
97 {
98     return (SAT_VP_CONST3*log10(sat_vp/SAT_VP_CONST1))/(SAT_VP_CONST2-log10(sat_vp/SAT_VP_CONST1)); //in °C 
99 }
100
101 inline WeatherPrecition wb(const WeatherPrecition& t, const WeatherPrecition& p, const WeatherPrecition& dp)
102 {
103     WeatherPrecition e = sat_vp(dp); 
104     WeatherPrecition tcur, tcvp, peq, diff;
105     WeatherPrecition tmin, tmax;
106
107     if (t > dp)
108     {
109         tmax = t;
110         tmin = dp;
111     }
112     else
113     {
114         tmax = dp;
115         tmin = t;
116     }
117
118     while (true) 
119     { 
120         tcur=(tmax+tmin)/2;
121         tcvp=sat_vp(tcur);
122         
123         peq = 0.000660*(1+0.00155*tcur)*p*(t-tcur);
124         
125         diff = peq-tcvp+e;
126         
127         if (fabs(diff) < 0.01) 
128             return tcur;        //in °C
129         
130         if (diff < 0) 
131             tmax=tcur; 
132         else 
133             tmin=tcur; 
134     }; 
135
136 }
137
138 inline WeatherPrecition Celsius(const WeatherPrecition& celsius)
139 {
140     return celsius + 273.16;                            //Kelvin
141 }
142
143 inline WeatherPrecition Fahrenheit(const WeatherPrecition& fahrenheit)
144 {
145     return (fahrenheit * 9.0 / 5.0) + 32.0 + 273.16;    //Kelvin
146 }
147
148 inline WeatherPrecition Kelvin2Celsius(const WeatherPrecition& kelvin)
149 {
150     return kelvin - 273.16;                             //Celsius
151 }
152
153 inline WeatherPrecition Kelvin2Fahrenheit(const WeatherPrecition& kelvin)
154 {
155     return ((kelvin - 273.16) * 9.0 / 5.0) + 32.0;       //Fahrenheit
156 }
157
158 inline WeatherPrecition Celsius2Fahrenheit(const WeatherPrecition& celsius)
159 {
160     return (celsius * 9.0 / 5.0) + 32.0;                 //Fahrenheit
161 }
162
163 inline WeatherPrecition Fahrenheit2Celsius(const WeatherPrecition& fahrenheit)
164 {
165     return (fahrenheit - 32.0) * 5.0 / 9.0;             //Celsius
166 }
167
168 inline WeatherPrecition Torr2Pascal(const WeatherPrecition& torr)
169 {
170     return (101325.0/760.0)*torr;
171 }
172
173 /****************************************************************************/
174 /* CLASS DECLARATION                                                        */
175 /****************************************************************************/
176
177 /****************************************************************************/
178 #endif /*FGWeatherUtils_H*/