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