Hack 46. Map Global Weather Conditions

Make your own animated weather maps, just like the ones they show on the evening news.

Lucky for the weather enthusiasts among usand who isn't a weather enthusiast, when it comes right down to it?there are loads of free weather images from geostationary satellites available on the Web. In particular, the University of Dundee provides an interface to frequently updated imagery from several satellites covering the entire globe (http://www.sat.dundee.ac.uk/pdus.html). The good people from the Xplanet project have stitched together these images into a single picture of global cloud cover, available for download to use in Xplanet or other applications. We'll fetch a sequence of these images and make an animated GIF of global weather conditions to amaze and astound your friends and family.

Information on the Xplanet cloud map is at http://xplanet.sourceforge.net/clouds.php. The cloud map is free to use, with the restriction that the image is not downloaded more often than once every three hours. The image is rather large, and excessive downloads have caused bandwidth problems previously, so this restriction is pretty rigorously enforced. That's okay by us, since the image is only updated every three hours anyway.

Xplanet can take this cloud map and overlay it on world map imagery from Blue Marble or elsewhere. Xplanet has lots of other cool features, so it's definitely worth some exploration. Download and install Xplanet from http://xplanet.sourceforge.net/. While Xplanet runs on both Windows and most Unix-like operating systems, we'll need the power of Unix-like shell scripting and cron-job scheduling to accomplish the task at hand. The default configuration for cloud maps on such systems is stored in the file /etc/xplanet/config/overlay_clouds (or wherever Xplanet installed it), and it uses clouds.jpg from the current directory for the weather imagery, and earth.jpg and night.jpg from /usr/share/xplanet/images for the base images.

To build our animated GIF, first make a directory on your system to hold the images. Then fetch the download_clouds.py script from the Xplanet cloud map page and put it in the same directory. Next, write the following shell commands to a file called render_clouds.sh, and then chmod +x the file:

#!/bin/sh cd /home/foobar/xplanet # or wherever you put the files python download_clouds.py clouds.jpg xplanet -config overlay_clouds -make_cloud_maps

This script fetches the latest cloud overlay and then calls Xplanet to create the final images. Run it once with ./render_clouds.sh to see it in action. Xplanet should generate two files in the current directory, one called day_clouds.jpg and the other night_clouds.jpg. Have a look at them in your favorite image viewer: one should contain the world's cloud cover over the daytime map, as depicted in Figure 4-19, and the other the same over a nighttime map. Pretty cool, huh? But we're not done yet!

Figure 4-19. Sample day_clouds.jpg from Xplanet, showing current cloud cover

Now we can collect a sequence of these images and then animate them. Let's start by adding the following line to render_clouds.sh:

cp day_clouds.jpg clouds_`date +%Y%m%d_%H%M`.jpg

This additional step copies the daytime cloud-cover map to a file with a timestamped name. We can build up a set of these files by having the cron daemon run render_clouds.sh every three hours, at some point during the hour. Add the following to your crontab, adjusting for local directory structure, and let it run overnight:

40 */3 * * * /home/foobar/xplanet/render_clouds.sh 2>/dev/null

It doesn't matter if this runs a little less than three hours since the last time you ran it; download_clouds.py will check the last-modification timestamp of clouds.jpg and bail if it ran too recently. The redirection to /dev/null is just to keep cron from spamming your inbox with the output of render_clouds.sh every three hours.

Within a day or so, you should have a directory with some of these files accumulated:

$ ls clouds_*.jpg clouds_20040707_1940.jpg clouds_20040707_2240.jpg clouds_20040708_0140.jpg clouds_20040708_0440.jpg clouds_20040708_0740.jpg clouds_20040708_1040.jpg clouds_20040708_1340.jpg clouds_20040708_1640.jpg

Now we can use the ImageMagick utility convert to create an animated gif. While many Linux distributions ship with ImageMagick, if it isn't already installed on your machine, you can find it at http://www.imagemagick.org/.

$ convert -resize 800x400 -loop 0 -delay 30 clouds_*.jpg clouds_animated.gif

You should get back a nice animated map of the world's weather in clouds_animated.gif, which you can use to do the aforementioned amazing and astounding. Turning this process into a cron job to automate the creation of the animated GIF is left as an exercise for the reader. One final note: you may find that the cloud cover renderings will start to fill up in that directory, making a rendering of recent weather phenomena increasingly difficult. In that case, the following crontab entry, which (at least on GNU-based systems) removes any cloud imagery two days old, might be handy:

* 0 * * * rm /home/foobar/xplanet/clouds_`date +%Y%m%d -d "2 days ago"`.jpg

Mikel Maron

Категории