]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_ctrl.cxx
Fix line endings
[flightgear.git] / src / Environment / environment_ctrl.cxx
1 // environment_ctrl.cxx -- manager for natural environment information.
2 //
3 // Written by David Megginson, started February 2002.
4 //
5 // Copyright (C) 2002  David Megginson - david@megginson.com
6 //
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.
11 //
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.
16 //
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, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <simgear/debug/logstream.hxx>
28
29 #include <stdlib.h>
30 #include <algorithm>
31
32 #include <simgear/structure/commands.hxx>
33 #include <simgear/structure/exception.hxx>
34
35 #include <Airports/simple.hxx>
36 #include <Main/fg_props.hxx>
37 #include <Main/util.hxx>
38
39 #include "environment_mgr.hxx"
40 #include "environment_ctrl.hxx"
41
42 SG_USING_STD(sort);
43
44
45 \f
46 ////////////////////////////////////////////////////////////////////////
47 // Implementation of FGEnvironmentCtrl abstract base class.
48 ////////////////////////////////////////////////////////////////////////
49
50 FGEnvironmentCtrl::FGEnvironmentCtrl ()
51   : _environment(0),
52     _lon_deg(0),
53     _lat_deg(0),
54     _elev_ft(0)
55 {
56 }
57
58 FGEnvironmentCtrl::~FGEnvironmentCtrl ()
59 {
60 }
61
62 void
63 FGEnvironmentCtrl::setEnvironment (FGEnvironment * environment)
64 {
65   _environment = environment;
66 }
67
68 void
69 FGEnvironmentCtrl::setLongitudeDeg (double lon_deg)
70 {
71   _lon_deg = lon_deg;
72 }
73
74 void
75 FGEnvironmentCtrl::setLatitudeDeg (double lat_deg)
76 {
77   _lat_deg = lat_deg;
78 }
79
80 void
81 FGEnvironmentCtrl::setElevationFt (double elev_ft)
82 {
83   _elev_ft = elev_ft;
84 }
85
86 void
87 FGEnvironmentCtrl::setPosition (double lon_deg, double lat_deg, double elev_ft)
88 {
89   _lon_deg = lon_deg;
90   _lat_deg = lat_deg;
91   _elev_ft = elev_ft;
92 }
93
94
95 \f
96 ////////////////////////////////////////////////////////////////////////
97 // Implementation of FGUserDefEnvironmentCtrl.
98 ////////////////////////////////////////////////////////////////////////
99
100 FGUserDefEnvironmentCtrl::FGUserDefEnvironmentCtrl ()
101   : _base_wind_speed_node(0),
102     _gust_wind_speed_node(0),
103     _current_wind_speed_kt(0),
104     _delta_wind_speed_kt(0)
105 {
106 }
107
108 FGUserDefEnvironmentCtrl::~FGUserDefEnvironmentCtrl ()
109 {
110 }
111
112 void
113 FGUserDefEnvironmentCtrl::init ()
114 {
115                                 // Fill in some defaults.
116   if (!fgHasNode("/environment/params/base-wind-speed-kt"))
117     fgSetDouble("/environment/params/base-wind-speed-kt",
118                 fgGetDouble("/environment/wind-speed-kt"));
119   if (!fgHasNode("/environment/params/gust-wind-speed-kt"))
120     fgSetDouble("/environment/params/gust-wind-speed-kt",
121                 fgGetDouble("/environment/params/base-wind-speed-kt"));
122
123   _base_wind_speed_node =
124     fgGetNode("/environment/params/base-wind-speed-kt", true);
125   _gust_wind_speed_node =
126     fgGetNode("/environment/params/gust-wind-speed-kt", true);
127
128   _current_wind_speed_kt = _base_wind_speed_node->getDoubleValue();
129   _delta_wind_speed_kt = 0.1;
130 }
131
132 void
133 FGUserDefEnvironmentCtrl::update (double dt)
134 {
135   double base_wind_speed = _base_wind_speed_node->getDoubleValue();
136   double gust_wind_speed = _gust_wind_speed_node->getDoubleValue();
137
138   if (gust_wind_speed < base_wind_speed) {
139       gust_wind_speed = base_wind_speed;
140       _gust_wind_speed_node->setDoubleValue(gust_wind_speed);
141   }
142
143   if (base_wind_speed == gust_wind_speed) {
144     _current_wind_speed_kt = base_wind_speed;
145   } else {
146     int rn = rand() % 128;
147     int sign = (_delta_wind_speed_kt < 0 ? -1 : 1);
148     double gust = _current_wind_speed_kt - base_wind_speed;
149     double incr = gust / 50;
150
151     if (rn == 0)
152       _delta_wind_speed_kt = - _delta_wind_speed_kt;
153     else if (rn < 4)
154       _delta_wind_speed_kt -= incr * sign;
155     else if (rn < 16)
156       _delta_wind_speed_kt += incr * sign;
157
158     _current_wind_speed_kt += _delta_wind_speed_kt;
159
160     if (_current_wind_speed_kt < base_wind_speed) {
161       _current_wind_speed_kt = base_wind_speed;
162       _delta_wind_speed_kt = 0.01;
163     } else if (_current_wind_speed_kt > gust_wind_speed) {
164       _current_wind_speed_kt = gust_wind_speed;
165       _delta_wind_speed_kt = -0.01;
166     }
167   }
168   
169   if (_environment != 0)
170     _environment->set_wind_speed_kt(_current_wind_speed_kt);
171 }
172
173
174 \f
175 ////////////////////////////////////////////////////////////////////////
176 // Implementation of FGInterpolateEnvironmentCtrl.
177 ////////////////////////////////////////////////////////////////////////
178
179 FGInterpolateEnvironmentCtrl::FGInterpolateEnvironmentCtrl ()
180 {
181 }
182
183 FGInterpolateEnvironmentCtrl::~FGInterpolateEnvironmentCtrl ()
184 {
185     unsigned int i;
186     for (i = 0; i < _boundary_table.size(); i++)
187         delete _boundary_table[i];
188     for (i = 0; i < _aloft_table.size(); i++)
189         delete _aloft_table[i];
190 }
191
192
193
194 void
195 FGInterpolateEnvironmentCtrl::init ()
196 {
197     read_table(fgGetNode("/environment/config/boundary", true),
198                _boundary_table);
199     read_table(fgGetNode("/environment/config/aloft", true),
200                _aloft_table);
201 }
202
203 void
204 FGInterpolateEnvironmentCtrl::reinit ()
205 {
206     unsigned int i;
207     for (i = 0; i < _boundary_table.size(); i++)
208         delete _boundary_table[i];
209     for (i = 0; i < _aloft_table.size(); i++)
210         delete _aloft_table[i];
211     _boundary_table.clear();
212     _aloft_table.clear();
213     init();
214 }
215
216 void
217 FGInterpolateEnvironmentCtrl::read_table (const SGPropertyNode * node,
218                                           vector<bucket *> &table)
219 {
220     for (int i = 0; i < node->nChildren(); i++) {
221         const SGPropertyNode * child = node->getChild(i);
222         if ( strcmp(child->getName(), "entry") == 0
223              && child->getStringValue("elevation-ft", "")[0] != '\0'
224              && ( child->getDoubleValue("elevation-ft") > 0.1 || i == 0 ) )
225         {
226             bucket * b = new bucket;
227             if (i > 0)
228                 b->environment.copy(table[i-1]->environment);
229             b->environment.read(child);
230             b->altitude_ft = b->environment.get_elevation_ft();
231             table.push_back(b);
232         }
233     }
234     sort(table.begin(), table.end());
235 }
236
237 void
238 FGInterpolateEnvironmentCtrl::update (double delta_time_sec)
239 {
240                                 // FIXME
241     double altitude_ft = fgGetDouble("/position/altitude-ft");
242     double altitude_agl_ft = fgGetDouble("/position/altitude-agl-ft");
243     double boundary_transition =
244         fgGetDouble("/environment/config/boundary-transition-ft", 500);
245
246     // double ground_elevation_ft = altitude_ft - altitude_agl_ft;
247
248     int length = _boundary_table.size();
249
250     if (length > 0) {
251                                 // boundary table
252         double boundary_limit = _boundary_table[length-1]->altitude_ft;
253         if (boundary_limit >= altitude_agl_ft) {
254             do_interpolate(_boundary_table, altitude_agl_ft,
255                            _environment);
256             return;
257         } else if ((boundary_limit + boundary_transition) >= altitude_agl_ft) {
258                                 // both tables
259             do_interpolate(_boundary_table, altitude_agl_ft, &env1);
260             do_interpolate(_aloft_table, altitude_ft, &env2);
261             double fraction =
262                 (altitude_agl_ft - boundary_limit) / boundary_transition;
263             interpolate(&env1, &env2, fraction, _environment);
264             return;
265         }
266     }
267
268                                 // aloft table
269     do_interpolate(_aloft_table, altitude_ft, _environment);
270 }
271
272 void
273 FGInterpolateEnvironmentCtrl::do_interpolate (vector<bucket *> &table,
274                                               double altitude_ft,
275                                               FGEnvironment * environment)
276 {
277     int length = table.size();
278     if (length == 0)
279         return;
280
281                                 // Boundary conditions
282     if ((length == 1) || (table[0]->altitude_ft >= altitude_ft)) {
283         environment->copy(table[0]->environment);
284         return;
285     } else if (table[length-1]->altitude_ft <= altitude_ft) {
286         environment->copy(table[length-1]->environment);
287         return;
288     }
289         
290                                 // Search the interpolation table
291     for (int i = 0; i < length - 1; i++) {
292         if ((i == length - 1) || (table[i]->altitude_ft <= altitude_ft)) {
293                 FGEnvironment * env1 = &(table[i]->environment);
294                 FGEnvironment * env2 = &(table[i+1]->environment);
295                 double fraction;
296                 if (table[i]->altitude_ft == table[i+1]->altitude_ft)
297                     fraction = 1.0;
298                 else
299                     fraction =
300                         ((altitude_ft - table[i]->altitude_ft) /
301                          (table[i+1]->altitude_ft - table[i]->altitude_ft));
302                 interpolate(env1, env2, fraction, environment);
303
304                 return;
305         }
306     }
307 }
308
309 bool
310 FGInterpolateEnvironmentCtrl::bucket::operator< (const bucket &b) const
311 {
312     return (altitude_ft < b.altitude_ft);
313 }
314
315
316 \f
317 ////////////////////////////////////////////////////////////////////////
318 // Implementation of FGMetarEnvironmentCtrl.
319 ////////////////////////////////////////////////////////////////////////
320
321 FGMetarEnvironmentCtrl::FGMetarEnvironmentCtrl ()
322     : env( new FGInterpolateEnvironmentCtrl ),
323       _icao( "" ),
324       search_interval_sec( 60.0 ),        // 1 minute
325       same_station_interval_sec( 900.0 ), // 15 minutes
326       search_elapsed( 9999.0 ),
327       fetch_elapsed( 9999.0 ),
328       proxy_host( fgGetNode("/sim/presets/proxy/host", true) ),
329       proxy_port( fgGetNode("/sim/presets/proxy/port", true) ),
330       proxy_auth( fgGetNode("/sim/presets/proxy/authentication", true) ),
331       metar_max_age( fgGetNode("/environment/params/metar-max-age-min", true) ),
332       _error_count( 0 ),
333       _stale_count( 0 ),
334       _dt( 0.0 ),
335       _error_dt( 0.0 ),
336       last_apt(0)
337 {
338 #if defined(ENABLE_THREADS)
339     thread = new MetarThread(this);
340     thread->start( 1 );
341 #endif // ENABLE_THREADS
342 }
343
344 FGMetarEnvironmentCtrl::~FGMetarEnvironmentCtrl ()
345 {
346 #if defined(ENABLE_THREADS)
347    thread->cancel();
348    thread->join();
349 #endif // ENABLE_THREADS
350
351    delete env;
352    env = NULL;
353 }
354
355
356 // use a "command" to set station temp at station elevation
357 static void set_temp_at_altitude( float temp_degc, float altitude_ft ) {
358     SGPropertyNode args;
359     SGPropertyNode *node = args.getNode("temp-degc", 0, true);
360     node->setFloatValue( temp_degc );
361     node = args.getNode("altitude-ft", 0, true);
362     node->setFloatValue( altitude_ft );
363     globals->get_commands()->execute("set-outside-air-temp-degc", &args);
364 }
365
366
367 static void set_dewpoint_at_altitude( float dewpoint_degc, float altitude_ft ) {
368     SGPropertyNode args;
369     SGPropertyNode *node = args.getNode("dewpoint-degc", 0, true);
370     node->setFloatValue( dewpoint_degc );
371     node = args.getNode("altitude-ft", 0, true);
372     node->setFloatValue( altitude_ft );
373     globals->get_commands()->execute("set-dewpoint-temp-degc", &args);
374 }
375
376
377 void
378 FGMetarEnvironmentCtrl::update_env_config ()
379 {
380     fgSetupWind( fgGetDouble("/environment/metar/base-wind-range-from"),
381                  fgGetDouble("/environment/metar/base-wind-range-to"),
382                  fgGetDouble("/environment/metar/base-wind-speed-kt"),
383                  fgGetDouble("/environment/metar/gust-wind-speed-kt") );
384
385     fgDefaultWeatherValue( "visibility-m",
386                            fgGetDouble("/environment/metar/min-visibility-m") );
387     set_temp_at_altitude( fgGetDouble("/environment/metar/temperature-degc"),
388                           station_elevation_ft );
389     set_dewpoint_at_altitude( fgGetDouble("/environment/metar/dewpoint-degc"),
390                               station_elevation_ft );
391     fgDefaultWeatherValue( "pressure-sea-level-inhg",
392                            fgGetDouble("/environment/metar/pressure-inhg") );
393 }
394
395 void
396 FGMetarEnvironmentCtrl::init ()
397 {
398     const SGPropertyNode *longitude
399         = fgGetNode( "/position/longitude-deg", true );
400     const SGPropertyNode *latitude
401         = fgGetNode( "/position/latitude-deg", true );
402
403     bool found_metar = false;
404     long max_age = metar_max_age->getLongValue();
405     // Don't check max age during init so that we don't loop over a lot
406     // of airports metar if there is a problem.
407     // The update() calls will find a correct metar if things went wrong here
408     metar_max_age->setLongValue(0);
409
410     while ( !found_metar && (_error_count < 3) ) {
411         const FGAirport* a = globals->get_airports()
412                    ->search( longitude->getDoubleValue(),
413                              latitude->getDoubleValue(),
414                              true );
415         if ( a ) {  
416             FGMetarResult result = fetch_data( a->getId() );
417             if ( result.m != NULL ) {
418                 SG_LOG( SG_GENERAL, SG_INFO, "closest station w/ metar = "
419                         << a->getId());
420                 last_apt = a;
421                 _icao = a->getId();
422                 search_elapsed = 0.0;
423                 fetch_elapsed = 0.0;
424                 update_metar_properties( result.m );
425                 update_env_config();
426                 env->init();
427                 found_metar = true;
428             } else {
429                 // mark as no metar so it doesn't show up in subsequent
430                 // searches.
431                 SG_LOG( SG_GENERAL, SG_INFO, "no metar at metar = "
432                         << a->getId() );
433                 globals->get_airports()->no_metar( a->getId() );
434             }
435         }
436     }
437     metar_max_age->setLongValue(max_age);
438 }
439
440 void
441 FGMetarEnvironmentCtrl::reinit ()
442 {
443     _error_count = 0;
444     _error_dt = 0.0;
445
446 #if 0
447     update_env_config();
448 #endif
449
450     env->reinit();
451 }
452
453 void
454 FGMetarEnvironmentCtrl::update(double delta_time_sec)
455 {
456
457     _dt += delta_time_sec;
458     if (_error_count >= 3)
459        return;
460
461     FGMetarResult result;
462
463     static const SGPropertyNode *longitude
464         = fgGetNode( "/position/longitude-deg", true );
465     static const SGPropertyNode *latitude
466         = fgGetNode( "/position/latitude-deg", true );
467     search_elapsed += delta_time_sec;
468     fetch_elapsed += delta_time_sec;
469
470     // if time for a new search request, push it onto the request
471     // queue
472     if ( search_elapsed > search_interval_sec ) {
473         const FGAirport* a = globals->get_airports()
474                    ->search( longitude->getDoubleValue(),
475                              latitude->getDoubleValue(),
476                              true );
477         if ( a ) {
478             if ( !last_apt || last_apt->getId() != a->getId()
479                  || fetch_elapsed > same_station_interval_sec )
480             {
481                 SG_LOG( SG_GENERAL, SG_INFO, "closest station w/ metar = "
482                         << a->getId());
483                 request_queue.push( a->getId() );
484                 last_apt = a;
485                 _icao = a->getId();
486                 search_elapsed = 0.0;
487                 fetch_elapsed = 0.0;
488             } else {
489                 search_elapsed = 0.0;
490                 SG_LOG( SG_GENERAL, SG_INFO, "same station, waiting = "
491                         << same_station_interval_sec - fetch_elapsed );
492             }
493         } else {
494             SG_LOG( SG_GENERAL, SG_WARN,
495                     "Unable to find any airports with metar" );
496         }
497     }
498
499 #if !defined(ENABLE_THREADS)
500     // No loader thread running so manually fetch the data
501     string id = "";
502     while ( !request_queue.empty() ) {
503         id = request_queue.front();
504         request_queue.pop();
505     }
506
507     if ( !id.empty() ) {
508         SG_LOG( SG_GENERAL, SG_INFO, "inline fetching = " << id );
509         result = fetch_data( id );
510         result_queue.push( result );
511     }
512 #endif // ENABLE_THREADS
513
514     // process any results from the loader.
515     while ( !result_queue.empty() ) {
516         result = result_queue.front();
517         result_queue.pop();
518         if ( result.m != NULL ) {
519             update_metar_properties( result.m );
520             delete result.m;
521             update_env_config();
522             env->reinit();
523         } else {
524             // mark as no metar so it doesn't show up in subsequent
525             // searches, and signal an immediate re-search.
526             SG_LOG( SG_GENERAL, SG_WARN,
527                     "no metar at station = " << result.icao );
528             globals->get_airports()->no_metar( result.icao );
529             search_elapsed = 9999.0;
530         }
531     }
532
533     env->update(delta_time_sec);
534 }
535
536
537 void
538 FGMetarEnvironmentCtrl::setEnvironment (FGEnvironment * environment)
539 {
540     env->setEnvironment(environment);
541 }
542
543 FGMetarResult
544 FGMetarEnvironmentCtrl::fetch_data( const string &icao )
545 {
546     FGMetarResult result;
547     result.icao = icao;
548
549     // if the last error was more than three seconds ago,
550     // then pretent nothing happened.
551     if (_error_dt < 3) {
552         _error_dt += _dt;
553
554     } else {
555         _error_dt = 0.0;
556         _error_count = 0;
557     }
558
559     // fetch station elevation if exists
560     const FGAirport* a = globals->get_airports()->search( icao );
561     if ( a ) {
562         station_elevation_ft = a->getElevation();
563     }
564
565     // fetch current metar data
566     try {
567         string host = proxy_host->getStringValue();
568         string auth = proxy_auth->getStringValue();
569         string port = proxy_port->getStringValue();
570         result.m = new FGMetar( icao, host, port, auth);
571
572         long max_age = metar_max_age->getLongValue();
573         long age = result.m->getAge_min();
574         if (max_age &&  age > max_age) {
575             SG_LOG( SG_GENERAL, SG_WARN, "METAR data too old (" << age << " min).");
576             delete result.m;
577             result.m = NULL;
578
579             if (++_stale_count > 10) {
580                 _error_count = 1000;
581                 throw sg_io_exception("More than 10 stale METAR messages in a row."
582                         " Check your system time!");
583             }
584         } else
585             _stale_count = 0;
586
587     } catch (const sg_io_exception& e) {
588         SG_LOG( SG_GENERAL, SG_WARN, "Error fetching live weather data: "
589                 << e.getFormattedMessage().c_str() );
590 #if defined(ENABLE_THREADS)
591         if (_error_count++ >= 3) {
592            SG_LOG( SG_GENERAL, SG_WARN, "Stop fetching data permanently.");
593            thread->cancel();
594            thread->join();
595         }
596 #endif
597
598         result.m = NULL;
599     }
600
601     _dt = 0;
602
603     return result;
604 }
605
606
607 void
608 FGMetarEnvironmentCtrl::update_metar_properties( const FGMetar *m )
609 {
610     int i;
611     double d;
612     char s[128];
613
614     fgSetString("/environment/metar/real-metar", m->getData());
615         // don't update with real weather when we use a custom weather scenario
616         const char *current_scenario = fgGetString("/environment/weather-scenario", "METAR");
617         if( strcmp(current_scenario, "METAR") && strcmp(current_scenario, "none"))
618                 return;
619     fgSetString("/environment/metar/last-metar", m->getData());
620     fgSetString("/environment/metar/station-id", m->getId());
621     fgSetDouble("/environment/metar/min-visibility-m",
622                 m->getMinVisibility().getVisibility_m() );
623     fgSetDouble("/environment/metar/max-visibility-m",
624                 m->getMaxVisibility().getVisibility_m() );
625
626     const SGMetarVisibility *dirvis = m->getDirVisibility();
627     for (i = 0; i < 8; i++, dirvis++) {
628         const char *min = "/environment/metar/visibility[%d]/min-m";
629         const char *max = "/environment/metar/visibility[%d]/max-m";
630
631         d = dirvis->getVisibility_m();
632
633         snprintf(s, 128, min, i);
634         fgSetDouble(s, d);
635         snprintf(s, 128, max, i);
636         fgSetDouble(s, d);
637     }
638
639     fgSetInt("/environment/metar/base-wind-range-from",
640              m->getWindRangeFrom() );
641     fgSetInt("/environment/metar/base-wind-range-to",
642              m->getWindRangeTo() );
643     fgSetDouble("/environment/metar/base-wind-speed-kt",
644                 m->getWindSpeed_kt() );
645     fgSetDouble("/environment/metar/gust-wind-speed-kt",
646                 m->getGustSpeed_kt() );
647     fgSetDouble("/environment/metar/temperature-degc",
648                 m->getTemperature_C() );
649     fgSetDouble("/environment/metar/dewpoint-degc",
650                 m->getDewpoint_C() );
651     fgSetDouble("/environment/metar/rel-humidity-norm",
652                 m->getRelHumidity() );
653     fgSetDouble("/environment/metar/pressure-inhg",
654                 m->getPressure_inHg() );
655
656     vector<SGMetarCloud> cv = m->getClouds();
657     vector<SGMetarCloud>::const_iterator cloud;
658
659     const char *cl = "/environment/clouds/layer[%i]";
660     for (i = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, i++) {
661         const char *coverage_string[5] = 
662             { "clear", "few", "scattered", "broken", "overcast" };
663         const double thickness[5] = { 0, 65, 600,750, 1000};
664         int q;
665
666         snprintf(s, 128, cl, i);
667         strncat(s, "/coverage", 128);
668         q = cloud->getCoverage();
669         fgSetString(s, coverage_string[q] );
670
671         snprintf(s, 128, cl, i);
672         strncat(s, "/elevation-ft", 128);
673         fgSetDouble(s, cloud->getAltitude_ft() + station_elevation_ft);
674
675         snprintf(s, 128, cl, i);
676         strncat(s, "/thickness-ft", 128);
677         fgSetDouble(s, thickness[q]);
678
679         snprintf(s, 128, cl, i);
680         strncat(s, "/span-m", 128);
681         fgSetDouble(s, 40000.0);
682     }
683
684     for (; i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
685         snprintf(s, 128, cl, i);
686         strncat(s, "/coverage", 128);
687         fgSetString(s, "clear");
688
689         snprintf(s, 128, cl, i);
690         strncat(s, "/elevation-ft", 128);
691         fgSetDouble(s, -9999);
692
693         snprintf(s, 128, cl, i);
694         strncat(s, "/thickness-ft", 128);
695         fgSetDouble(s, 0);
696
697         snprintf(s, 128, cl, i);
698         strncat(s, "/span-m", 128);
699         fgSetDouble(s, 40000.0);
700     }
701
702     fgSetDouble("/environment/metar/rain-norm", m->getRain());
703     fgSetDouble("/environment/metar/hail-norm", m->getHail());
704     fgSetDouble("/environment/metar/snow-norm", m->getSnow());
705     fgSetBool("/environment/metar/snow-cover", m->getSnowCover());
706 }
707
708
709 #if defined(ENABLE_THREADS)
710 /**
711  * Ensure mutex is unlocked.
712  */
713 void
714 metar_cleanup_handler( void* arg )
715 {
716     FGMetarEnvironmentCtrl* fetcher = (FGMetarEnvironmentCtrl*) arg;
717     fetcher->mutex.unlock();
718 }
719
720 /**
721  *
722  */
723 void
724 FGMetarEnvironmentCtrl::MetarThread::run()
725 {
726     pthread_cleanup_push( metar_cleanup_handler, fetcher );
727     while ( true )
728     {
729         set_cancel( SGThread::CANCEL_DISABLE );
730
731         string icao = fetcher->request_queue.pop();
732         SG_LOG( SG_GENERAL, SG_INFO, "Thread: fetch metar data = " << icao );
733         FGMetarResult result = fetcher->fetch_data( icao );
734
735         set_cancel( SGThread::CANCEL_DEFERRED );
736
737         fetcher->result_queue.push( result );
738     }
739     pthread_cleanup_pop(1);
740 }
741 #endif // ENABLE_THREADS
742
743
744 // end of environment_ctrl.cxx