1 // metarproperties.cxx -- Parse a METAR and write properties
3 // Written by David Megginson, started May 2002.
4 // Rewritten by Torsten Dreyer, August 2010
6 // Copyright (C) 2002 David Megginson - david@megginson.com
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.
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.
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.
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>
37 namespace Environment {
39 static vector<string> coverage_string;
41 MetarProperties::MetarProperties( SGPropertyNode_ptr 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),
50 _base_wind_range_from(0),
51 _base_wind_range_to(0),
53 _wind_from_north_fps(0.0),
54 _wind_from_east_fps(0.0),
60 _sea_level_temperature(0.0),
61 _sea_level_dewpoint(0.0),
62 _sea_level_pressure(29.92),
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);
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 );
107 MetarProperties::~MetarProperties()
112 static const double thickness_value[] = { 0, 65, 600, 750, 1000 };
114 void MetarProperties::set_metar( const char * metar )
118 SGSharedPtr<FGMetar> m;
120 m = new FGMetar( _metar );
122 catch( sg_io_exception ) {
123 SG_LOG( SG_GENERAL, SG_WARN, "Can't parse metar: " << _metar );
124 _metarValidNode->setBoolValue(false);
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);
135 _min_visibility = m->getMinVisibility().getVisibility_m();
136 _max_visibility = m->getMaxVisibility().getVisibility_m();
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();
143 vis->setDoubleValue("min-m", v);
144 vis->setDoubleValue("max-m", v);
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();
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();
162 // 1. check the id given in the metar
163 FGAirport* a = FGAirport::findByIdent(m->getId());
165 // 2. if unknown, find closest airport with metar to current position
167 SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(), _latitude_n->getDoubleValue());
168 a = FGAirport::findClosest(pos, 10000.0, &_airportWithMetarFilter);
171 // 3. otherwise use ground elevation
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();
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();
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();
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;
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++ ) {
216 const string & phenomenon = w != NULL && j < w->phenomena.size() ? w->phenomena[j].c_str() : "";
217 n->getChild( "phenomenon", j, true )->setStringValue( phenomenon );
219 const string & description = w != NULL && j < w->descriptions.size() ? w->descriptions[j].c_str() : "";
220 n->getChild( "description", j, true )->setStringValue( description );
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;
233 vector<SGMetarCloud> cv = m->getClouds();
234 vector<SGMetarCloud>::const_iterator cloud, cloud_end = cv.end();
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
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 );
250 SGMetarCloud::Coverage coverage = SGMetarCloud::COVERAGE_NIL;
251 double thickness = 0;
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 )
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);
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
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;
292 i >= metarClouds.size() || coverage == SGMetarCloud::COVERAGE_CLEAR ?
294 metarClouds[i].getAltitude_ft() + _station_elevation;
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 );
305 _rain = m->getRain();
306 _hail = m->getHail();
307 _snow = m->getSnow();
308 _snow_cover = m->getSnowCover();
309 _metarValidNode->setBoolValue(true);
312 } // namespace Environment