]> git.mxchange.org Git - flightgear.git/blobdiff - src/Environment/metarproperties.cxx
cmake changes for osg::CullSettings::ClearMask and Fedora
[flightgear.git] / src / Environment / metarproperties.cxx
index 5860259675eee50d5b2ecb30cdaed1916cf3f63a..2f6cac2be2525b01809f289190645f89ce11a9d9 100644 (file)
 #include "fgmetar.hxx"
 #include "environment.hxx"
 #include "atmosphere.hxx"
+#include "metarairportfilter.hxx"
 #include <simgear/scene/sky/cloud.hxx>
 #include <simgear/structure/exception.hxx>
+#include <Main/fg_props.hxx>
 
 using std::string;
 
 namespace Environment {
 
+static vector<string> coverage_string;
+
 MetarProperties::MetarProperties( SGPropertyNode_ptr rootNode ) :
   _rootNode(rootNode),
   _metarValidNode( rootNode->getNode( "valid", true ) ),
@@ -62,6 +66,14 @@ MetarProperties::MetarProperties( SGPropertyNode_ptr rootNode ) :
   _snow(0.0),
   _snow_cover(false)
 {
+  // Hack to avoid static initialization order problems on OSX
+  if( coverage_string.size() == 0 ) {
+    coverage_string.push_back(SGCloudLayer::SG_CLOUD_CLEAR_STRING);
+    coverage_string.push_back(SGCloudLayer::SG_CLOUD_FEW_STRING);
+    coverage_string.push_back(SGCloudLayer::SG_CLOUD_SCATTERED_STRING);
+    coverage_string.push_back(SGCloudLayer::SG_CLOUD_BROKEN_STRING);
+    coverage_string.push_back(SGCloudLayer::SG_CLOUD_OVERCAST_STRING);
+  }
   // don't tie metar-valid, so listeners get triggered
   _metarValidNode->setBoolValue( false );
   _tiedProperties.setRoot( _rootNode );
@@ -90,19 +102,13 @@ MetarProperties::MetarProperties( SGPropertyNode_ptr rootNode ) :
   _tiedProperties.Tie("hail-norm", &_hail );
   _tiedProperties.Tie("snow-norm", &_snow);
   _tiedProperties.Tie("snow-cover", &_snow_cover );
+  _tiedProperties.Tie("decoded", this, &MetarProperties::get_decoded );
 }
 
 MetarProperties::~MetarProperties()
 {
 }
 
-static const string coverage_string[] = { 
-  SGCloudLayer::SG_CLOUD_CLEAR_STRING,
-  SGCloudLayer::SG_CLOUD_FEW_STRING,
-  SGCloudLayer::SG_CLOUD_SCATTERED_STRING,
-  SGCloudLayer::SG_CLOUD_BROKEN_STRING,
-  SGCloudLayer::SG_CLOUD_OVERCAST_STRING,
-};
 
 static const double thickness_value[] = { 0, 65, 600, 750, 1000 };
 
@@ -120,6 +126,13 @@ void MetarProperties::set_metar( const char * metar )
         return;
     }
 
+    _decoded.clear();
+    const vector<string> weather = m->getWeather();
+    for( vector<string>::const_iterator it = weather.begin(); it != weather.end(); it++ ) {
+        if( false == _decoded.empty() ) _decoded.append(", ");
+        _decoded.append(*it);
+    }
+
     _min_visibility = m->getMinVisibility().getVisibility_m();
     _max_visibility = m->getMaxVisibility().getVisibility_m();
 
@@ -149,13 +162,15 @@ void MetarProperties::set_metar( const char * metar )
     {
         // 1. check the id given in the metar
         FGAirport* a = FGAirport::findByIdent(m->getId());
-/*
+
         // 2. if unknown, find closest airport with metar to current position
         if( a == NULL ) {
-            SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(), _latitude_n->getDoubleValue());
-            a = FGAirport::findClosest(pos, 10000.0, &_airportWithMetarFilter);
+            SGGeod pos = SGGeod::fromDeg(
+                fgGetDouble( "/position/longitude-deg", 0.0 ),
+                fgGetDouble( "/position/latitude-deg", 0.0 ) );
+            a = FGAirport::findClosest(pos, 10000.0, MetarAirportFilter::instance() );
         }
-*/
+
         // 3. otherwise use ground elevation
         if( a != NULL ) {
             _station_elevation = a->getElevation();
@@ -164,12 +179,10 @@ void MetarProperties::set_metar( const char * metar )
             _station_longitude = towerPosition.getLongitudeDeg();
             _station_id = a->ident();
         } else {
-            _station_elevation = 0.0;
-            _station_latitude = 0.0;
-            _station_longitude = 0.0;
+            _station_elevation = fgGetDouble("/position/ground-elev-m", 0.0 ) * SG_METER_TO_FEET;
+            _station_latitude = fgGetDouble( "/position/latitude-deg", 0.0 );
+            _station_longitude = fgGetDouble( "/position/longitude-deg", 0.0 );
             _station_id = "XXXX";
-//            station_elevation_ft = _ground_elevation_n->getDoubleValue() * SG_METER_TO_FEET;
-//            _station_id = m->getId();
         }
     }
 
@@ -186,6 +199,38 @@ void MetarProperties::set_metar( const char * metar )
         _sea_level_pressure = P_layer(0, elevation_m, fieldPressure, _temperature + atmodel::freezing, atmodel::ISA::lam0) / atmodel::inHg;
     }
 
+    bool isBC = false;
+    bool isBR = false;
+    bool isFG = false;
+    bool isMI = false;
+    bool isHZ = false;
+
+    {
+        for( unsigned i = 0; i < 3; i++ ) {
+            SGPropertyNode_ptr n = _rootNode->getChild("weather", i, true );
+            vector<struct SGMetar::Weather> weather = m->getWeather2();
+            struct SGMetar::Weather * w = i < weather.size() ? &weather[i] : NULL;
+            n->getNode("intensity",true)->setIntValue( w != NULL ? w->intensity : 0 );
+            n->getNode("vincinity",true)->setBoolValue( w != NULL ? w->vincinity : false );
+            for( unsigned j = 0; j < 3; j++ ) { 
+
+                const string & phenomenon = w != NULL && j < w->phenomena.size() ? w->phenomena[j].c_str() : "";
+                n->getChild( "phenomenon", j, true )->setStringValue( phenomenon );
+
+                const string & description = w != NULL && j < w->descriptions.size() ? w->descriptions[j].c_str() : "";
+                n->getChild( "description", j, true )->setStringValue( description );
+
+                // need to know later, 
+                // if its fog(FG) (might be shallow(MI) or patches(BC)) or haze (HZ) or mist(BR)
+                if( phenomenon == "FG" ) isFG = true;
+                if( phenomenon == "HZ" ) isHZ = true;
+                if( phenomenon == "BR" ) isBR = true;
+                if( description == "MI" ) isMI = true;
+                if( description == "BC" ) isBC = true;
+            }
+        }
+    }
+
     vector<SGMetarCloud> cv = m->getClouds();
     vector<SGMetarCloud>::const_iterator cloud, cloud_end = cv.end();
 
@@ -193,17 +238,79 @@ void MetarProperties::set_metar( const char * metar )
         static const char * LAYER = "layer";
         SGPropertyNode_ptr cloudsNode = _rootNode->getNode("clouds", true );
         const vector<SGMetarCloud> & metarClouds = m->getClouds();
-        for( unsigned i = 0; i < 5; i++ ) {
-            SGPropertyNode_ptr layerNode = cloudsNode->getChild(LAYER, i, true );
-            int coverage = i < metarClouds.size() ? metarClouds[i].getCoverage() : 0;
-            double elevation = i >= metarClouds.size() || coverage == 0 ? -9999.0 : metarClouds[i].getAltitude_ft() + _station_elevation;
+        unsigned layerOffset = 0; // Oh, this is ugly!
+
+        // fog/mist/haze cloud layer does not work with 3d clouds yet :-(
+        bool setGroundCloudLayer = _rootNode->getBoolValue("set-ground-cloud-layer", false ) &&
+              false == (fgGetBool("/sim/rendering/shader-effects", false ) && 
+                        fgGetBool("/sim/rendering/clouds3d-enable", false ) );
+
+        if( setGroundCloudLayer ) {
+            // create a cloud layer #0 starting at the ground if its fog, mist or haze
+
+            // make sure layer actually starts at ground and set it's bottom at a constant
+            // value below the station's elevation
+            const double LAYER_BOTTOM_STATION_OFFSET =
+              fgGetDouble( "/environment/params/fog-mist-haze-layer/offset-from-station-elevation-ft", -200 );
+
+            SGMetarCloud::Coverage coverage = SGMetarCloud::COVERAGE_NIL;
+            double thickness = 0;
+            double alpha = 1.0;
+
+            if( isFG ) { // fog
+                coverage = SGMetarCloud::getCoverage( isBC ? 
+                    fgGetString( "/environment/params/fog-mist-haze-layer/fog-bc-2dlayer-coverage", SGMetarCloud::COVERAGE_SCATTERED_STRING ) :
+                    fgGetString( "/environment/params/fog-mist-haze-layer/fog-2dlayer-coverage", SGMetarCloud::COVERAGE_BROKEN_STRING )
+                );
+
+                thickness = isMI ? 
+                   fgGetDouble("/environment/params/fog-mist-haze-layer/fog-shallow-thickness-ft",30) - LAYER_BOTTOM_STATION_OFFSET : // shallow fog, 10m/30ft
+                   fgGetDouble("/environment/params/fog-mist-haze-layer/fog-thickness-ft",500) - LAYER_BOTTOM_STATION_OFFSET; // fog, 150m/500ft
+                alpha =  fgGetDouble("/environment/params/fog-mist-haze-layer/fog-2dlayer-alpha", 1.0);
+            } else if( isBR ) { // mist
+                coverage = SGMetarCloud::getCoverage(fgGetString("/environment/params/fog-mist-haze-layer/mist-2dlayer-coverage", SGMetarCloud::COVERAGE_OVERCAST_STRING));
+                thickness =  fgGetDouble("/environment/params/fog-mist-haze-layer/mist-thickness-ft",2000) - LAYER_BOTTOM_STATION_OFFSET;
+                alpha =  fgGetDouble("/environment/params/fog-mist-haze-layer/mist-2dlayer-alpha",0.8);
+            } else if( isHZ ) { // haze
+                coverage = SGMetarCloud::getCoverage(fgGetString("/environment/params/fog-mist-haze-layer/mist-2dlayer-coverage", SGMetarCloud::COVERAGE_OVERCAST_STRING));
+                thickness =  fgGetDouble("/environment/params/fog-mist-haze-layer/haze-thickness-ft",2000) - LAYER_BOTTOM_STATION_OFFSET;
+                alpha =  fgGetDouble("/environment/params/fog-mist-haze-layer/haze-2dlayer-alpha",0.6);
+            }
+
+            if( coverage != SGMetarCloud::COVERAGE_NIL ) {
+
+                // if there is a layer above the fog, limit the top to one foot below that layer's bottom
+                if( metarClouds[0].getCoverage() != SGMetarCloud::COVERAGE_CLEAR )
+                    thickness = metarClouds[0].getAltitude_ft() - LAYER_BOTTOM_STATION_OFFSET - 1;
+
+                SGPropertyNode_ptr layerNode = cloudsNode->getChild(LAYER, 0, true );
+                layerNode->setDoubleValue( "coverage-type", SGCloudLayer::getCoverageType(coverage_string[coverage]) );
+                layerNode->setStringValue( "coverage", coverage_string[coverage] );
+                layerNode->setDoubleValue( "elevation-ft", _station_elevation + LAYER_BOTTOM_STATION_OFFSET );
+                layerNode->setDoubleValue( "thickness-ft", thickness );
+                layerNode->setDoubleValue( "visibility-m", _min_visibility );
+                layerNode->setDoubleValue( "alpha", alpha );
+                _min_visibility = _max_visibility =
+                  fgGetDouble("/environment/params/fog-mist-haze-layer/visibility-above-layer-m",20000.0); // assume good visibility above the fog
+                layerOffset = 1;  // shudder
+            }
+        } 
+
+        for( unsigned i = 0; i < 5-layerOffset; i++ ) {
+            SGPropertyNode_ptr layerNode = cloudsNode->getChild(LAYER, i+layerOffset, true );
+            SGMetarCloud::Coverage coverage = i < metarClouds.size() ? metarClouds[i].getCoverage() : SGMetarCloud::COVERAGE_CLEAR;
+            double elevation = 
+                i >= metarClouds.size() || coverage == SGMetarCloud::COVERAGE_CLEAR ? 
+                -9999.0 : 
+                metarClouds[i].getAltitude_ft() + _station_elevation;
+
             layerNode->setStringValue( "coverage", coverage_string[coverage] );
             layerNode->setDoubleValue( "coverage-type", SGCloudLayer::getCoverageType(coverage_string[coverage]) );
             layerNode->setDoubleValue( "elevation-ft", elevation );
             layerNode->setDoubleValue( "thickness-ft", thickness_value[coverage]);
             layerNode->setDoubleValue( "span-m", 40000 );
+            layerNode->setDoubleValue( "visibility-m", 50.0 );
         }
-
     }
 
     _rain = m->getRain();