]> git.mxchange.org Git - flightgear.git/blobdiff - src/Environment/metarproperties.cxx
Merge branch 'next' of gitorious.org:fg/flightgear into next
[flightgear.git] / src / Environment / metarproperties.cxx
index 5860259675eee50d5b2ecb30cdaed1916cf3f63a..ea605d73a2872b770bc1d7b95b6439dfcf57ce41 100644 (file)
@@ -35,6 +35,8 @@ using std::string;
 
 namespace Environment {
 
+static vector<string> coverage_string;
+
 MetarProperties::MetarProperties( SGPropertyNode_ptr rootNode ) :
   _rootNode(rootNode),
   _metarValidNode( rootNode->getNode( "valid", true ) ),
@@ -62,6 +64,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 +100,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 +124,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();
 
@@ -186,6 +197,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( description == "MI" ) isMI = true;
+                if( description == "BC" ) isBC = true;
+                if( description == "BR" ) isBR = true;
+            }
+        }
+    }
+
     vector<SGMetarCloud> cv = m->getClouds();
     vector<SGMetarCloud>::const_iterator cloud, cloud_end = cv.end();
 
@@ -193,17 +236,65 @@ 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!
+        bool setGroundCloudLayer = _rootNode->getBoolValue("set-ground-cloud-layer", 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_BELOW_STATION_ELEVATION=200;
+
+            SGMetarCloud::Coverage coverage = SGMetarCloud::COVERAGE_NIL;
+            double thickness = 0;
+            double alpha = 1.0;
+
+            if( isFG ) { // fog
+                coverage = isBC ? SGMetarCloud::COVERAGE_SCATTERED : SGMetarCloud::COVERAGE_BROKEN;
+                thickness = isMI ? 
+                   30 + LAYER_BOTTOM_BELOW_STATION_ELEVATION : // shallow fog, 10m/30ft
+                   500 + LAYER_BOTTOM_BELOW_STATION_ELEVATION; // fog, 150m/500ft
+                alpha = 1.0;
+
+            } else if( isBR ) { // mist
+                coverage = SGMetarCloud::COVERAGE_OVERCAST;
+                thickness = 2000 + LAYER_BOTTOM_BELOW_STATION_ELEVATION;
+                alpha = 0.8;
+            } else if( isHZ ) { // hase
+                coverage = SGMetarCloud::COVERAGE_OVERCAST;
+                thickness = 2000 + LAYER_BOTTOM_BELOW_STATION_ELEVATION;
+                alpha = 0.6;
+            }
+            // fog is "overcast" by default of "broken" for patches of fog
+            if( coverage != SGMetarCloud::COVERAGE_NIL ) {
+                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_BELOW_STATION_ELEVATION );
+                layerNode->setDoubleValue( "thickness-ft", thickness );
+                layerNode->setDoubleValue( "visibility-m", _min_visibility );
+                layerNode->setDoubleValue( "alpha", alpha );
+                _min_visibility = _max_visibility = 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();