Geektool Weather Radar - Canada

Discussion in 'UI Customization' started by yetiboy, Aug 11, 2013.

  1. yetiboy macrumors member

    Joined:
    Mar 23, 2011
    #1
    Hi everyone. I've used numerous geeklets from this forum over the past few years and I've always appreciated the help. Hopefully someone can help me again - although this may be a bit more demanding.

    I'm trying to add a local weather image(s) to my desktop, but I don't want just a static one. I'd like to use this with the 3 hour animation:

    http://weather.gc.ca/radar/index_e.html?id=XFT

    Taking a look at the source, I can see how the animation is put together on the site using radar images from every 20 minutes (you can see the Image List near the bottom of the source for both the short (1hr) and long(3hr) animations). I have NO idea how to create a script to actually get those images though, since the list would be made dynamically. I would guess you would have to take the local time on the computer and create a list of images based on going back 20 minutes at a time - but that's WAY beyond my expertise. Anyone have any ideas?
     
  2. yetiboy thread starter macrumors member

    Joined:
    Mar 23, 2011
    #2
    Well, got bored with waiting (over 300 views and no responses, which is too bad), so I decided to teach myself some python and create a script myself. Actually got it to work, which stuns me to no end - had some hiccups, and it's definitely not pretty, but it works. I've included it below if anyone wants to take a look and perhaps improve on it. Feel free to steal and modify.

    To use it, save the script somewhere create a geeklet that calls it every 5-10 minutes (or even longer, if you like). There's no sense having it check any faster than that since the images are only updated on the website every 10 minutes. I would then create a subfolder for the images (make sure to change the path in the code) and make a simple slideshow - create an image geeklet, point it to the folder, set the refresh rate to 1 second. Voila, slideshow of your radar images.

    Note: The radar images don't show up until 10-15 minutes after the current time, so I set the script to start at a minimum of 20 minutes before the current UTC time. I've also only used 6 images (for now), but it can be easily adjusted up or down by changing the first number in the if loop. Oh, and I haven't taken into account leap years when going back in time to the previous radar images. If you want to augment to the code to account for it, have at it - I just won't check my desktop on February 29th.

    yeti

    Code:
    #! /usr/bin/python
    # -*- coding: utf-8 -*-
    
    import urllib2
    import datetime
    
    def change_hour():
    	global hour
    	hour -= 1
    	if hour == -1:
    		hour = 23
    		change_day()
    	return
    
    def change_day():
    	global day
    	day -= 1
    	if day == 0:
    		change_month()
    	return
    
    def change_month():
    	global month,day
    	month -= 1
    	if month == 0:
    		change_year()
    		month = 12
    	elif month == 2:
    		day = 28
    	elif month == 4 or month == 6 or month == 9 or month == 10:
    		day = 30
    	else:
    		day = 31
    	return
    
    def change_year():
    	global year
    	year -= 1
    	return
    
    
    current_time=datetime.datetime.utcnow()
    year=current_time.year
    month=current_time.month
    day=current_time.day
    hour=current_time.hour
    minute=current_time.minute
    
    current_radar_image = []
    current_radar_image_local = []
    
    for i in range(6,0,-1):
    
    	minute=(minute-20)
    	if minute < -10:
    		change_hour()
    		minute = 40
    		new_minute = 40
    	elif minute < 0:
    		minute = 50
    		new_minute = 50
    		change_hour()
    	elif minute < 10:
    		minute = 0
    		new_minute = 0
    	else:
    		new_minute = minute
    	
    	minute_string = str(new_minute)
    	minute_string = (minute_string[0:1]+"0")
    
    
    	j=str(i)
    	cril_url=("/path/to/weather/images/folder/weather"+j+".gif")
    	cril_url_string=str(cril_url)
    	cril_url_string=cril_url_string.strip()
    	current_radar_image_local.append(cril_url_string)
    
    	if month < 10:
    		month_string = ("0"+(str(month)))
    	else:
    		month_string = str(month)
    
    	if day < 10:
    		day_string = ("0"+(str(day)))
    	else:
    		day_string = str(day)
    
    	if hour < 10:
    		hour_string = ("0"+(str(hour)))
    	else:
    		hour_string = str(hour)
    
    	year_string = str(year)
    
    	cri_url=('http://weather.gc.ca/data/radar/temp_image/XFT/XFT_PRECIP_RAIN_'+year_string+"_"+month_string+"_"+day_string+"_"+hour_string+"_"+minute_string+".GIF")
    	cri_url_string=str(cri_url)
    	cri_url_string=cri_url_string.strip()
    	current_radar_image.append(cri_url_string)
    
    for k,l in zip(current_radar_image,current_radar_image_local):
    	response = urllib2.urlopen(k)
    	with open(l,'wb') as outfile:
    		outfile.write(response.read())
    	response.close()
     

Share This Page