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 "metarairportfilter.hxx"
32 #include <simgear/scene/sky/cloud.hxx>
33 #include <simgear/structure/exception.hxx>
34 #include <Main/fg_props.hxx>
38 namespace Environment {
40 static vector<string> coverage_string;
42 MetarProperties::MetarProperties( SGPropertyNode_ptr 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),
51 _base_wind_range_from(0),
52 _base_wind_range_to(0),
54 _wind_from_north_fps(0.0),
55 _wind_from_east_fps(0.0),
61 _sea_level_temperature(0.0),
62 _sea_level_dewpoint(0.0),
63 _sea_level_pressure(29.92),
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);
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 );
108 MetarProperties::~MetarProperties()
113 static const double thickness_value[] = { 0, 65, 600, 750, 1000 };
115 void MetarProperties::set_metar( const char * metar )
119 SGSharedPtr<FGMetar> m;
121 m = new FGMetar( _metar );
123 catch( sg_io_exception ) {
124 SG_LOG( SG_GENERAL, SG_WARN, "Can't parse metar: " << _metar );
125 _metarValidNode->setBoolValue(false);
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);
136 _min_visibility = m->getMinVisibility().getVisibility_m();
137 _max_visibility = m->getMaxVisibility().getVisibility_m();
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();
144 vis->setDoubleValue("min-m", v);
145 vis->setDoubleValue("max-m", v);
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();
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();
163 // 1. check the id given in the metar
164 FGAirport* a = FGAirport::findByIdent(m->getId());
166 // 2. if unknown, find closest airport with metar to current position
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() );
174 // 3. otherwise use ground elevation
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();
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";
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();
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;
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++ ) {
217 const string & phenomenon = w != NULL && j < w->phenomena.size() ? w->phenomena[j].c_str() : "";
218 n->getChild( "phenomenon", j, true )->setStringValue( phenomenon );
220 const string & description = w != NULL && j < w->descriptions.size() ? w->descriptions[j].c_str() : "";
221 n->getChild( "description", j, true )->setStringValue( description );
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;
234 vector<SGMetarCloud> cv = m->getClouds();
235 vector<SGMetarCloud>::const_iterator cloud, cloud_end = cv.end();
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!
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 ) );
248 if( setGroundCloudLayer ) {
249 // create a cloud layer #0 starting at the ground if its fog, mist or haze
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 );
256 SGMetarCloud::Coverage coverage = SGMetarCloud::COVERAGE_NIL;
257 double thickness = 0;
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 )
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);
280 if( coverage != SGMetarCloud::COVERAGE_NIL ) {
282 // if there is a layer above the fog, limit the top to one foot below that layer's bottom
283 if( metarClouds[0].getCoverage() != SGMetarCloud::COVERAGE_CLEAR )
284 thickness = metarClouds[0].getAltitude_ft() - LAYER_BOTTOM_STATION_OFFSET - 1;
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
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;
303 i >= metarClouds.size() || coverage == SGMetarCloud::COVERAGE_CLEAR ?
305 metarClouds[i].getAltitude_ft() + _station_elevation;
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 );
316 _rain = m->getRain();
317 _hail = m->getHail();
318 _snow = m->getSnow();
319 _snow_cover = m->getSnowCover();
320 _metarValidNode->setBoolValue(true);
323 } // namespace Environment