]> git.mxchange.org Git - flightgear.git/blob - src/Main/metar_main.cxx
Fix my mailing address by replacing it with my web page.
[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, 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20 //
21 // $Id$
22
23 #include <iomanip>
24 #include <sstream>
25 #include <string.h>
26
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/structure/exception.hxx>
29 #include <simgear/environment/metar.hxx>
30
31 using namespace std;
32
33 // text color
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
43 #else
44 #       define R ""
45 #       define G ""
46 #       define Y ""
47 #       define B ""
48 #       define M ""
49 #       define C ""
50 #       define W ""
51 #       define N ""
52 #endif
53
54
55
56 const char *azimuthName(double d)
57 {
58         const char *dir[] = {
59                 "N", "NNE", "NE", "ENE",
60                 "E", "ESE", "SE", "SSE",
61                 "S", "SSW", "SW", "WSW",
62                 "W", "WNW", "NW", "NNW"
63         };
64         d += 11.25;
65         while (d < 0)
66                 d += 360;
67         while (d >= 360)
68                 d -= 360;
69         return dir[int(d / 22.5)];
70 }
71
72
73 // round double to 10^g
74 double rnd(double r, int g = 0)
75 {
76         double f = pow(10.0, g);
77         return f * rint(r / f);
78 }
79
80
81 ostream& operator<<(ostream& s, SGMetarVisibility& v)
82 {
83         ostringstream buf;
84         int m = v.getModifier();
85         const char *mod;
86         if (m == SGMetarVisibility::GREATER_THAN)
87                 mod = ">=";
88         else if (m == SGMetarVisibility::LESS_THAN)
89                 mod = "<";
90         else
91                 mod = "";
92         buf << mod;
93
94         double dist = rnd(v.getVisibility_m(), 1);
95         if (dist < 1000.0)
96                 buf << rnd(dist, 1) << " m";
97         else
98                 buf << rnd(dist / 1000.0, -1) << " km";
99
100         const char *dir = "";
101         int i;
102         if ((i = v.getDirection()) != -1) {
103                 dir = azimuthName(i);
104                 buf << " " << dir;
105         }
106         buf << "\t\t\t\t\t" << mod << rnd(v.getVisibility_sm(), -1) << " US-miles " << dir;
107         return s << buf.str();
108 }
109
110
111 void printReport(SGMetar *m)
112 {
113 #define NaN SGMetarNaN
114         const char *s;
115         char buf[256];
116         double d;
117         int i, lineno;
118
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)";
125         else
126                 s = "";
127
128         cout << "METAR Report" << s << endl;
129         cout << "============" << endl;
130         cout << "Airport-Id:\t\t" << m->getId() << endl;
131
132
133         // date/time
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;
141
142
143         // visibility
144         SGMetarVisibility minvis = m->getMinVisibility();
145         SGMetarVisibility maxvis = m->getMaxVisibility();
146         double min = minvis.getVisibility_m();
147         double max = maxvis.getVisibility_m();
148         if (min != NaN) {
149                 if (max != NaN) {
150                         cout << "min. Visibility:\t" << minvis << endl;
151                         cout << "max. Visibility:\t" << maxvis << endl;
152                 } else
153                         cout << "Visibility:\t\t" << minvis << endl;
154         }
155
156
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;
162
163
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;
170
171
172         // wind
173         d = m->getWindSpeed_kmh();
174         cout << "Wind:\t\t\t";
175         if (d < .1)
176                 cout << "none" << endl;
177         else {
178                 if ((i = m->getWindDir()) == -1)
179                         cout << "from variable directions";
180                 else
181                         cout << "from the " << azimuthName(i) << " (" << i << "°)";
182                 cout << " at " << rnd(d, -1) << " km/h";
183
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";
187                 cout << endl;
188
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";
194                         cout << endl;
195                 }
196
197                 int from = m->getWindRangeFrom();
198                 int to = m->getWindRangeTo();
199                 if (from != to) {
200                         cout << "\t\t\tvariable from " << azimuthName(from);
201                         cout << " to " << azimuthName(to);
202                         cout << " (" << from << "°--" << to << "°)" << endl;
203                 }
204         }
205
206
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;
211
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;
216                 }
217         }
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;
221         }
222
223
224         // weather phenomena
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();
229         }
230         if (i)
231                 cout << endl;
232
233
234         // cloud layers
235         const char *coverage_string[5] = {
236                 "clear skies", "few clouds", "scattered clouds", "broken clouds", "sky overcast"
237         };
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");
242
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 << ')';
249                 if (d != NaN)
250                         cout << "\t\t\t" << rnd(cloud->getAltitude_m(), 1) << " m";
251                 cout << endl;
252         }
253
254
255         // runways
256         map<string, SGMetarRunway> rm = m->getRunways();
257         map<string, SGMetarRunway>::iterator runway;
258         for (runway = rm.begin(); runway != rm.end(); runway++) {
259                 lineno = 0;
260                 if (!strcmp(runway->first.c_str(), "ALL"))
261                         cout << "All runways:\t\t";
262                 else
263                         cout << "Runway " << runway->first << ":\t\t";
264                 SGMetarRunway rwy = runway->second;
265
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);
275                 }
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);
281                 }
282
283                 if (surface.size()) {
284                         vector<string>::iterator rwysurf = surface.begin();
285                         for (i = 0; rwysurf != surface.end(); rwysurf++, i++) {
286                                 if (i)
287                                         cout << ", ";
288                                 cout << *rwysurf;
289                         }
290                         lineno++;
291                 }
292
293                 // assemble visibility string
294                 SGMetarVisibility minvis = rwy.getMinVisibility();
295                 SGMetarVisibility maxvis = rwy.getMaxVisibility();
296                 if ((d = minvis.getVisibility_m()) != NaN) {
297                         if (lineno++)
298                                 cout << endl << "\t\t\t";
299                         cout << minvis;
300                 }
301                 if (maxvis.getVisibility_m() != d) {
302                         cout << endl << "\t\t\t" << maxvis << endl;
303                         lineno++;
304                 }
305
306                 if (rwy.getWindShear()) {
307                         if (lineno++)
308                                 cout << endl << "\t\t\t";
309                         cout << "critical wind shear" << endl;
310                 }
311                 cout << endl;
312         }
313         cout << endl;
314 #undef NaN
315 }
316
317
318 void printArgs(SGMetar *m, double airport_elevation)
319 {
320 #define NaN SGMetarNaN
321         vector<string> args;
322         char buf[256];
323         int i;
324
325         // ICAO id
326         sprintf(buf, "--airport=%s ", m->getId());
327         args.push_back(buf);
328
329         // report time
330         sprintf(buf, "--start-date-gmt=%4d:%02d:%02d:%02d:%02d:00 ",
331                         m->getYear(), m->getMonth(), m->getDay(),
332                         m->getHour(), m->getMinute());
333         args.push_back(buf);
334
335         // cloud layers
336         const char *coverage_string[5] = {
337                 "clear", "few", "scattered", "broken", "overcast"
338         };
339         vector<SGMetarCloud> cv = m->getClouds();
340         vector<SGMetarCloud>::iterator cloud;
341         for (i = 0, cloud = cv.begin(); i < 5; i++) {
342                 int coverage = 0;
343                 double altitude = -99999;
344                 if (cloud != cv.end()) {
345                         coverage = cloud->getCoverage();
346                         altitude = coverage ? cloud->getAltitude_ft() + airport_elevation : -99999;
347                         cloud++;
348                 }
349                 sprintf(buf, "--prop:/environment/clouds/layer[%d]/coverage=%s ", i, coverage_string[coverage]);
350                 args.push_back(buf);
351                 sprintf(buf, "--prop:/environment/clouds/layer[%d]/elevation-ft=%.0lf ", i, altitude);
352                 args.push_back(buf);
353                 sprintf(buf, "--prop:/environment/clouds/layer[%d]/thickness-ft=500 ", i);
354                 args.push_back(buf);
355         }
356
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);
370
371                 sprintf(&buf[pos], "elevation-ft=%.0lf", elevation);
372                 args.push_back(buf);
373                 sprintf(&buf[pos], "turbulence-norm=%.0lf", 0.0);
374                 args.push_back(buf);
375
376                 if (visibility != NaN) {
377                         sprintf(&buf[pos], "visibility-m=%.0lf", visibility);
378                         args.push_back(buf);
379                 }
380                 if (temperature != NaN) {
381                         sprintf(&buf[pos], "temperature-degc=%.0lf", temperature);
382                         args.push_back(buf);
383                 }
384                 if (dewpoint != NaN) {
385                         sprintf(&buf[pos], "dewpoint-degc=%.0lf", dewpoint);
386                         args.push_back(buf);
387                 }
388                 if (pressure != NaN) {
389                         sprintf(&buf[pos], "pressure-sea-level-inhg=%.0lf", pressure);
390                         args.push_back(buf);
391                 }
392                 if (wind_dir != NaN) {
393                         sprintf(&buf[pos], "wind-from-heading-deg=%d", wind_dir);
394                         args.push_back(buf);
395                 }
396                 if (wind_speed != NaN) {
397                         sprintf(&buf[pos], "wind-speed-kt=%.0lf", wind_speed);
398                         args.push_back(buf);
399                 }
400         }
401
402         // wind dir@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);
410                 else
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);
415                 args.push_back(buf);
416         }
417         
418
419         // output everything
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;
424         }
425         cout << endl;
426 #undef NaN
427 }
428
429
430 void getproxy(string& host, string& port)
431 {
432         host = "";
433         port = "80";
434
435         const char *p = getenv("http_proxy");
436         if (!p)
437                 return;
438         while (isspace(*p))
439                 p++;
440         if (!strncmp(p, "http://", 7))
441                 p += 7;
442         if (!*p)
443                 return;
444
445         char s[256], *t;
446         strncpy(s, p, 255);
447         s[255] = '\0';
448
449         for (t = s + strlen(s); t > s; t--)
450                 if (!isspace(t[-1]) && t[-1] != '/')
451                         break;
452         *t = '\0';
453
454         t = strchr(s, ':');
455         if (t) {
456                 *t++ = '\0';
457                 port = t;
458         }
459         host = s;
460 }
461
462
463 void help()
464 {
465         printf(
466                 "Usage: metar [-v] [-e elevation] [-r|-c] <list of ICAO airport ids or METAR strings>\n"
467                 "       metar -h\n"
468                 "\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"
475                 "Environment:\n"
476                 "       http_proxy           set proxy in the form \"http://host:port/\"\n"
477                 "\n"
478                 "Examples:\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"
484                 "\n"
485         );
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                 help();
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                         help();
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                                 cout << "Proxy host: '" << proxy_host << "'" << endl;
522                                 cout << "Proxy port: '" << proxy_port << "'" << endl << endl;
523                                 shown = true;
524                         }
525
526                         try {
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");
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
539                                 if (report)
540                                         printReport(m);
541                                 else
542                                         printArgs(m, elevation);
543
544                                 delete m;
545                         } catch (const sg_io_exception& e) {
546                                 cerr << R"ERROR: " << e.getFormattedMessage().c_str() << ""N << endl << endl;
547                         }
548                 }
549         }
550         return 0;
551 }
552
553