3 # fetch a metar, taf or short taf from http://weather.noaa.gov
5 # This is designed to be used in a IFRAME and returns HTML.
6 # It will only query the website once every 30 minutes, the rest
7 # of the time it will cache the result in an 'easily guessable'
8 # place in /tmp (consider that as a warning).
10 # Call it from a web page like this:-
12 # <iframe src="cgi-bin/fetch_weather.pl?icao=EGSH&metar=1"
13 # name="METAR for EGSH" frameborder="1" width="90%" height="50">
14 # [Your user agent does not support frames or is currently configured
15 # not to display frames. However, you may visit
16 # <A href="cgi-bin/fetch_weather.pl?icao=EGSH&metar=1">METAR for EGSH</A>]
19 # You can set as many of these as you like:-
20 # metar=1 for a metar (default, if no options)
21 # staf=1 for a short form (usually more uptodate) TAF
22 # taf=1 for a full 18 hour TAF
23 # break=1 insert a "<br /><br />" between each result
27 # Copyright (c) 2003 Dirk Koopman G1TLH
35 my $site_code = uc $q->param('icao');
37 push @sort, 'taf' if $q->param('taf');
38 push @sort, 'staf' if $q->param('staf');
39 push @sort, 'metar' if $q->param('metar') || @sort == 0;
40 my $dobrk = $q->param('break');
42 error("No ICAO (valid) site code ($site_code) specified") unless $site_code && $site_code =~ /^[A-Z]{4}$/;
45 my ($sort, $fn, $started);
47 while ($sort = shift @sort) {
48 $fn = "$base/${sort}_$site_code";
50 my ($mt, $size) = (stat $fn)[9,7];
54 my $brk = "<br /></br />" unless @sort;
56 if ($mt + 30*60 < time || $size == 0) {
66 open IN, "$fn" or die "cannot open $fn $!\n";
74 my $brk = shift || "";
75 my $ua = new LWP::UserAgent;
77 my $req = new HTTP::Request GET =>
78 "http://weather.noaa.gov/cgi-bin/mget$sort.pl?cccc=$site_code";
80 my $response = $ua->request($req);
82 if (!$response->is_success) {
83 error("METAR Fetch $site_code Error", $response->error_as_HTML);
86 # Yep, get the data and find the METAR.
90 $data = $response->as_string; # grap response
91 $data =~ s/\n//go; # remove newlines
92 $data =~ m/($site_code\s\d+Z.*?)</go; # find the METAR string
93 my $metar = $1; # keep it
96 if (length($metar)<10) {
97 error("METAR ($metar) is too short");
100 # pass the data to the METAR module.
101 if ($sort =~ /taf/) {
106 my $s = $m->as_string;
107 send_metar($s, $brk);
117 print $q->header(-type=>'text/html', -expires=>'+60m');
118 print $q->start_html(-title=>"Weather for $site_code", -style=>{'src'=>'/style.css'},);
130 open OUT, ">$fn" or die "cannot open $fn $!\n";
138 my $brk = shift || "";
141 print "<div class=frame>$s</div>$brk";
148 print $q->header(-type=>'text/html', -expires=>'+60m');
149 print $q->start_html($err);
151 print $more if $more;