2008-10-07

This is a script to get weather radar images from Finnish Meteorological Institute FMI.

First there's the script and then I'll explain some things.

To set up the script, check and set the parameters, and naturally you need wget. You can make the script a cron job.
#!/bin/bash
# Get precipitation radar images from FMI.
# Use as you like.
# Parameters:
# $url: URL of image page
# $tmpfile
# $dirname: save directory
# $fulltmpfile: full name of tmpfile
# $num_get: number of images to get.

url="http://fmi.fi/saa/sadejapi.html"
tmpfile="sadejapi.html"
dirname=<path to save directory>
fulltmpfile=$dirname$tmpfile
num_get=10

i=0

wget -q $url -O $fulltmpfile

imageurl=( `cat $fulltmpfile |grep "anim_images_anim_anim = new Array" | sed 's/^[^(]*//' |awk -F, '{i=1; while ($i){print $i; i++}}' |sed -e 's/^["(]*//' -e 's/[");]*$//'` )

timestamps=( `cat $fulltmpfile |grep "anim_timestamps = new Array" | sed 's/^[^(]*//' |awk -F, '{i=1; while ($i){print $i; i++}}' |sed -e 's/^["(]*//' -e 's/[");]*$//'` )

for ((i=0; i<$num_get; i++)); do wget -q ${imageurl[$i]} -O "$dirname${timestamps[$i]}.png"; done

So how does this work. Stupidly. Beware, it will break easily. $imageurl is made into array of image URL's with Bash ( values ) notation. URL's are found from the downloaded image page from a JavaScript array anim_images_anim_anim. The array is given in a single line, so first sed strips away all the stuff until the '(' which starts the array content. Then awk splits the array using ',' as delimiter, outputting the tokens as lines. Then another sed takes the quotes, parenthesis, and semicolons from the beginning and end of the lines. The same thing is done for $timestamps. Then the for just gets the URL's and saves them as $timestamps.png.

Home