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