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 );
105 MetarProperties::~MetarProperties()
110 static const double thickness_value[] = { 0, 65, 600, 750, 1000 };
112 void MetarProperties::set_metar( const char * metar )
116 SGSharedPtr<FGMetar> m;
118 m = new FGMetar( _metar );
120 catch( sg_io_exception ) {
121 SG_LOG( SG_GENERAL, SG_WARN, "Can't parse metar: " << _metar );
122 _metarValidNode->setBoolValue(false);
126 _min_visibility = m->getMinVisibility().getVisibility_m();
127 _max_visibility = m->getMaxVisibility().getVisibility_m();
129 const SGMetarVisibility *dirvis = m->getDirVisibility();
130 for ( int i = 0; i < 8; i++, dirvis++) {
131 SGPropertyNode *vis = _rootNode->getChild("visibility", i, true);
132 double v = dirvis->getVisibility_m();
134 vis->setDoubleValue("min-m", v);
135 vis->setDoubleValue("max-m", v);
138 _base_wind_dir = m->getWindDir();
139 _base_wind_range_from = m->getWindRangeFrom();
140 _base_wind_range_to = m->getWindRangeTo();
141 _wind_speed = m->getWindSpeed_kt();
143 double speed_fps = _wind_speed * SG_NM_TO_METER * SG_METER_TO_FEET / 3600.0;
144 _wind_from_north_fps = speed_fps * cos((double)_base_wind_dir * SGD_DEGREES_TO_RADIANS);
145 _wind_from_east_fps = speed_fps * sin((double)_base_wind_dir * SGD_DEGREES_TO_RADIANS);
146 _gusts = m->getGustSpeed_kt();
147 _temperature = m->getTemperature_C();
148 _dewpoint = m->getDewpoint_C();
149 _humidity = m->getRelHumidity();
150 _pressure = m->getPressure_inHg();
153 // 1. check the id given in the metar
154 FGAirport* a = FGAirport::findByIdent(m->getId());
156 // 2. if unknown, find closest airport with metar to current position
158 SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(), _latitude_n->getDoubleValue());
159 a = FGAirport::findClosest(pos, 10000.0, &_airportWithMetarFilter);
162 // 3. otherwise use ground elevation
164 _station_elevation = a->getElevation();
165 const SGGeod & towerPosition = a->getTowerLocation();
166 _station_latitude = towerPosition.getLatitudeDeg();
167 _station_longitude = towerPosition.getLongitudeDeg();
168 _station_id = a->ident();
170 _station_elevation = 0.0;
171 _station_latitude = 0.0;
172 _station_longitude = 0.0;
173 _station_id = "XXXX";
174 // station_elevation_ft = _ground_elevation_n->getDoubleValue() * SG_METER_TO_FEET;
175 // _station_id = m->getId();
179 { // calculate sea level temperature, dewpoint and pressure
180 FGEnvironment dummy; // instantiate a dummy so we can leech a method
181 dummy.set_elevation_ft( _station_elevation );
182 dummy.set_temperature_degc( _temperature );
183 dummy.set_dewpoint_degc( _dewpoint );
184 _sea_level_temperature = dummy.get_temperature_sea_level_degc();
185 _sea_level_dewpoint = dummy.get_dewpoint_sea_level_degc();
187 double elevation_m = _station_elevation * SG_FEET_TO_METER;
188 double fieldPressure = FGAtmo::fieldPressure( elevation_m, _pressure * atmodel::inHg );
189 _sea_level_pressure = P_layer(0, elevation_m, fieldPressure, _temperature + atmodel::freezing, atmodel::ISA::lam0) / atmodel::inHg;
192 vector<SGMetarCloud> cv = m->getClouds();
193 vector<SGMetarCloud>::const_iterator cloud, cloud_end = cv.end();
196 static const char * LAYER = "layer";
197 SGPropertyNode_ptr cloudsNode = _rootNode->getNode("clouds", true );
198 const vector<SGMetarCloud> & metarClouds = m->getClouds();
199 for( unsigned i = 0; i < 5; i++ ) {
200 SGPropertyNode_ptr layerNode = cloudsNode->getChild(LAYER, i, true );
201 int coverage = i < metarClouds.size() ? metarClouds[i].getCoverage() : 0;
202 double elevation = i >= metarClouds.size() || coverage == 0 ? -9999.0 : metarClouds[i].getAltitude_ft() + _station_elevation;
203 layerNode->setStringValue( "coverage", coverage_string[coverage] );
204 layerNode->setDoubleValue( "coverage-type", SGCloudLayer::getCoverageType(coverage_string[coverage]) );
205 layerNode->setDoubleValue( "elevation-ft", elevation );
206 layerNode->setDoubleValue( "thickness-ft", thickness_value[coverage]);
207 layerNode->setDoubleValue( "span-m", 40000 );
212 _rain = m->getRain();
213 _hail = m->getHail();
214 _snow = m->getSnow();
215 _snow_cover = m->getSnowCover();
216 _metarValidNode->setBoolValue(true);
219 } // namespace Environment