5 # this has been taken from Geo::METAR and modified
10 # fetch_temp.pl is a program that demonstrates how to get the current
11 # temperature from a nearby (or not) airport using Geo::METAR and the
14 # Given an airport site code on the command line, fetch_temp.pl
15 # fetches the current temperature and displays it on the
16 # command-line. For fun, here are some example airports:
23 # and of course: EGSH (Norwich)
30 while ($ARGV[0] =~ /^-/ && @ARGV > 1) {
31 my @f = split //, shift @ARGV;
34 push @sort, 'taf' if $f eq 't' && ! grep $_ eq 'taf', @sort;
35 push @sort, 'staf' if $f eq 's' && ! grep $_ eq 'staf', @sort;
36 push @sort, 'metar' if $f eq 'm' && ! grep $_ eq 'metar', @sort;
37 $debug++ if $f eq 'x';
41 push @sort, 'metar' unless @sort;
43 my $site_code = uc shift @ARGV;
45 die "Usage: $0 [-mts] <site_code>\n" unless $site_code;
47 # Get the modules we need.
55 foreach $sort (@sort) {
57 my $ua = new LWP::UserAgent;
59 my $req = new HTTP::Request GET =>
60 "http://weather.noaa.gov/cgi-bin/mget$sort.pl?cccc=$site_code";
62 my $response = $ua->request($req);
64 if ($response->is_success) {
66 # Yep, get the data and find the METAR.
70 $data = $response->as_string; # grap response
71 $data =~ s/\n//go; # remove newlines
72 $data =~ m/($site_code\s\d+Z.*?)</go; # find the METAR string
73 my $metar = $1; # keep it
77 if (length($metar)<10) {
78 die "METAR is too short! Something went wrong.";
81 # pass the data to the METAR module.
82 if ($sort =~ /taf$/) {
87 print $m->raw, "\n" if $raw;
88 print join "\n", $m->as_chunk_strings, "\n" if $debug;
89 print $m->as_string, "\n";
93 print $response->as_string, "\n";