#!/usr/bin/env ruby

require "getoptlong"

parser = GetoptLong.new
parser.set_options(
                   ###    global option   ###
                   ['--DelTime',      GetoptLong::REQUIRED_ARGUMENT],
                   ['--Time',         GetoptLong::REQUIRED_ARGUMENT],
                   ['--info',         GetoptLong::REQUIRED_ARGUMENT],
                   ['--Num',          GetoptLong::REQUIRED_ARGUMENT]
                   )
begin
  parser.each_option do |name, arg|
    eval "$OPT_#{name.sub(/^--/, '').gsub(/-/, '_')} = '#{arg}'" 
  end
    rescue
  exit(1)
end

file = ARGV[0]
info = $OPT_info
time0= $OPT_Time.to_f
num  = $OPT_Num.to_f
delt = $OPT_DelTime.to_f

# ¿Þ¤ÎÅý¹ç
order = Math::log10(time0+delt*num).to_i + 1 
fmt = "%0"+(order.to_i).to_s+"d"  

for l in 0..num
  time = sprintf(fmt, time0.to_f+(l*delt).to_f)
  outputfile  = info+"-Anim_"+time.to_s+".png"  
  outputfile1 = info+"_VelZ_"+time.to_s+".png"  
  outputfile2 = info+"_DensCloud_"+time.to_s+".png"  
  outputfile3 = info+"_PotTempDist_"+time.to_s+".png"  
  outputfile4 = info+"_Exner_"+time.to_s+".png"  

  convert1 = "convert -geometry 600x600 +append "+outputfile1+" "+outputfile2+" tmp1.png"
  system( convert1 )
  p convert1  

  convert2 = "convert -geometry 600x600 +append "+outputfile3+" "+outputfile4+" tmp2.png"
  system( convert2 )
  p convert2

  convert3 = "convert -append tmp1.png tmp2.png "+outputfile
  system( convert3 )
  p convert3
end
