]> git.mxchange.org Git - simgear.git/blob - simgear/environment/metar.hxx
e501a0c4611ff8b8c6a235cd0cfb92063d6ef7c6
[simgear.git] / simgear / environment / metar.hxx
1 // metar interface class
2 //
3 // Written by Melchior FRANZ, started December 2003.
4 //
5 // Copyright (C) 2003  Melchior FRANZ - mfranz@aon.at
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifndef _METAR_HXX
24 #define _METAR_HXX
25
26 #include <vector>
27 #include <map>
28 #include <string>
29
30 #include <simgear/constants.h>
31
32 using std::vector;
33 using std::map;
34 using std::string;
35
36 const double SGMetarNaN = -1E20;
37 #define NaN SGMetarNaN
38
39 struct Token {
40         const char *id;
41         const char *text;
42 };
43
44
45 class SGMetar;
46
47 class SGMetarVisibility {
48         friend class SGMetar;
49 public:
50         SGMetarVisibility() :
51                 _distance(NaN),
52                 _direction(-1),
53                 _modifier(EQUALS),
54                 _tendency(NONE) {}
55
56         enum Modifier {
57                 NOGO,
58                 EQUALS,
59                 LESS_THAN,
60                 GREATER_THAN
61         };
62
63         enum Tendency {
64                 NONE,
65                 STABLE,
66                 INCREASING,
67                 DECREASING
68         };
69
70         void set(double dist, int dir = -1, int mod = -1, int tend = -1);
71
72         inline double   getVisibility_m()       const { return _distance; }
73         inline double   getVisibility_ft()      const { return _distance == NaN ? NaN : _distance * SG_METER_TO_FEET; }
74         inline double   getVisibility_sm()      const { return _distance == NaN ? NaN : _distance * SG_METER_TO_SM; }
75         inline int      getDirection()          const { return _direction; }
76         inline int      getModifier()           const { return _modifier; }
77         inline int      getTendency()           const { return _tendency; }
78
79 protected:
80         double  _distance;
81         int     _direction;
82         int     _modifier;
83         int     _tendency;
84 };
85
86
87 // runway condition (surface and visibility)
88 class SGMetarRunway {
89         friend class SGMetar;
90 public:
91         SGMetarRunway() :
92                 _deposit(-1),
93                 _deposit_string(0),
94                 _extent(-1),
95                 _extent_string(0),
96                 _depth(NaN),
97                 _friction(NaN),
98                 _friction_string(0),
99                 _comment(0),
100                 _wind_shear(false) {}
101
102         inline int                      getDeposit()            const { return _deposit; }
103         inline const char               *getDepositString()     const { return _deposit_string; }
104         inline double                   getExtent()             const { return _extent; }
105         inline const char               *getExtentString()      const { return _extent_string; }
106         inline double                   getDepth()              const { return _depth; }
107         inline double                   getFriction()           const { return _friction; }
108         inline const char               *getFrictionString()    const { return _friction_string; }
109         inline const char               *getComment()           const { return _comment; }
110         inline const bool               getWindShear()          const { return _wind_shear; }
111         inline const SGMetarVisibility& getMinVisibility()      const { return _min_visibility; }
112         inline const SGMetarVisibility& getMaxVisibility()      const { return _max_visibility; }
113
114 protected:
115         SGMetarVisibility _min_visibility;
116         SGMetarVisibility _max_visibility;
117         int             _deposit;
118         const char      *_deposit_string;
119         int             _extent;
120         const char      *_extent_string;
121         double          _depth;
122         double          _friction;
123         const char      *_friction_string;
124         const char      *_comment;
125         bool            _wind_shear;
126 };
127
128
129 // cloud layer
130 class SGMetarCloud {
131         friend class SGMetar;
132 public:
133         enum Coverage {
134                 COVERAGE_NIL = -1,
135                 COVERAGE_CLEAR = 0,
136                 COVERAGE_FEW = 1,
137                 COVERAGE_SCATTERED = 2,
138                 COVERAGE_BROKEN = 3,
139                 COVERAGE_OVERCAST = 4
140         };
141
142         static const char * COVERAGE_NIL_STRING;
143         static const char * COVERAGE_CLEAR_STRING;
144         static const char * COVERAGE_FEW_STRING;
145         static const char * COVERAGE_SCATTERED_STRING;
146         static const char * COVERAGE_BROKEN_STRING;
147         static const char * COVERAGE_OVERCAST_STRING;
148
149         SGMetarCloud() : _coverage(COVERAGE_NIL), _altitude(NaN), _type(0), _type_long(0) {}
150
151         void set(double alt, Coverage cov = COVERAGE_NIL );
152
153         inline Coverage getCoverage() const { return _coverage; }
154         static Coverage getCoverage( const std::string & coverage );
155         inline double getAltitude_m() const { return _altitude; }
156         inline double getAltitude_ft() const { return _altitude == NaN ? NaN : _altitude * SG_METER_TO_FEET; }
157         inline const char *getTypeString() const { return _type; }
158         inline const char *getTypeLongString() const { return _type_long; }
159
160 protected:
161         Coverage _coverage;     // quarters: 0 -> clear ... 4 -> overcast
162         double _altitude;       // 1000 m
163         const char *_type;      // CU
164         const char *_type_long; // cumulus
165 };
166
167
168 class SGMetar {
169 public:
170         SGMetar(const string& m, const string& proxy = "", const string& port = "",
171                         const string &auth = "", const time_t time = 0);
172         ~SGMetar();
173
174         enum ReportType {
175                 NONE,
176                 AUTO,
177                 COR,
178                 RTD
179         };
180
181         enum Intensity {
182                 NIL = 0,
183                 LIGHT = 1,
184                 MODERATE = 2,
185                 HEAVY = 3
186         };
187
188         struct Weather {
189                 Weather() { intensity = NIL; vincinity = false; }
190                 Intensity intensity;
191                 bool      vincinity;
192                 vector<string> descriptions;
193                 vector<string> phenomena;
194         };
195
196         inline const char *getData()            const { return _data; }
197         inline const char *getUnusedData()      const { return _m; }
198         inline const bool getProxy()            const { return _x_proxy; }
199         inline const char *getId()              const { return _icao; }
200         inline int      getYear()               const { return _year; }
201         inline int      getMonth()              const { return _month; }
202         inline int      getDay()                const { return _day; }
203         inline int      getHour()               const { return _hour; }
204         inline int      getMinute()             const { return _minute; }
205         inline int      getReportType()         const { return _report_type; }
206
207         inline int      getWindDir()            const { return _wind_dir; }
208         inline double   getWindSpeed_mps()      const { return _wind_speed; }
209         inline double   getWindSpeed_kmh()      const { return _wind_speed == NaN ? NaN : _wind_speed * SG_MPS_TO_KMH; }
210         inline double   getWindSpeed_kt()       const { return _wind_speed == NaN ? NaN : _wind_speed * SG_MPS_TO_KT; }
211         inline double   getWindSpeed_mph()      const { return _wind_speed == NaN ? NaN : _wind_speed * SG_MPS_TO_MPH; }
212
213         inline double   getGustSpeed_mps()      const { return _gust_speed; }
214         inline double   getGustSpeed_kmh()      const { return _gust_speed == NaN ? NaN : _gust_speed * SG_MPS_TO_KMH; }
215         inline double   getGustSpeed_kt()       const { return _gust_speed == NaN ? NaN : _gust_speed * SG_MPS_TO_KT; }
216         inline double   getGustSpeed_mph()      const { return _gust_speed == NaN ? NaN : _gust_speed * SG_MPS_TO_MPH; }
217
218         inline int      getWindRangeFrom()      const { return _wind_range_from; }
219         inline int      getWindRangeTo()        const { return _wind_range_to; }
220
221         inline const SGMetarVisibility& getMinVisibility()      const { return _min_visibility; }
222         inline const SGMetarVisibility& getMaxVisibility()      const { return _max_visibility; }
223         inline const SGMetarVisibility& getVertVisibility()     const { return _vert_visibility; }
224         inline const SGMetarVisibility *getDirVisibility()      const { return _dir_visibility; }
225
226         inline double   getTemperature_C()      const { return _temp; }
227         inline double   getTemperature_F()      const { return _temp == NaN ? NaN : 1.8 * _temp + 32; }
228         inline double   getDewpoint_C()         const { return _dewp; }
229         inline double   getDewpoint_F()         const { return _dewp == NaN ? NaN : 1.8 * _dewp + 32; }
230         inline double   getPressure_hPa()       const { return _pressure == NaN ? NaN : _pressure / 100; }
231         inline double   getPressure_inHg()      const { return _pressure == NaN ? NaN : _pressure * SG_PA_TO_INHG; }
232
233         inline int      getRain()               const { return _rain; }
234         inline int      getHail()               const { return _hail; }
235         inline int      getSnow()               const { return _snow; }
236         inline bool     getCAVOK()              const { return _cavok; }
237
238         double          getRelHumidity()        const;
239
240         inline const vector<SGMetarCloud>& getClouds()  const   { return _clouds; }
241         inline const map<string, SGMetarRunway>& getRunways()   const   { return _runways; }
242         inline const vector<string>& getWeather()               const   { return _weather; }
243         inline const vector<struct Weather> getWeather2()       const   { return _weather2; }
244
245 protected:
246         string  _url;
247         int     _grpcount;
248         bool    _x_proxy;
249         char    *_data;
250         char    *_m;
251         char    _icao[5];
252         int     _year;
253         int     _month;
254         int     _day;
255         int     _hour;
256         int     _minute;
257         int     _report_type;
258         int     _wind_dir;
259         double  _wind_speed;
260         double  _gust_speed;
261         int     _wind_range_from;
262         int     _wind_range_to;
263         double  _temp;
264         double  _dewp;
265         double  _pressure;
266         int     _rain;
267         int     _hail;
268         int     _snow;
269         bool    _cavok;
270         vector<struct Weather> _weather2;
271
272         SGMetarVisibility               _min_visibility;
273         SGMetarVisibility               _max_visibility;
274         SGMetarVisibility               _vert_visibility;
275         SGMetarVisibility               _dir_visibility[8];
276         vector<SGMetarCloud>            _clouds;
277         map<string, SGMetarRunway>      _runways;
278         vector<string>                  _weather;
279
280         bool    scanPreambleDate();
281         bool    scanPreambleTime();
282         void    useCurrentDate();
283
284         bool    scanType();
285         bool    scanId();
286         bool    scanDate();
287         bool    scanModifier();
288         bool    scanWind();
289         bool    scanVariability();
290         bool    scanVisibility();
291         bool    scanRwyVisRange();
292         bool    scanSkyCondition();
293         bool    scanWeather();
294         bool    scanTemperature();
295         bool    scanPressure();
296         bool    scanRunwayReport();
297         bool    scanWindShear();
298         bool    scanTrendForecast();
299         bool    scanColorState();
300         bool    scanRemark();
301         bool    scanRemainder();
302
303         int     scanNumber(char **str, int *num, int min, int max = 0);
304         bool    scanBoundary(char **str);
305         const struct Token *scanToken(char **str, const struct Token *list);
306         char    *loadData(const char *id, const string& proxy, const string& port,
307                         const string &auth, time_t time);
308         void    normalizeData();
309 };
310
311 #undef NaN
312 #endif // _METAR_HXX