]> git.mxchange.org Git - flightgear.git/blob - src/Environment/metarproperties.cxx
Keep MSVC happy with the Environment updates
[flightgear.git] / src / Environment / metarproperties.cxx
1 // metarproperties.cxx -- Parse a METAR and write properties
2 //
3 // Written by David Megginson, started May 2002.
4 // Rewritten by Torsten Dreyer, August 2010
5 //
6 // Copyright (C) 2002  David Megginson - david@megginson.com
7 //
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.
12 //
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.
17 //
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.
21 //
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
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
34 using std::string;
35
36 namespace Environment {
37
38 MetarProperties::MetarProperties( SGPropertyNode_ptr rootNode ) :
39   _rootNode(rootNode),
40   _metarValidNode( rootNode->getNode( "valid", true ) ),
41   _station_elevation(0.0),
42   _station_latitude(0.0),
43   _station_longitude(0.0),
44   _min_visibility(16000.0),
45   _max_visibility(16000.0),
46   _base_wind_dir(0),
47   _base_wind_range_from(0),
48   _base_wind_range_to(0),
49   _wind_speed(0.0),
50   _wind_from_north_fps(0.0),
51   _wind_from_east_fps(0.0),
52   _gusts(0.0),
53   _temperature(0.0),
54   _dewpoint(0.0),
55   _humidity(0.0),
56   _pressure(0.0),
57   _sea_level_temperature(0.0),
58   _sea_level_dewpoint(0.0),
59   _sea_level_pressure(29.92),
60   _rain(0.0),
61   _hail(0.0),
62   _snow(0.0),
63   _snow_cover(false)
64 {
65   // don't tie metar-valid, so listeners get triggered
66   _metarValidNode->setBoolValue( false );
67   _tiedProperties.setRoot( _rootNode );
68   _tiedProperties.Tie("data", this, &MetarProperties::get_metar, &MetarProperties::set_metar );
69   _tiedProperties.Tie("station-id", this, &MetarProperties::get_station_id );
70   _tiedProperties.Tie("station-elevation-ft", &_station_elevation );
71   _tiedProperties.Tie("station-latitude-deg", &_station_latitude );
72   _tiedProperties.Tie("station-longitude-deg", &_station_longitude );
73   _tiedProperties.Tie("min-visibility-m", &_min_visibility );
74   _tiedProperties.Tie("max-visibility-m", &_max_visibility );
75   _tiedProperties.Tie("base-wind-range-from", &_base_wind_range_from );
76   _tiedProperties.Tie("base-wind-range-to", &_base_wind_range_to );
77   _tiedProperties.Tie("base-wind-speed-kt", &_wind_speed );
78   _tiedProperties.Tie("base-wind-dir-deg", &_base_wind_dir );
79   _tiedProperties.Tie("base-wind-from-north-fps", &_wind_from_north_fps );
80   _tiedProperties.Tie("base-wind-from-east-fps", &_wind_from_east_fps );
81   _tiedProperties.Tie("gust-wind-speed-kt", &_gusts );
82   _tiedProperties.Tie("temperature-degc", &_temperature );
83   _tiedProperties.Tie("dewpoint-degc", &_dewpoint );
84   _tiedProperties.Tie("rel-humidity-norm", &_humidity );
85   _tiedProperties.Tie("pressure-inhg", &_pressure );
86   _tiedProperties.Tie("temperature-sea-level-degc", &_sea_level_temperature );
87   _tiedProperties.Tie("dewpoint-sea-level-degc", &_sea_level_dewpoint );
88   _tiedProperties.Tie("pressure-sea-level-inhg", &_sea_level_pressure );
89   _tiedProperties.Tie("rain-norm", &_rain );
90   _tiedProperties.Tie("hail-norm", &_hail );
91   _tiedProperties.Tie("snow-norm", &_snow);
92   _tiedProperties.Tie("snow-cover", &_snow_cover );
93 }
94
95 MetarProperties::~MetarProperties()
96 {
97 }
98
99 static const string coverage_string[] = { 
100   SGCloudLayer::SG_CLOUD_CLEAR_STRING,
101   SGCloudLayer::SG_CLOUD_FEW_STRING,
102   SGCloudLayer::SG_CLOUD_SCATTERED_STRING,
103   SGCloudLayer::SG_CLOUD_BROKEN_STRING,
104   SGCloudLayer::SG_CLOUD_OVERCAST_STRING,
105 };
106
107 static const double thickness_value[] = { 0, 65, 600, 750, 1000 };
108
109 void MetarProperties::set_metar( const char * metar )
110 {
111     _metar = metar;
112
113     SGSharedPtr<FGMetar> m;
114     try {
115         m = new FGMetar( _metar );
116     }
117     catch( sg_io_exception ) {
118         SG_LOG( SG_GENERAL, SG_WARN, "Can't parse metar: " << _metar );
119         _metarValidNode->setBoolValue(false);
120         return;
121     }
122
123     _min_visibility = m->getMinVisibility().getVisibility_m();
124     _max_visibility = m->getMaxVisibility().getVisibility_m();
125
126     const SGMetarVisibility *dirvis = m->getDirVisibility();
127     for ( int i = 0; i < 8; i++, dirvis++) {
128         SGPropertyNode *vis = _rootNode->getChild("visibility", i, true);
129         double v = dirvis->getVisibility_m();
130
131         vis->setDoubleValue("min-m", v);
132         vis->setDoubleValue("max-m", v);
133     }
134
135     _base_wind_dir = m->getWindDir();
136     _base_wind_range_from = m->getWindRangeFrom();
137     _base_wind_range_to = m->getWindRangeTo();
138     _wind_speed = m->getWindSpeed_kt();
139
140     double speed_fps = _wind_speed * SG_NM_TO_METER * SG_METER_TO_FEET / 3600.0;
141     _wind_from_north_fps = speed_fps * cos((double)_base_wind_dir * SGD_DEGREES_TO_RADIANS);
142     _wind_from_east_fps = speed_fps * sin((double)_base_wind_dir * SGD_DEGREES_TO_RADIANS);
143     _gusts = m->getGustSpeed_kt();
144     _temperature = m->getTemperature_C();
145     _dewpoint = m->getDewpoint_C();
146     _humidity = m->getRelHumidity();
147     _pressure = m->getPressure_inHg();
148
149     {
150         // 1. check the id given in the metar
151         FGAirport* a = FGAirport::findByIdent(m->getId());
152 /*
153         // 2. if unknown, find closest airport with metar to current position
154         if( a == NULL ) {
155             SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(), _latitude_n->getDoubleValue());
156             a = FGAirport::findClosest(pos, 10000.0, &_airportWithMetarFilter);
157         }
158 */
159         // 3. otherwise use ground elevation
160         if( a != NULL ) {
161             _station_elevation = a->getElevation();
162             const SGGeod & towerPosition = a->getTowerLocation();
163             _station_latitude = towerPosition.getLatitudeDeg();
164             _station_longitude = towerPosition.getLongitudeDeg();
165             _station_id = a->ident();
166         } else {
167             _station_elevation = 0.0;
168             _station_latitude = 0.0;
169             _station_longitude = 0.0;
170             _station_id = "XXXX";
171 //            station_elevation_ft = _ground_elevation_n->getDoubleValue() * SG_METER_TO_FEET;
172 //            _station_id = m->getId();
173         }
174     }
175
176     {    // calculate sea level temperature, dewpoint and pressure
177         FGEnvironment dummy; // instantiate a dummy so we can leech a method
178         dummy.set_elevation_ft( _station_elevation );
179         dummy.set_temperature_degc( _temperature );
180         dummy.set_dewpoint_degc( _dewpoint );
181         _sea_level_temperature = dummy.get_temperature_sea_level_degc();
182         _sea_level_dewpoint = dummy.get_dewpoint_sea_level_degc();
183
184         double elevation_m = _station_elevation * SG_FEET_TO_METER;
185         double fieldPressure = FGAtmo::fieldPressure( elevation_m, _pressure * atmodel::inHg );
186         _sea_level_pressure = P_layer(0, elevation_m, fieldPressure, _temperature + atmodel::freezing, atmodel::ISA::lam0) / atmodel::inHg;
187     }
188
189     vector<SGMetarCloud> cv = m->getClouds();
190     vector<SGMetarCloud>::const_iterator cloud, cloud_end = cv.end();
191
192     {
193         static const char * LAYER = "layer";
194         SGPropertyNode_ptr cloudsNode = _rootNode->getNode("clouds", true );
195         const vector<SGMetarCloud> & metarClouds = m->getClouds();
196         for( unsigned i = 0; i < 5; i++ ) {
197             SGPropertyNode_ptr layerNode = cloudsNode->getChild(LAYER, i, true );
198             int coverage = i < metarClouds.size() ? metarClouds[i].getCoverage() : 0;
199             double elevation = i >= metarClouds.size() || coverage == 0 ? -9999.0 : metarClouds[i].getAltitude_ft() + _station_elevation;
200             layerNode->setStringValue( "coverage", coverage_string[coverage] );
201             layerNode->setDoubleValue( "coverage-type", SGCloudLayer::getCoverageType(coverage_string[coverage]) );
202             layerNode->setDoubleValue( "elevation-ft", elevation );
203             layerNode->setDoubleValue( "thickness-ft", thickness_value[coverage]);
204             layerNode->setDoubleValue( "span-m", 40000 );
205         }
206
207     }
208
209     _rain = m->getRain();
210     _hail = m->getHail();
211     _snow = m->getSnow();
212     _snow_cover = m->getSnowCover();
213     _metarValidNode->setBoolValue(true);
214 }
215
216 } // namespace Environment