[nSLUG] The tide turned in Armdale Cove...

Rich budman85 at eastlink.ca
Thu Aug 28 23:40:10 ADT 2008


>  more, attrib. to  Robert Grudin, `Time and the Art of Living'.
>
> Indeed so.  I made a script to fetch current satellite weather images
> from the CanGov site where the filenames are date- &
> time-based. Figuring out how compose the filename for the most recent
> image prior to present time, allowing for possible backup into
> previous day, month and year and for AST/DST roll-over was a pain and
> still fails once in, oh, 50 or so tries.
>
> Thanks for the pointers, help and -- heh -- humiliation. :-)
>
> - Mike
>
>   

Here is a script I wrote a while back that animates the images.
I had it using Image Magick module, but that broke in the recent version 
of Slack,
so I use the command line tools which works the same.

I put it in a crontab and add enable these meta tags on my home page
           "<META HTTP-EQUIV=\"expires\" content=\"-1\">\n",
           "<META HTTP-EQUIV=\"Cache-Control\" content=\"no-cache, 
must-revalidate\">\n",
           "<META HTTP-EQUIV=\"Pragma\" content=\"no-cache\">\n",
           "<META HTTP-EQUIV=\"refresh\" content=\"+300;URL=$url\">\n",
this way the page will always have a fresh radar image.


#!/usr/bin/perl -w


use strict;
use LWP;
use LWP::Simple;
use Tie::IxHash;

my $weatherURL = "http://weatheroffice.gc.ca";
my $RadarStation = "XGO";
my $location = "$weatherURL/data/radar/temp_image/${RadarStation}";
my $basedir = "$ENV{HOME}/weather";
my $RegionMapName = "${RadarStation}_PRECIP_RAIN";
my $imagePath = "$basedir/tempimages";
my $MaxImages = 6;
my $HtmlPath = "$ENV{HOME}/public_html/weather";
my $AnimatedImage = "$HtmlPath/wmap.gif";
my $Roads = $basedir."/".$RadarStation."_roads.gif";
my $Towns = $basedir."/".lc($RadarStation)."_towns.gif";
   
    # clear image dir
   
    opendir(DIR,$imagePath)||die "Cannot access $imagePath: $!\n";
    while ( defined( my $file = readdir(DIR) ) ) {
         next if $file eq "." || $file eq "..";
         if ( $file =~ /^Map.*GIF$/ ) { unlink "$imagePath/$file"; }
    }

    # retrieve the images

    my @Files;
    chdir $imagePath;
    my $ua = LWP::UserAgent->new(agent=>'Mozilla');
    my $cmd;

    for my $t (0 .. $MaxImages - 1) {
        my $maptime = time - ($t * 600);
        my $id = $MaxImages - $t;
        my $ImageName = "Map$id.GIF";

        my $file = &GetFilename($maptime);
        print "Retrieving $file\n";
        my $imageFile = "$imagePath/$ImageName";
        getstore("$location/$file",$imageFile);
        print " size: ",-s $imageFile,"\n" if -f $imageFile;

        if ( -s $imageFile ) {
            my ($hour,$min) = (localtime($maptime))[2,1];
            $min = int($min / 10) * 10;
            my $m = ($hour <= 12 ? "am" : "pm" );
            $hour -= 12 if $hour > 12;
            my $timestamp = sprintf "%2d:%02d%s",$hour,$min,$m;
            # overlay roads
            qx(composite -compose over -geometry +0+0 $Roads $ImageName 
$ImageName);
            # overlay towns
            qx(composite -compose over -geometry +0+0 $Towns $ImageName 
$ImageName);
            # add timestamp
            qx(convert $ImageName -pointsize 14 -gravity NorthEast -fill 
white -annotate +6+14 '
$timestamp' $ImageName);
            push @Files, $ImageName;
        }

    }

    # animate the images
    my $imageList = join(" ",reverse @Files);
    unlink $AnimatedImage;
    qx(convert -set delay 100 -loop 0 $imageList $AnimatedImage);


sub GetFilename {
    my $curtime = shift;

    my ($min,$hour,$day,$month,$year) = (gmtime($curtime))[1 .. 5];
    $year += 1900;
    $min = int($min / 10) * 10;
    $month++;
    return sprintf "%s_%4d_%02d_%02d_%02d_%02d.GIF", $RegionMapName,
                        $year, $month, $day, $hour, $min;
}
   
   




More information about the nSLUG mailing list