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.
29 #include <simgear/debug/logstream.hxx>
30 #include <simgear/environment/metar.hxx>
31 #include <simgear/structure/exception.hxx>
36 #if defined(__linux__) || defined(__sun) || defined(__CYGWIN__) \
37 || defined( __FreeBSD__ ) || defined ( sgi )
38 # define R "\033[31;1m" // red
39 # define G "\033[32;1m" // green
40 # define Y "\033[33;1m" // yellow
41 # define B "\033[34;1m" // blue
42 # define M "\033[35;1m" // magenta
43 # define C "\033[36;1m" // cyan
44 # define W "\033[37;1m" // white
45 # define N "\033[m" // normal
59 const char *azimuthName(double d)
62 "N", "NNE", "NE", "ENE",
63 "E", "ESE", "SE", "SSE",
64 "S", "SSW", "SW", "WSW",
65 "W", "WNW", "NW", "NNW"
72 return dir[int(d / 22.5)];
76 // round double to 10^g
77 double rnd(double r, int g = 0)
79 double f = pow(10.0, g);
80 return f * floor(r / f + 0.5);
84 ostream& operator<<(ostream& s, const SGMetarVisibility& v)
87 int m = v.getModifier();
89 if (m == SGMetarVisibility::GREATER_THAN)
91 else if (m == SGMetarVisibility::LESS_THAN)
97 double dist = rnd(v.getVisibility_m(), 1);
99 buf << rnd(dist, 1) << " m";
101 buf << rnd(dist / 1000.0, -1) << " km";
103 const char *dir = "";
105 if ((i = v.getDirection()) != -1) {
106 dir = azimuthName(i);
109 buf << "\t\t\t\t\t" << mod << rnd(v.getVisibility_sm(), -1) << " US-miles " << dir;
110 return s << buf.str();
114 void printReport(SGMetar *m)
116 #define NaN SGMetarNaN
122 if ((i = m->getReportType()) == SGMetar::AUTO)
123 s = "\t\t(automatically generated)";
124 else if (i == SGMetar::COR)
125 s = "\t\t(manually corrected)";
126 else if (i == SGMetar::RTD)
127 s = "\t\t(routine delayed)";
131 cout << "METAR Report" << s << endl;
132 cout << "============" << endl;
133 cout << "Airport-Id:\t\t" << m->getId() << endl;
137 int year = m->getYear();
138 int month = m->getMonth();
139 cout << "Report time:\t\t" << year << '/' << month << '/' << m->getDay();
140 cout << ' ' << m->getHour() << ':';
141 cout << setw(2) << setfill('0') << m->getMinute() << " UTC" << endl;
145 SGMetarVisibility minvis = m->getMinVisibility();
146 SGMetarVisibility maxvis = m->getMaxVisibility();
147 double min = minvis.getVisibility_m();
148 double max = maxvis.getVisibility_m();
151 cout << "min. Visibility:\t" << minvis << endl;
152 cout << "max. Visibility:\t" << maxvis << endl;
154 cout << "Visibility:\t\t" << minvis << endl;
158 // directed visibility
159 const SGMetarVisibility *dirvis = m->getDirVisibility();
160 for (i = 0; i < 8; i++, dirvis++)
161 if (dirvis->getVisibility_m() != NaN)
162 cout << "\t\t\t" << *dirvis << endl;
165 // vertical visibility
166 SGMetarVisibility vertvis = m->getVertVisibility();
167 if ((d = vertvis.getVisibility_ft()) != NaN)
168 cout << "Vert. visibility:\t" << vertvis << endl;
169 else if (vertvis.getModifier() == SGMetarVisibility::NOGO)
170 cout << "Vert. visibility:\timpossible to determine" << endl;
174 d = m->getWindSpeed_kmh();
175 cout << "Wind:\t\t\t";
177 cout << "none" << endl;
179 if ((i = m->getWindDir()) == -1)
180 cout << "from variable directions";
182 cout << "from the " << azimuthName(i) << " (" << i << "°)";
183 cout << " at " << rnd(d, -1) << " km/h";
185 cout << "\t\t" << rnd(m->getWindSpeed_kt(), -1) << " kt";
186 cout << " = " << rnd(m->getWindSpeed_mph(), -1) << " mph";
187 cout << " = " << rnd(m->getWindSpeed_mps(), -1) << " m/s";
190 if ((d = m->getGustSpeed_kmh()) != NaN) {
191 cout << "\t\t\twith gusts at " << rnd(d, -1) << " km/h";
192 cout << "\t\t\t" << rnd(m->getGustSpeed_kt(), -1) << " kt";
193 cout << " = " << rnd(m->getGustSpeed_mph(), -1) << " mph";
194 cout << " = " << rnd(m->getGustSpeed_mps(), -1) << " m/s";
198 int from = m->getWindRangeFrom();
199 int to = m->getWindRangeTo();
201 cout << "\t\t\tvariable from " << azimuthName(from);
202 cout << " to " << azimuthName(to);
203 cout << " (" << from << "°--" << to << "°)" << endl;
208 // temperature/humidity/air pressure
209 if ((d = m->getTemperature_C()) != NaN) {
210 cout << "Temperature:\t\t" << d << "°C\t\t\t\t\t";
211 cout << rnd(m->getTemperature_F(), -1) << "°F" << endl;
213 if ((d = m->getDewpoint_C()) != NaN) {
214 cout << "Dewpoint:\t\t" << d << "°C\t\t\t\t\t";
215 cout << rnd(m->getDewpoint_F(), -1) << "°F" << endl;
216 cout << "Rel. Humidity:\t\t" << rnd(m->getRelHumidity()) << "%" << endl;
219 if ((d = m->getPressure_hPa()) != NaN) {
220 cout << "Pressure:\t\t" << rnd(d) << " hPa\t\t\t\t";
221 cout << rnd(m->getPressure_inHg(), -2) << " in. Hg" << endl;
226 vector<string> wv = m->getWeather();
227 vector<string>::iterator weather;
228 for (i = 0, weather = wv.begin(); weather != wv.end(); weather++, i++) {
229 cout << (i ? ", " : "Weather:\t\t") << weather->c_str();
236 const char *coverage_string[5] = {
237 "clear skies", "few clouds", "scattered clouds", "broken clouds", "sky overcast"
239 vector<SGMetarCloud> cv = m->getClouds();
240 vector<SGMetarCloud>::iterator cloud;
241 for (lineno = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, lineno++) {
242 cout << (lineno ? "\t\t\t" : "Sky condition:\t\t");
244 if ((i = cloud->getCoverage()) != -1)
245 cout << coverage_string[i];
246 if ((d = cloud->getAltitude_ft()) != NaN)
247 cout << " at " << rnd(d, 1) << " ft";
248 if ((s = cloud->getTypeLongString()))
249 cout << " (" << s << ')';
251 cout << "\t\t\t" << rnd(cloud->getAltitude_m(), 1) << " m";
257 map<string, SGMetarRunway> rm = m->getRunways();
258 map<string, SGMetarRunway>::iterator runway;
259 for (runway = rm.begin(); runway != rm.end(); runway++) {
261 if (!strcmp(runway->first.c_str(), "ALL"))
262 cout << "All runways:\t\t";
264 cout << "Runway " << runway->first << ":\t\t";
265 SGMetarRunway rwy = runway->second;
267 // assemble surface string
268 vector<string> surface;
269 if ((s = rwy.getDepositString()) && strlen(s))
270 surface.push_back(s);
271 if ((s = rwy.getExtentString()) && strlen(s))
272 surface.push_back(s);
273 if ((d = rwy.getDepth()) != NaN) {
274 sprintf(buf, "%.1lf mm", d * 1000.0);
275 surface.push_back(buf);
277 if ((s = rwy.getFrictionString()) && strlen(s))
278 surface.push_back(s);
279 if ((d = rwy.getFriction()) != NaN) {
280 sprintf(buf, "friction: %.2lf", d);
281 surface.push_back(buf);
284 if (surface.size()) {
285 vector<string>::iterator rwysurf = surface.begin();
286 for (i = 0; rwysurf != surface.end(); rwysurf++, i++) {
294 // assemble visibility string
295 SGMetarVisibility minvis = rwy.getMinVisibility();
296 SGMetarVisibility maxvis = rwy.getMaxVisibility();
297 if ((d = minvis.getVisibility_m()) != NaN) {
299 cout << endl << "\t\t\t";
302 if (maxvis.getVisibility_m() != d) {
303 cout << endl << "\t\t\t" << maxvis << endl;
307 if (rwy.getWindShear()) {
309 cout << endl << "\t\t\t";
310 cout << "critical wind shear" << endl;
319 void printArgs(SGMetar *m, double airport_elevation)
321 #define NaN SGMetarNaN
327 sprintf(buf, "--airport=%s ", m->getId());
331 sprintf(buf, "--start-date-gmt=%4d:%02d:%02d:%02d:%02d:00 ",
332 m->getYear(), m->getMonth(), m->getDay(),
333 m->getHour(), m->getMinute());
337 const char *coverage_string[5] = {
338 "clear", "few", "scattered", "broken", "overcast"
340 vector<SGMetarCloud> cv = m->getClouds();
341 vector<SGMetarCloud>::iterator cloud;
342 for (i = 0, cloud = cv.begin(); i < 5; i++) {
344 double altitude = -99999;
345 if (cloud != cv.end()) {
346 coverage = cloud->getCoverage();
347 altitude = coverage ? cloud->getAltitude_ft() + airport_elevation : -99999;
350 sprintf(buf, "--prop:/environment/clouds/layer[%d]/coverage=%s ", i, coverage_string[coverage]);
352 sprintf(buf, "--prop:/environment/clouds/layer[%d]/elevation-ft=%.0lf ", i, altitude);
354 sprintf(buf, "--prop:/environment/clouds/layer[%d]/thickness-ft=500 ", i);
358 // environment (temperature, dewpoint, visibility, pressure)
359 // metar sets don't provide aloft information; we have to
360 // set the same values for all boundary levels
361 int wind_dir = m->getWindDir();
362 double visibility = m->getMinVisibility().getVisibility_m();
363 double dewpoint = m->getDewpoint_C();
364 double temperature = m->getTemperature_C();
365 double pressure = m->getPressure_inHg();
366 double wind_speed = m->getWindSpeed_kt();
367 double elevation = -100;
368 for (i = 0; i < 3; i++, elevation += 2000.0) {
369 sprintf(buf, "--prop:/environment/config/boundary/entry[%d]/", i);
370 int pos = strlen(buf);
372 sprintf(&buf[pos], "elevation-ft=%.0lf", elevation);
374 sprintf(&buf[pos], "turbulence-norm=%.0lf", 0.0);
377 if (visibility != NaN) {
378 sprintf(&buf[pos], "visibility-m=%.0lf", visibility);
381 if (temperature != NaN) {
382 sprintf(&buf[pos], "temperature-degc=%.0lf", temperature);
385 if (dewpoint != NaN) {
386 sprintf(&buf[pos], "dewpoint-degc=%.0lf", dewpoint);
389 if (pressure != NaN) {
390 sprintf(&buf[pos], "pressure-sea-level-inhg=%.0lf", pressure);
393 if (wind_dir != NaN) {
394 sprintf(&buf[pos], "wind-from-heading-deg=%d", wind_dir);
397 if (wind_speed != NaN) {
398 sprintf(&buf[pos], "wind-speed-kt=%.0lf", wind_speed);
404 int range_from = m->getWindRangeFrom();
405 int range_to = m->getWindRangeTo();
406 double gust_speed = m->getGustSpeed_kt();
407 if (wind_speed != NaN && wind_dir != -1) {
408 strcpy(buf, "--wind=");
409 if (range_from != -1 && range_to != -1)
410 sprintf(&buf[strlen(buf)], "%d:%d", range_from, range_to);
412 sprintf(&buf[strlen(buf)], "%d", wind_dir);
413 sprintf(&buf[strlen(buf)], "@%.0lf", wind_speed);
414 if (gust_speed != NaN)
415 sprintf(&buf[strlen(buf)], ":%.0lf", gust_speed);
421 //cout << "fgfs" << endl;
422 vector<string>::iterator arg;
423 for (i = 0, arg = args.begin(); arg != args.end(); i++, arg++) {
424 cout << "\t" << *arg << endl;
431 void getproxy(string& host, string& port)
436 const char *p = getenv("http_proxy");
441 if (!strncmp(p, "http://", 7))
450 for (t = s + strlen(s); t > s; t--)
451 if (!isspace(t[-1]) && t[-1] != '/')
467 "Usage: metar [-v] [-e elevation] [-r|-c] <list of ICAO airport ids or METAR strings>\n"
470 " -h|--help show this help\n"
471 " -v|--verbose verbose output\n"
472 " -r|--report print report (default)\n"
473 " -c|--command-line print command line\n"
474 " -e E|--elevation E set airport elevation to E meters\n"
475 " (added to cloud bases in command line mode)\n"
477 " http_proxy set proxy in the form \"http://host:port/\"\n"
480 " $ metar ksfo koak\n"
481 " $ metar -c ksfo -r ksfo\n"
482 " $ metar \"LOWL 161500Z 19004KT 160V240 9999 FEW035 SCT300 29/23 Q1006 NOSIG\"\n"
483 " $ fgfs `metar -e 183 -c loww`\n"
484 " $ 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 cerr << "Proxy host: '" << proxy_host << "'" << endl;
522 cerr << "Proxy port: '" << proxy_port << "'" << endl << endl;
527 SGMetar *m = new SGMetar(argv[i], proxy_host, proxy_port, "", time(0));
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;
541 printArgs(m, elevation);
544 } catch (const sg_io_exception& e) {
545 cerr << R"ERROR: " << e.getFormattedMessage().c_str() << ""N << endl << endl;