]> git.mxchange.org Git - flightgear.git/blob - src/Environment/fgmetar.cxx
Fix line endings
[flightgear.git] / src / Environment / fgmetar.cxx
1 // metar interface class
2 //
3 // Written by Melchior FRANZ, started January 2005.
4 //
5 // Copyright (C) 2005  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 /**
24  * @file fgmetar.cxx
25  * Implements FGMetar class that inherits from SGMetar.
26  *
27  * o provide defaults for unset values
28  * o interpolate/randomize data (GREATER_THAN)
29  * o derive additional values (time, age, snow cover)
30  * o consider minimum identifier (CAVOK, mil. color codes)
31  *
32  * TODO
33  * - NSC & mil. color codes
34  */
35
36 #include <simgear/math/sg_random.h>
37 #include <simgear/timing/sg_time.hxx>
38 #include <Main/fg_props.hxx>
39
40 #include "fgmetar.hxx"
41
42
43 FGMetar::FGMetar(const string& icao, const string& proxy, const string& port, const string& auth) :
44         SGMetar(icao, proxy, port, auth, _rq_time = globals->get_time_params()->get_cur_time()),
45         _snow_cover(false)
46 {
47         int i;
48         double d;
49
50         // CAVOK: visibility >= 10km; lowest cloud layer >= 5000 ft; any coverage
51         if (getCAVOK()) {
52                 if (_min_visibility.getVisibility_m() == SGMetarNaN)
53                         _min_visibility.set(12000.0);
54
55                 if (_max_visibility.getVisibility_m() == SGMetarNaN)
56                         _min_visibility.set(12000.0);
57
58                 vector<SGMetarCloud> cv = _clouds;;
59                 if (!cv.size()) {
60                         SGMetarCloud cl;
61                         cl.set(5500 * SG_FEET_TO_METER, 2);
62                         _clouds.push_back(cl);
63                 }
64         }
65
66         // visibility
67         d = _min_visibility.getVisibility_m();
68         if (d == SGMetarNaN)
69                 d = 10000.0;
70         if (_min_visibility.getModifier() == SGMetarVisibility::GREATER_THAN)
71                 d += 15000.0;// * sg_random();
72         _min_visibility.set(d);
73
74         if (_max_visibility.getVisibility_m() == SGMetarNaN)
75                 _max_visibility.set(d);
76
77         for (i = 0; i < 8; i++) {
78                 d = _dir_visibility[i].getVisibility_m();
79                 if (d == SGMetarNaN)
80                         _dir_visibility[i].set(10000.0);
81                 if (_dir_visibility[i].getModifier() == SGMetarVisibility::GREATER_THAN)
82                         d += 15000.0;// * sg_random();
83                 _dir_visibility[i].set(d);
84         }
85
86         // wind
87         if (_wind_dir == -1) {
88                 if (_wind_range_from == -1) {
89                         _wind_dir = 0;
90                         _wind_range_from = 0;
91                         _wind_range_to = 359;
92                 } else {
93                         _wind_dir = (_wind_range_from + _wind_range_to) / 2;
94                 }
95         } else if (_wind_range_from == -1) {
96                 _wind_range_from = _wind_range_to = _wind_dir;
97         }
98
99         if (_gust_speed == SGMetarNaN)
100                 _gust_speed = 0.0;
101
102         // clouds
103         vector<SGMetarCloud> cv = _clouds;
104         vector<SGMetarCloud>::iterator cloud;
105
106         for (i = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, i++) {
107                 int cov = cloud->getCoverage();
108                 if (cov == -1)
109                         cov = 0;
110
111                 double alt = cloud->getAltitude_ft();
112                 if (alt == SGMetarNaN)
113                         alt = -9999;
114
115                 cloud->set(alt, cov);
116         }
117
118
119         // temperature/pressure
120         if (_temp == SGMetarNaN)
121                 _temp = 15.0;
122
123         if (_dewp == SGMetarNaN)
124                 _dewp = 0.0;
125
126         if (_pressure == SGMetarNaN)
127                 _pressure = 30.0 * SG_INHG_TO_PA;
128
129         // snow cover
130         map<string, SGMetarRunway> rm = getRunways();
131         map<string, SGMetarRunway>::const_iterator runway;
132         for (runway = rm.begin(); runway != rm.end(); runway++) {
133                 SGMetarRunway rwy = runway->second;
134                 if (rwy.getDeposit() >= 3 ) {
135                         _snow_cover = true;
136                         break;
137                 }
138         }
139         if (_temp < 5.0 && _snow)
140                 _snow_cover = true;
141         if (_temp < 1.0 && getRelHumidity() > 80)
142                 _snow_cover = true;
143
144         _time = sgTimeGetGMT(_year - 1900, _month - 1, _day, _hour, _minute, 0);
145
146         SG_LOG(SG_GENERAL, SG_INFO, _data);
147         if (_x_proxy)
148                 SG_LOG(SG_GENERAL, SG_INFO, "METAR from proxy");
149         else
150                 SG_LOG(SG_GENERAL, SG_INFO, "METAR from weather.noaa.gov");
151 }
152
153
154 long FGMetar::getAge_min() const
155 {
156         time_t now = _x_proxy ? _rq_time : time(0);
157         return (now - _time) / 60;
158 }
159