You are not logged in.

#1 2009-09-27 16:21:32

rollenwiese
Member
Registered: 2008-02-24
Posts: 69

Script fetching up to date NWS radar image

This is one way to have a collection of up to date NWS radar images on your desktop. Please Let me know if you have problems/suggestions/enhancements.

EDIT: New version 5/22/10 - http://bbs.archlinux.org/viewtopic.php? … 88#p762888
EDIT: New version 10/31/09 -

Last edited by rollenwiese (2010-05-23 01:13:18)

Offline

#2 2009-10-23 20:27:10

rollenwiese
Member
Registered: 2008-02-24
Posts: 69

Re: Script fetching up to date NWS radar image

Re-worked this script. I do not recommend using the version from the initial post.

EDIT: slightly updated from 10/23 (no functional usage changes)
Changes from 10/23:
1) setting LOGFILE_LINES to 0 disables logging.
2) changed default number of wget retries to 1

I only know of US radar regions.
NOAA radar annoyingly picks up ground clutter at night, but has better quality images.
Intellicast does not have the clutter problem, but their radar images are ugly.

Changes from the initial version:
1) Added the Intellicast server.
2) Handles multiple regions (REGION_LIST). Must also be in list for respective server URL.
3) Image frames for each region are stored in their own directory (below script path).
4) Gracefully skip any missing image frames.
5) Only refresh all images to the newest from the URL if no local images are present. Otherwise see #4.
6) Use mogrify to reduce color saturation to my taste.
7) Also use mogrify to resize images to 350px high. Ctrl+F mogrify in the code to adjust to your liking.

#!/bin/bash
#
# Greg G
# 2009-10-31
#
# ----------------------------------------------------------------------
#
# radar.sh [newframe ] [nextframe REGION] [purge]
#
# [newframe] retrieves the latest image from the appropriate sever for all
# regions in REGION_LIST
# URL_NOAA server updates at a 10 minute interval. 
# URL_INTELLICAST server updates at 15 minute interval.
# Rotates out the oldest frame.
# Intended to be used with crontab.
#
# [nextframe] increments a sybolic link target (current.gif) to the next
# frame in the series. 
# Intended to be used with conky
#
# [purge] removes all frames for all regions in REGION_LIST
#
# Must use either 'newframe' or 'nextframe' or 'purge'
#
# example crontab entry:
# */10 * * * * ~/conky/radar/radar.sh newframe
#
# example conky entry:
#
# ${image /scripts/radar/northeast/current.gif}
# ${exec /scripts/radar/radar.sh nextframe northeast}
#
# make sure imlib_cache_size is set to 0
#
# ----------------------------------------------------------------------
#
# Region serves as the root filename when downloading the most recent
# image from the IMAGE_SERVER_URL
#
# ----------------------------------------------------------------------
#
# Legend for Intellicast radar regions
#
# usa - Continental US
# lit - AR, Little Rock
# prc - AZ, Prescott
# bfl - CA, Bakersfield
# den - CO, Denver
# hfd - CT, Hartford
# eyw - FL, Key West
# pie - FL, St Petersburg
# dsm - IA, Des Moines
# myl - ID, McCall
# spi - IL, Springfield
# sln - KS, Salina
# bwg - KY, Bowling Green
# msy - LA, New Orleans
# cad - MI, Cadillac
# stc - MN, St Cloud
# jef - MO, Jefferson City
# tvr - MS, Vicksburg
# lwt - MT, Lewistown
# clt - NC, Charlotte
# bis - ND, Bismarck
# lbf - NE, North Platte
# bml - NH, Berlin
# row - NM, Roswell
# rno - NV, Reno
# bgm - NY, Binghamton
# day - OH, Dayton
# law - OK, Lawton
# rdm - OR, Redmond
# pir - SD, Pierre
# bro - TX, Brownsville
# sat - TX, San Antonio
# pvu - UT, Provo
# fcx - VA, Roanoake
# shd - VA, Staunton
# tiw - WA, Tacoma
# riw - WY, Riverton
#
# ----------------------------------------------------------------------

URL_INTELLICAST="http://images.intellicast.com/WxImages/Radar"
URL_NOAA="http://radar.weather.gov/ridge/Conus/RadarImg"

# Number of local radar image frames to maintain for each region in REGION_LIST
MAXFRAMES=9

# Number lines to keep the log file at, 0 disables logging
LOGFILE_LINES=25

# Place any regions from arrays below here to have updated with 'radar.sh newframe'
REGION_LIST="
northeast
latest
"

REGION_LIST_NOAA="
alaska
centgrtlakes
greatlakes
hawaii
latest
northeast
northrockies
pacnorthwest
pacsouthwest
southeast
southmissvly
southplains
southrockies
uppermissvly"

REGION_LIST_INTELLICAST="
usa
lit
prc
bfl
den
hfd
eyw
pie
dsm
myl
spi
sln
bwg
msy
cad
stc
jef
tvr
lwt
clt
bis
lbf
bml
row
rno
bgm
day
law
rdm
pir
bro
sat
pvu
fcx
shd
tiw
riw" 

# Below here should not need to be modified, but go ahead.

SCRIPT_PATH=`dirname "$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")"`
IMAGE_SERVER_URL=""
IMAGE_EXT=".gif"
CF_INDEXFILE="cf_index.tmp"
CF_LINKFILE="current""$IMAGE_EXT"
LOGFILE_NAME="$SCRIPT_PATH"/"radar.log"


updatelog() {

    if [ -n "$1" ] && [ $LOGFILE_LINES -gt 0 ]; then
        echo "`date +"%b %d %H:%M:%S"` $1" >> $LOGFILE_NAME
    fi

    if [ -f $LOGFILE_NAME ]; then
        linecount=`wc -l "$LOGFILE_NAME"`
        linecount=${linecount% *}
        if [ $linecount -gt $LOGFILE_LINES ]; then
            awk 'FNR>1' < $LOGFILE_NAME > tmp.log ; mv tmp.log $LOGFILE_NAME
        fi
    fi
}
    
set_image_server_url(){

    for urlregionitem in $REGION_LIST_INTELLICAST
    do
        if [ "$urlregionitem" == "$1" ]; then IMAGE_SERVER_URL=$URL_INTELLICAST
        fi
    done
    for urlregionitem in $REGION_LIST_NOAA
    do
        if [ "$urlregionitem" == "$1" ]; then IMAGE_SERVER_URL=$URL_NOAA
        fi
    done
}

chk_validregion() {

    # returns 1 if the region passed in $1 is valid
    # returns 2 if the region passed in $1 is invalid
    if [ -n "$1" ]; then
            for validregionitem in $REGION_LIST
            do
                if [ "$validregionitem" == "$1" ]; then 
                    for validregionitem in $REGION_LIST_NOAA
                    do
                        if [ "$validregionitem" == "$1" ]; then return 1
                        fi
                    done
                    for validregionitem in $REGION_LIST_INTELLICAST
                    do
                        if [ "$validregionitem" == "$1" ]; then return 1
                        fi
                    done
                fi
            done
            
            return 0
    else
        return 0
    fi
}

chk_oldestframe(){
    
    # returns the index of the oldest existing image frame
    if [ ! -d $SCRIPT_PATH/$1 ]; then return 0
    else 
        let j=$MAXFRAMES
        while [ $j -gt 0 ]
        do
            filename="$SCRIPT_PATH"/"$1"/"$1""$j""$IMAGE_EXT"
            if [ -f $filename ]; then
                return $j
            fi
            let j=$j-1
        done
        return $j
    fi
}

newframe(){

# refresh:
# if there are no local images for that region, the downloaded image is
# copied to each frame for $MAXFRAMES (a refresh)

# update, no rotation:
# if only the newest image in the rotation is missing (1), the downloaded
# image is copied to image 1 

# update, rotation:
# if the downloaded image is newer than the newest local image (1) then 
# then the oldest local image is deleted and all the local images are
# rotated back. The downloaded image is then copied to the newest local
# image (1)

# no update:
# if the downloaded image is not newer than the newest local image (1)
# no action is taken

    cd $SCRIPT_PATH
    
    for regionitem in $REGION_LIST
    do
        mkdir -p $regionitem
        cd $SCRIPT_PATH/$regionitem
    
        set_image_server_url $regionitem
    
        newframe="$regionitem""$IMAGE_EXT"
        wget -t 1 -q $IMAGE_SERVER_URL/$newframe
        moddate=$(stat -c %y $newframe)

        oldestframe="$regionitem""$MAXFRAMES""$IMAGE_EXT"
        newestframe="$regionitem""1""$IMAGE_EXT"        
    
        chk_oldestframe $regionitem
        oldestframeindex=$?

        if [ $oldestframeindex -lt 1 ]; then
            # refresh
            mogrify -resize x350 $newframe
            mogrify -modulate 100,30,100 $newframe
            touch -d "${moddate}" $newframe
            let i=$MAXFRAMES
            while [ $i -gt 0 ]
            do
                tofilename="$regionitem""$i""$IMAGE_EXT"
                cp $newframe $tofilename
                let i=$i-1
            done
            rm -f $newframe
            updatelog "refresh         : $regionitem"
        else
            # non-refresh update
            if [ ! -f $newestframe ]; then
                # update first frame, no rotation
                mogrify -resize x350 $newframe
                mogrify -modulate 100,30,100 $newframe
                touch -d "${moddate}" $newframe
                mv $newframe $newestframe
                updatelog "new frame       : $regionitem (no rotation)"
            elif [ $newframe -nt $newestframe ]; then
                # update first frame, rotatation
                rm -f $oldestframe
                let oldindex=$MAXFRAMES-1
                while [ $oldindex -gt 0 ]
                do
                    let destindex=$oldindex+1
                    fromfilename="$regionitem""$oldindex""$IMAGE_EXT"
                    tofilename="$regionitem""$destindex""$IMAGE_EXT"
                    mv $fromfilename $tofilename
                    let oldindex=$oldindex-1
                done
                mogrify -resize x350 $newframe
                mogrify -modulate 100,30,100 $newframe
                touch -d "${moddate}" $newframe
                mv $newframe $newestframe
                updatelog "new frame       : $regionitem (rotation)"
            else
                # no update
                rm -f $newframe
                updatelog "no update       : $regionitem"
            fi
        fi

        cd $SCRIPT_PATH

    done
}

nextframe(){
    
    cd $SCRIPT_PATH/$1

    if [ -f $CF_INDEXFILE ]; then
        cf_index=`cat $CF_INDEXFILE`
    else 
        echo $MAXFRAMES > $CF_INDEXFILE
    fi

    if [ -z "$cf_index" ];
        then let cf_index=1
        elif [ $cf_index -lt 1 ]; then let cf_index=$MAXFRAMES
        elif [ $cf_index -gt $MAXFRAMES ]; then let cf_index=1
    fi

    linktarget="$1""$cf_index""$IMAGE_EXT"
    if [ -f $linktarget ]; then ln -sf $linktarget $CF_LINKFILE
    fi
    let cf_index=$cf_index-1
    echo $cf_index > $CF_INDEXFILE

    cd $SCRIPT_PATH    
}


cd $SCRIPT_PATH

case $1 in
    "newframe")
        newframe
        ;;

     "nextframe")
        chk_validregion $2
        validregion=$?
        chk_oldestframe $2
        oldestframeindex=$?
        if [ $validregion -eq 1 ] && [ $oldestframeindex -lt 1 ]; then
            newframe
            nextframe $2
        elif [ $validregion -eq 1 ]; then nextframe $2
        else echo "invalid region argument"
        fi
        ;;

     "purge")
        for regionitem in $REGION_LIST
        do
            rm -rf $regionitem
        done            
        echo "purged all frames."
        updatelog "purge           : purged all frames"
        echo
        ;;

      *)
        echo
        echo "Usage : $0 [newframe ] [nextframe REGION] [purge]"
        echo
        echo "'newframe'   retrieves the latest image from the appropriate server for all regions"
        echo "'nextframe'  increments a symbolic link target (current.gif) to the next frame in the region series"
        echo "'purge'      removes frames for all regions in REGION_LIST"
        echo
        exit 1
esac

Last edited by rollenwiese (2009-10-31 17:00:27)

Offline

#3 2009-10-23 21:22:45

Kooothor
Member
From: Paname
Registered: 2008-08-02
Posts: 226

Re: Script fetching up to date NWS radar image

Screenshot of what it does ?


ktr

Offline

#4 2009-10-23 22:10:27

rollenwiese
Member
Registered: 2008-02-24
Posts: 69

Re: Script fetching up to date NWS radar image

Screenshot: http://imgur.com/yi5QW.jpg
NOAA Radar image shown. Animation effect of course is lost in this image.

Use Conky to display 'current.gif', a symbolic link to 1 of $MAXFRAMES radar images. Everytime conky updates (1.6 sec), './radar.sh nextframe' is exec'd and the link is updated to the next radar image in the series. Giving the effect of an animated radar loop on your desktop. Setting 'imlib_cache_size 0' in your conky.conf file is critical to getting this effect.

Use crontab -e and to add a job calling './radar.sh newframe'. The latest radar image will be seamlessly rotated in to the series and the oldest rotated out. At the interval you choose, maintaining $MAXFRAMES number of images. You could also use conky to do this, but with cron I know the images are always as up to date as possible regardless of how conky has been used.

cron:

*/10 * * * * /home/gregory/conky/radar/radar.sh newframe

conky:

alignment bottom_right
gap_x 13
gap_y 445
background yes
double_buffer yes
minimum_size 368 350
imlib_cache_size 0
no_buffers yes
own_window yes
own_window_type normal 
own_window_transparent no
own_window_hints below,undecorated,skip_taskbar,skip_pager
own_window_colour D3E2E9
total_run_times 0
update_interval 1.6
border_width 0
draw_borders no
draw_shades no
draw_outline no
default_color black

TEXT
${image /home/gregory/conky/radar/northeast/current.gif}
${exec /home/gregory/conky/radar/radar.sh nextframe northeast}

Last edited by rollenwiese (2009-10-23 22:16:34)

Offline

#5 2009-11-03 12:38:55

jwcxz
Member
Registered: 2008-09-23
Posts: 239
Website

Re: Script fetching up to date NWS radar image

I saw your comment on my screenshot in the other thread, rollenwiese.  I like your idea of using an animated radar map.  Mine is just stationary and rotates every 15 minutes (by using imlib_cache_flush_interval 900).  I use an external cron progam to pull all the weather data and process it into a readable format.  The script that Conky calls just reads that data; it's much easier on the bandwidth.

Sometime when I get a chance, I'll mess with your script, though.  As I had mentioned earlier, I think that if I use a blank map as a reference image, I might be able to pull the weather "out" of the map...  I'll have to investigate further.


-- jwc
http://jwcxz.com/ | blog
dotman - manage your dotfiles across multiple environments
icsy - an alarm for powernappers

Offline

#6 2009-11-04 01:19:20

monstermudder78
Member
Registered: 2008-05-18
Posts: 120

Re: Script fetching up to date NWS radar image

Very nice, thanks.  If you are wishing to distribute or make the script more friendly might want to use use variables(?) and group them all in one place (at the beginning?) of the script (.  Also, not sure if I missed the instruction somewhere or if I am a special case, but I had to first run nextframe then newframe, or vice versa, to get it started.

Offline

#7 2009-11-15 16:37:02

rollenwiese
Member
Registered: 2008-02-24
Posts: 69

Re: Script fetching up to date NWS radar image

monstermudder, thank you, you're correct. Before adding to conky you would have to run 'radar.sh newframe' to get an initial frame set to work with, then add to crontab/conky, or however you wish to update/display the images.

monstermudder78 wrote:

Very nice, thanks.  If you are wishing to distribute or make the script more friendly might want to use use variables(?) and group them all in one place (at the beginning?) of the script (.  Also, not sure if I missed the instruction somewhere or if I am a special case, but I had to first run nextframe then newframe, or vice versa, to get it started.

Offline

#8 2010-05-23 01:06:53

rollenwiese
Member
Registered: 2008-02-24
Posts: 69

Re: Script fetching up to date NWS radar image

Reworked this script to download animated gifs rather than individual frames. No more reliance on conky for creating the animation and a much less complicated script.  Just paste which regions you want to keep up to date in the $REGION_LIST and add the crontab entry in the script comments.

Install gifsicle and imagemagick before using the script. Use 'gifview -a northeast-animated.gif' in X to view the image. Gifview is part of the gifsicle package.

#!/bin/bash
#
# Greg G
# 2010-05-22
#
# REQUIRED -------------------------------------------------------------
#
# gifsicle  
# imagemagick
#
# gifview comes with gifsicle, a lightweight gif viewer
#
# USAGE ----------------------------------------------------------------
#
# radar.sh [update] [purge]
#
# [update] retrieves the latest image from the appropriate sever for all
# regions in REGION_LIST
# URL_NOAA server updates at a 10 minute interval. 
# URL_INTELLICAST server updates at 15 minute interval.
#
# [purge] removes all frames for all regions in REGION_LIST
#
# Must use either 'update' or 'purge'
#
# CRONTAB --------------------------------------------------------------
#
# */10 * * * * ~/scripts/radar/radar.sh update
#
# ----------------------------------------------------------------------
#
# Region serves as the root filename when downloading the most recent
# image from the IMAGE_SERVER_URL
#
# ----------------------------------------------------------------------
#
# Intellicast radar regions
#
# usa - Continental US
# lit - AR, Little Rock
# prc - AZ, Prescott
# bfl - CA, Bakersfield
# den - CO, Denver
# hfd - CT, Hartford
# eyw - FL, Key West
# pie - FL, St Petersburg
# dsm - IA, Des Moines
# myl - ID, McCall
# spi - IL, Springfield
# sln - KS, Salina
# bwg - KY, Bowling Green
# msy - LA, New Orleans
# cad - MI, Cadillac
# stc - MN, St Cloud
# jef - MO, Jefferson City
# tvr - MS, Vicksburg
# lwt - MT, Lewistown
# clt - NC, Charlotte
# bis - ND, Bismarck
# lbf - NE, North Platte
# bml - NH, Berlin
# row - NM, Roswell
# rno - NV, Reno
# bgm - NY, Binghamton
# day - OH, Dayton
# law - OK, Lawton
# rdm - OR, Redmond
# pir - SD, Pierre
# bro - TX, Brownsville
# sat - TX, San Antonio
# pvu - UT, Provo
# fcx - VA, Roanoake
# shd - VA, Staunton
# tiw - WA, Tacoma
# riw - WY, Riverton
#
# ----------------------------------------------------------------------

URL_INTELLICAST="http://images.intellicast.com/WxImages/Radar"
URL_INTELLICAST_ANIMATED="http://images.intellicast.com/WxImages/RadarLoop"
URL_NOAA="http://radar.weather.gov/ridge/Conus/RadarImg"
URL_NOAA_ANIMATED="http://radar.weather.gov/Conus/Loop"

# Number lines to keep the log file at, 0 disables logging
LOGFILE_LINES=25

# Place any regions from arrays below here to have updated with 'radar.sh update'
REGION_LIST="
Nat
northeast
usa
"

REGION_LIST_NOAA="
Nat
alaska
centgrtlakes
greatlakes
hawaii
latest
northeast
northrockies
pacnorthwest
pacsouthwest
southeast
southmissvly
southplains
southrockies
uppermissvly
"

REGION_LIST_INTELLICAST="
usa
lit
prc
bfl
den
hfd
eyw
pie
dsm
myl
spi
sln
bwg
msy
cad
stc
jef
tvr
lwt
clt
bis
lbf
bml
row
rno
bgm
day
law
rdm
pir
bro
sat
pvu
fcx
shd
tiw
riw" 

SCRIPT_PATH=`dirname "$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")"`
TARGETIMAGE_URL=""
TARGETIMAGE_FILENAME=""
ANIMATION_FILENAME=""
LOGFILE_NAME="$SCRIPT_PATH"/"radar.log"


updatelog() {

    if [ -n "$1" ] && [ $LOGFILE_LINES -gt 0 ]; then
        echo "`date +"%b %d %H:%M:%S"` $1" >> $LOGFILE_NAME
    fi

    if [ -f $LOGFILE_NAME ]; then
        linecount=`wc -l "$LOGFILE_NAME"`
        linecount=${linecount% *}
        if [ $linecount -gt $LOGFILE_LINES ]; then
            awk 'FNR>1' < $LOGFILE_NAME > tmp.log ; mv tmp.log $LOGFILE_NAME
        fi
    fi
}

validate_region() {

    for noaaregionitem in $REGION_LIST_NOAA
        do
            if [ $noaaregionitem == $1 ]; then return 0
            fi
        done

    for intellicastregionitem in $REGION_LIST_INTELLICAST
        do
            if [ $intellicastregionitem == $1 ]; then return 0
            fi
        done

    return 1
}
    
set_targetimage_info() {

    for urlregionitem in $REGION_LIST_INTELLICAST
    do
        if [ "$urlregionitem" == "$1" ]; then
            TARGETIMAGE_URL="$URL_INTELLICAST_ANIMATED"
            TARGETIMAGE_FILENAME="$1"_None_anim.gif
            ANIMATION_FILENAME="$1"-animated.gif
        fi
    done

    for urlregionitem in $REGION_LIST_NOAA
    do
        if [ "$urlregionitem" == "$1" ]; then
            TARGETIMAGE_URL="$URL_NOAA_ANIMATED"
            TARGETIMAGE_FILENAME="$1"_loop.gif
            # fix inconsistency in file naming convention
            if [ "$urlregionitem" == "Nat" ]; then TARGETIMAGE_FILENAME="$1"Loop.gif
            fi
            ANIMATION_FILENAME="$1"-animated.gif

        fi
    done
}

update() {

    cd $SCRIPT_PATH
    
    for regionitem in $REGION_LIST
    do
        validate_region $regionitem
        isvalid=$?
        if [ $isvalid -eq 0 ]; then
            mkdir -p $regionitem
            cd $SCRIPT_PATH/$regionitem
            set_targetimage_info $regionitem
            wget -q -N -t 1 $TARGETIMAGE_URL/$TARGETIMAGE_FILENAME
            if [ $TARGETIMAGE_FILENAME -nt $ANIMATION_FILENAME ]; then
                moddate=$(stat -c %y $TARGETIMAGE_FILENAME)        
                gifsicle -w -e --unoptimize $TARGETIMAGE_FILENAME
                mogrify -resize x350 $TARGETIMAGE_FILENAME.*
                mogrify -modulate 100,30,100 $TARGETIMAGE_FILENAME.*
                # crops the NOAA 'Nat' image to show only the Eastern half of the United States
                if [ $regionitem == "Nat" ]; then mogrify -gravity East -crop 376x-8+0 +repage $TARGETIMAGE_FILENAME.*
                fi
                gifsicle -w -m -O2 -l $TARGETIMAGE_FILENAME.* -o $ANIMATION_FILENAME
                touch -d "${moddate}" $ANIMATION_FILENAME
                updatelog "updated         : $regionitem"
            else
                updatelog "no update       : $regionitem"
            fi
        else
            echo "$regionitem is invalid"
            updatelog "invalid region  : $regionitem"
        fi
        cd $SCRIPT_PATH
    done

}


cd $SCRIPT_PATH

case $1 in
    "update")
        update
        ;;

     "purge")
        for regionitem in $REGION_LIST
        do
            rm -rf $regionitem
        done            
        echo "purged all frames."
        updatelog "purge           : purged all frames"
        echo
        ;;

      *)
        echo
        echo "Usage : $0 [update ] [purge]"
        echo
        echo "'update'     retrieves the latest image from the appropriate server for all regions"
        echo "'purge'      removes frames for all regions in REGION_LIST"
        echo
        exit 1
esac

Last edited by rollenwiese (2010-05-23 02:53:27)

Offline

Board footer

Powered by FluxBB