Sunday, June 28, 2009

Xubuntu desktop wallpaper from a website (NOAA Radar)

This post has been superseded by a newer post. Much of this information is still valid, but the newer post has more detail amd examples, and includes more services.



Living in the midwest, I check the weather radar a lot to protect my laundry drying outside from lots of pesky thunderstorms. So I want to make the radar image my desktop picture in Xubuntu, and to have it automatically update.

To get the image: I'm using http://radar.weather.gov/lite/N0R/MKX_0.png. It is from the United States National Weather Service and updates every 5-6 minutes.

A shell script to refresh the radar image as the desktop picture:

#!/bin/sh
# BEGIN CONFIGURATION
# This is the path of the final output files that get read by other processes.
# Working files show up here, too. You may wish to create your own directory.
Local_Path=/home/me/.config/weather/
Local_Name="${Local_Path}radar_image.png"
Username=<your username>

# This is the closest weather station for observations. Find your weather 
# station: http://www.nws.noaa.gov/xml/current_obs/. Format is four letters. 
# All UPPER CASE. For example, 'KMKE' for Mitchell Field in Milwaukee, WI
Station=KMKE

# This is the closest weather radar. Find your radar: http://radar.weather.gov/
# Check the link to your weather radar, for example:
# http://radar.weather.gov/ridge/radar.php?rid=mkx&product=N0R&overlay=11101111&loop=no
# The radar name is in the 'rid=' field. In this example, mkx is Milwaukee, WI
# Format is UPPER CASE. For example, 'MKX' for Milwaukee.
Radar_Name=MKX
Radar_Url="http://radar.weather.gov/lite/N0R/${Radar_Name}_0.png"

# (OPTIONAL) The height of your top menu bar, in pixels.
# The radar image is padded by this amount on the top edge so the menu doesn't
# block the timestamp in the image.
#Radar_Image_Top_Padding=15
# END CONFIGURATION

# BEGIN RADAR IMAGE
# Download the radar image.
echo "Weather Update: Downloading the most recent radar image available..."
curl -o $Local_Name $Radar_Url

# (OPTIONAL) Use imagemagick to pad the image top so the timestamp is not 
# blocked by the menu bar.
#convert $Local_Name -background none -splice 0x${Radar_Image_Top_Padding} $Local_Name

# Refresh desktop background with 'xfdesktop -reload'. NOTE - some versions 
# of XFCE flicker all the icons brighter when this occurs, providing visual 
# feedback that the refresh occurred. An Alternate method to avoid the 
# flicker is below.
# The 'DISPLAY=:0.0' prefix is required so root processes like cron and 
# Upstart can process it.
# The 'sudo -u $Username' is required because a root process (like an Upstart 
# trigger) may be trying to change a user's desktop. Sudo changes the command 
# to run as user instead of root. Use your username, of course.
DISPLAY=:0.0 sudo -u $Username xfconf-query -v -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s $Local_Name
DISPLAY=:0.0 sudo -u $Username xfdesktop -reload

# Alternate method to avoid the flicker by changing desktop pictures for 
# just a moment, then changing it back.
#DISPLAY=:0.0 sudo -u $Username xfconf-query -v -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s /usr/share/xfce4/backdrops/xfce4logo.png
#DISPLAY=:0.0 sudo -u $Username xfconf-query -v -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s $Local_Name

# END RADAR IMAGE
I'll save this script as radar_background.sh, and make it executable with sudo chmod +x radar_background.sh.

Notes:
  1. The DISPLAY=:0.0 element is explained here.
  2. The xfconf-query command, and how to change the background using DBus, are discussed in the XFCE forums

Updating the desktop picture manually:

Since we have it in a shell script already, we can create a .bashrc alias to run the script manually.
nano .bashrc opens the .bashrc for editing.
Add the line alias radar='/home/me/radar_background.sh' to the bottom of the file and save it.
Open a new terminal window (terminals only read the .bashrc upon starting) and try the command radar.



Updating the desktop picture automatically: Since we can update the desktop image using bash commands, let's make a crontab entry to update the desktop image automatically. Here's what it looks like - a crontab entry with just the shell command:

# m h  dom mon dow   command
*/20 * * * * /home/me/radar_background.sh
Note that the desktop picture will refresh every 20 minutes.

Explaining the cron instructions:

# m h dom mon dow command - That's just part of the crontab

*/20 * * * * - tells the machine to run the script every 20 minutes. */5 * * * * will run the script every five minutes.

> /dev/null at the end (optional) - tells the machine to not e-mail me every time the script runs.

Make sure the command is all on one line (not wrapped), or you'll get crontab errors.

No comments: