Skip to content

Instantly share code, notes, and snippets.

@JamesHarrison
Created February 5, 2009 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesHarrison/58650 to your computer and use it in GitHub Desktop.
Save JamesHarrison/58650 to your computer and use it in GitHub Desktop.
Two classes for scraping and viewing met office satellite data
# tiny little class to scrape Met Office satellite and radar image feeds and save them
require 'net/http'
require 'ftools'
class MetOfficeGrabber
def initialize(type, save_folder, file_format='jpg', base_url='http://www.metoffice.gov.uk/weather/images')
@type = type.to_s # eg ukir_sat or ukvis_sat
@url = URI.parse(base_url.to_s) # met office website
@path = save_folder.to_s # save location - will be folderised by type
@format = file_format.to_s # what format is the file on the website? radar uses gif.
# make save location if it doesn't exist
Dir.mkdir(@path) unless File.exists?(@path)
Dir.mkdir("#{@path}/#{@type}") unless File.exists?("#{@path}/#{@type}")
end
def get_image(datetime)
return false if File.exists?("#{@path}/#{@type}/#{datetime.to_s}.#{@format}") # we've gotten this file..
begin
res = Net::HTTP.start(@url.host, @url.port) do |http|
http.get(@url.path+"/#{@type}_#{datetime.to_s}.#{@format}")
end
return true if res.code == "404"
File.open("#{@path}/#{@type}/#{datetime.to_s}.#{@format}", "wb") do |f|
f << res.body
end
return true
rescue Exception => e
puts "Error: Could not retrieve image for #{datetime}, reason: #{e.inspect}"
return false
end
end
def scrape_latest
now = Time.now.utc.to_i
n = 0
# Walk the last 24 hours
(0..23).each do |offset|
break unless get_image(Time.at(now-(3600*offset)).strftime('%Y%m%d%H00'))
n += 1
end
puts "Got #{n.to_s} new images for #{@type.to_s}"
end
def scrape_latest_radar
now = Time.now.utc.to_i
# Specialised walk for radar
n = 0
even = true
(0..12).each do |offset|
even ? even = false : even = true
if even
break unless get_image(Time.at(now-(1800*offset)).strftime('%Y%m%d%H00'))
else
break unless get_image(Time.at(now-(1800*offset)).strftime('%Y%m%d%H30'))
end
n += 1
end
puts "Got #{n.to_s} new images for #{@type.to_s}"
end
end
# Scrape the three usual feeds
MetOfficeGrabber.new('ukvis_sat', 'metoffice').scrape_latest
MetOfficeGrabber.new('ukir_sat', 'metoffice').scrape_latest
MetOfficeGrabber.new('uk_britradar', 'metoffice', 'gif').scrape_latest_radar
# another little class to grab satellite pics, despeckle and noise-reduce, and package as a GIF image
require 'rubygems'
require 'RMagick'
require 'ftools'
class Animator
def initialize(path,outfile)
@i = Magick::ImageList.new
@i.delay = 200
Dir.glob(path+"/*").sort{|a,b| a <=> b }.each do |f|
puts "Reading "+f.to_s
img = Magick::Image.read(f)[0]
puts "Done reading #{f.to_s}, applying filters"
img = img.enhance
img = img.despeckle
@i << img
puts "Done filtering #{f.to_s}"
end
puts "Done! Writing"
@i.write(outfile)
end
end
Animator.new("metoffice/ukvis_sat", "vis.gif")
Animator.new("metoffice/ukir_sat", "ir.gif")
Animator.new("metoffice/uk_britradar", "radar.gif")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment