Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/usr/bin/ruby -Ku
  2.  
  3. require 'open-uri'
  4.  
  5. img_width = 0
  6. img_height = 0
  7. img_data = [] # [h][w] で [r,g,b]がでてくるようにする
  8. open('http://www.tepco.co.jp/forecast/html/images/juyo-j.gif'){|file|
  9.   IO::popen('giftopnm', 'r+'){|io|
  10.     io.write file.read
  11.     raise 'Unknown format!' if /^P6/ !~ io.readline
  12.     raise 'Unknown size!' if /(\d+) (\d+)/ !~ io.readline
  13.     img_width, img_height = [$1.to_i, $2.to_i]
  14.     $stderr.puts "Original image w, h: #{img_width}, #{img_height}"
  15.     img_height.times{|i|
  16.       same_row_data = []
  17.       img_width.times{|j|
  18.         same_row_data << [io.getc, io.getc, io.getc]
  19.       }
  20.       img_data << same_row_data
  21.     }
  22.   }
  23. }
  24.  
  25. converted_data = {}
  26.  
  27. # 画像から情報を読み取る
  28. # 0時(Y = 69), 23時(Y = 573)
  29. hr = 0
  30. (69.0).step(573, (573 - 69).to_f / 23){|f|
  31.   i = f.to_i
  32.   #$stderr.puts "#{hr}時台(Y = #{i}) : #{img_data[275][i].inspect}"
  33.  
  34.   same_hr_info = {}
  35.  
  36.   status = :waiting
  37.   # 計画停電なし(水), あり(オレンジ), まだ(黄) # X = 275のデータを元に
  38.   if img_data[275][i][0] > 0xC0 && img_data[275][i][1] < 0x60 && img_data[275][i][2] > 0xC0 then
  39.     # 255, 64, 255
  40.     status = :not_operated
  41.   elsif img_data[275][i][0] < 0x20 && img_data[275][i][1] > 0xC0 \
  42.       && img_data[275][i][2] < 0xA0 && img_data[275][i][2] > 0x60 then
  43.     # 0, 255, 128
  44.     status = :operated
  45.   else
  46.     # 191, 255, 255
  47.   end
  48.   same_hr_info[:status] = status
  49.  
  50.   # 万kWを読み込む (Y => 0: 286, 6000: 55)
  51.   consumption = 0
  52.   if status != :waiting then
  53.     # 上に見ていって色がかわる画素を探す
  54.     275.step(56, -1){|j|
  55.       if (img_data[j][i] == [0, 0, 0]) \
  56.           or (img_data[j][i][0] > 0xB0 && img_data[j][i][1] > 0xE0 && img_data[j][i][2] > 0xE0) then
  57.         consumption = ((286 - j).to_f / (286 - 55) * 6000).to_i
  58.         break
  59.       end
  60.     }
  61.   end
  62.   same_hr_info[:consumption] = consumption
  63.  
  64.   converted_data[hr] = same_hr_info
  65.   hr += 1
  66. }
  67.  
  68. converted_data.to_a.sort{|a, b| a[0] <=> b[0]}.each{|i, info|
  69.   $stderr.puts "#{i}: #{info.inspect}"
  70. }