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