add back the contents of this directory
[spider.git] / Geo / TAF / example / cmd_taf.pl
1 #!/usr/bin/perl
2 #
3 # This example takes METARs and TAFs from the standard input and
4 # prints them out in a readable form, in something approaching English
5 #
6 # Note that this is a state machine which can take any old rubbish and looks
7 # for a start of a forecast in the input. It then searches for a blank line
8 # before looking for the next.
9 #
10 # You can METARs from ftp://weather.noaa.gov/data/observations/metar/ and
11 # TAFs from ftp://weather.noaa.gov/data/forecasts/taf/ and 
12 # from ftp://weather.noaa.gov/data/forecasts/shorttaf/
13 # directories. This program will parse these files directly
14 #
15 # You will need to press <return> twice to get any output if you are entering
16 # stuff manually.
17 #
18 # $Id$
19 #
20 # Copyright (c) 2003 Dirk Koopman G1TLH
21 #
22
23 use strict;
24 use Geo::TAF;
25
26 my $in;
27 my $t;
28
29 while (<STDIN>) {
30         chomp;
31         if (/^\s*$/) {
32                 if ($in) {
33                         $t = new Geo::TAF;
34                         if ($in =~ /(?:METAR|TAF)/) {
35                                 $t->decode($in);
36                         } elsif ($in =~ /[QA]\d\d\d\d/) {
37                                 $t->metar($in);
38                         } else {
39                                 $t->taf($in);
40                         }
41                         print_taf($t);
42                         undef $in;
43                         undef $t ;
44                 }
45         } else {
46                 if ($in) {
47                         $in .= $_;
48                 } else {
49                         next unless Geo::TAF::is_weather($_);
50                         $in = $_;
51                 }
52         }
53 }
54
55 print_taf($t) if $t;
56
57 sub print_taf
58 {
59         my $t = shift;
60         
61         print $t->raw, "\n\n";
62
63         my $spc = "";
64         foreach my $c ($t->chunks) {
65 #               print "\n", $c->as_chunk, " ";
66                 if ((ref $c) =~ /::(?:PROB|TEMPO|BECMG|FROM)$/) {
67                         print "\n\t";
68                         $spc = '';
69                 }
70                 print $spc, $c->as_string;
71                 if ((ref $c) =~ /::(?:VALID)$/) {
72                         print "\n\t";
73                         $spc = '';
74                 } else {
75                         $spc = ' ';
76                 }
77         }
78         print "\n\n";
79 }