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_ctrl.hxx"
37 #include "environment_mgr.hxx"
38 #include "fgmetar.hxx"
39 #include "fgclouds.hxx"
44 FGClouds::FGClouds(FGEnvironmentCtrl * controller) :
45 station_elevation_ft(0.0),
46 _controller( controller ),
50 fgSetString("/environment/weather-scenario", "METAR");
52 FGClouds::~FGClouds() {
55 int FGClouds::get_update_event(void) const {
58 void FGClouds::set_update_event(int count) {
63 void FGClouds::init(void) {
64 if( snd_lightning == NULL ) {
65 snd_lightning = new SGSoundSample(globals->get_fg_root().c_str(), "Sounds/thunder.wav", true);
66 snd_lightning->set_max_dist(7000.0f);
67 snd_lightning->set_reference_dist(3000.0f);
68 SGSoundMgr *soundMgr = globals->get_soundmgr();
69 soundMgr->add( snd_lightning, "thunder" );
70 sgEnviro.set_soundMgr( soundMgr );
74 SGNewCloud *FGClouds::buildCloud(SGPropertyNode *cloud_def_root, string name) {
75 SGPropertyNode *cld_def=NULL;
77 cld_def = cloud_def_root->getChild(name.c_str());
78 string base_name = name.substr(0,2);
80 if( name[2] == '-' ) {
81 cld_def = cloud_def_root->getChild(base_name.c_str());
86 string familly = cld_def->getStringValue("familly", base_name.c_str());
87 SGNewCloud *cld = new SGNewCloud(familly);
88 for(int i = 0; i < cld_def->nChildren() ; i++) {
89 SGPropertyNode *abox = cld_def->getChild(i);
90 if( strcmp(abox->getName(), "box") == 0) {
91 double x = abox->getDoubleValue("x");
92 double y = abox->getDoubleValue("y");
93 double z = abox->getDoubleValue("z");
94 double size = abox->getDoubleValue("size");
95 int type = abox->getIntValue("type", SGNewCloud::CLbox_standard);
96 cld->addContainer(x, y, z, size, (SGNewCloud::CLbox_type) type);
103 void FGClouds::buildLayer(SGCloudField *layer, string name, double alt, double coverage) {
108 int CloudVarietyCount = 0;
109 double totalCount = 0.0;
111 SGPropertyNode *cloud_def_root = fgGetNode("/environment/cloudlayers/clouds", false);
112 SGPropertyNode *layer_def_root = fgGetNode("/environment/cloudlayers/layers", false);
115 // when we don't generate clouds the layer is rendered in 2D
116 if( coverage == 0.0 )
118 if( layer_def_root == NULL || cloud_def_root == NULL)
120 if( name == "ci" || name == "sc" || name == "st")
123 SGPropertyNode *layer_def=NULL;
125 layer_def = layer_def_root->getChild(name.c_str());
127 if( name[2] == '-' ) {
128 string base_name = name.substr(0,2);
129 layer_def = layer_def_root->getChild(base_name.c_str());
135 double grid_x_size = layer_def->getDoubleValue("grid-x-size", 1000.0);
136 double grid_y_size = layer_def->getDoubleValue("grid-y-size", 1000.0);
137 double grid_x_rand = layer_def->getDoubleValue("grid-x-rand", grid_x_size);
138 double grid_y_rand = layer_def->getDoubleValue("grid-y-rand", grid_y_size);
139 double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
141 for(int i = 0; i < layer_def->nChildren() ; i++) {
142 SGPropertyNode *acloud = layer_def->getChild(i);
143 if( strcmp(acloud->getName(), "cloud") == 0) {
144 string cloud_name = acloud->getStringValue("name");
145 tCloudVariety[CloudVarietyCount].name = cloud_name;
146 double count = acloud->getDoubleValue("count", 1.0);
147 tCloudVariety[CloudVarietyCount].count = count;
149 cloud_name = cloud_name + "-%d";
150 char variety_name[50];
153 snprintf(variety_name, sizeof(variety_name), cloud_name.c_str(), variety);
154 } while( cloud_def_root->getChild(variety_name, 0, false) );
157 if( CloudVarietyCount < 20 )
161 totalCount = 1.0 / totalCount;
162 double currCoverage = 0.0;
164 for(double px = 0.0; px < SGCloudField::fieldSize; px += grid_x_size) {
165 for(double py = 0.0; py < SGCloudField::fieldSize; py += grid_y_size) {
166 double x = px + grid_x_rand * (sg_random() - 0.5);
167 double y = py + grid_y_rand * (sg_random() - 0.5);
168 double z = alt + grid_z_rand * (sg_random() - 0.5);
169 double choice = sg_random();
170 currCoverage += coverage;
171 if( currCoverage < 1.0 )
175 for(int i = 0; i < CloudVarietyCount ; i ++) {
176 choice -= tCloudVariety[i].count * totalCount;
177 if( choice <= 0.0 ) {
178 SGNewCloud *cld = buildCloud(cloud_def_root, tCloudVariety[i].name);
181 layer->addCloud(pos, cld);
191 // TODO:call this after real metar updates
192 void FGClouds::buildMETAR(void) {
193 SGPropertyNode *metar_root = fgGetNode("/environment", true);
195 double wind_speed_kt = metar_root->getDoubleValue("wind-speed-kt");
196 double temperature_degc = metar_root->getDoubleValue("temperature-sea-level-degc");
197 double dewpoint_degc = metar_root->getDoubleValue("dewpoint-sea-level-degc");
198 double pressure_mb = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
200 double dewp = pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
201 double temp = pow(10.0, 7.5 * temperature_degc / (237.7 + temperature_degc));
202 double rel_humidity = dewp * 100 / temp;
204 // formule d'Epsy, base d'un cumulus
205 double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
206 double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
208 bool cu_seen = false;
210 for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
211 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
213 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
214 double alt_m = alt_ft * SG_FEET_TO_METER;
215 string coverage = cloud_root->getStringValue("coverage");
216 double coverage_norm = 0.0;
217 if( coverage == "few" )
218 coverage_norm = 2.0/8.0; // <1-2
219 else if( coverage == "scattered" )
220 coverage_norm = 4.0/8.0; // 3-4
221 else if( coverage == "broken" )
222 coverage_norm = 6.0/8.0; // 5-7
223 else if( coverage == "overcast" )
224 coverage_norm = 8.0/8.0; // 8
226 string layer_type = "nn";
227 if( coverage == "cirrus" ) {
229 } else if( alt_ft > 16500 ) {
230 // layer_type = "ci|cs|cc";
232 } else if( alt_ft > 6500 ) {
233 // layer_type = "as|ac|ns";
235 if( pressure_mb < 1005.0 && coverage_norm >= 5.5 )
238 // layer_type = "st|cu|cb|sc";
239 // +/- 20% from stratus probable base
240 if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m )
242 // +/- 20% from cumulus probable base
243 else if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m )
246 // above formulae is far from perfect
249 else if( alt_ft < 4500 )
256 SGCloudField *layer3D = thesky->get_cloud_layer(iLayer)->get_layer3D();
257 buildLayer(layer3D, layer_type, alt_m, coverage_norm);
261 // copy from FGMetarEnvironmentCtrl until better
263 FGClouds::update_metar_properties( FGMetar *m )
269 fgSetString("/environment/metar/station-id", m->getId());
270 fgSetDouble("/environment/metar/min-visibility-m",
271 m->getMinVisibility().getVisibility_m() );
272 fgSetDouble("/environment/metar/max-visibility-m",
273 m->getMaxVisibility().getVisibility_m() );
275 SGMetarVisibility *dirvis = m->getDirVisibility();
276 for (i = 0; i < 8; i++, dirvis++) {
277 const char *min = "/environment/metar/visibility[%d]/min-m";
278 const char *max = "/environment/metar/visibility[%d]/max-m";
280 d = dirvis->getVisibility_m();
282 snprintf(s, 128, min, i);
284 snprintf(s, 128, max, i);
288 fgSetInt("/environment/metar/base-wind-range-from",
289 m->getWindRangeFrom() );
290 fgSetInt("/environment/metar/base-wind-range-to",
291 m->getWindRangeTo() );
292 fgSetDouble("/environment/metar/base-wind-speed-kt",
293 m->getWindSpeed_kt() );
294 fgSetDouble("/environment/metar/gust-wind-speed-kt",
295 m->getGustSpeed_kt() );
296 fgSetDouble("/environment/metar/temperature-degc",
297 m->getTemperature_C() );
298 fgSetDouble("/environment/metar/dewpoint-degc",
299 m->getDewpoint_C() );
300 fgSetDouble("/environment/metar/rel-humidity-norm",
301 m->getRelHumidity() );
302 fgSetDouble("/environment/metar/pressure-inhg",
303 m->getPressure_inHg() );
305 vector<SGMetarCloud> cv = m->getClouds();
306 vector<SGMetarCloud>::iterator cloud;
308 const char *cl = "/environment/clouds/layer[%i]";
309 for (i = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, i++) {
310 const char *coverage_string[5] =
311 { "clear", "few", "scattered", "broken", "overcast" };
312 const double thickness[5] = { 0, 65, 600,750, 1000};
315 snprintf(s, 128, cl, i);
316 strncat(s, "/coverage", 128);
317 q = cloud->getCoverage();
318 fgSetString(s, coverage_string[q] );
320 snprintf(s, 128, cl, i);
321 strncat(s, "/elevation-ft", 128);
322 fgSetDouble(s, cloud->getAltitude_ft() + station_elevation_ft);
324 snprintf(s, 128, cl, i);
325 strncat(s, "/thickness-ft", 128);
326 fgSetDouble(s, thickness[q]);
328 snprintf(s, 128, cl, i);
329 strncat(s, "/span-m", 128);
330 fgSetDouble(s, 40000.0);
333 for (; i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
334 snprintf(s, 128, cl, i);
335 strncat(s, "/coverage", 128);
336 fgSetString(s, "clear");
338 snprintf(s, 128, cl, i);
339 strncat(s, "/elevation-ft", 128);
340 fgSetDouble(s, -9999);
342 snprintf(s, 128, cl, i);
343 strncat(s, "/thickness-ft", 128);
346 snprintf(s, 128, cl, i);
347 strncat(s, "/span-m", 128);
348 fgSetDouble(s, 40000.0);
351 fgSetDouble("/environment/metar/rain-norm", m->getRain());
352 fgSetDouble("/environment/metar/hail-norm", m->getHail());
353 fgSetDouble("/environment/metar/snow-norm", m->getSnow());
354 fgSetBool("/environment/metar/snow-cover", m->getSnowCover());
358 FGClouds::update_env_config ()
360 fgSetupWind( fgGetDouble("/environment/metar/base-wind-range-from"),
361 fgGetDouble("/environment/metar/base-wind-range-to"),
362 fgGetDouble("/environment/metar/base-wind-speed-kt"),
363 fgGetDouble("/environment/metar/gust-wind-speed-kt") );
365 fgDefaultWeatherValue( "visibility-m",
366 fgGetDouble("/environment/metar/min-visibility-m") );
368 set_temp_at_altitude( fgGetDouble("/environment/metar/temperature-degc"),
369 station_elevation_ft );
370 set_dewpoint_at_altitude( fgGetDouble("/environment/metar/dewpoint-degc"),
371 station_elevation_ft );
373 fgDefaultWeatherValue( "pressure-sea-level-inhg",
374 fgGetDouble("/environment/metar/pressure-inhg") );
378 void FGClouds::setLayer( int iLayer, float alt_ft, string coverage, string layer_type ) {
379 double coverage_norm = 0.0;
380 if( coverage == "few" )
381 coverage_norm = 2.0/8.0; // <1-2
382 else if( coverage == "scattered" )
383 coverage_norm = 4.0/8.0; // 3-4
384 else if( coverage == "broken" )
385 coverage_norm = 6.0/8.0; // 5-7
386 else if( coverage == "overcast" )
387 coverage_norm = 8.0/8.0; // 8
389 SGCloudField *layer3D = thesky->get_cloud_layer(iLayer)->get_layer3D();
390 buildLayer(layer3D, layer_type, station_elevation_ft + alt_ft * SG_FEET_TO_METER, coverage_norm);
393 void FGClouds::buildScenario( string scenario ) {
395 string station = fgGetString("/environment/metar/station-id", "XXXX");
397 // fetch station elevation if exists
398 if( station == "XXXX" )
399 station_elevation_ft = fgGetDouble("/position/ground-elev-m", 0.0);
401 FGAirport a = globals->get_airports()->search( station );
402 station_elevation_ft = a.getElevation();
405 for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
406 thesky->get_cloud_layer(iLayer)->get_layer3D()->clear();
409 station += " 011000Z ";
410 if( scenario == "Fair weather" ) {
411 fakeMetar = "15003KT 12SM SCT033 FEW200 20/08 Q1015 NOSIG";
412 setLayer(0, 3300.0, "scattered", "cu");
413 } else if( scenario == "Thunderstorm" ) {
414 fakeMetar = "15012KT 08SM TSRA SCT040 BKN070 20/12 Q0995";
415 setLayer(0, 4000.0, "scattered", "cb");
416 setLayer(1, 7000.0, "scattered", "ns");
419 FGMetar *m = new FGMetar( station + fakeMetar );
420 update_metar_properties( m );
422 // propagate aloft tables
423 _controller->reinit();
425 fgSetString("/environment/metar/last-metar", m->getData());
426 // TODO:desactivate real metar updates
427 if( scenario == "Fair weather" ) {
428 fgSetString("/environment/clouds/layer[1]/coverage", "cirrus");
433 void FGClouds::build(void) {
434 string scenario = fgGetString("/environment/weather-scenario", "METAR");
436 if( scenario == "METAR" ) {
437 string realMetar = fgGetString("/environment/metar/real-metar", "");
438 if( realMetar != "" ) {
439 fgSetString("/environment/metar/last-metar", realMetar.c_str());
440 FGMetar *m = new FGMetar( realMetar );
441 update_metar_properties( m );
443 // propagate aloft tables
444 _controller->reinit();
449 buildScenario( scenario );
452 if( snd_lightning == NULL )