Version 3 of A little rain forecaster

Updated 2002-05-15 18:39:42

Richard Suchenwirth 2002-05-15 - The following code gets maps of predicted precipitation (for North Atlantic, Europe, and parts of adjacent continents) for the next 15 days, in 12 hour steps, from a German weather station via HTTP and displays them in a canvas.

 proc cycle listName {
    upvar 1 $listName list
    set res [lindex $list 0]
    set list [concat [lrange $list 1 end] [list $res]]
 }
 proc every {ms body} {eval $body; after $ms [info level 0]}

 package require http
 # http::config -proxyhost proxy -proxyport 80

 set prefix http://129.13.102.67/pics/Rvtmrf
 set suffix 4.gif

 pack [canvas .c -width 800 -height 680] -expand 1 -fill both

 wm title . "Loading..."

 for {set i 12} {$i<=504} {incr i 12} {
    set gif [http::data [http::geturl $prefix$i$suffix]]
    if ![catch {lappend ims [image create photo im$i -data $gif]}] {
        .c create image 0 0 -anchor nw -image im$i -tag im$i
    }
 }
 wm title . "Film"

 bind . q exit
 bind . <Control-c> exit

 every 1000 {.c raise [lindex [cycle ::ims] 0]}

KBK I found that the above caused my Windows desktop to, uhhhm, "become unresponsive." Apparently, the Windows implementation, at least on my machine, can't cope with 32 images of that size inside a canvas at once.

Changing so that the canvas has only two image objects and loading them in alternation helps a lot. (It also allows the animation to start while the images are still downloading, if done right.) Here's an alternative implementation:


 proc every {ms body} {eval $body; after $ms [info level 0]}

 proc cycleImages { } {
     variable ims
     variable imsIndex
     variable buffer
     if { [llength $ims] > 0 } {
         .c raise buffer$buffer
         wm title . "image $imsIndex"
         set buffer [expr { 2 - $buffer }]
         incr imsIndex
         if { $imsIndex >= [llength $ims] } {
             set imsIndex 0
         }
         .c itemconfig buffer$buffer -image [lindex $ims $imsIndex]
     }
 }

 proc loadImage { i } {
     variable prefix
     variable suffix
     if { $i > 504 } return
     http::geturl $prefix$i$suffix -command [list loadFinished $i]
 }

 proc loadFinished { i token } {
     variable ims
     lappend ims [image create photo -data [http::data $token]]
     http::cleanup $token
     after 0 [list loadImage [expr { $i + 12 }]]
 }


 package require http
 # http::config -proxyhost webcache -proxyport 8080

 set prefix http://129.13.102.67/pics/Rvtmrf
 set suffix 4.gif

 pack [canvas .c -width 800 -height 680 -bg white] -expand 1 -fill both
 .c create image 10 10 -anchor nw -tag buffer1
 .c create image 10 10 -anchor nw -tag buffer2

 set ims {}
 set imsIndex 0
 set buffer 1

 bind . q exit
 bind . <Control-c> exit

 every 1000 cycleImages

 loadImage 12

Arts and crafts of Tcl-Tk programming