Created
June 1, 2011 21:58
-
-
Save yanks/1003432 to your computer and use it in GitHub Desktop.
the weather
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'RMagick' | |
def retrieve_image(url) | |
data = open(url).read | |
data | |
end | |
def composite(img, dest_image_data) | |
if dest_image_data.nil? then | |
return img | |
end | |
begin | |
tmp_img = Magick::Image.from_blob(dest_image_data).first | |
if img == nil then | |
img = tmp_img | |
else | |
img = img.composite(tmp_img, Magick::CenterGravity, Magick::OverCompositeOp) | |
end | |
rescue | |
puts "error" | |
end | |
img | |
end | |
radar_id = ARGV[0] | |
if radar_id.nil? || radar_id.length != 3 then | |
puts "usage: radar <identifier>" | |
exit | |
end | |
html = Nokogiri::HTML(open("http://radar.weather.gov/radar.php?rid=#{radar_id}")) | |
#get map | |
map_graphics = html.xpath("//div[@id='bkgrnd']//img") | |
img = nil | |
map_graphics.each() do |graphic| | |
#src | |
url = graphic['src'] | |
#check for nothing | |
if url[0..1] != '#' then | |
#get it | |
data = retrieve_image("http://radar.weather.gov/#{url}") | |
img = composite(img, data) | |
end | |
end | |
#get radar | |
radar_img = retrieve_image("http://radar.weather.gov/RadarImg/N0R/#{radar_id}_N0R_0.gif") | |
img = composite(img, radar_img) | |
radar_legend = retrieve_image("http://radar.weather.gov/Legend/N0R/#{radar_id}_N0R_Legend_0.gif") | |
img = composite(img, radar_legend) | |
img.write('radarimage.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment