]> git.mxchange.org Git - flightgear.git/blob - src/Environment/fgmetar.cxx
Move viewer-related sources to separate folder.
[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, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <simgear/math/sg_random.h>
40 #include <simgear/timing/sg_time.hxx>
41 #include <Main/fg_props.hxx>
42
43 #include "fgmetar.hxx"
44
45
46 FGMetar::FGMetar(const string& icao) :
47         SGMetar(icao),
48         _snow_cover(false)
49 {
50         int i;
51         double d;
52
53         // CAVOK: visibility >= 10km; lowest cloud layer >= 5000 ft; any coverage
54         if (getCAVOK()) {
55                 if (_min_visibility.getVisibility_m() == SGMetarNaN)
56                         _min_visibility.set(12000.0);
57
58                 if (_max_visibility.getVisibility_m() == SGMetarNaN)
59                         _min_visibility.set(12000.0);
60
61                 vector<SGMetarCloud> cv = _clouds;;
62                 if (cv.empty()) {
63                         SGMetarCloud cl;
64                         cl.set(5500 * SG_FEET_TO_METER, SGMetarCloud::COVERAGE_SCATTERED);
65                         _clouds.push_back(cl);
66                 }
67         }
68
69         // visibility
70         d = _min_visibility.getVisibility_m();
71         if (d == SGMetarNaN)
72                 d = 10000.0;
73         if (_min_visibility.getModifier() == SGMetarVisibility::GREATER_THAN)
74                 d += 15000.0;// * sg_random();
75         _min_visibility.set(d);
76
77         if (_max_visibility.getVisibility_m() == SGMetarNaN)
78                 _max_visibility.set(d);
79
80         for (i = 0; i < 8; i++) {
81                 d = _dir_visibility[i].getVisibility_m();
82                 if (d == SGMetarNaN)
83                         _dir_visibility[i].set(10000.0);
84                 if (_dir_visibility[i].getModifier() == SGMetarVisibility::GREATER_THAN)
85                         d += 15000.0;// * sg_random();
86                 _dir_visibility[i].set(d);
87         }
88
89         // wind
90         if (_wind_dir == -1) {
91                 if (_wind_range_from == -1) {
92                         _wind_dir = 0;
93                         _wind_range_from = 0;
94                         _wind_range_to = 359;
95                 } else {
96                         _wind_dir = (_wind_range_from + _wind_range_to) / 2;
97                 }
98         } else if (_wind_range_from == -1) {
99                 _wind_range_from = _wind_range_to = _wind_dir;
100         }
101
102         if (_wind_speed == SGMetarNaN)
103                 _wind_speed = 0.0;
104         if (_gust_speed == SGMetarNaN)
105                 _gust_speed = 0.0;
106
107         // clouds
108         vector<SGMetarCloud> cv = _clouds;
109         vector<SGMetarCloud>::iterator cloud, cv_end = cv.end();
110
111         for (i = 0, cloud = cv.begin(); cloud != cv_end; ++cloud, i++) {
112                 SGMetarCloud::Coverage cov = cloud->getCoverage();
113                 if (cov == SGMetarCloud::COVERAGE_NIL)
114                         cov = SGMetarCloud::COVERAGE_CLEAR;
115
116                 double alt = cloud->getAltitude_ft();
117                 if (alt == SGMetarNaN)
118                         alt = -9999;
119
120                 cloud->set(alt, cov);
121         }
122
123
124         // temperature/pressure
125         if (_temp == SGMetarNaN)
126                 _temp = 15.0;
127
128         if (_dewp == SGMetarNaN)
129                 _dewp = 0.0;
130
131         if (_pressure == SGMetarNaN)
132                 _pressure = 30.0 * SG_INHG_TO_PA;
133
134         // snow cover
135         map<string, SGMetarRunway> rm = getRunways();
136         map<string, SGMetarRunway>::const_iterator runway, rm_end = rm.end();
137         for (runway = rm.begin(); runway != rm_end; ++runway) {
138                 SGMetarRunway rwy = runway->second;
139                 if (rwy.getDeposit() >= 3 ) {
140                         _snow_cover = true;
141                         break;
142                 }
143         }
144         if (_temp < 5.0 && _snow)
145                 _snow_cover = true;
146         if (_temp < 1.0 && getRelHumidity() > 80)
147                 _snow_cover = true;
148
149         _time = sgTimeGetGMT(_year - 1900, _month - 1, _day, _hour, _minute, 0);
150
151         SG_LOG(SG_ENVIRONMENT, SG_INFO, _data);
152         if (_x_proxy)
153                 SG_LOG(SG_ENVIRONMENT, SG_INFO, "METAR from proxy");
154         else
155                 SG_LOG(SG_ENVIRONMENT, SG_INFO, "METAR from weather.noaa.gov");
156 }
157
158
159 long FGMetar::getAge_min() const
160 {
161         time_t now = _x_proxy ? _rq_time : time(0);
162         return (now - _time) / 60;
163 }
164