#!/usr/bin/ruby -Ku
require 'open-uri'
img_width = 0
img_height = 0
img_data = [] # [h][w] で [r,g,b]がでてくるようにする
open('http://www.tepco.co.jp/forecast/html/images/juyo-j.gif'){|file|
IO::popen('giftopnm', 'r+'){|io|
io.write file.read
raise 'Unknown format!' if /^P6/ !~ io.readline
raise 'Unknown size!' if /(\d+) (\d+)/ !~ io.readline
img_width, img_height = [$1.to_i, $2.to_i]
$stderr.puts "Original image w, h: #{img_width}, #{img_height}"
img_height.times{|i|
same_row_data = []
img_width.times{|j|
same_row_data << [io.getc, io.getc, io.getc]
}
img_data << same_row_data
}
}
}
converted_data = {}
# 画像から情報を読み取る
# 0時(Y = 69), 23時(Y = 573)
hr = 0
(69.0).step(573, (573 - 69).to_f / 23){|f|
i = f.to_i
#$stderr.puts "#{hr}時台(Y = #{i}) : #{img_data[275][i].inspect}"
same_hr_info = {}
status = :waiting
# 計画停電なし(水), あり(オレンジ), まだ(黄) # X = 275のデータを元に
if img_data[275][i][0] > 0xC0 && img_data[275][i][1] < 0x60 && img_data[275][i][2] > 0xC0 then
# 255, 64, 255
status = :not_operated
elsif img_data[275][i][0] < 0x20 && img_data[275][i][1] > 0xC0 \
&& img_data[275][i][2] < 0xA0 && img_data[275][i][2] > 0x60 then
# 0, 255, 128
status = :operated
else
# 191, 255, 255
end
same_hr_info[:status] = status
# 万kWを読み込む (Y => 0: 286, 6000: 55)
consumption = 0
if status != :waiting then
# 上に見ていって色がかわる画素を探す
275.step(56, -1){|j|
if (img_data[j][i] == [0, 0, 0]) \
or (img_data[j][i][0] > 0xB0 && img_data[j][i][1] > 0xE0 && img_data[j][i][2] > 0xE0) then
consumption = ((286 - j).to_f / (286 - 55) * 6000).to_i
break
end
}
end
same_hr_info[:consumption] = consumption
converted_data[hr] = same_hr_info
hr += 1
}
converted_data.to_a.sort{|a, b| a[0] <=> b[0]}.each{|i, info|
$stderr.puts "#{i}: #{info.inspect}"
}