1 /*****************************************************************************
3 Module: FGLocalWeatherDatabase.cpp
4 Author: Christian Mayer
6 Called by: main program
8 -------- Copyright (C) 1999 Christian Mayer (fgfs@christianmayer.de) --------
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
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
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.
24 Further information about the GNU General Public License can also be found on
25 the world wide web at http://www.gnu.org.
27 FUNCTIONAL DESCRIPTION
28 ------------------------------------------------------------------------------
29 Database for the local weather
30 This database is the only one that gets called from FG
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
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
47 18.05.2000 Christian Mayer Minor clean-ups. Changed the code to use
48 FGWeatherUtils.h for unit conversion
49 *****************************************************************************/
51 /****************************************************************************/
53 /****************************************************************************/
54 #include <simgear/compiler.h>
55 #include <simgear/constants.h>
57 #include <Aircraft/aircraft.hxx>
59 #include "FGLocalWeatherDatabase.h"
61 #include "FGWeatherParse.h"
63 #include "FGWeatherUtils.h"
65 /****************************************************************************/
66 /********************************** CODE ************************************/
67 /****************************************************************************/
69 FGLocalWeatherDatabase* FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 0;
70 FGLocalWeatherDatabase *WeatherDatabase;
72 void FGLocalWeatherDatabase::init( const WeatherPrecision visibility,
73 const DatabaseWorkingType type,
76 FGPhysicalProperties f[2]; //make an standard weather that's the same at the whole world
78 cerr << "Initializing FGLocalWeatherDatabase\n";
79 cerr << "-----------------------------------\n";
81 if (theFGLocalWeatherDatabase)
83 cerr << "Error: only one local weather allowed";
87 setWeatherVisibility(visibility);
89 DatabaseStatus = type;
90 database = 0; //just get sure...
93 //I don't need to set theThunderstorm as Thunderstorm == false
95 switch(DatabaseStatus)
99 cerr << "Error: there's no global database anymore!\n";
106 FGWeatherParse *parsed_data = new FGWeatherParse();
108 parsed_data->input( "weather/current.gz" );
109 unsigned int n = parsed_data->stored_stations();
111 sgVec2 *p = new sgVec2 [n];
112 FGPhysicalProperties *f = new FGPhysicalProperties[n];
115 for (unsigned int i = 0; i < n; i++)
117 f[i] = parsed_data->getFGPhysicalProperties(i);
118 parsed_data->getPosition(i, p[i]);
124 // free the memory of the parsed data to ease the required memory
125 // for the very memory consuming spherical interpolation
128 //and finally init the interpolation
129 cerr << "\nInitialiating Interpolation. (2-3 minutes on a PII-350)\n";
130 database = new SphereInterpolate<FGPhysicalProperties>(n, p, f);
132 //and free my allocations:
135 cerr << "Finished weather init.\n";
141 cerr << "FGLocalWeatherDatabase error: Distant database isn't implemented yet!\n";
142 cerr << " using random mode instead!\n";
147 double x[2] = {0.0, 0.0}; //make an standard weather that's the same at the whole world
148 double y[2] = {0.0, 0.0}; //make an standard weather that's the same at the whole world
149 double z[2] = {1.0, -1.0}; //make an standard weather that's the same at the whole world
150 database = new SphereInterpolate<FGPhysicalProperties>(2,x,y,z,f);
155 cerr << "FGLocalWeatherDatabase error: Unknown database type specified!\n";
159 FGLocalWeatherDatabase::~FGLocalWeatherDatabase()
165 /****************************************************************************/
166 /* reset the whole database */
167 /****************************************************************************/
168 void FGLocalWeatherDatabase::reset(const DatabaseWorkingType type)
170 cerr << "FGLocalWeatherDatabase::reset isn't supported yet\n";
173 /****************************************************************************/
174 /* update the database. Since the last call we had dt seconds */
175 /****************************************************************************/
176 void FGLocalWeatherDatabase::update(const WeatherPrecision dt)
178 //if (DatabaseStatus==use_global)
179 // global->update(dt);
182 void FGLocalWeatherDatabase::update(const sgVec3& p) //position has changed
184 sgCopyVec3(last_known_position, p);
186 //uncomment this when you are using the GlobalDatabase
188 cerr << "****\nupdate(p) inside\n";
189 cerr << "Parameter: " << p[0] << "/" << p[1] << "/" << p[2] << "\n";
191 sgSetVec2( p2d, p[0], p[1] );
192 cerr << FGPhysicalProperties2D(get(p2d), p2d);
198 void FGLocalWeatherDatabase::update(const sgVec3& p, const WeatherPrecision dt) //time and/or position has changed
200 sgCopyVec3(last_known_position, p);
203 /****************************************************************************/
204 /* Get the physical properties on the specified point p out of the database */
205 /****************************************************************************/
206 FGPhysicalProperty FGLocalWeatherDatabase::get(const sgVec3& p) const
208 return FGPhysicalProperty(database->Evaluate(p), p[2]);
212 /* fix a problem with mw compilers in that they don't know the
213 difference between the next two methods. Since the first one
214 doesn't seem to be used anywhere, I commented it out. This is
215 supposed to be fixed in the forthcoming CodeWarrior Release
218 FGPhysicalProperties FGLocalWeatherDatabase::get(const sgVec2& p) const
220 return database->Evaluate(p);
224 WeatherPrecision FGLocalWeatherDatabase::getAirDensity(const sgVec3& p) const
226 FGPhysicalProperty dummy(database->Evaluate(p), p[2]);
229 (dummy.AirPressure*FG_WEATHER_DEFAULT_AIRDENSITY*FG_WEATHER_DEFAULT_TEMPERATURE) /
230 (dummy.Temperature*FG_WEATHER_DEFAULT_AIRPRESSURE);
234 void FGLocalWeatherDatabase::setSnowRainIntensity(const WeatherPrecision x, const sgVec2& p)
236 /* not supported yet */
239 void FGLocalWeatherDatabase::setSnowRainType(const SnowRainType x, const sgVec2& p)
241 /* not supported yet */
244 void FGLocalWeatherDatabase::setLightningProbability(const WeatherPrecision x, const sgVec2& p)
246 /* not supported yet */
249 void FGLocalWeatherDatabase::setProperties(const FGPhysicalProperties2D& x)
251 /* not supported yet */
254 void fgUpdateWeatherDatabase(void)
261 current_aircraft.fdm_state->get_Latitude(),
262 current_aircraft.fdm_state->get_Longitude(),
263 current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER);
265 WeatherDatabase->update( position );
267 // get the data on 'the bus' for the FDM
269 /* FGPhysicalProperty porperty = WeatherDatabase->get(position);
271 current_aircraft.fdm_state->set_Static_temperature( Kelvin2Rankine(porperty.Temperature) );
272 current_aircraft.fdm_state->set_Static_pressure( Pascal2psf(porperty.AirPressure) );
274 current_aircraft.fdm_state->set_Density( SIdensity2JSBsim( Density(porperty.AirPressure, porperty.Temperature) ) );
276 #define MSTOFPS 3.2808 //m/s to ft/s
277 current_aircraft.fdm_state->set_Velocities_Local_Airmass(porperty.Wind[1]*MSTOFPS,
278 porperty.Wind[0]*MSTOFPS,
279 porperty.Wind[2]*MSTOFPS); */