]> git.mxchange.org Git - flightgear.git/blobdiff - src/Environment/environment_ctrl.cxx
ignore resets for now because every z/Z key press would trigger a call to NOAA. We...
[flightgear.git] / src / Environment / environment_ctrl.cxx
index eb2761b12a58099081fbb36401c2e866c4004583..6e6f350c99296bb644c3674fdc1a162bf2304982 100644 (file)
 #include <stdlib.h>
 #include <algorithm>
 
+#include <simgear/structure/exception.hxx>
+
 #include <Main/fg_props.hxx>
 
+#include "environment_mgr.hxx"
 #include "environment_ctrl.hxx"
 
 SG_USING_STD(sort);
 
+// FIXME, from options.cxx
+extern void fgSetupWind (double min_hdg, double max_hdg, double speed, double gust);
+
 
 \f
 ////////////////////////////////////////////////////////////////////////
@@ -303,4 +309,228 @@ FGInterpolateEnvironmentCtrl::bucket::operator< (const bucket &b) const
 }
 
 
+\f
+////////////////////////////////////////////////////////////////////////
+// Implementation of FGMetarEnvironmentCtrl.
+////////////////////////////////////////////////////////////////////////
+
+FGMetarEnvironmentCtrl::FGMetarEnvironmentCtrl ()
+    : env( new FGInterpolateEnvironmentCtrl ),
+      _icao( strdup( fgGetString("/sim/presets/airport-id") ) )
+{
+}
+
+FGMetarEnvironmentCtrl::~FGMetarEnvironmentCtrl ()
+{
+    if (_icao) {
+      free(_icao);
+      _icao = NULL;
+    }
+
+   delete env;
+   env = NULL;
+}
+
+
+void
+FGMetarEnvironmentCtrl::init ()
+{
+    if (_icao != NULL) {
+        free(_icao);
+        _icao = NULL;
+    }
+
+    fetch_data(_icao);
+    fgSetupWind( fgGetDouble("/environment/metar/base-wind-range-from"),
+                 fgGetDouble("/environment/metar/base-wind-range-to"),
+                 fgGetDouble("/environment/metar/base-wind-speed-kt"),
+                 fgGetDouble("/environment/metar/gust-wind-speed-kt") );
+
+    fgSetDouble("/environment/visibility-m",
+        fgGetDouble("/environment/metar/min-visibility-m"));
+    fgSetDouble("/environment/temperature-degc",
+        fgGetDouble("/environment/metar/temperature-degc"));
+    fgSetDouble("/environment/dewpoint-degc",
+        fgGetDouble("/environment/metar/dewpoint-degc"));
+    fgSetDouble("/environment/pressure-inhg",
+        fgGetDouble("/environment/metar/pressure-inhg"));
+
+    env->init();
+}
+
+void
+FGMetarEnvironmentCtrl::reinit ()
+{
+#if 0
+    if (_icao != NULL) {
+        free(_icao);
+        _icao = NULL;
+    }
+
+    fetch_data(_icao);
+    fgSetupWind( fgGetDouble("/environment/metar/base-wind-range-from"),
+                 fgGetDouble("/environment/metar/base-wind-range-to"),
+                 fgGetDouble("/environment/metar/base-wind-speed-kt"),
+                 fgGetDouble("/environment/metar/gust-wind-speed-kt") );
+
+    fgSetDouble("/environment/visibility-m",
+        fgGetDouble("/environment/metar/min-visibility-m"));
+    fgSetDouble("/environment/pressure-inhg",
+        fgGetDouble("/environment/metar/pressure-inhg"));
+
+    // FIXME: The following seem to egt overriden?
+    fgSetDouble("/environment/temperature-degc",
+        fgGetDouble("/environment/metar/temperature-degc"));
+    fgSetDouble("/environment/dewpoint-degc",
+        fgGetDouble("/environment/metar/dewpoint-degc"));
+#endif
+
+    env->reinit();
+}
+
+void
+FGMetarEnvironmentCtrl::update(double delta_time_sec)
+{
+    env->update(delta_time_sec);
+}
+
+void
+FGMetarEnvironmentCtrl::setEnvironment (FGEnvironment * environment)
+{
+    env->setEnvironment(environment);
+}
+
+void
+FGMetarEnvironmentCtrl::fetch_data (const char *icao)
+{
+    char s[128];
+    double d, dt;
+    int i;
+
+    if ((icao == NULL) && (_icao == NULL)) {
+        _icao = strdup( fgGetString("/sim/presets/airport-id") );
+
+    } else if (icao != NULL) {
+        if (_icao != NULL)
+            free(_icao);
+
+        _icao = strdup(icao);
+    }
+
+    SGMetar *m;
+    try {
+        m = new SGMetar(_icao);
+    } catch (const sg_io_exception& e) {
+        SG_LOG( SG_GENERAL, SG_WARN, "Error fetching live weather data: "
+                                      << e.getFormattedMessage().c_str() );
+        return;
+    }
+
+    d = m->getMinVisibility().getVisibility_m();
+    d = (d != SGMetarNaN) ? d : 10000;
+    fgSetDouble("/environment/metar/min-visibility-m", d);
+
+    dt =  m->getMaxVisibility().getVisibility_m();
+    d = (dt != SGMetarNaN) ? dt : d;
+    fgSetDouble("/environment/metar/max-visibility-m", d);
+
+    SGMetarVisibility *dirvis = m->getDirVisibility();
+    for (i = 0; i < 8; i++, dirvis++) {
+        const char *min = "/environment/metar/visibility[%d]/min-m";
+        const char *max = "/environment/metar/visibility[%d]/max-m";
+        char s[128];
+
+        d = dirvis->getVisibility_m();
+        d = (d != SGMetarNaN) ? d : 10000;
+
+        snprintf(s, 128, min, i);
+        fgSetDouble(s, d);
+        snprintf(s, 128, max, i);
+        fgSetDouble(s, d);
+    }
+
+    i = m->getWindDir();
+    if ( i == -1 ) {
+        fgSetInt("/environment/metar/base-wind-range-from",
+                    m->getWindRangeFrom() );
+        fgSetInt("/environment/metar/base-wind-range-to",
+                    m->getWindRangeTo() );
+    } else {
+        fgSetInt("/environment/metar/base-wind-range-from", i);
+        fgSetInt("/environment/metar/base-wind-range-to", i);
+    }
+    fgSetDouble("/environment/metar/base-wind-speed-kt",
+                m->getWindSpeed_kt() );
+
+    d = m->getGustSpeed_kt();
+    d = (d != SGMetarNaN) ? d : 0.0;
+    fgSetDouble("/environment/metar/gust-wind-speed-kt", d);
+
+    d = m->getTemperature_C();
+    if (d != SGMetarNaN) {
+        dt = m->getDewpoint_C();
+        dt = (dt != SGMetarNaN) ? dt : 0.0;
+        fgSetDouble("/environment/metar/dewpoint-degc", dt);
+        fgSetDouble("/environment/metar/rel-humidity-norm",
+                    m->getRelHumidity() );
+    }   
+    d = (d != SGMetarNaN) ? d : 15.0;
+    fgSetDouble("/environment/metar/temperature-degc", d);
+
+    d = m->getPressure_inHg();
+    d = (d != SGMetarNaN) ? d : 30.0;
+    fgSetDouble("/environment/metar/pressure-inhg", d);
+
+    vector<SGMetarCloud> cv = m->getClouds();
+    vector<SGMetarCloud>::iterator cloud;
+
+    const char *cl = "/environment/clouds/layer[%i]";
+    for (i = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, i++) {
+        const char *coverage_string[5] = 
+            { "clear", "few", "scattered", "broken", "overcast" };
+        const double thickness[5] = { 0, 65, 600,750, 1000};
+        int q;
+
+        snprintf(s, 128, cl, i);
+        strncat(s, "/coverage", 128);
+        q = cloud->getCoverage();
+        q = (q != -1 ) ? q : 0;
+        fgSetString(s, coverage_string[q] );
+
+        snprintf(s, 128, cl, i);
+        strncat(s, "/elevation-ft", 128);
+        d = cloud->getAltitude_ft();
+        d = (d != SGMetarNaN) ? d : -9999;
+        fgSetDouble(s, d);
+
+        snprintf(s, 128, cl, i);
+        strncat(s, "/thickness-ft", 128);
+        fgSetDouble(s, thickness[q]);
+
+        snprintf(s, 128, cl, i);
+        strncat(s, "/span-m", 128);
+        fgSetDouble(s, 40000.0);
+    }
+    for (; i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
+        snprintf(s, 128, cl, i);
+        strncat(s, "/coverage", 128);
+        fgSetString(s, "clear");
+
+        snprintf(s, 128, cl, i);
+        strncat(s, "/elevation-ft", 128);
+        fgSetDouble(s, -9999);
+
+        snprintf(s, 128, cl, i);
+        strncat(s, "/thickness-ft", 128);
+        fgSetDouble(s, 0);
+
+        snprintf(s, 128, cl, i);
+        strncat(s, "/span-m", 128);
+        fgSetDouble(s, 40000.0);
+    }
+
+    delete m;
+}
+
+
 // end of environment_ctrl.cxx