1 // Build a cloud layer based on metar
3 // Written by Harald JOHNSEN, started April 2005.
5 // Copyright (C) 2005 Harald JOHNSEN - hjohnsen@evc.net
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
23 #include <Main/fg_props.hxx>
25 #include <simgear/constants.h>
26 #include <simgear/sound/soundmgr_openal.hxx>
27 #include <simgear/scene/sky/sky.hxx>
28 #include <simgear/environment/visual_enviro.hxx>
29 #include <simgear/scene/sky/cloudfield.hxx>
30 #include <simgear/scene/sky/newcloud.hxx>
31 #include <simgear/math/sg_random.h>
32 #include <Main/globals.hxx>
33 #include <Airports/simple.hxx>
34 #include <Main/util.hxx>
36 #include "environment_mgr.hxx"
37 #include "fgmetar.hxx"
38 #include "fgclouds.hxx"
43 FGClouds::FGClouds() :
44 station_elevation_ft(0.0),
48 fgSetString("/environment/weather-scenario", "METAR");
50 FGClouds::~FGClouds() {
53 int FGClouds::get_update_event(void) const {
56 void FGClouds::set_update_event(int count) {
61 void FGClouds::init(void) {
62 if( snd_lightning == NULL ) {
63 snd_lightning = new SGSoundSample(globals->get_fg_root().c_str(), "Sounds/thunder.wav", true);
64 snd_lightning->set_max_dist(7000.0f);
65 snd_lightning->set_reference_dist(3000.0f);
66 SGSoundMgr *soundMgr = globals->get_soundmgr();
67 soundMgr->add( snd_lightning, "thunder" );
68 sgEnviro.set_soundMgr( soundMgr );
72 SGNewCloud *FGClouds::buildCloud(SGPropertyNode *cloud_def_root, string name) {
73 SGPropertyNode *cld_def=NULL;
75 cld_def = cloud_def_root->getChild(name.c_str());
76 string base_name = name.substr(0,2);
78 if( name[2] == '-' ) {
79 cld_def = cloud_def_root->getChild(base_name.c_str());
84 string familly = cld_def->getStringValue("familly", base_name.c_str());
85 SGNewCloud *cld = new SGNewCloud(familly);
86 for(int i = 0; i < cld_def->nChildren() ; i++) {
87 SGPropertyNode *abox = cld_def->getChild(i);
88 if( strcmp(abox->getName(), "box") == 0) {
89 double x = abox->getDoubleValue("x");
90 double y = abox->getDoubleValue("y");
91 double z = abox->getDoubleValue("z");
92 double size = abox->getDoubleValue("size");
93 int type = abox->getIntValue("type", SGNewCloud::CLbox_standard);
94 cld->addContainer(x, y, z, size, (SGNewCloud::CLbox_type) type);
101 void FGClouds::buildLayer(SGCloudField *layer, string name, double alt, double coverage) {
106 int CloudVarietyCount = 0;
107 double totalCount = 0.0;
109 SGPropertyNode *cloud_def_root = fgGetNode("/environment/config/cloudlayers/clouds", false);
110 SGPropertyNode *layer_def_root = fgGetNode("/environment/config/cloudlayers/layers", false);
113 // when we don't generate clouds the layer is rendered in 2D
114 if( coverage == 0.0 )
116 if( layer_def_root == NULL || cloud_def_root == NULL)
118 if( name == "ci" || name == "sc" || name == "st")
121 SGPropertyNode *layer_def=NULL;
123 layer_def = layer_def_root->getChild(name.c_str());
125 if( name[2] == '-' ) {
126 string base_name = name.substr(0,2);
127 layer_def = layer_def_root->getChild(base_name.c_str());
133 double grid_x_size = layer_def->getDoubleValue("grid-x-size", 1000.0);
134 double grid_y_size = layer_def->getDoubleValue("grid-y-size", 1000.0);
135 double grid_x_rand = layer_def->getDoubleValue("grid-x-rand", grid_x_size);
136 double grid_y_rand = layer_def->getDoubleValue("grid-y-rand", grid_y_size);
137 double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
139 for(int i = 0; i < layer_def->nChildren() ; i++) {
140 SGPropertyNode *acloud = layer_def->getChild(i);
141 if( strcmp(acloud->getName(), "cloud") == 0) {
142 string cloud_name = acloud->getStringValue("name");
143 tCloudVariety[CloudVarietyCount].name = cloud_name;
144 double count = acloud->getDoubleValue("count", 1.0);
145 tCloudVariety[CloudVarietyCount].count = count;
147 cloud_name = cloud_name + "-%d";
148 char variety_name[50];
151 snprintf(variety_name, sizeof(variety_name), cloud_name.c_str(), variety);
152 } while( cloud_def_root->getChild(variety_name, 0, false) );
155 if( CloudVarietyCount < 20 )
159 totalCount = 1.0 / totalCount;
160 double currCoverage = 0.0;
162 for(double px = 0.0; px < SGCloudField::fieldSize; px += grid_x_size) {
163 for(double py = 0.0; py < SGCloudField::fieldSize; py += grid_y_size) {
164 double x = px + grid_x_rand * (sg_random() - 0.5);
165 double y = py + grid_y_rand * (sg_random() - 0.5);
166 double z = alt + grid_z_rand * (sg_random() - 0.5);
167 double choice = sg_random();
168 currCoverage += coverage;
169 if( currCoverage < 1.0 )
173 for(int i = 0; i < CloudVarietyCount ; i ++) {
174 choice -= tCloudVariety[i].count * totalCount;
175 if( choice <= 0.0 ) {
176 SGNewCloud *cld = buildCloud(cloud_def_root, tCloudVariety[i].name);
179 layer->addCloud(pos, cld);
189 // TODO:call this after real metar updates
190 void FGClouds::buildMETAR(void) {
191 SGPropertyNode *metar_root = fgGetNode("/environment", true);
193 double wind_speed_kt = metar_root->getDoubleValue("wind-speed-kt");
194 double temperature_degc = metar_root->getDoubleValue("temperature-sea-level-degc");
195 double dewpoint_degc = metar_root->getDoubleValue("dewpoint-sea-level-degc");
196 double pressure_mb = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
198 double dewp = pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
199 double temp = pow(10.0, 7.5 * temperature_degc / (237.7 + temperature_degc));
200 double rel_humidity = dewp * 100 / temp;
202 // formule d'Epsy, base d'un cumulus
203 double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
204 double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
206 bool cu_seen = false;
208 for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
209 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
211 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
212 double alt_m = alt_ft * SG_FEET_TO_METER;
213 string coverage = cloud_root->getStringValue("coverage");
214 double coverage_norm = 0.0;
215 if( coverage == "few" )
216 coverage_norm = 2.0/8.0; // <1-2
217 else if( coverage == "scattered" )
218 coverage_norm = 4.0/8.0; // 3-4
219 else if( coverage == "broken" )
220 coverage_norm = 6.0/8.0; // 5-7
221 else if( coverage == "overcast" )
222 coverage_norm = 8.0/8.0; // 8
224 string layer_type = "nn";
225 if( coverage == "cirrus" ) {
227 } else if( alt_ft > 16500 ) {
228 // layer_type = "ci|cs|cc";
230 } else if( alt_ft > 6500 ) {
231 // layer_type = "as|ac|ns";
233 if( pressure_mb < 1005.0 && coverage_norm >= 5.5 )
236 // layer_type = "st|cu|cb|sc";
237 // +/- 20% from stratus probable base
238 if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m )
240 // +/- 20% from cumulus probable base
241 else if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m )
244 // above formulae is far from perfect
247 else if( alt_ft < 4500 )
254 SGCloudField *layer3D = thesky->get_cloud_layer(iLayer)->get_layer3D();
255 buildLayer(layer3D, layer_type, alt_m, coverage_norm);
259 // copy from FGMetarEnvironmentCtrl until better
261 FGClouds::update_metar_properties( FGMetar *m )
267 fgSetString("/environment/metar/station-id", m->getId());
268 fgSetDouble("/environment/metar/min-visibility-m",
269 m->getMinVisibility().getVisibility_m() );
270 fgSetDouble("/environment/metar/max-visibility-m",
271 m->getMaxVisibility().getVisibility_m() );
273 SGMetarVisibility *dirvis = m->getDirVisibility();
274 for (i = 0; i < 8; i++, dirvis++) {
275 const char *min = "/environment/metar/visibility[%d]/min-m";
276 const char *max = "/environment/metar/visibility[%d]/max-m";
278 d = dirvis->getVisibility_m();
280 snprintf(s, 128, min, i);
282 snprintf(s, 128, max, i);
286 fgSetInt("/environment/metar/base-wind-range-from",
287 m->getWindRangeFrom() );
288 fgSetInt("/environment/metar/base-wind-range-to",
289 m->getWindRangeTo() );
290 fgSetDouble("/environment/metar/base-wind-speed-kt",
291 m->getWindSpeed_kt() );
292 fgSetDouble("/environment/metar/gust-wind-speed-kt",
293 m->getGustSpeed_kt() );
294 fgSetDouble("/environment/metar/temperature-degc",
295 m->getTemperature_C() );
296 fgSetDouble("/environment/metar/dewpoint-degc",
297 m->getDewpoint_C() );
298 fgSetDouble("/environment/metar/rel-humidity-norm",
299 m->getRelHumidity() );
300 fgSetDouble("/environment/metar/pressure-inhg",
301 m->getPressure_inHg() );
303 vector<SGMetarCloud> cv = m->getClouds();
304 vector<SGMetarCloud>::iterator cloud;
306 const char *cl = "/environment/clouds/layer[%i]";
307 for (i = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, i++) {
308 const char *coverage_string[5] =
309 { "clear", "few", "scattered", "broken", "overcast" };
310 const double thickness[5] = { 0, 65, 600,750, 1000};
313 snprintf(s, 128, cl, i);
314 strncat(s, "/coverage", 128);
315 q = cloud->getCoverage();
316 fgSetString(s, coverage_string[q] );
318 snprintf(s, 128, cl, i);
319 strncat(s, "/elevation-ft", 128);
320 fgSetDouble(s, cloud->getAltitude_ft() + station_elevation_ft);
322 snprintf(s, 128, cl, i);
323 strncat(s, "/thickness-ft", 128);
324 fgSetDouble(s, thickness[q]);
326 snprintf(s, 128, cl, i);
327 strncat(s, "/span-m", 128);
328 fgSetDouble(s, 40000.0);
331 for (; i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
332 snprintf(s, 128, cl, i);
333 strncat(s, "/coverage", 128);
334 fgSetString(s, "clear");
336 snprintf(s, 128, cl, i);
337 strncat(s, "/elevation-ft", 128);
338 fgSetDouble(s, -9999);
340 snprintf(s, 128, cl, i);
341 strncat(s, "/thickness-ft", 128);
344 snprintf(s, 128, cl, i);
345 strncat(s, "/span-m", 128);
346 fgSetDouble(s, 40000.0);
349 fgSetDouble("/environment/metar/rain-norm", m->getRain());
350 fgSetDouble("/environment/metar/hail-norm", m->getHail());
351 fgSetDouble("/environment/metar/snow-norm", m->getSnow());
352 fgSetBool("/environment/metar/snow-cover", m->getSnowCover());
356 FGClouds::update_env_config ()
358 fgSetupWind( fgGetDouble("/environment/metar/base-wind-range-from"),
359 fgGetDouble("/environment/metar/base-wind-range-to"),
360 fgGetDouble("/environment/metar/base-wind-speed-kt"),
361 fgGetDouble("/environment/metar/gust-wind-speed-kt") );
363 fgDefaultWeatherValue( "visibility-m",
364 fgGetDouble("/environment/metar/min-visibility-m") );
366 set_temp_at_altitude( fgGetDouble("/environment/metar/temperature-degc"),
367 station_elevation_ft );
368 set_dewpoint_at_altitude( fgGetDouble("/environment/metar/dewpoint-degc"),
369 station_elevation_ft );
371 fgDefaultWeatherValue( "pressure-sea-level-inhg",
372 fgGetDouble("/environment/metar/pressure-inhg") );
376 void FGClouds::setLayer( int iLayer, float alt_ft, string coverage, string layer_type ) {
377 double coverage_norm = 0.0;
378 if( coverage == "few" )
379 coverage_norm = 2.0/8.0; // <1-2
380 else if( coverage == "scattered" )
381 coverage_norm = 4.0/8.0; // 3-4
382 else if( coverage == "broken" )
383 coverage_norm = 6.0/8.0; // 5-7
384 else if( coverage == "overcast" )
385 coverage_norm = 8.0/8.0; // 8
387 SGCloudField *layer3D = thesky->get_cloud_layer(iLayer)->get_layer3D();
388 buildLayer(layer3D, layer_type, station_elevation_ft + alt_ft * SG_FEET_TO_METER, coverage_norm);
391 void FGClouds::buildScenario( string scenario ) {
393 string station = fgGetString("/environment/metar/station-id", "XXXX");
395 // fetch station elevation if exists
396 FGAirport a = globals->get_airports()->search( station );
397 station_elevation_ft = a.getElevation();
399 for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
400 thesky->get_cloud_layer(iLayer)->get_layer3D()->clear();
403 station += " 011000Z ";
404 if( scenario == "Fair weather" ) {
405 fakeMetar = "15003KT 12SM SCT033 FEW200 20/08 Q1015 NOSIG";
406 setLayer(0, 3300.0, "scattered", "cu");
407 } else if( scenario == "Thunderstorm" ) {
408 fakeMetar = "15012KT 08SM TSRA SCT040 BKN070 20/12 Q0995";
409 setLayer(0, 4000.0, "scattered", "cb");
410 setLayer(1, 7000.0, "scattered", "ns");
413 FGMetar *m = new FGMetar( station + fakeMetar );
414 update_metar_properties( m );
416 fgSetString("/environment/metar/last-metar", m->getData());
417 // TODO:desactivate real metar updates
418 if( scenario == "Fair weather" ) {
419 fgSetString("/environment/clouds/layer[1]/coverage", "cirrus");
424 void FGClouds::build(void) {
425 string scenario = fgGetString("/environment/weather-scenario", "METAR");
427 if( scenario == "METAR" ) {
428 string realMetar = fgGetString("/environment/metar/last-metar", "");
429 if( realMetar != "" ) {
430 FGMetar *m = new FGMetar( realMetar );
431 update_metar_properties( m );
437 buildScenario( scenario );
440 if( snd_lightning == NULL )