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/environment/metar.hxx>
29 #include <simgear/structure/exception.hxx>
34 #if defined(__linux__) || defined(__sun) || defined(__CYGWIN__) \
35 || defined( __FreeBSD__ ) || defined ( sgi )
36 # define R "\033[31;1m" // red
37 # define G "\033[32;1m" // green
38 # define Y "\033[33;1m" // yellow
39 # define B "\033[34;1m" // blue
40 # define M "\033[35;1m" // magenta
41 # define C "\033[36;1m" // cyan
42 # define W "\033[37;1m" // white
43 # define N "\033[m" // normal
57 const char *azimuthName(double d)
60 "N", "NNE", "NE", "ENE",
61 "E", "ESE", "SE", "SSE",
62 "S", "SSW", "SW", "WSW",
63 "W", "WNW", "NW", "NNW"
70 return dir[int(d / 22.5)];
74 // round double to 10^g
75 double rnd(double r, int g = 0)
77 double f = pow(10.0, g);
78 return f * rint(r / f);
82 ostream& operator<<(ostream& s, SGMetarVisibility& v)
85 int m = v.getModifier();
87 if (m == SGMetarVisibility::GREATER_THAN)
89 else if (m == SGMetarVisibility::LESS_THAN)
95 double dist = rnd(v.getVisibility_m(), 1);
97 buf << rnd(dist, 1) << " m";
99 buf << rnd(dist / 1000.0, -1) << " km";
101 const char *dir = "";
103 if ((i = v.getDirection()) != -1) {
104 dir = azimuthName(i);
107 buf << "\t\t\t\t\t" << mod << rnd(v.getVisibility_sm(), -1) << " US-miles " << dir;
108 return s << buf.str();
112 void printReport(SGMetar *m)
114 #define NaN SGMetarNaN
120 if ((i = m->getReportType()) == SGMetar::AUTO)
121 s = "\t\t(automatically generated)";
122 else if (i == SGMetar::COR)
123 s = "\t\t(manually corrected)";
124 else if (i == SGMetar::RTD)
125 s = "\t\t(routine delayed)";
129 cout << "METAR Report" << s << endl;
130 cout << "============" << endl;
131 cout << "Airport-Id:\t\t" << m->getId() << endl;
135 int year = m->getYear();
136 int month = m->getMonth();
137 cout << "Report time:\t\t" << year << '/' << month << '/' << m->getDay();
138 cout << ' ' << m->getHour() << ':';
139 cout << setw(2) << setfill('0') << m->getMinute() << " UTC" << endl;
143 SGMetarVisibility minvis = m->getMinVisibility();
144 SGMetarVisibility maxvis = m->getMaxVisibility();
145 double min = minvis.getVisibility_m();
146 double max = maxvis.getVisibility_m();
149 cout << "min. Visibility:\t" << minvis << endl;
150 cout << "max. Visibility:\t" << maxvis << endl;
152 cout << "Visibility:\t\t" << minvis << endl;
156 // directed visibility
157 SGMetarVisibility *dirvis = m->getDirVisibility();
158 for (i = 0; i < 8; i++, dirvis++)
159 if (dirvis->getVisibility_m() != NaN)
160 cout << "\t\t\t" << *dirvis << endl;
163 // vertical visibility
164 SGMetarVisibility vertvis = m->getVertVisibility();
165 if ((d = vertvis.getVisibility_ft()) != NaN)
166 cout << "Vert. visibility:\t" << vertvis << endl;
167 else if (vertvis.getModifier() == SGMetarVisibility::NOGO)
168 cout << "Vert. visibility:\timpossible to determine" << endl;
172 d = m->getWindSpeed_kmh();
173 cout << "Wind:\t\t\t";
175 cout << "none" << endl;
177 if ((i = m->getWindDir()) == -1)
178 cout << "from variable directions";
180 cout << "from the " << azimuthName(i) << " (" << i << "°)";
181 cout << " at " << rnd(d, -1) << " km/h";
183 cout << "\t\t" << rnd(m->getWindSpeed_kt(), -1) << " kt";
184 cout << " = " << rnd(m->getWindSpeed_mph(), -1) << " mph";
185 cout << " = " << rnd(m->getWindSpeed_mps(), -1) << " m/s";
188 if ((d = m->getGustSpeed_kmh()) != NaN) {
189 cout << "\t\t\twith gusts at " << rnd(d, -1) << " km/h";
190 cout << "\t\t\t" << rnd(m->getGustSpeed_kt(), -1) << " kt";
191 cout << " = " << rnd(m->getGustSpeed_mph(), -1) << " mph";
192 cout << " = " << rnd(m->getGustSpeed_mps(), -1) << " m/s";
196 int from = m->getWindRangeFrom();
197 int to = m->getWindRangeTo();
199 cout << "\t\t\tvariable from " << azimuthName(from);
200 cout << " to " << azimuthName(to);
201 cout << " (" << from << "°--" << to << "°)" << endl;
206 // temperature/humidity/air pressure
207 if ((d = m->getTemperature_C()) != NaN) {
208 cout << "Temperature:\t\t" << d << "°C\t\t\t\t\t";
209 cout << rnd(m->getTemperature_F(), -1) << "°F" << endl;
211 if ((d = m->getDewpoint_C()) != NaN) {
212 cout << "Dewpoint:\t\t" << d << "°C\t\t\t\t\t";
213 cout << rnd(m->getDewpoint_F(), -1) << "°F" << endl;
214 cout << "Rel. Humidity:\t\t" << rnd(m->getRelHumidity()) << "%" << endl;
217 if ((d = m->getPressure_hPa()) != NaN) {
218 cout << "Pressure:\t\t" << rnd(d) << " hPa\t\t\t\t";
219 cout << rnd(m->getPressure_inHg(), -2) << " in. Hg" << endl;
224 vector<string> wv = m->getWeather();
225 vector<string>::iterator weather;
226 for (i = 0, weather = wv.begin(); weather != wv.end(); weather++, i++) {
227 cout << (i ? ", " : "Weather:\t\t") << weather->c_str();
234 const char *coverage_string[5] = {
235 "clear skies", "few clouds", "scattered clouds", "broken clouds", "sky overcast"
237 vector<SGMetarCloud> cv = m->getClouds();
238 vector<SGMetarCloud>::iterator cloud;
239 for (lineno = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, lineno++) {
240 cout << (lineno ? "\t\t\t" : "Sky condition:\t\t");
242 if ((i = cloud->getCoverage()) != -1)
243 cout << coverage_string[i];
244 if ((d = cloud->getAltitude_ft()) != NaN)
245 cout << " at " << rnd(d, 1) << " ft";
246 if ((s = cloud->getTypeLongString()))
247 cout << " (" << s << ')';
249 cout << "\t\t\t" << rnd(cloud->getAltitude_m(), 1) << " m";
255 map<string, SGMetarRunway> rm = m->getRunways();
256 map<string, SGMetarRunway>::iterator runway;
257 for (runway = rm.begin(); runway != rm.end(); runway++) {
259 if (!strcmp(runway->first.c_str(), "ALL"))
260 cout << "All runways:\t\t";
262 cout << "Runway " << runway->first << ":\t\t";
263 SGMetarRunway rwy = runway->second;
265 // assemble surface string
266 vector<string> surface;
267 if ((s = rwy.getDepositString()) && strlen(s))
268 surface.push_back(s);
269 if ((s = rwy.getExtentString()) && strlen(s))
270 surface.push_back(s);
271 if ((d = rwy.getDepth()) != NaN) {
272 sprintf(buf, "%.0lf mm", d * 1000.0);
273 surface.push_back(buf);
275 if ((s = rwy.getFrictionString()) && strlen(s))
276 surface.push_back(s);
277 if ((d = rwy.getFriction()) != NaN) {
278 sprintf(buf, "friction: %.2lf", d);
279 surface.push_back(buf);
282 if (surface.size()) {
283 vector<string>::iterator rwysurf = surface.begin();
284 for (i = 0; rwysurf != surface.end(); rwysurf++, i++) {
292 // assemble visibility string
293 SGMetarVisibility minvis = rwy.getMinVisibility();
294 SGMetarVisibility maxvis = rwy.getMaxVisibility();
295 if ((d = minvis.getVisibility_m()) != NaN) {
297 cout << endl << "\t\t\t";
300 if (maxvis.getVisibility_m() != d) {
301 cout << endl << "\t\t\t" << maxvis << endl;
305 if (rwy.getWindShear()) {
307 cout << endl << "\t\t\t";
308 cout << "critical wind shear" << endl;
317 void printArgs(SGMetar *m, double airport_elevation)
319 #define NaN SGMetarNaN
325 sprintf(buf, "--airport=%s ", m->getId());
329 sprintf(buf, "--start-date-gmt=%4d:%02d:%02d:%02d:%02d:00 ",
330 m->getYear(), m->getMonth(), m->getDay(),
331 m->getHour(), m->getMinute());
335 const char *coverage_string[5] = {
336 "clear", "few", "scattered", "broken", "overcast"
338 vector<SGMetarCloud> cv = m->getClouds();
339 vector<SGMetarCloud>::iterator cloud;
340 for (i = 0, cloud = cv.begin(); i < 5; i++) {
342 double altitude = -99999;
343 if (cloud != cv.end()) {
344 coverage = cloud->getCoverage();
345 altitude = coverage ? cloud->getAltitude_ft() + airport_elevation : -99999;
348 sprintf(buf, "--prop:/environment/clouds/layer[%d]/coverage=%s ", i, coverage_string[coverage]);
350 sprintf(buf, "--prop:/environment/clouds/layer[%d]/elevation-ft=%.0lf ", i, altitude);
352 sprintf(buf, "--prop:/environment/clouds/layer[%d]/thickness-ft=500 ", i);
356 // environment (temperature, dewpoint, visibility, pressure)
357 // metar sets don't provide aloft information; we have to
358 // set the same values for all boundary levels
359 int wind_dir = m->getWindDir();
360 double visibility = m->getMinVisibility().getVisibility_m();
361 double dewpoint = m->getDewpoint_C();
362 double temperature = m->getTemperature_C();
363 double pressure = m->getPressure_inHg();
364 double wind_speed = m->getWindSpeed_kt();
365 double elevation = -100;
366 for (i = 0; i < 3; i++, elevation += 2000.0) {
367 sprintf(buf, "--prop:/environment/config/boundary/entry[%d]/", i);
368 int pos = strlen(buf);
370 sprintf(&buf[pos], "elevation-ft=%.0lf", elevation);
372 sprintf(&buf[pos], "turbulence-norm=%.0lf", 0.0);
375 if (visibility != NaN) {
376 sprintf(&buf[pos], "visibility-m=%.0lf", visibility);
379 if (temperature != NaN) {
380 sprintf(&buf[pos], "temperature-degc=%.0lf", temperature);
383 if (dewpoint != NaN) {
384 sprintf(&buf[pos], "dewpoint-degc=%.0lf", dewpoint);
387 if (pressure != NaN) {
388 sprintf(&buf[pos], "pressure-sea-level-inhg=%.0lf", pressure);
391 if (wind_dir != NaN) {
392 sprintf(&buf[pos], "wind-from-heading-deg=%d", wind_dir);
395 if (wind_speed != NaN) {
396 sprintf(&buf[pos], "wind-speed-kt=%.0lf", wind_speed);
402 int range_from = m->getWindRangeFrom();
403 int range_to = m->getWindRangeTo();
404 double gust_speed = m->getGustSpeed_kt();
405 if (wind_speed != NaN && wind_dir != -1) {
406 strcpy(buf, "--wind=");
407 if (range_from != -1 && range_to != -1)
408 sprintf(&buf[strlen(buf)], "%d:%d", range_from, range_to);
410 sprintf(&buf[strlen(buf)], "%d", wind_dir);
411 sprintf(&buf[strlen(buf)], "@%.0lf", wind_speed);
412 if (gust_speed != NaN)
413 sprintf(&buf[strlen(buf)], ":%.0lf", gust_speed);
419 //cout << "fgfs" << endl;
420 vector<string>::iterator arg;
421 for (i = 0, arg = args.begin(); arg != args.end(); i++, arg++) {
422 cout << "\t" << *arg << endl;
429 void getproxy(string& host, string& port)
434 const char *p = getenv("http_proxy");
439 if (!strncmp(p, "http://", 7))
448 for (t = s + strlen(s); t > s; t--)
449 if (!isspace(t[-1]) && t[-1] != '/')
465 "Usage: metar [-v] [-e elevation] [-r|-c] <list of ICAO airport ids or METAR strings>\n"
468 " -h|--help show this help\n"
469 " -v|--verbose verbose output\n"
470 " -r|--report print report (default)\n"
471 " -c|--command-line print command line\n"
472 " -e E|--elevation E set airport elevation to E meters\n"
473 " (added to cloud bases in command line mode)\n"
475 " http_proxy set proxy in the form \"http://host:port/\"\n"
478 " $ metar ksfo koak\n"
479 " $ metar -c ksfo -r ksfo\n"
480 " $ metar \"LOWL 161500Z 19004KT 160V240 9999 FEW035 SCT300 29/23 Q1006 NOSIG\"\n"
481 " $ fgfs `metar -e 183 -c loww`\n"
482 " $ http_proxy=http://localhost:3128/ metar ksfo\n"
488 int main(int argc, char *argv[])
491 bool verbose = false;
492 double elevation = 0.0;
499 string proxy_host, proxy_port;
500 getproxy(proxy_host, proxy_port);
502 for (int i = 1; i < argc; i++) {
503 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
505 else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose"))
507 else if (!strcmp(argv[i], "-r") || !strcmp(argv[i], "--report"))
509 else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--command-line"))
511 else if (!strcmp(argv[i], "-e") || !strcmp(argv[i], "--elevation")) {
513 cerr << "-e option used without elevation" << endl;
516 elevation = strtod(argv[i], 0);
518 static bool shown = false;
519 if (verbose && !shown) {
520 cerr << "Proxy host: '" << proxy_host << "'" << endl;
521 cerr << "Proxy port: '" << proxy_port << "'" << endl << endl;
526 SGMetar *m = new SGMetar(argv[i], proxy_host, proxy_port, "", time(0));
527 //SGMetar *m = new SGMetar("2004/01/11 01:20\nLOWG 110120Z AUTO VRB01KT 0050 1600N R35/0600 FG M06/M06 Q1019 88//////\n");
530 cerr << G"INPUT: " << m->getData() << ""N << endl;
532 const char *unused = m->getUnusedData();
534 cerr << R"UNUSED: " << unused << ""N << endl;
540 printArgs(m, elevation);
543 } catch (const sg_io_exception& e) {
544 cerr << R"ERROR: " << e.getFormattedMessage().c_str() << ""N << endl << endl;