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>
36 namespace Environment {
38 static vector<string> coverage_string;
40 MetarProperties::MetarProperties( SGPropertyNode_ptr 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),
49 _base_wind_range_from(0),
50 _base_wind_range_to(0),
52 _wind_from_north_fps(0.0),
53 _wind_from_east_fps(0.0),
59 _sea_level_temperature(0.0),
60 _sea_level_dewpoint(0.0),
61 _sea_level_pressure(29.92),
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);
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 );
106 MetarProperties::~MetarProperties()
111 static const double thickness_value[] = { 0, 65, 600, 750, 1000 };
113 void MetarProperties::set_metar( const char * metar )
117 SGSharedPtr<FGMetar> m;
119 m = new FGMetar( _metar );
121 catch( sg_io_exception ) {
122 SG_LOG( SG_GENERAL, SG_WARN, "Can't parse metar: " << _metar );
123 _metarValidNode->setBoolValue(false);
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);
134 _min_visibility = m->getMinVisibility().getVisibility_m();
135 _max_visibility = m->getMaxVisibility().getVisibility_m();
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();
142 vis->setDoubleValue("min-m", v);
143 vis->setDoubleValue("max-m", v);
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();
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();
161 // 1. check the id given in the metar
162 FGAirport* a = FGAirport::findByIdent(m->getId());
164 // 2. if unknown, find closest airport with metar to current position
166 SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(), _latitude_n->getDoubleValue());
167 a = FGAirport::findClosest(pos, 10000.0, &_airportWithMetarFilter);
170 // 3. otherwise use ground elevation
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();
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();
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();
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;
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++ ) {
215 const string & phenomenon = w != NULL && j < w->phenomena.size() ? w->phenomena[j].c_str() : "";
216 n->getChild( "phenomenon", j, true )->setStringValue( phenomenon );
218 const string & description = w != NULL && j < w->descriptions.size() ? w->descriptions[j].c_str() : "";
219 n->getChild( "description", j, true )->setStringValue( description );
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;
232 vector<SGMetarCloud> cv = m->getClouds();
233 vector<SGMetarCloud>::const_iterator cloud, cloud_end = cv.end();
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 );
242 if( setGroundCloudLayer && isFG ) {
243 // make sure fog actually starts at ground and set it's bottom at a constant
244 // value below the station's elevation
245 const double LAYER_BOTTOM_BELOW_STATION_ELEVATION=200;
247 // fog - create a cloud layer #0 starting at the ground
248 // fog is "overcast" by default of "broken" for patches of fog
249 SGPropertyNode_ptr layerNode = cloudsNode->getChild(LAYER, 0, true );
250 SGMetarCloud::Coverage coverage = isBC ? SGMetarCloud::COVERAGE_SCATTERED : SGMetarCloud::COVERAGE_BROKEN;
251 layerNode->setDoubleValue( "coverage-type", SGCloudLayer::getCoverageType(coverage_string[coverage]) );
252 layerNode->setStringValue( "coverage", coverage_string[coverage] );
253 layerNode->setDoubleValue( "elevation-ft", _station_elevation - LAYER_BOTTOM_BELOW_STATION_ELEVATION );
254 layerNode->setDoubleValue( "thickness-ft", isMI ?
255 30 + LAYER_BOTTOM_BELOW_STATION_ELEVATION : // shallow fog, 10m/30ft
256 500 + LAYER_BOTTOM_BELOW_STATION_ELEVATION ); // fog, 150m/500ft
257 layerNode->setDoubleValue( "visibility-m", _min_visibility );
258 _min_visibility = _max_visibility = 20000.0; // assume good visibility above the fog
259 layerOffset = 1; // shudder
260 } else if( setGroundCloudLayer && isHZ ) {
263 for( unsigned i = 0; i < 5-layerOffset; i++ ) {
264 SGPropertyNode_ptr layerNode = cloudsNode->getChild(LAYER, i+layerOffset, true );
265 SGMetarCloud::Coverage coverage = i < metarClouds.size() ? metarClouds[i].getCoverage() : SGMetarCloud::COVERAGE_CLEAR;
267 i >= metarClouds.size() || coverage == SGMetarCloud::COVERAGE_CLEAR ?
269 metarClouds[i].getAltitude_ft() + _station_elevation;
271 layerNode->setStringValue( "coverage", coverage_string[coverage] );
272 layerNode->setDoubleValue( "coverage-type", SGCloudLayer::getCoverageType(coverage_string[coverage]) );
273 layerNode->setDoubleValue( "elevation-ft", elevation );
274 layerNode->setDoubleValue( "thickness-ft", thickness_value[coverage]);
275 layerNode->setDoubleValue( "span-m", 40000 );
276 layerNode->setDoubleValue( "visibility-m", 50.0 );
280 _rain = m->getRain();
281 _hail = m->getHail();
282 _snow = m->getSnow();
283 _snow_cover = m->getSnowCover();
284 _metarValidNode->setBoolValue(true);
287 } // namespace Environment