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.
23 #include "metarproperties.hxx"
24 #include "fgmetar.hxx"
25 #include "environment.hxx"
26 #include "atmosphere.hxx"
27 #include <simgear/scene/sky/cloud.hxx>
28 #include <simgear/structure/exception.hxx>
32 namespace Environment {
34 MetarProperties::MetarProperties( SGPropertyNode_ptr rootNode ) :
36 _metarValidNode( rootNode->getNode( "valid", true ) ),
37 _station_elevation(0.0),
38 _station_latitude(0.0),
39 _station_longitude(0.0),
40 _min_visibility(16000.0),
41 _max_visibility(16000.0),
43 _base_wind_range_from(0),
44 _base_wind_range_to(0),
46 _wind_from_north_fps(0.0),
47 _wind_from_east_fps(0.0),
53 _sea_level_temperature(0.0),
54 _sea_level_dewpoint(0.0),
55 _sea_level_pressure(29.92),
61 // don't tie metar-valid, so listeners get triggered
62 _metarValidNode->setBoolValue( false );
63 _tiedProperties.setRoot( _rootNode );
64 _tiedProperties.Tie("data", this, &MetarProperties::get_metar, &MetarProperties::set_metar );
65 _tiedProperties.Tie("station-id", this, &MetarProperties::get_station_id );
66 _tiedProperties.Tie("station-elevation-ft", &_station_elevation );
67 _tiedProperties.Tie("station-latitude-deg", &_station_latitude );
68 _tiedProperties.Tie("station-longitude-deg", &_station_longitude );
69 _tiedProperties.Tie("min-visibility-m", &_min_visibility );
70 _tiedProperties.Tie("max-visibility-m", &_max_visibility );
71 _tiedProperties.Tie("base-wind-range-from", &_base_wind_range_from );
72 _tiedProperties.Tie("base-wind-range-to", &_base_wind_range_to );
73 _tiedProperties.Tie("base-wind-speed-kt", &_wind_speed );
74 _tiedProperties.Tie("base-wind-dir-deg", &_base_wind_dir );
75 _tiedProperties.Tie("base-wind-from-north-fps", &_wind_from_north_fps );
76 _tiedProperties.Tie("base-wind-from-east-fps", &_wind_from_east_fps );
77 _tiedProperties.Tie("gust-wind-speed-kt", &_gusts );
78 _tiedProperties.Tie("temperature-degc", &_temperature );
79 _tiedProperties.Tie("dewpoint-degc", &_dewpoint );
80 _tiedProperties.Tie("rel-humidity-norm", &_humidity );
81 _tiedProperties.Tie("pressure-inhg", &_pressure );
82 _tiedProperties.Tie("temperature-sea-level-degc", &_sea_level_temperature );
83 _tiedProperties.Tie("dewpoint-sea-level-degc", &_sea_level_dewpoint );
84 _tiedProperties.Tie("pressure-sea-level-inhg", &_sea_level_pressure );
85 _tiedProperties.Tie("rain-norm", &_rain );
86 _tiedProperties.Tie("hail-norm", &_hail );
87 _tiedProperties.Tie("snow-norm", &_snow);
88 _tiedProperties.Tie("snow-cover", &_snow_cover );
91 MetarProperties::~MetarProperties()
95 static const string coverage_string[] = {
96 SGCloudLayer::SG_CLOUD_CLEAR_STRING,
97 SGCloudLayer::SG_CLOUD_FEW_STRING,
98 SGCloudLayer::SG_CLOUD_SCATTERED_STRING,
99 SGCloudLayer::SG_CLOUD_BROKEN_STRING,
100 SGCloudLayer::SG_CLOUD_OVERCAST_STRING,
103 static const double thickness_value[] = { 0, 65, 600, 750, 1000 };
105 void MetarProperties::set_metar( const char * metar )
109 SGSharedPtr<FGMetar> m;
111 m = new FGMetar( _metar );
113 catch( sg_io_exception ) {
114 SG_LOG( SG_GENERAL, SG_WARN, "Can't parse metar: " << _metar );
115 _metarValidNode->setBoolValue(false);
119 _min_visibility = m->getMinVisibility().getVisibility_m();
120 _max_visibility = m->getMaxVisibility().getVisibility_m();
122 const SGMetarVisibility *dirvis = m->getDirVisibility();
123 for ( int i = 0; i < 8; i++, dirvis++) {
124 SGPropertyNode *vis = _rootNode->getChild("visibility", i, true);
125 double v = dirvis->getVisibility_m();
127 vis->setDoubleValue("min-m", v);
128 vis->setDoubleValue("max-m", v);
131 _base_wind_dir = m->getWindDir();
132 _base_wind_range_from = m->getWindRangeFrom();
133 _base_wind_range_to = m->getWindRangeTo();
134 _wind_speed = m->getWindSpeed_kt();
136 double speed_fps = _wind_speed * SG_NM_TO_METER * SG_METER_TO_FEET / 3600.0;
137 _wind_from_north_fps = speed_fps * cos((double)_base_wind_dir * SGD_DEGREES_TO_RADIANS);
138 _wind_from_east_fps = speed_fps * sin((double)_base_wind_dir * SGD_DEGREES_TO_RADIANS);
139 _gusts = m->getGustSpeed_kt();
140 _temperature = m->getTemperature_C();
141 _dewpoint = m->getDewpoint_C();
142 _humidity = m->getRelHumidity();
143 _pressure = m->getPressure_inHg();
146 // 1. check the id given in the metar
147 FGAirport* a = FGAirport::findByIdent(m->getId());
149 // 2. if unknown, find closest airport with metar to current position
151 SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(), _latitude_n->getDoubleValue());
152 a = FGAirport::findClosest(pos, 10000.0, &_airportWithMetarFilter);
155 // 3. otherwise use ground elevation
157 _station_elevation = a->getElevation();
158 const SGGeod & towerPosition = a->getTowerLocation();
159 _station_latitude = towerPosition.getLatitudeDeg();
160 _station_longitude = towerPosition.getLongitudeDeg();
161 _station_id = a->ident();
163 _station_elevation = 0.0;
164 _station_latitude = 0.0;
165 _station_longitude = 0.0;
166 _station_id = "XXXX";
167 // station_elevation_ft = _ground_elevation_n->getDoubleValue() * SG_METER_TO_FEET;
168 // _station_id = m->getId();
172 { // calculate sea level temperature, dewpoint and pressure
173 FGEnvironment dummy; // instantiate a dummy so we can leech a method
174 dummy.set_elevation_ft( _station_elevation );
175 dummy.set_temperature_degc( _temperature );
176 dummy.set_dewpoint_degc( _dewpoint );
177 _sea_level_temperature = dummy.get_temperature_sea_level_degc();
178 _sea_level_dewpoint = dummy.get_dewpoint_sea_level_degc();
180 double elevation_m = _station_elevation * SG_FEET_TO_METER;
181 double fieldPressure = FGAtmo::fieldPressure( elevation_m, _pressure * atmodel::inHg );
182 _sea_level_pressure = P_layer(0, elevation_m, fieldPressure, _temperature + atmodel::freezing, atmodel::ISA::lam0) / atmodel::inHg;
185 vector<SGMetarCloud> cv = m->getClouds();
186 vector<SGMetarCloud>::const_iterator cloud, cloud_end = cv.end();
189 static const char * LAYER = "layer";
190 SGPropertyNode_ptr cloudsNode = _rootNode->getNode("clouds", true );
191 const vector<SGMetarCloud> & metarClouds = m->getClouds();
192 for( unsigned i = 0; i < 5; i++ ) {
193 SGPropertyNode_ptr layerNode = cloudsNode->getChild(LAYER, i, true );
194 int coverage = i < metarClouds.size() ? metarClouds[i].getCoverage() : 0;
195 double elevation = i >= metarClouds.size() || coverage == 0 ? -9999.0 : metarClouds[i].getAltitude_ft() + _station_elevation;
196 layerNode->setStringValue( "coverage", coverage_string[coverage] );
197 layerNode->setDoubleValue( "coverage-type", SGCloudLayer::getCoverageType(coverage_string[coverage]) );
198 layerNode->setDoubleValue( "elevation-ft", elevation );
199 layerNode->setDoubleValue( "thickness-ft", thickness_value[coverage]);
200 layerNode->setDoubleValue( "span-m", 40000 );
205 _rain = m->getRain();
206 _hail = m->getHail();
207 _snow = m->getSnow();
208 _snow_cover = m->getSnowCover();
209 _metarValidNode->setBoolValue(true);
212 } // namespace Environment