]> git.mxchange.org Git - simgear.git/blob - simgear/environment/metar.hxx
Melchior FRANZ:
[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, 59 Temple Place, Suite 330, Boston, MA 02111-1307, 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 SG_USING_STD(vector);
33 SG_USING_STD(map);
34 SG_USING_STD(string);
35
36 const double SGMetarNaN = -1E20;
37 #define NaN SGMetarNaN
38
39 struct Token {
40         char    *id;
41         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 SGMetarVisibility        getMinVisibility()      const { return _min_visibility; }
112         inline 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         SGMetarCloud() :
134                 _coverage(-1),
135                 _altitude(NaN),
136                 _type(0),
137                 _type_long(0) {}
138
139         void set(double alt, int cov = -1);
140
141         inline int      getCoverage()           const { return _coverage; }
142         inline double   getAltitude_m()         const { return _altitude; }
143         inline double   getAltitude_ft()        const { return _altitude == NaN ? NaN : _altitude * SG_METER_TO_FEET; }
144         inline char     *getTypeString()        const { return _type; }
145         inline char     *getTypeLongString()    const { return _type_long; }
146
147 protected:
148         int     _coverage;              // quarters: 0 -> clear ... 4 -> overcast
149         double  _altitude;              // 1000 m
150         char    *_type;                 // CU
151         char    *_type_long;            // cumulus
152 };
153
154
155 class SGMetar {
156 public:
157         SGMetar(const string& m, const string& proxy = "", const string& port = "",
158                         const string &auth = "", const time_t time = 0);
159         ~SGMetar();
160
161         enum ReportType {
162                 NONE,
163                 AUTO,
164                 COR,
165                 RTD
166         };
167
168         inline const char *getData()            const { return _data; }
169         inline const char *getUnusedData()      const { return _m; }
170         inline const bool getProxy()            const { return _x_proxy; }
171         inline const char *getId()              const { return _icao; }
172         inline int      getYear()               const { return _year; }
173         inline int      getMonth()              const { return _month; }
174         inline int      getDay()                const { return _day; }
175         inline int      getHour()               const { return _hour; }
176         inline int      getMinute()             const { return _minute; }
177         inline int      getReportType()         const { return _report_type; }
178
179         inline int      getWindDir()            const { return _wind_dir; }
180         inline double   getWindSpeed_mps()      const { return _wind_speed; }
181         inline double   getWindSpeed_kmh()      const { return _wind_speed == NaN ? NaN : _wind_speed * SG_MPS_TO_KMH; }
182         inline double   getWindSpeed_kt()       const { return _wind_speed == NaN ? NaN : _wind_speed * SG_MPS_TO_KT; }
183         inline double   getWindSpeed_mph()      const { return _wind_speed == NaN ? NaN : _wind_speed * SG_MPS_TO_MPH; }
184
185         inline double   getGustSpeed_mps()      const { return _gust_speed; }
186         inline double   getGustSpeed_kmh()      const { return _gust_speed == NaN ? NaN : _gust_speed * SG_MPS_TO_KMH; }
187         inline double   getGustSpeed_kt()       const { return _gust_speed == NaN ? NaN : _gust_speed * SG_MPS_TO_KT; }
188         inline double   getGustSpeed_mph()      const { return _gust_speed == NaN ? NaN : _gust_speed * SG_MPS_TO_MPH; }
189
190         inline int      getWindRangeFrom()      const { return _wind_range_from; }
191         inline int      getWindRangeTo()        const { return _wind_range_to; }
192
193         inline SGMetarVisibility& getMinVisibility()    { return _min_visibility; }
194         inline SGMetarVisibility& getMaxVisibility()    { return _max_visibility; }
195         inline SGMetarVisibility& getVertVisibility()   { return _vert_visibility; }
196         inline SGMetarVisibility *getDirVisibility()    { return _dir_visibility; }
197
198         inline double   getTemperature_C()      const { return _temp; }
199         inline double   getTemperature_F()      const { return _temp == NaN ? NaN : 1.8 * _temp + 32; }
200         inline double   getDewpoint_C()         const { return _dewp; }
201         inline double   getDewpoint_F()         const { return _dewp == NaN ? NaN : 1.8 * _dewp + 32; }
202         inline double   getPressure_hPa()       const { return _pressure == NaN ? NaN : _pressure / 100; }
203         inline double   getPressure_inHg()      const { return _pressure == NaN ? NaN : _pressure * SG_PA_TO_INHG; }
204
205         inline int      getRain()               const { return _rain; }
206         inline int      getHail()               const { return _hail; }
207         inline int      getSnow()               const { return _snow; }
208         inline bool     getCAVOK()              const { return _cavok; }
209
210         double          getRelHumidity()        const;
211
212         inline vector<SGMetarCloud>& getClouds()        { return _clouds; }
213         inline map<string, SGMetarRunway>& getRunways() { return _runways; }
214         inline vector<string>& getWeather()             { return _weather; }
215
216 protected:
217         string  _url;
218         int     _grpcount;
219         bool    _x_proxy;
220         char    *_data;
221         char    *_m;
222         char    _icao[5];
223         int     _year;
224         int     _month;
225         int     _day;
226         int     _hour;
227         int     _minute;
228         int     _report_type;
229         int     _wind_dir;
230         double  _wind_speed;
231         double  _gust_speed;
232         int     _wind_range_from;
233         int     _wind_range_to;
234         double  _temp;
235         double  _dewp;
236         double  _pressure;
237         int     _rain;
238         int     _hail;
239         int     _snow;
240         bool    _cavok;
241
242         SGMetarVisibility               _min_visibility;
243         SGMetarVisibility               _max_visibility;
244         SGMetarVisibility               _vert_visibility;
245         SGMetarVisibility               _dir_visibility[8];
246         vector<SGMetarCloud>            _clouds;
247         map<string, SGMetarRunway>      _runways;
248         vector<string>                  _weather;
249
250         bool    scanPreambleDate();
251         bool    scanPreambleTime();
252         void    useCurrentDate();
253
254         bool    scanType();
255         bool    scanId();
256         bool    scanDate();
257         bool    scanModifier();
258         bool    scanWind();
259         bool    scanVariability();
260         bool    scanVisibility();
261         bool    scanRwyVisRange();
262         bool    scanSkyCondition();
263         bool    scanWeather();
264         bool    scanTemperature();
265         bool    scanPressure();
266         bool    scanRunwayReport();
267         bool    scanWindShear();
268         bool    scanTrendForecast();
269         bool    scanColorState();
270         bool    scanRemark();
271         bool    scanRemainder();
272
273         int     scanNumber(char **str, int *num, int min, int max = 0);
274         bool    scanBoundary(char **str);
275         const struct Token *scanToken(char **str, const struct Token *list);
276         char    *loadData(const char *id, const string& proxy, const string& port,
277                         const string &auth, time_t time);
278         void    normalizeData();
279 };
280
281 #undef NaN
282 #endif // _METAR_HXX