Skip to content

Instantly share code, notes, and snippets.

@pepsin
Last active July 27, 2016 00:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pepsin/2332ef243e3285ca68c6 to your computer and use it in GitHub Desktop.
Save pepsin/2332ef243e3285ca68c6 to your computer and use it in GitHub Desktop.
require 'net/http'
require "cairo"
def get_url time, x, y
str = "http://himawari8-dl.nict.go.jp/himawari8/img/D531106/20d/550/#{time}00_#{x}_#{y}.png"
end
def merge time
puts "Start image process"
w = 20
h = 20
partial_width = 550
surface = Cairo::ImageSurface.new(w * partial_width, h * partial_width)
cr = Cairo::Context.new(surface)
arr = []
need_stop = false
w.times do |x|
h.times do |y|
name = "himawari_#{x}_#{y}.png"
arr.push name
puts name
png = Cairo::ImageSurface.from_png(name) rescue nil
if png.nil?
need_stop = true
File.delete name
break
end
cr.matrix = Cairo::Matrix.new(1,0,0,1, x * partial_width, y * partial_width)
cr.set_source(*png)
cr.paint()
png.destroy
end
if need_stop
break
end
end
if need_stop
surface.destroy
cr.destroy
start
end
cr.target.write_to_png("#{time.split("/").join("_")}.png")
cr.destroy
surface.destroy
arr.each do |file|
File.delete file
end
end
def run time
merge_name = time.split("/").join("_") + ".png"
unless File.exists? merge_name
ts = []
need_break = false
20.times do |x|
thread = Thread.new do
20.times do |y|
name = "himawari_#{x}_#{y}.png"
unless File.exists? name
url = get_url time, x, y
uri = URI.parse(url)
req = Net::HTTP::Get.new uri.path
res = Net::HTTP.start(uri.host, uri.port, use_ssl: false) do |http|
http.request req
end rescue nil
if res.nil?
need_break = true
end
unless need_break
file = File.open(name, "w+")
file.write(res.body)
file.close
end
end
end
end
ts.push thread
end
ts.map(&:join)
if need_break
start
return
end
sleep 5
end
unless File.exists? merge_name
merge time
end
end
def start
24.times do |hour|
hour = hour.to_s
if hour.length < 2
hour = "0" + hour
end
6.times do |minute|
minute = (minute * 10).to_s
if minute.length < 2
minute = "0" + minute
end
str = "2015/12/01/" + hour + minute
puts str
run str
end
end
end
start
@fanpei91
Copy link

这里有个我也debug不出的bug, 望修复, 我特想要20d的图. 辛苦啦!
Mac OS X 版本号: 10.10.5
Ruby版本号: 2.2.0
Cairo是通过gem安装的.

himawari8.rb:22:in `from_png': error while reading from input stream (Cairo::ReadError)
    from himawari8.rb:22:in `block (2 levels) in merge'
    from himawari8.rb:18:in `times'
    from himawari8.rb:18:in `block in merge'
    from himawari8.rb:17:in `times'
    from himawari8.rb:17:in `merge'
    from himawari8.rb:67:in `run'
    from himawari8.rb:83:in `block (2 levels) in <main>'
    from himawari8.rb:76:in `times'
    from himawari8.rb:76:in `block in <main>'
    from himawari8.rb:71:in `times'
    from himawari8.rb:71:in `<main>'

@pepsin
Copy link
Author

pepsin commented Dec 10, 2015

@old-woman 新版本已经修复这个问题了

@fanpei91
Copy link

@pepsin 太感谢了! 看起来没问题了. 你下载拼接花了多长时间? 直连的么?

@pepsin
Copy link
Author

pepsin commented Dec 11, 2015

@old-woman 两三个小时吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment