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)
29 while ($ARGV[0] =~ /^-/ && @ARGV > 1) {
30 my @f = split //, shift @ARGV;
33 push @sort, 'taf' if $f eq 't' && ! grep $_ eq 'taf', @sort;
34 push @sort, 'staf' if $f eq 's' && ! grep $_ eq 'staf', @sort;
35 push @sort, 'metar' if $f eq 'm' && ! grep $_ eq 'metar', @sort;
38 push @sort, 'metar' unless @sort;
40 my $site_code = uc shift @ARGV;
42 die "Usage: $0 [-mts] <site_code>\n" unless $site_code;
44 # Get the modules we need.
52 foreach $sort (@sort) {
54 my $ua = new LWP::UserAgent;
56 my $req = new HTTP::Request GET =>
57 "http://weather.noaa.gov/cgi-bin/mget$sort.pl?cccc=$site_code";
59 my $response = $ua->request($req);
61 if ($response->is_success) {
63 # Yep, get the data and find the METAR.
67 $data = $response->as_string; # grap response
68 $data =~ s/\n//go; # remove newlines
69 $data =~ m/($site_code\s\d+Z.*?)</go; # find the METAR string
70 my $metar = $1; # keep it
74 if (length($metar)<10) {
75 die "METAR is too short! Something went wrong.";
78 # pass the data to the METAR module.
79 if ($sort =~ /taf$/) {
84 print $m->as_string, "\n";
88 print $response->as_string, "\n";