]> git.mxchange.org Git - flightgear.git/blob - src/Environment/metarproperties.cxx
Merge branch 'next' of git://gitorious.org/fg/flightgear into next
[flightgear.git] / src / Environment / metarproperties.cxx
1 // metarproperties.cxx -- Parse a METAR and write properties
2 //
3 // Written by David Megginson, started May 2002.
4 // Rewritten by Torsten Dreyer, August 2010
5 //
6 // Copyright (C) 2002  David Megginson - david@megginson.com
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include "metarproperties.hxx"
28 #include "fgmetar.hxx"
29 #include "environment.hxx"
30 #include "atmosphere.hxx"
31 #include "metarairportfilter.hxx"
32 #include <simgear/scene/sky/cloud.hxx>
33 #include <simgear/structure/exception.hxx>
34 #include <Main/fg_props.hxx>
35
36 using std::string;
37
38 namespace Environment {
39
40 static vector<string> coverage_string;
41
42 MetarProperties::MetarProperties( SGPropertyNode_ptr rootNode ) :
43   _rootNode(rootNode),
44   _metarValidNode( rootNode->getNode( "valid", true ) ),
45   _station_elevation(0.0),
46   _station_latitude(0.0),
47   _station_longitude(0.0),
48   _min_visibility(16000.0),
49   _max_visibility(16000.0),
50   _base_wind_dir(0),
51   _base_wind_range_from(0),
52   _base_wind_range_to(0),
53   _wind_speed(0.0),
54   _wind_from_north_fps(0.0),
55   _wind_from_east_fps(0.0),
56   _gusts(0.0),
57   _temperature(0.0),
58   _dewpoint(0.0),
59   _humidity(0.0),
60   _pressure(0.0),
61   _sea_level_temperature(0.0),
62   _sea_level_dewpoint(0.0),
63   _sea_level_pressure(29.92),
64   _rain(0.0),
65   _hail(0.0),
66   _snow(0.0),
67   _snow_cover(false)
68 {
69   // Hack to avoid static initialization order problems on OSX
70   if( coverage_string.size() == 0 ) {
71     coverage_string.push_back(SGCloudLayer::SG_CLOUD_CLEAR_STRING);
72     coverage_string.push_back(SGCloudLayer::SG_CLOUD_FEW_STRING);
73     coverage_string.push_back(SGCloudLayer::SG_CLOUD_SCATTERED_STRING);
74     coverage_string.push_back(SGCloudLayer::SG_CLOUD_BROKEN_STRING);
75     coverage_string.push_back(SGCloudLayer::SG_CLOUD_OVERCAST_STRING);
76   }
77   // don't tie metar-valid, so listeners get triggered
78   _metarValidNode->setBoolValue( false );
79   _tiedProperties.setRoot( _rootNode );
80   _tiedProperties.Tie("data", this, &MetarProperties::get_metar, &MetarProperties::set_metar );
81   _tiedProperties.Tie("station-id", this, &MetarProperties::get_station_id );
82   _tiedProperties.Tie("station-elevation-ft", &_station_elevation );
83   _tiedProperties.Tie("station-latitude-deg", &_station_latitude );
84   _tiedProperties.Tie("station-longitude-deg", &_station_longitude );
85   _tiedProperties.Tie("min-visibility-m", &_min_visibility );
86   _tiedProperties.Tie("max-visibility-m", &_max_visibility );
87   _tiedProperties.Tie("base-wind-range-from", &_base_wind_range_from );
88   _tiedProperties.Tie("base-wind-range-to", &_base_wind_range_to );
89   _tiedProperties.Tie("base-wind-speed-kt", &_wind_speed );
90   _tiedProperties.Tie("base-wind-dir-deg", &_base_wind_dir );
91   _tiedProperties.Tie("base-wind-from-north-fps", &_wind_from_north_fps );
92   _tiedProperties.Tie("base-wind-from-east-fps", &_wind_from_east_fps );
93   _tiedProperties.Tie("gust-wind-speed-kt", &_gusts );
94   _tiedProperties.Tie("temperature-degc", &_temperature );
95   _tiedProperties.Tie("dewpoint-degc", &_dewpoint );
96   _tiedProperties.Tie("rel-humidity-norm", &_humidity );
97   _tiedProperties.Tie("pressure-inhg", &_pressure );
98   _tiedProperties.Tie("temperature-sea-level-degc", &_sea_level_temperature );
99   _tiedProperties.Tie("dewpoint-sea-level-degc", &_sea_level_dewpoint );
100   _tiedProperties.Tie("pressure-sea-level-inhg", &_sea_level_pressure );
101   _tiedProperties.Tie("rain-norm", &_rain );
102   _tiedProperties.Tie("hail-norm", &_hail );
103   _tiedProperties.Tie("snow-norm", &_snow);
104   _tiedProperties.Tie("snow-cover", &_snow_cover );
105   _tiedProperties.Tie("decoded", this, &MetarProperties::get_decoded );
106 }
107
108 MetarProperties::~MetarProperties()
109 {
110 }
111
112
113 static const double thickness_value[] = { 0, 65, 600, 750, 1000 };
114
115 void MetarProperties::set_metar( const char * metar )
116 {
117     _metar = metar;
118
119     SGSharedPtr<FGMetar> m;
120     try {
121         m = new FGMetar( _metar );
122     }
123     catch( sg_io_exception ) {
124         SG_LOG( SG_GENERAL, SG_WARN, "Can't parse metar: " << _metar );
125         _metarValidNode->setBoolValue(false);
126         return;
127     }
128
129     _decoded.clear();
130     const vector<string> weather = m->getWeather();
131     for( vector<string>::const_iterator it = weather.begin(); it != weather.end(); it++ ) {
132         if( false == _decoded.empty() ) _decoded.append(", ");
133         _decoded.append(*it);
134     }
135
136     _min_visibility = m->getMinVisibility().getVisibility_m();
137     _max_visibility = m->getMaxVisibility().getVisibility_m();
138
139     const SGMetarVisibility *dirvis = m->getDirVisibility();
140     for ( int i = 0; i < 8; i++, dirvis++) {
141         SGPropertyNode *vis = _rootNode->getChild("visibility", i, true);
142         double v = dirvis->getVisibility_m();
143
144         vis->setDoubleValue("min-m", v);
145         vis->setDoubleValue("max-m", v);
146     }
147
148     _base_wind_dir = m->getWindDir();
149     _base_wind_range_from = m->getWindRangeFrom();
150     _base_wind_range_to = m->getWindRangeTo();
151     _wind_speed = m->getWindSpeed_kt();
152
153     double speed_fps = _wind_speed * SG_NM_TO_METER * SG_METER_TO_FEET / 3600.0;
154     _wind_from_north_fps = speed_fps * cos((double)_base_wind_dir * SGD_DEGREES_TO_RADIANS);
155     _wind_from_east_fps = speed_fps * sin((double)_base_wind_dir * SGD_DEGREES_TO_RADIANS);
156     _gusts = m->getGustSpeed_kt();
157     _temperature = m->getTemperature_C();
158     _dewpoint = m->getDewpoint_C();
159     _humidity = m->getRelHumidity();
160     _pressure = m->getPressure_inHg();
161
162     {
163         // 1. check the id given in the metar
164         FGAirport* a = FGAirport::findByIdent(m->getId());
165
166         // 2. if unknown, find closest airport with metar to current position
167         if( a == NULL ) {
168             SGGeod pos = SGGeod::fromDeg(
169                 fgGetDouble( "/position/longitude-deg", 0.0 ),
170                 fgGetDouble( "/position/latitude-deg", 0.0 ) );
171             a = FGAirport::findClosest(pos, 10000.0, MetarAirportFilter::instance() );
172         }
173
174         // 3. otherwise use ground elevation
175         if( a != NULL ) {
176             _station_elevation = a->getElevation();
177             const SGGeod & towerPosition = a->getTowerLocation();
178             _station_latitude = towerPosition.getLatitudeDeg();
179             _station_longitude = towerPosition.getLongitudeDeg();
180             _station_id = a->ident();
181         } else {
182             _station_elevation = fgGetDouble("/position/ground-elev-m", 0.0 ) * SG_METER_TO_FEET;
183             _station_latitude = fgGetDouble( "/position/latitude-deg", 0.0 );
184             _station_longitude = fgGetDouble( "/position/longitude-deg", 0.0 );
185             _station_id = "XXXX";
186         }
187     }
188
189     {    // calculate sea level temperature, dewpoint and pressure
190         FGEnvironment dummy; // instantiate a dummy so we can leech a method
191         dummy.set_elevation_ft( _station_elevation );
192         dummy.set_temperature_degc( _temperature );
193         dummy.set_dewpoint_degc( _dewpoint );
194         _sea_level_temperature = dummy.get_temperature_sea_level_degc();
195         _sea_level_dewpoint = dummy.get_dewpoint_sea_level_degc();
196
197         double elevation_m = _station_elevation * SG_FEET_TO_METER;
198         double fieldPressure = FGAtmo::fieldPressure( elevation_m, _pressure * atmodel::inHg );
199         _sea_level_pressure = P_layer(0, elevation_m, fieldPressure, _temperature + atmodel::freezing, atmodel::ISA::lam0) / atmodel::inHg;
200     }
201
202     bool isBC = false;
203     bool isBR = false;
204     bool isFG = false;
205     bool isMI = false;
206     bool isHZ = false;
207
208     {
209         for( unsigned i = 0; i < 3; i++ ) {
210             SGPropertyNode_ptr n = _rootNode->getChild("weather", i, true );
211             vector<struct SGMetar::Weather> weather = m->getWeather2();
212             struct SGMetar::Weather * w = i < weather.size() ? &weather[i] : NULL;
213             n->getNode("intensity",true)->setIntValue( w != NULL ? w->intensity : 0 );
214             n->getNode("vincinity",true)->setBoolValue( w != NULL ? w->vincinity : false );
215             for( unsigned j = 0; j < 3; j++ ) { 
216
217                 const string & phenomenon = w != NULL && j < w->phenomena.size() ? w->phenomena[j].c_str() : "";
218                 n->getChild( "phenomenon", j, true )->setStringValue( phenomenon );
219
220                 const string & description = w != NULL && j < w->descriptions.size() ? w->descriptions[j].c_str() : "";
221                 n->getChild( "description", j, true )->setStringValue( description );
222
223                 // need to know later, 
224                 // if its fog(FG) (might be shallow(MI) or patches(BC)) or haze (HZ) or mist(BR)
225                 if( phenomenon == "FG" ) isFG = true;
226                 if( phenomenon == "HZ" ) isHZ = true;
227                 if( phenomenon == "BR" ) isBR = true;
228                 if( description == "MI" ) isMI = true;
229                 if( description == "BC" ) isBC = true;
230             }
231         }
232     }
233
234     vector<SGMetarCloud> cv = m->getClouds();
235     vector<SGMetarCloud>::const_iterator cloud, cloud_end = cv.end();
236
237     {
238         static const char * LAYER = "layer";
239         SGPropertyNode_ptr cloudsNode = _rootNode->getNode("clouds", true );
240         const vector<SGMetarCloud> & metarClouds = m->getClouds();
241         unsigned layerOffset = 0; // Oh, this is ugly!
242
243         // fog/mist/haze cloud layer does not work with 3d clouds yet :-(
244         bool setGroundCloudLayer = _rootNode->getBoolValue("set-ground-cloud-layer", false ) &&
245               false == (fgGetBool("/sim/rendering/shader-effects", false ) && 
246                         fgGetBool("/sim/rendering/clouds3d-enable", false ) );
247
248         if( setGroundCloudLayer ) {
249             // create a cloud layer #0 starting at the ground if its fog, mist or haze
250
251             // make sure layer actually starts at ground and set it's bottom at a constant
252             // value below the station's elevation
253             const double LAYER_BOTTOM_STATION_OFFSET =
254               fgGetDouble( "/environment/params/fog-mist-haze-layer/offset-from-station-elevation-ft", -200 );
255
256             SGMetarCloud::Coverage coverage = SGMetarCloud::COVERAGE_NIL;
257             double thickness = 0;
258             double alpha = 1.0;
259
260             if( isFG ) { // fog
261                 coverage = SGMetarCloud::getCoverage( isBC ? 
262                     fgGetString( "/environment/params/fog-mist-haze-layer/fog-bc-2dlayer-coverage", SGMetarCloud::COVERAGE_SCATTERED_STRING ) :
263                     fgGetString( "/environment/params/fog-mist-haze-layer/fog-2dlayer-coverage", SGMetarCloud::COVERAGE_BROKEN_STRING )
264                 );
265
266                 thickness = isMI ? 
267                    fgGetDouble("/environment/params/fog-mist-haze-layer/fog-shallow-thickness-ft",30) - LAYER_BOTTOM_STATION_OFFSET : // shallow fog, 10m/30ft
268                    fgGetDouble("/environment/params/fog-mist-haze-layer/fog-thickness-ft",500) - LAYER_BOTTOM_STATION_OFFSET; // fog, 150m/500ft
269                 alpha =  fgGetDouble("/environment/params/fog-mist-haze-layer/fog-2dlayer-alpha", 1.0);
270             } else if( isBR ) { // mist
271                 coverage = SGMetarCloud::getCoverage(fgGetString("/environment/params/fog-mist-haze-layer/mist-2dlayer-coverage", SGMetarCloud::COVERAGE_OVERCAST_STRING));
272                 thickness =  fgGetDouble("/environment/params/fog-mist-haze-layer/mist-thickness-ft",2000) - LAYER_BOTTOM_STATION_OFFSET;
273                 alpha =  fgGetDouble("/environment/params/fog-mist-haze-layer/mist-2dlayer-alpha",0.8);
274             } else if( isHZ ) { // haze
275                 coverage = SGMetarCloud::getCoverage(fgGetString("/environment/params/fog-mist-haze-layer/mist-2dlayer-coverage", SGMetarCloud::COVERAGE_OVERCAST_STRING));
276                 thickness =  fgGetDouble("/environment/params/fog-mist-haze-layer/haze-thickness-ft",2000) - LAYER_BOTTOM_STATION_OFFSET;
277                 alpha =  fgGetDouble("/environment/params/fog-mist-haze-layer/haze-2dlayer-alpha",0.6);
278             }
279
280             if( coverage != SGMetarCloud::COVERAGE_NIL ) {
281
282                 // if there is a layer above the fog, limit the top to one foot below that layer's bottom
283                 if( metarClouds.size() > 0 && metarClouds[0].getCoverage() != SGMetarCloud::COVERAGE_CLEAR )
284                     thickness = metarClouds[0].getAltitude_ft() - LAYER_BOTTOM_STATION_OFFSET - 1;
285
286                 SGPropertyNode_ptr layerNode = cloudsNode->getChild(LAYER, 0, true );
287                 layerNode->setDoubleValue( "coverage-type", SGCloudLayer::getCoverageType(coverage_string[coverage]) );
288                 layerNode->setStringValue( "coverage", coverage_string[coverage] );
289                 layerNode->setDoubleValue( "elevation-ft", _station_elevation + LAYER_BOTTOM_STATION_OFFSET );
290                 layerNode->setDoubleValue( "thickness-ft", thickness );
291                 layerNode->setDoubleValue( "visibility-m", _min_visibility );
292                 layerNode->setDoubleValue( "alpha", alpha );
293                 _min_visibility = _max_visibility =
294                   fgGetDouble("/environment/params/fog-mist-haze-layer/visibility-above-layer-m",20000.0); // assume good visibility above the fog
295                 layerOffset = 1;  // shudder
296             }
297         } 
298
299         for( unsigned i = 0; i < 5-layerOffset; i++ ) {
300             SGPropertyNode_ptr layerNode = cloudsNode->getChild(LAYER, i+layerOffset, true );
301             SGMetarCloud::Coverage coverage = i < metarClouds.size() ? metarClouds[i].getCoverage() : SGMetarCloud::COVERAGE_CLEAR;
302             double elevation = 
303                 i >= metarClouds.size() || coverage == SGMetarCloud::COVERAGE_CLEAR ? 
304                 -9999.0 : 
305                 metarClouds[i].getAltitude_ft() + _station_elevation;
306
307             layerNode->setStringValue( "coverage", coverage_string[coverage] );
308             layerNode->setDoubleValue( "coverage-type", SGCloudLayer::getCoverageType(coverage_string[coverage]) );
309             layerNode->setDoubleValue( "elevation-ft", elevation );
310             layerNode->setDoubleValue( "thickness-ft", thickness_value[coverage]);
311             layerNode->setDoubleValue( "span-m", 40000 );
312             layerNode->setDoubleValue( "visibility-m", 50.0 );
313         }
314     }
315
316     _rain = m->getRain();
317     _hail = m->getHail();
318     _snow = m->getSnow();
319     _snow_cover = m->getSnowCover();
320     _metarValidNode->setBoolValue(true);
321 }
322
323 } // namespace Environment