#!/usr/bin/env ruby

vars = ["DensCloud", "PotTempDev", "VelX", "VelZ"]
imgtimes = Array.new
prefix = ""

Dir.glob("*#{vars[0]}*png"){ |var0|
#  p var0
  if /(.+)_#{vars[0]}_([0-9]+).png/ =~ var0
    imgtimes.push($2)
    prefix = $1
  end
} 
imgtimes.sort!
p imgtimes
p prefix

i = 1
imgtimes.each{ |num|

  f0 = "#{prefix}_#{vars[0]}_#{num}.png"
  f1 = "#{prefix}_#{vars[1]}_#{num}.png"
  f2 = "#{prefix}_#{vars[2]}_#{num}.png"
  f3 = "#{prefix}_#{vars[3]}_#{num}.png"

  if FileTest.exist?(f0)
    if FileTest.exist?(f1)
      if FileTest.exist?(f2)
        if FileTest.exist?(f3)
          
          comm1 = "convert +append -geometry 640x640 #{f0} #{f1} tmp1.png"
          comm2 = "convert +append -geometry 640x640 #{f2} #{f3} tmp2.png"
          comm3 = "convert -append tmp1.png tmp2.png anim_#{sprintf("%03d",i)}.jpg"
          p comm1
          p comm2
          p comm3
          
          system( comm1 )
          system( comm2 )
          system( comm3 )
          i = i + 1
        end
      end
    end
  end
}

system("ffmpeg -f image2 -sameq -r 1 -i anim_%03d.jpg anim.mp4")
