amesh.rb
require 'open-uri'
require 'RMagick'
include Magick
# 現在アクセス可能な雨量画像のファイル名一覧を含むjsを取得 現在時刻から計算することも可能っぽい
times_url = "http://tokyo-ame.jwa.or.jp/scripts/mesh_index.js"
times_js = open(times_url).read()
times = times_js.sub("Amesh.setIndexList([", "").sub(");", "").chomp!.chomp!.split(",").map!{|t| t[1..-2].to_i}
# でかいほうのgif画像最新一件を取得 /100/じゃなくて/000/にすると小さくなる
gif_url = "http://tokyo-ame.jwa.or.jp/mesh/100/#{times[0]}.gif"
amesh_gif = open(gif_url).read()
amesh_magick = Magick::Image.from_blob(amesh_gif).first
# 色と雨量の対応関係
color_precipitation_map = {
Magick::Pixel.new(0, 0, 0, 0) => 0,
Magick::Pixel.new(0, 0, 257, 65535) => 0,
Magick::Pixel.new(52428, 65535, 65535, 0) => 3,
Magick::Pixel.new(26214, 39321, 65535, 0) => 10,
Magick::Pixel.new(13107, 13107, 65535, 0) => 20,
Magick::Pixel.new(0, 65535, 0, 0) => 30,
Magick::Pixel.new(65535, 65535, 0, 0) => 40,
Magick::Pixel.new(65535, 39321, 0, 0) => 50,
Magick::Pixel.new(65535, 0, 65535, 0) => 80,
Magick::Pixel.new(65535, 0, 0, 0) => 100,
}
# ここで必要だったら magick_image.corp(x, y, w, h) などで切り抜く
target_area = amesh_magick
# 切り出し領域のピクセル全部で、色から雨量への換算をする
target_area.each_pixel do |px, col, row|
precipitation = color_precipitation_map.fetch(px, -1)
case precipitation
when 0
when 1..100
# 雨が観測されている地域の雨量と画像内の座標
p [precipitation, col, row]
when -1
# 未知の色が使われ始めた?
p [px.to_color, px, col, row]
end
end