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, 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/structure/exception.hxx>
29 #include <simgear/environment/metar.hxx>
34 #if defined(__linux__) || defined( __sun__ ) ||defined(__CYGWIN__) || defined( __FreeBSD__ ) || defined ( sgi )
35 # define R "\033[31;1m" // red
36 # define G "\033[32;1m" // green
37 # define Y "\033[33;1m" // yellow
38 # define B "\033[34;1m" // blue
39 # define M "\033[35;1m" // magenta
40 # define C "\033[36;1m" // cyan
41 # define W "\033[37;1m" // white
42 # define N "\033[m" // normal
56 const char *azimuthName(double d)
59 "N", "NNE", "NE", "ENE",
60 "E", "ESE", "SE", "SSE",
61 "S", "SSW", "SW", "WSW",
62 "W", "WNW", "NW", "NNW"
69 return dir[int(d / 22.5)];
73 // round double to 10^g
74 double rnd(double r, int g = 0)
76 double f = pow(10.0, g);
77 return f * rint(r / f);
81 ostream& operator<<(ostream& s, SGMetarVisibility& v)
84 int m = v.getModifier();
86 if (m == SGMetarVisibility::GREATER_THAN)
88 else if (m == SGMetarVisibility::LESS_THAN)
94 double dist = rnd(v.getVisibility_m(), 1);
96 buf << rnd(dist, 1) << " m";
98 buf << rnd(dist / 1000.0, -1) << " km";
100 const char *dir = "";
102 if ((i = v.getDirection()) != -1) {
103 dir = azimuthName(i);
106 buf << "\t\t\t\t\t" << mod << rnd(v.getVisibility_sm(), -1) << " US-miles " << dir;
107 return s << buf.str();
111 void printReport(SGMetar *m)
113 #define NaN SGMetarNaN
119 if ((i = m->getReportType()) == SGMetar::AUTO)
120 s = "\t\t(automatically generated)";
121 else if (i == SGMetar::COR)
122 s = "\t\t(manually corrected)";
123 else if (i == SGMetar::RTD)
124 s = "\t\t(routine delayed)";
128 cout << "METAR Report" << s << endl;
129 cout << "============" << endl;
130 cout << "Airport-Id:\t\t" << m->getId() << endl;
134 int year = m->getYear();
135 int month = m->getMonth();
136 cout << "Report time:\t\t";
137 if (year != -1 && month != -1)
138 cout << year << '/' << month << '/' << m->getDay();
139 cout << ' ' << m->getHour() << ':';
140 cout << setw(2) << setfill('0') << m->getMinute() << " UTC" << endl;
144 SGMetarVisibility minvis = m->getMinVisibility();
145 SGMetarVisibility maxvis = m->getMaxVisibility();
146 double min = minvis.getVisibility_m();
147 double max = maxvis.getVisibility_m();
150 cout << "min. Visibility:\t" << minvis << endl;
151 cout << "max. Visibility:\t" << maxvis << endl;
153 cout << "Visibility:\t\t" << minvis << endl;
157 // directed visibility
158 SGMetarVisibility *dirvis = m->getDirVisibility();
159 for (i = 0; i < 8; i++, dirvis++)
160 if (dirvis->getVisibility_m() != NaN)
161 cout << "\t\t\t" << *dirvis << endl;
164 // vertical visibility
165 SGMetarVisibility vertvis = m->getVertVisibility();
166 if ((d = vertvis.getVisibility_ft()) != NaN)
167 cout << "Vert. visibility:\t" << vertvis << endl;
168 else if (vertvis.getModifier() == SGMetarVisibility::NOGO)
169 cout << "Vert. visibility:\timpossible to determine" << endl;
173 d = m->getWindSpeed_kmh();
174 cout << "Wind:\t\t\t";
176 cout << "none" << endl;
178 if ((i = m->getWindDir()) == -1)
179 cout << "from variable directions";
181 cout << "from the " << azimuthName(i) << " (" << i << "°)";
182 cout << " at " << rnd(d, -1) << " km/h";
184 cout << "\t\t" << rnd(m->getWindSpeed_kt(), -1) << " kt";
185 cout << " = " << rnd(m->getWindSpeed_mph(), -1) << " mph";
186 cout << " = " << rnd(m->getWindSpeed_mps(), -1) << " m/s";
189 if ((d = m->getGustSpeed_kmh()) != NaN) {
190 cout << "\t\t\twith gusts at " << rnd(d, -1) << " km/h";
191 cout << "\t\t\t" << rnd(m->getGustSpeed_kt(), -1) << " kt";
192 cout << " = " << rnd(m->getGustSpeed_mph(), -1) << " mph";
193 cout << " = " << rnd(m->getGustSpeed_mps(), -1) << " m/s";
197 int from = m->getWindRangeFrom();
198 int to = m->getWindRangeTo();
200 cout << "\t\t\tvariable from " << azimuthName(from);
201 cout << " to " << azimuthName(to);
202 cout << " (" << from << "°--" << to << "°)" << endl;
207 // temperature/humidity/air pressure
208 if ((d = m->getTemperature_C()) != NaN) {
209 cout << "Temperature:\t\t" << d << "°C\t\t\t\t\t";
210 cout << rnd(m->getTemperature_F(), -1) << "°F" << endl;
212 if ((d = m->getDewpoint_C()) != NaN) {
213 cout << "Dewpoint:\t\t" << d << "°C\t\t\t\t\t";
214 cout << rnd(m->getDewpoint_F(), -1) << "°F" << endl;
215 cout << "Rel. Humidity:\t\t" << rnd(m->getRelHumidity()) << "%" << endl;
218 if ((d = m->getPressure_hPa()) != NaN) {
219 cout << "Pressure:\t\t" << rnd(d) << " hPa\t\t\t\t";
220 cout << rnd(m->getPressure_inHg(), -2) << " in. Hg" << endl;
225 vector<string> wv = m->getWeather();
226 vector<string>::iterator weather;
227 for (i = 0, weather = wv.begin(); weather != wv.end(); weather++, i++) {
228 cout << (i ? ", " : "Weather:\t\t") << weather->c_str();
235 const char *coverage_string[5] = {
236 "clear skies", "few clouds", "scattered clouds", "broken clouds", "sky overcast"
238 vector<SGMetarCloud> cv = m->getClouds();
239 vector<SGMetarCloud>::iterator cloud;
240 for (lineno = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, lineno++) {
241 cout << (lineno ? "\t\t\t" : "Sky condition:\t\t");
243 if ((i = cloud->getCoverage()) != -1)
244 cout << coverage_string[i];
245 if ((d = cloud->getAltitude_ft()) != NaN)
246 cout << " at " << rnd(d, 1) << " ft";
247 if ((s = cloud->getTypeLongString()))
248 cout << " (" << s << ')';
250 cout << "\t\t\t" << rnd(cloud->getAltitude_m(), 1) << " m";
256 map<string, SGMetarRunway> rm = m->getRunways();
257 map<string, SGMetarRunway>::iterator runway;
258 for (runway = rm.begin(); runway != rm.end(); runway++) {
260 if (!strcmp(runway->first.c_str(), "ALL"))
261 cout << "All runways:\t\t";
263 cout << "Runway " << runway->first << ":\t\t";
264 SGMetarRunway rwy = runway->second;
266 // assemble surface string
267 vector<string> surface;
268 if ((s = rwy.getDeposit()) && strlen(s))
269 surface.push_back(s);
270 if ((s = rwy.getExtentString()) && strlen(s))
271 surface.push_back(s);
272 if ((d = rwy.getDepth()) != NaN) {
273 sprintf(buf, "%.0lf mm", d * 1000.0);
274 surface.push_back(buf);
276 if ((s = rwy.getFrictionString()) && strlen(s))
277 surface.push_back(s);
278 if ((d = rwy.getFriction()) != NaN) {
279 sprintf(buf, "friction: %.2lf", d);
280 surface.push_back(buf);
283 if (surface.size()) {
284 vector<string>::iterator rwysurf = surface.begin();
285 for (i = 0; rwysurf != surface.end(); rwysurf++, i++) {
293 // assemble visibility string
294 SGMetarVisibility minvis = rwy.getMinVisibility();
295 SGMetarVisibility maxvis = rwy.getMaxVisibility();
296 if ((d = minvis.getVisibility_m()) != NaN) {
298 cout << endl << "\t\t\t";
301 if (maxvis.getVisibility_m() != d) {
302 cout << endl << "\t\t\t" << maxvis << endl;
306 if (rwy.getWindShear()) {
308 cout << endl << "\t\t\t";
309 cout << "critical wind shear" << endl;
318 void printArgs(SGMetar *m, double airport_elevation)
320 #define NaN SGMetarNaN
326 sprintf(buf, "--airport=%s ", m->getId());
330 sprintf(buf, "--start-date-gmt=%4d:%02d:%02d:%02d:%02d:00 ",
331 m->getYear(), m->getMonth(), m->getDay(),
332 m->getHour(), m->getMinute());
336 const char *coverage_string[5] = {
337 "clear", "few", "scattered", "broken", "overcast"
339 vector<SGMetarCloud> cv = m->getClouds();
340 vector<SGMetarCloud>::iterator cloud;
341 for (i = 0, cloud = cv.begin(); i < 5; i++) {
343 double altitude = -99999;
344 if (cloud != cv.end()) {
345 coverage = cloud->getCoverage();
346 altitude = coverage ? cloud->getAltitude_ft() + airport_elevation : -99999;
349 sprintf(buf, "--prop:/environment/clouds/layer[%d]/coverage=%s ", i, coverage_string[coverage]);
351 sprintf(buf, "--prop:/environment/clouds/layer[%d]/elevation-ft=%.0lf ", i, altitude);
353 sprintf(buf, "--prop:/environment/clouds/layer[%d]/thickness-ft=500 ", i);
357 // environment (temperature, dewpoint, visibility, pressure)
358 // metar sets don't provide aloft information; we have to
359 // set the same values for all boundary levels
360 int wind_dir = m->getWindDir();
361 double visibility = m->getMinVisibility().getVisibility_m();
362 double dewpoint = m->getDewpoint_C();
363 double temperature = m->getTemperature_C();
364 double pressure = m->getPressure_inHg();
365 double wind_speed = m->getWindSpeed_kt();
366 double elevation = -100;
367 for (i = 0; i < 3; i++, elevation += 2000.0) {
368 sprintf(buf, "--prop:/environment/config/boundary/entry[%d]/", i);
369 int pos = strlen(buf);
371 sprintf(&buf[pos], "elevation-ft=%.0lf", elevation);
373 sprintf(&buf[pos], "turbulence-norm=%.0lf", 0.0);
376 if (visibility != NaN) {
377 sprintf(&buf[pos], "visibility-m=%.0lf", visibility);
380 if (temperature != NaN) {
381 sprintf(&buf[pos], "temperature-degc=%.0lf", temperature);
384 if (dewpoint != NaN) {
385 sprintf(&buf[pos], "dewpoint-degc=%.0lf", dewpoint);
388 if (pressure != NaN) {
389 sprintf(&buf[pos], "pressure-sea-level-inhg=%.0lf", pressure);
392 if (wind_dir != NaN) {
393 sprintf(&buf[pos], "wind-from-heading-deg=%d", wind_dir);
396 if (wind_speed != NaN) {
397 sprintf(&buf[pos], "wind-speed-kt=%.0lf", wind_speed);
403 int range_from = m->getWindRangeFrom();
404 int range_to = m->getWindRangeTo();
405 double gust_speed = m->getGustSpeed_kt();
406 if (wind_speed != NaN && wind_dir != -1) {
407 strcpy(buf, "--wind=");
408 if (range_from != -1 && range_to != -1)
409 sprintf(&buf[strlen(buf)], "%d:%d", range_from, range_to);
411 sprintf(&buf[strlen(buf)], "%d", wind_dir);
412 sprintf(&buf[strlen(buf)], "@%.0lf", wind_speed);
413 if (gust_speed != NaN)
414 sprintf(&buf[strlen(buf)], ":%.0lf", gust_speed);
420 //cout << "fgfs" << endl;
421 vector<string>::iterator arg;
422 for (i = 0, arg = args.begin(); arg != args.end(); i++, arg++) {
423 cout << "\t" << *arg << endl;
430 void getproxy(string& host, string& port)
435 const char *p = getenv("http_proxy");
440 if (!strncmp(p, "http://", 7))
449 for (t = s + strlen(s); t > s; t--)
450 if (!isspace(t[-1]) && t[-1] != '/')
466 "Usage: metar [-v] [-e elevation] [-r|-c] <list of ICAO airport ids or METAR strings>\n"
469 " -h|--help show this help\n"
470 " -v|--verbose verbose output\n"
471 " -r|--report print report (default)\n"
472 " -c|--command-line print command line\n"
473 " -e E|--elevation E set airport elevation to E meters\n"
474 " (added to cloud bases in command line mode)\n"
476 " http_proxy set proxy in the form \"http://host:port/\"\n"
479 " $ metar ksfo koak\n"
480 " $ metar -c ksfo -r ksfo\n"
481 " $ metar \"LOWL 161500Z 19004KT 160V240 9999 FEW035 SCT300 29/23 Q1006 NOSIG\"\n"
482 " $ fgfs `metar -e 183 -c loww`\n"
483 " $ http_proxy=http://localhost:3128/ metar ksfo\n"
489 int main(int argc, char *argv[])
492 bool verbose = false;
493 double elevation = 0.0;
500 string proxy_host, proxy_port;
501 getproxy(proxy_host, proxy_port);
503 for (int i = 1; i < argc; i++) {
504 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
506 else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose"))
508 else if (!strcmp(argv[i], "-r") || !strcmp(argv[i], "--report"))
510 else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--command-line"))
512 else if (!strcmp(argv[i], "-e") || !strcmp(argv[i], "--elevation")) {
514 cerr << "-e option used without elevation" << endl;
517 elevation = strtod(argv[i], 0);
519 static bool shown = false;
520 if (verbose && !shown) {
521 cout << "Proxy host: '" << proxy_host << "'" << endl;
522 cout << "Proxy port: '" << proxy_port << "'" << endl << endl;
527 SGMetar *m = new SGMetar(argv[i], proxy_host, proxy_port);
528 //SGMetar *m = new SGMetar("2004/01/11 01:20\nLOWG 110120Z AUTO VRB01KT 0050 1600N R35/0600 FG M06/M06 Q1019 88//////\n");
531 cerr << G"INPUT: " << m->getData() << ""N << endl;
533 const char *unused = m->getUnusedData();
535 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;