1 // metar interface class demo
3 // Written by Melchior FRANZ, started December 2003.
5 // Copyright (C) 2003 Melchior FRANZ - mfranz@aon.at
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.
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.
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.
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/environment/metar.hxx>
32 #include <simgear/structure/exception.hxx>
37 #if defined(__linux__) || defined(__sun) || defined(__CYGWIN__) \
38 || defined( __FreeBSD__ ) || defined ( sgi )
39 # define R "\033[31;1m" // red
40 # define G "\033[32;1m" // green
41 # define Y "\033[33;1m" // yellow
42 # define B "\033[34;1m" // blue
43 # define M "\033[35;1m" // magenta
44 # define C "\033[36;1m" // cyan
45 # define W "\033[37;1m" // white
46 # define N "\033[m" // normal
60 const char *azimuthName(double d)
63 "N", "NNE", "NE", "ENE",
64 "E", "ESE", "SE", "SSE",
65 "S", "SSW", "SW", "WSW",
66 "W", "WNW", "NW", "NNW"
73 return dir[int(d / 22.5)];
77 // round double to 10^g
78 double rnd(double r, int g = 0)
80 double f = pow(10.0, g);
81 return f * floor(r / f + 0.5);
85 ostream& operator<<(ostream& s, const SGMetarVisibility& v)
88 int m = v.getModifier();
90 if (m == SGMetarVisibility::GREATER_THAN)
92 else if (m == SGMetarVisibility::LESS_THAN)
98 double dist = rnd(v.getVisibility_m(), 1);
100 buf << rnd(dist, 1) << " m";
102 buf << rnd(dist / 1000.0, -1) << " km";
104 const char *dir = "";
106 if ((i = v.getDirection()) != -1) {
107 dir = azimuthName(i);
110 buf << "\t\t\t\t\t" << mod << rnd(v.getVisibility_sm(), -1) << " US-miles " << dir;
111 return s << buf.str();
115 void printReport(SGMetar *m)
117 #define NaN SGMetarNaN
123 if ((i = m->getReportType()) == SGMetar::AUTO)
124 s = "\t\t(automatically generated)";
125 else if (i == SGMetar::COR)
126 s = "\t\t(manually corrected)";
127 else if (i == SGMetar::RTD)
128 s = "\t\t(routine delayed)";
132 cout << "METAR Report" << s << endl;
133 cout << "============" << endl;
134 cout << "Airport-Id:\t\t" << m->getId() << endl;
138 int year = m->getYear();
139 int month = m->getMonth();
140 cout << "Report time:\t\t" << year << '/' << month << '/' << m->getDay();
141 cout << ' ' << m->getHour() << ':';
142 cout << setw(2) << setfill('0') << m->getMinute() << " UTC" << endl;
146 SGMetarVisibility minvis = m->getMinVisibility();
147 SGMetarVisibility maxvis = m->getMaxVisibility();
148 double min = minvis.getVisibility_m();
149 double max = maxvis.getVisibility_m();
152 cout << "min. Visibility:\t" << minvis << endl;
153 cout << "max. Visibility:\t" << maxvis << endl;
155 cout << "Visibility:\t\t" << minvis << endl;
159 // directed visibility
160 const SGMetarVisibility *dirvis = m->getDirVisibility();
161 for (i = 0; i < 8; i++, dirvis++)
162 if (dirvis->getVisibility_m() != NaN)
163 cout << "\t\t\t" << *dirvis << endl;
166 // vertical visibility
167 SGMetarVisibility vertvis = m->getVertVisibility();
168 if ((d = vertvis.getVisibility_ft()) != NaN)
169 cout << "Vert. visibility:\t" << vertvis << endl;
170 else if (vertvis.getModifier() == SGMetarVisibility::NOGO)
171 cout << "Vert. visibility:\timpossible to determine" << endl;
175 d = m->getWindSpeed_kmh();
176 cout << "Wind:\t\t\t";
178 cout << "none" << endl;
180 if ((i = m->getWindDir()) == -1)
181 cout << "from variable directions";
183 cout << "from the " << azimuthName(i) << " (" << i << "°)";
184 cout << " at " << rnd(d, -1) << " km/h";
186 cout << "\t\t" << rnd(m->getWindSpeed_kt(), -1) << " kt";
187 cout << " = " << rnd(m->getWindSpeed_mph(), -1) << " mph";
188 cout << " = " << rnd(m->getWindSpeed_mps(), -1) << " m/s";
191 if ((d = m->getGustSpeed_kmh()) != NaN) {
192 cout << "\t\t\twith gusts at " << rnd(d, -1) << " km/h";
193 cout << "\t\t\t" << rnd(m->getGustSpeed_kt(), -1) << " kt";
194 cout << " = " << rnd(m->getGustSpeed_mph(), -1) << " mph";
195 cout << " = " << rnd(m->getGustSpeed_mps(), -1) << " m/s";
199 int from = m->getWindRangeFrom();
200 int to = m->getWindRangeTo();
202 cout << "\t\t\tvariable from " << azimuthName(from);
203 cout << " to " << azimuthName(to);
204 cout << " (" << from << "°--" << to << "°)" << endl;
209 // temperature/humidity/air pressure
210 if ((d = m->getTemperature_C()) != NaN) {
211 cout << "Temperature:\t\t" << d << "°C\t\t\t\t\t";
212 cout << rnd(m->getTemperature_F(), -1) << "°F" << endl;
214 if ((d = m->getDewpoint_C()) != NaN) {
215 cout << "Dewpoint:\t\t" << d << "°C\t\t\t\t\t";
216 cout << rnd(m->getDewpoint_F(), -1) << "°F" << endl;
217 cout << "Rel. Humidity:\t\t" << rnd(m->getRelHumidity()) << "%" << endl;
220 if ((d = m->getPressure_hPa()) != NaN) {
221 cout << "Pressure:\t\t" << rnd(d) << " hPa\t\t\t\t";
222 cout << rnd(m->getPressure_inHg(), -2) << " in. Hg" << endl;
227 vector<string> wv = m->getWeather();
228 vector<string>::iterator weather;
229 for (i = 0, weather = wv.begin(); weather != wv.end(); weather++, i++) {
230 cout << (i ? ", " : "Weather:\t\t") << weather->c_str();
237 const char *coverage_string[5] = {
238 "clear skies", "few clouds", "scattered clouds", "broken clouds", "sky overcast"
240 vector<SGMetarCloud> cv = m->getClouds();
241 vector<SGMetarCloud>::iterator cloud;
242 for (lineno = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, lineno++) {
243 cout << (lineno ? "\t\t\t" : "Sky condition:\t\t");
245 if ((i = cloud->getCoverage()) != -1)
246 cout << coverage_string[i];
247 if ((d = cloud->getAltitude_ft()) != NaN)
248 cout << " at " << rnd(d, 1) << " ft";
249 if ((s = cloud->getTypeLongString()))
250 cout << " (" << s << ')';
252 cout << "\t\t\t" << rnd(cloud->getAltitude_m(), 1) << " m";
258 map<string, SGMetarRunway> rm = m->getRunways();
259 map<string, SGMetarRunway>::iterator runway;
260 for (runway = rm.begin(); runway != rm.end(); runway++) {
262 if (!strcmp(runway->first.c_str(), "ALL"))
263 cout << "All runways:\t\t";
265 cout << "Runway " << runway->first << ":\t\t";
266 SGMetarRunway rwy = runway->second;
268 // assemble surface string
269 vector<string> surface;
270 if ((s = rwy.getDepositString()) && strlen(s))
271 surface.push_back(s);
272 if ((s = rwy.getExtentString()) && strlen(s))
273 surface.push_back(s);
274 if ((d = rwy.getDepth()) != NaN) {
275 sprintf(buf, "%.1lf mm", d * 1000.0);
276 surface.push_back(buf);
278 if ((s = rwy.getFrictionString()) && strlen(s))
279 surface.push_back(s);
280 if ((d = rwy.getFriction()) != NaN) {
281 sprintf(buf, "friction: %.2lf", d);
282 surface.push_back(buf);
285 if (surface.size()) {
286 vector<string>::iterator rwysurf = surface.begin();
287 for (i = 0; rwysurf != surface.end(); rwysurf++, i++) {
295 // assemble visibility string
296 SGMetarVisibility minvis = rwy.getMinVisibility();
297 SGMetarVisibility maxvis = rwy.getMaxVisibility();
298 if ((d = minvis.getVisibility_m()) != NaN) {
300 cout << endl << "\t\t\t";
303 if (maxvis.getVisibility_m() != d) {
304 cout << endl << "\t\t\t" << maxvis << endl;
308 if (rwy.getWindShear()) {
310 cout << endl << "\t\t\t";
311 cout << "critical wind shear" << endl;
320 void printArgs(SGMetar *m, double airport_elevation)
322 #define NaN SGMetarNaN
328 sprintf(buf, "--airport=%s ", m->getId());
332 sprintf(buf, "--start-date-gmt=%4d:%02d:%02d:%02d:%02d:00 ",
333 m->getYear(), m->getMonth(), m->getDay(),
334 m->getHour(), m->getMinute());
338 const char *coverage_string[5] = {
339 "clear", "few", "scattered", "broken", "overcast"
341 vector<SGMetarCloud> cv = m->getClouds();
342 vector<SGMetarCloud>::iterator cloud;
343 for (i = 0, cloud = cv.begin(); i < 5; i++) {
345 double altitude = -99999;
346 if (cloud != cv.end()) {
347 coverage = cloud->getCoverage();
348 altitude = coverage ? cloud->getAltitude_ft() + airport_elevation : -99999;
351 sprintf(buf, "--prop:/environment/clouds/layer[%d]/coverage=%s ", i, coverage_string[coverage]);
353 sprintf(buf, "--prop:/environment/clouds/layer[%d]/elevation-ft=%.0lf ", i, altitude);
355 sprintf(buf, "--prop:/environment/clouds/layer[%d]/thickness-ft=500 ", i);
359 // environment (temperature, dewpoint, visibility, pressure)
360 // metar sets don't provide aloft information; we have to
361 // set the same values for all boundary levels
362 int wind_dir = m->getWindDir();
363 double visibility = m->getMinVisibility().getVisibility_m();
364 double dewpoint = m->getDewpoint_C();
365 double temperature = m->getTemperature_C();
366 double pressure = m->getPressure_inHg();
367 double wind_speed = m->getWindSpeed_kt();
368 double elevation = -100;
369 for (i = 0; i < 3; i++, elevation += 2000.0) {
370 sprintf(buf, "--prop:/environment/config/boundary/entry[%d]/", i);
371 int pos = strlen(buf);
373 sprintf(&buf[pos], "elevation-ft=%.0lf", elevation);
375 sprintf(&buf[pos], "turbulence-norm=%.0lf", 0.0);
378 if (visibility != NaN) {
379 sprintf(&buf[pos], "visibility-m=%.0lf", visibility);
382 if (temperature != NaN) {
383 sprintf(&buf[pos], "temperature-degc=%.0lf", temperature);
386 if (dewpoint != NaN) {
387 sprintf(&buf[pos], "dewpoint-degc=%.0lf", dewpoint);
390 if (pressure != NaN) {
391 sprintf(&buf[pos], "pressure-sea-level-inhg=%.0lf", pressure);
394 if (wind_dir != NaN) {
395 sprintf(&buf[pos], "wind-from-heading-deg=%d", wind_dir);
398 if (wind_speed != NaN) {
399 sprintf(&buf[pos], "wind-speed-kt=%.0lf", wind_speed);
405 int range_from = m->getWindRangeFrom();
406 int range_to = m->getWindRangeTo();
407 double gust_speed = m->getGustSpeed_kt();
408 if (wind_speed != NaN && wind_dir != -1) {
409 strcpy(buf, "--wind=");
410 if (range_from != -1 && range_to != -1)
411 sprintf(&buf[strlen(buf)], "%d:%d", range_from, range_to);
413 sprintf(&buf[strlen(buf)], "%d", wind_dir);
414 sprintf(&buf[strlen(buf)], "@%.0lf", wind_speed);
415 if (gust_speed != NaN)
416 sprintf(&buf[strlen(buf)], ":%.0lf", gust_speed);
422 //cout << "fgfs" << endl;
423 vector<string>::iterator arg;
424 for (i = 0, arg = args.begin(); arg != args.end(); i++, arg++) {
425 cout << "\t" << *arg << endl;
432 void getproxy(string& host, string& port)
437 const char *p = getenv("http_proxy");
442 if (!strncmp(p, "http://", 7))
451 for (t = s + strlen(s); t > s; t--)
452 if (!isspace(t[-1]) && t[-1] != '/')
468 "Usage: metar [-v] [-e elevation] [-r|-c] <list of ICAO airport ids or METAR strings>\n"
471 " -h|--help show this help\n"
472 " -v|--verbose verbose output\n"
473 " -r|--report print report (default)\n"
474 " -c|--command-line print command line\n"
475 " -e E|--elevation E set airport elevation to E meters\n"
476 " (added to cloud bases in command line mode)\n"
478 " http_proxy set proxy in the form \"http://host:port/\"\n"
481 " $ metar ksfo koak\n"
482 " $ metar -c ksfo -r ksfo\n"
483 " $ metar \"LOWL 161500Z 19004KT 160V240 9999 FEW035 SCT300 29/23 Q1006 NOSIG\"\n"
484 " $ fgfs `metar -e 183 -c loww`\n"
485 " $ http_proxy=http://localhost:3128/ metar ksfo\n"
490 int main(int argc, char *argv[])
493 bool verbose = false;
494 double elevation = 0.0;
501 string proxy_host, proxy_port;
502 getproxy(proxy_host, proxy_port);
504 for (int i = 1; i < argc; i++) {
505 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
507 else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose"))
509 else if (!strcmp(argv[i], "-r") || !strcmp(argv[i], "--report"))
511 else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--command-line"))
513 else if (!strcmp(argv[i], "-e") || !strcmp(argv[i], "--elevation")) {
515 cerr << "-e option used without elevation" << endl;
518 elevation = strtod(argv[i], 0);
520 static bool shown = false;
521 if (verbose && !shown) {
522 cerr << "Proxy host: '" << proxy_host << "'" << endl;
523 cerr << "Proxy port: '" << proxy_port << "'" << endl << endl;
528 SGMetar *m = new SGMetar(argv[i], proxy_host, proxy_port, "", time(0));
529 //SGMetar *m = new SGMetar("2004/01/11 01:20\nLOWG 110120Z AUTO VRB01KT 0050 1600N R35/0600 FG M06/M06 Q1019 88//////\n");
532 cerr << G"INPUT: " << m->getData() << ""N << endl;
534 const char *unused = m->getUnusedData();
536 cerr << R"UNUSED: " << unused << ""N << endl;
542 printArgs(m, elevation);
545 } catch (const sg_io_exception& e) {
546 cerr << R"ERROR: " << e.getFormattedMessage().c_str() << ""N << endl << endl;