]> git.mxchange.org Git - flightgear.git/blob - src/WeatherCM/FGLocalWeatherDatabase.cpp
I tested:
[flightgear.git] / src / WeatherCM / FGLocalWeatherDatabase.cpp
1 /*****************************************************************************
2
3  Module:       FGLocalWeatherDatabase.cpp
4  Author:       Christian Mayer
5  Date started: 28.05.99
6  Called by:    main program
7
8  -------- Copyright (C) 1999 Christian Mayer (fgfs@christianmayer.de) --------
9
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU General Public License as published by the Free Software
12  Foundation; either version 2 of the License, or (at your option) any later
13  version.
14
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  details.
19
20  You should have received a copy of the GNU General Public License along with
21  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  Place - Suite 330, Boston, MA  02111-1307, USA.
23
24  Further information about the GNU General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 ------------------------------------------------------------------------------
29 Database for the local weather
30 This database is the only one that gets called from FG
31
32 HISTORY
33 ------------------------------------------------------------------------------
34 28.05.1999 Christian Mayer      Created
35 16.06.1999 Durk Talsma          Portability for Linux
36 20.06.1999 Christian Mayer      added lots of consts
37 11.10.1999 Christian Mayer      changed set<> to map<> on Bernie Bright's 
38                                 suggestion
39 19.10.1999 Christian Mayer      change to use PLIB's sg instead of Point[2/3]D
40                                 and lots of wee code cleaning
41 14.12.1999 Christian Mayer      Changed the internal structure to use Dave
42                                 Eberly's spherical interpolation code. This
43                                 stops our dependancy on the (ugly) voronoi
44                                 code and simplyfies the code structure a lot.
45 07.05.2000 Tony Peden           Added functionality to get the weather data
46                                 on 'the bus'
47 18.05.2000 Christian Mayer      Minor clean-ups. Changed the code to use 
48                                 FGWeatherUtils.h for unit conversion
49 *****************************************************************************/
50
51 /****************************************************************************/
52 /* INCLUDES                                                                 */
53 /****************************************************************************/
54 #include <simgear/compiler.h>
55 #include <simgear/constants.h>
56
57 #include <Aircraft/aircraft.hxx>
58
59 #include "FGLocalWeatherDatabase.h"
60
61 #include "FGWeatherParse.h"
62
63 #include "FGWeatherUtils.h"
64
65 /****************************************************************************/
66 /********************************** CODE ************************************/
67 /****************************************************************************/
68
69 FGLocalWeatherDatabase* FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 0;
70 FGLocalWeatherDatabase *WeatherDatabase;
71
72 void FGLocalWeatherDatabase::init( const WeatherPrecision visibility,
73                                    const DatabaseWorkingType type,
74                                    const string &root )
75 {
76     cerr << "Initializing FGLocalWeatherDatabase\n";
77     cerr << "-----------------------------------\n";
78
79     if (theFGLocalWeatherDatabase)
80     {
81         cerr << "Error: only one local weather allowed";
82         exit(-1);
83     }
84
85     setWeatherVisibility(visibility);
86
87     DatabaseStatus = type;
88     database = 0;           //just get sure...
89
90     Thunderstorm = false;
91     //I don't need to set theThunderstorm as Thunderstorm == false
92
93     switch(DatabaseStatus)
94     {
95     case use_global:
96         {
97             cerr << "Error: there's no global database anymore!\n";
98             exit(-1);
99         }
100         break;
101
102     case use_internet:
103         {
104             FGWeatherParse *parsed_data = new FGWeatherParse();
105
106             parsed_data->input( "weather/current.gz" );
107             unsigned int n = parsed_data->stored_stations();
108
109             sgVec2               *p = new sgVec2              [n];
110             FGPhysicalProperties *f = new FGPhysicalProperties[n];
111
112             // fill the database
113             for (unsigned int i = 0; i < n; i++) 
114             {
115                 f[i] = parsed_data->getFGPhysicalProperties(i);
116                 parsed_data->getPosition(i, p[i]);
117
118                 if ( (i%100) == 0)
119                     cerr << ".";
120             }
121
122             // free the memory of the parsed data to ease the required memory
123             // for the very memory consuming spherical interpolation
124             delete parsed_data;
125
126             //and finally init the interpolation
127             cerr << "\nInitialiating Interpolation. (2-3 minutes on a PII-350)\n";
128             database = new SphereInterpolate<FGPhysicalProperties>(n, p, f);
129
130             //and free my allocations:
131             delete[] p;
132             delete[] f;
133             cerr << "Finished weather init.\n";
134
135         }
136         break;
137
138     case distant:
139         cerr << "FGLocalWeatherDatabase error: Distant database isn't implemented yet!\n";
140         cerr << "  using random mode instead!\n";
141     case random:
142     case manual:
143     case default_mode:
144         {
145             double x[2] = {0.0,  0.0};  //make an standard weather that's the same at the whole world
146             double y[2] = {0.0,  0.0};  //make an standard weather that's the same at the whole world
147             double z[2] = {1.0, -1.0};  //make an standard weather that's the same at the whole world
148             FGPhysicalProperties f[2];  //make an standard weather that's the same at the whole world
149             database = new SphereInterpolate<FGPhysicalProperties>(2,x,y,z,f);
150         }
151         break;
152
153     default:
154         cerr << "FGLocalWeatherDatabase error: Unknown database type specified!\n";
155     };
156 }
157
158 FGLocalWeatherDatabase::~FGLocalWeatherDatabase()
159 {
160     //Tidying up:
161     delete database;
162 }
163
164 /****************************************************************************/
165 /* reset the whole database                                                 */
166 /****************************************************************************/
167 void FGLocalWeatherDatabase::reset(const DatabaseWorkingType type)
168 {
169     cerr << "FGLocalWeatherDatabase::reset isn't supported yet\n";
170 }
171
172 /****************************************************************************/
173 /* update the database. Since the last call we had dt seconds               */
174 /****************************************************************************/
175 void FGLocalWeatherDatabase::update(const WeatherPrecision dt)
176 {
177     //if (DatabaseStatus==use_global)
178     //  global->update(dt);
179 }
180
181 void FGLocalWeatherDatabase::update(const sgVec3& p) //position has changed
182 {
183     sgCopyVec3(last_known_position, p);
184
185     //uncomment this when you are using the GlobalDatabase
186     /*
187     cerr << "****\nupdate(p) inside\n";
188     cerr << "Parameter: " << p[0] << "/" << p[1] << "/" << p[2] << "\n";
189     sgVec2 p2d;
190     sgSetVec2( p2d, p[0], p[1] );
191     cerr << FGPhysicalProperties2D(get(p2d), p2d);
192     cerr << "****\n";
193     */
194     
195 }
196
197 void FGLocalWeatherDatabase::update(const sgVec3& p, const WeatherPrecision dt)   //time and/or position has changed
198 {
199     sgCopyVec3(last_known_position, p);
200 }
201
202 /****************************************************************************/
203 /* Get the physical properties on the specified point p out of the database */
204 /****************************************************************************/
205 FGPhysicalProperty FGLocalWeatherDatabase::get(const sgVec3& p) const
206 {
207     return FGPhysicalProperty(database->Evaluate(p), p[3]);
208 }
209
210 #ifdef macintosh
211     /* fix a problem with mw compilers in that they don't know the
212        difference between the next two methods. Since the first one
213        doesn't seem to be used anywhere, I commented it out. This is
214        supposed to be fixed in the forthcoming CodeWarrior Release
215        6. */
216 #else
217 FGPhysicalProperties FGLocalWeatherDatabase::get(const sgVec2& p) const
218 {
219     return database->Evaluate(p);
220 }
221 #endif
222
223 WeatherPrecision FGLocalWeatherDatabase::getAirDensity(const sgVec3& p) const
224 {
225     FGPhysicalProperty dummy(database->Evaluate(p), p[3]);
226
227     return 
228         (dummy.AirPressure*FG_WEATHER_DEFAULT_AIRDENSITY*FG_WEATHER_DEFAULT_TEMPERATURE) / 
229         (dummy.Temperature*FG_WEATHER_DEFAULT_AIRPRESSURE);
230 }
231
232
233 void FGLocalWeatherDatabase::setSnowRainIntensity(const WeatherPrecision x, const sgVec2& p)
234 {
235     /* not supported yet */
236 }
237
238 void FGLocalWeatherDatabase::setSnowRainType(const SnowRainType x, const sgVec2& p)
239 {
240     /* not supported yet */
241 }
242
243 void FGLocalWeatherDatabase::setLightningProbability(const WeatherPrecision x, const sgVec2& p)
244 {
245     /* not supported yet */
246 }
247
248 void FGLocalWeatherDatabase::setProperties(const FGPhysicalProperties2D& x)
249 {
250     /* not supported yet */
251 }
252
253 void fgUpdateWeatherDatabase(void)
254 {
255     sgVec3 position;
256     sgVec3 wind;
257     
258     
259     sgSetVec3(position, 
260         current_aircraft.fdm_state->get_Latitude(),
261         current_aircraft.fdm_state->get_Longitude(),
262         current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER);
263     
264     WeatherDatabase->update( position );
265        
266     // get the data on 'the bus' for the FDM
267
268    /*  FGPhysicalProperty porperty = WeatherDatabase->get(position);
269
270     current_aircraft.fdm_state->set_Static_temperature( Kelvin2Rankine(porperty.Temperature) );
271     current_aircraft.fdm_state->set_Static_pressure( Pascal2psf(porperty.AirPressure) );
272
273     current_aircraft.fdm_state->set_Density( SIdensity2JSBsim( Density(porperty.AirPressure, porperty.Temperature) ) );
274     
275 #define MSTOFPS  3.2808 //m/s to ft/s
276     current_aircraft.fdm_state->set_Velocities_Local_Airmass(porperty.Wind[1]*MSTOFPS,
277         porperty.Wind[0]*MSTOFPS,
278         porperty.Wind[2]*MSTOFPS); */
279
280     
281 }
282