]> git.mxchange.org Git - flightgear.git/blob - src/Main/metar_main.cxx
Juggle fgfs link order to keep GNU ld happy.
[flightgear.git] / src / Main / metar_main.cxx
1 // metar interface class demo
2 //
3 // Written by Melchior FRANZ, started December 2003.
4 //
5 // Copyright (C) 2003  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 #include <iomanip>
24 #include <sstream>
25 #include <iostream>
26 #include <string.h>
27 #include <time.h>
28
29 #include <simgear/debug/logstream.hxx>
30 #include <simgear/environment/metar.hxx>
31 #include <simgear/structure/exception.hxx>
32
33 using namespace std;
34
35 // text color
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
46 #else
47 #       define R ""
48 #       define G ""
49 #       define Y ""
50 #       define B ""
51 #       define M ""
52 #       define C ""
53 #       define W ""
54 #       define N ""
55 #endif
56
57
58
59 const char *azimuthName(double d)
60 {
61         const char *dir[] = {
62                 "N", "NNE", "NE", "ENE",
63                 "E", "ESE", "SE", "SSE",
64                 "S", "SSW", "SW", "WSW",
65                 "W", "WNW", "NW", "NNW"
66         };
67         d += 11.25;
68         while (d < 0)
69                 d += 360;
70         while (d >= 360)
71                 d -= 360;
72         return dir[int(d / 22.5)];
73 }
74
75
76 // round double to 10^g
77 double rnd(double r, int g = 0)
78 {
79         double f = pow(10.0, g);
80         return f * floor(r / f + 0.5);
81 }
82
83
84 ostream& operator<<(ostream& s, const SGMetarVisibility& v)
85 {
86         ostringstream buf;
87         int m = v.getModifier();
88         const char *mod;
89         if (m == SGMetarVisibility::GREATER_THAN)
90                 mod = ">=";
91         else if (m == SGMetarVisibility::LESS_THAN)
92                 mod = "<";
93         else
94                 mod = "";
95         buf << mod;
96
97         double dist = rnd(v.getVisibility_m(), 1);
98         if (dist < 1000.0)
99                 buf << rnd(dist, 1) << " m";
100         else
101                 buf << rnd(dist / 1000.0, -1) << " km";
102
103         const char *dir = "";
104         int i;
105         if ((i = v.getDirection()) != -1) {
106                 dir = azimuthName(i);
107                 buf << " " << dir;
108         }
109         buf << "\t\t\t\t\t" << mod << rnd(v.getVisibility_sm(), -1) << " US-miles " << dir;
110         return s << buf.str();
111 }
112
113
114 void printReport(SGMetar *m)
115 {
116 #define NaN SGMetarNaN
117         const char *s;
118         char buf[256];
119         double d;
120         int i, lineno;
121
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)";
128         else
129                 s = "";
130
131         cout << "METAR Report" << s << endl;
132         cout << "============" << endl;
133         cout << "Airport-Id:\t\t" << m->getId() << endl;
134
135
136         // date/time
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;
142
143
144         // visibility
145         SGMetarVisibility minvis = m->getMinVisibility();
146         SGMetarVisibility maxvis = m->getMaxVisibility();
147         double min = minvis.getVisibility_m();
148         double max = maxvis.getVisibility_m();
149         if (min != NaN) {
150                 if (max != NaN) {
151                         cout << "min. Visibility:\t" << minvis << endl;
152                         cout << "max. Visibility:\t" << maxvis << endl;
153                 } else
154                         cout << "Visibility:\t\t" << minvis << endl;
155         }
156
157
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;
163
164
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;
171
172
173         // wind
174         d = m->getWindSpeed_kmh();
175         cout << "Wind:\t\t\t";
176         if (d < .1)
177                 cout << "none" << endl;
178         else {
179                 if ((i = m->getWindDir()) == -1)
180                         cout << "from variable directions";
181                 else
182                         cout << "from the " << azimuthName(i) << " (" << i << "°)";
183                 cout << " at " << rnd(d, -1) << " km/h";
184
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";
188                 cout << endl;
189
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";
195                         cout << endl;
196                 }
197
198                 int from = m->getWindRangeFrom();
199                 int to = m->getWindRangeTo();
200                 if (from != to) {
201                         cout << "\t\t\tvariable from " << azimuthName(from);
202                         cout << " to " << azimuthName(to);
203                         cout << " (" << from << "°--" << to << "°)" << endl;
204                 }
205         }
206
207
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;
212
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;
217                 }
218         }
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;
222         }
223
224
225         // weather phenomena
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();
230         }
231         if (i)
232                 cout << endl;
233
234
235         // cloud layers
236         const char *coverage_string[5] = {
237                 "clear skies", "few clouds", "scattered clouds", "broken clouds", "sky overcast"
238         };
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");
243
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 << ')';
250                 if (d != NaN)
251                         cout << "\t\t\t" << rnd(cloud->getAltitude_m(), 1) << " m";
252                 cout << endl;
253         }
254
255
256         // runways
257         map<string, SGMetarRunway> rm = m->getRunways();
258         map<string, SGMetarRunway>::iterator runway;
259         for (runway = rm.begin(); runway != rm.end(); runway++) {
260                 lineno = 0;
261                 if (!strcmp(runway->first.c_str(), "ALL"))
262                         cout << "All runways:\t\t";
263                 else
264                         cout << "Runway " << runway->first << ":\t\t";
265                 SGMetarRunway rwy = runway->second;
266
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);
276                 }
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);
282                 }
283
284                 if (surface.size()) {
285                         vector<string>::iterator rwysurf = surface.begin();
286                         for (i = 0; rwysurf != surface.end(); rwysurf++, i++) {
287                                 if (i)
288                                         cout << ", ";
289                                 cout << *rwysurf;
290                         }
291                         lineno++;
292                 }
293
294                 // assemble visibility string
295                 SGMetarVisibility minvis = rwy.getMinVisibility();
296                 SGMetarVisibility maxvis = rwy.getMaxVisibility();
297                 if ((d = minvis.getVisibility_m()) != NaN) {
298                         if (lineno++)
299                                 cout << endl << "\t\t\t";
300                         cout << minvis;
301                 }
302                 if (maxvis.getVisibility_m() != d) {
303                         cout << endl << "\t\t\t" << maxvis << endl;
304                         lineno++;
305                 }
306
307                 if (rwy.getWindShear()) {
308                         if (lineno++)
309                                 cout << endl << "\t\t\t";
310                         cout << "critical wind shear" << endl;
311                 }
312                 cout << endl;
313         }
314         cout << endl;
315 #undef NaN
316 }
317
318
319 void printArgs(SGMetar *m, double airport_elevation)
320 {
321 #define NaN SGMetarNaN
322         vector<string> args;
323         char buf[256];
324         int i;
325
326         // ICAO id
327         sprintf(buf, "--airport=%s ", m->getId());
328         args.push_back(buf);
329
330         // report time
331         sprintf(buf, "--start-date-gmt=%4d:%02d:%02d:%02d:%02d:00 ",
332                         m->getYear(), m->getMonth(), m->getDay(),
333                         m->getHour(), m->getMinute());
334         args.push_back(buf);
335
336         // cloud layers
337         const char *coverage_string[5] = {
338                 "clear", "few", "scattered", "broken", "overcast"
339         };
340         vector<SGMetarCloud> cv = m->getClouds();
341         vector<SGMetarCloud>::iterator cloud;
342         for (i = 0, cloud = cv.begin(); i < 5; i++) {
343                 int coverage = 0;
344                 double altitude = -99999;
345                 if (cloud != cv.end()) {
346                         coverage = cloud->getCoverage();
347                         altitude = coverage ? cloud->getAltitude_ft() + airport_elevation : -99999;
348                         cloud++;
349                 }
350                 sprintf(buf, "--prop:/environment/clouds/layer[%d]/coverage=%s ", i, coverage_string[coverage]);
351                 args.push_back(buf);
352                 sprintf(buf, "--prop:/environment/clouds/layer[%d]/elevation-ft=%.0lf ", i, altitude);
353                 args.push_back(buf);
354                 sprintf(buf, "--prop:/environment/clouds/layer[%d]/thickness-ft=500 ", i);
355                 args.push_back(buf);
356         }
357
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);
371
372                 sprintf(&buf[pos], "elevation-ft=%.0lf", elevation);
373                 args.push_back(buf);
374                 sprintf(&buf[pos], "turbulence-norm=%.0lf", 0.0);
375                 args.push_back(buf);
376
377                 if (visibility != NaN) {
378                         sprintf(&buf[pos], "visibility-m=%.0lf", visibility);
379                         args.push_back(buf);
380                 }
381                 if (temperature != NaN) {
382                         sprintf(&buf[pos], "temperature-degc=%.0lf", temperature);
383                         args.push_back(buf);
384                 }
385                 if (dewpoint != NaN) {
386                         sprintf(&buf[pos], "dewpoint-degc=%.0lf", dewpoint);
387                         args.push_back(buf);
388                 }
389                 if (pressure != NaN) {
390                         sprintf(&buf[pos], "pressure-sea-level-inhg=%.0lf", pressure);
391                         args.push_back(buf);
392                 }
393                 if (wind_dir != NaN) {
394                         sprintf(&buf[pos], "wind-from-heading-deg=%d", wind_dir);
395                         args.push_back(buf);
396                 }
397                 if (wind_speed != NaN) {
398                         sprintf(&buf[pos], "wind-speed-kt=%.0lf", wind_speed);
399                         args.push_back(buf);
400                 }
401         }
402
403         // wind dir@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);
411                 else
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);
416                 args.push_back(buf);
417         }
418         
419
420         // output everything
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;
425         }
426         cout << endl;
427 #undef NaN
428 }
429
430
431 void getproxy(string& host, string& port)
432 {
433         host = "";
434         port = "80";
435
436         const char *p = getenv("http_proxy");
437         if (!p)
438                 return;
439         while (isspace(*p))
440                 p++;
441         if (!strncmp(p, "http://", 7))
442                 p += 7;
443         if (!*p)
444                 return;
445
446         char s[256], *t;
447         strncpy(s, p, 255);
448         s[255] = '\0';
449
450         for (t = s + strlen(s); t > s; t--)
451                 if (!isspace(t[-1]) && t[-1] != '/')
452                         break;
453         *t = '\0';
454
455         t = strchr(s, ':');
456         if (t) {
457                 *t++ = '\0';
458                 port = t;
459         }
460         host = s;
461 }
462
463
464 void usage()
465 {
466         printf(
467                 "Usage: metar [-v] [-e elevation] [-r|-c] <list of ICAO airport ids or METAR strings>\n"
468                 "       metar -h\n"
469                 "\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"
476                 "Environment:\n"
477                 "       http_proxy           set proxy in the form \"http://host:port/\"\n"
478                 "\n"
479                 "Examples:\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"
485                 "\n"
486         );
487 }
488
489 int main(int argc, char *argv[])
490 {
491         bool report = true;
492         bool verbose = false;
493         double elevation = 0.0;
494
495         if (argc <= 1) {
496                 usage();
497                 return 0;
498         }
499
500         string proxy_host, proxy_port;
501         getproxy(proxy_host, proxy_port);
502
503         for (int i = 1; i < argc; i++) {
504                 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
505                         usage();
506                 else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose"))
507                         verbose = true;
508                 else if (!strcmp(argv[i], "-r") || !strcmp(argv[i], "--report"))
509                         report = true;
510                 else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--command-line"))
511                         report = false;
512                 else if (!strcmp(argv[i], "-e") || !strcmp(argv[i], "--elevation")) {
513                         if (++i >= argc) {
514                                 cerr << "-e option used without elevation" << endl;
515                                 return 1;
516                         }
517                         elevation = strtod(argv[i], 0);
518                 } else {
519                         static bool shown = false;
520                         if (verbose && !shown) {
521                                 cerr << "Proxy host: '" << proxy_host << "'" << endl;
522                                 cerr << "Proxy port: '" << proxy_port << "'" << endl << endl;
523                                 shown = true;
524                         }
525
526                         try {
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");
529
530                                 if (verbose) {
531                                         cerr << G"INPUT: " << m->getData() << ""N << endl;
532
533                                         const char *unused = m->getUnusedData();
534                                         if (*unused)
535                                                 cerr << R"UNUSED: " << unused << ""N << endl;
536                                 }
537
538                                 if (report)
539                                         printReport(m);
540                                 else
541                                         printArgs(m, elevation);
542
543                                 delete m;
544                         } catch (const sg_io_exception& e) {
545                                 cerr << R"ERROR: " << e.getFormattedMessage().c_str() << ""N << endl << endl;
546                         }
547                 }
548         }
549         return 0;
550 }
551
552