#!/usr/bin/env ruby
#
#
require "numru/ggraph"
include NumRu

LDUMP=true
# LDUMP=false

# Omega=0.3 ; Min = -4e-5 ; Max = 4e-5
Omega=0.6 ; Min = -6e-5 ; Max = 6e-5
# Omega=0.9 ; Min = -1e-4 ; Max = 1e-4
# Omega=1.2 ; Min = -1e-4 ; Max = 1e-4

gtURL=sprintf("igwave1_Omega%3.1f.nc@rho",Omega)

rho = GPhys::IO.open_gturl( gtURL )
t_na = rho.axis(2).pos.val
nt = t_na.size

DCL.sgscmn(4)
DCL.swlset('LWAIT',false)
DCL.swlset('LWAIT0',false)

if LDUMP then
  DCL.swlset('LWAIT1',false)
  DCL.swlset('LDUMP',true)
else
  DCL.swlset('LALT',true)
end

DCL.uzfact(0.7)
DCL.sglset('lfull',true)
DCL.gropn(1)
GGraph.set_fig('viewport'=>[0.2,0.8,0.2,0.5])
for it in 0..nt-1
  if LDUMP then  
    GGraph.tone rho[true,true,it], true, 'min'=>Min, 'max'=>Max, 'inf_min'=>true, 'inf_max'=>true, 'annotate'=>false
  else
    GGraph.tone rho[true,true,it], true, 'min'=>Min, 'max'=>Max, 'inf_min'=>true, 'inf_max'=>true
  end
end
DCL.grcls

if LDUMP then
  for it in 0..nt-1
    command = sprintf("pngtopnm dcl_%04d.png | pnmcrop | pnmtopng > tmp_%04d.png", it+1, it+1)
    system(command)
  end

  command = 'ffmpeg -r 10 -i tmp_%04d.png -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -r 10 tmp.mp4'
  system(command)

  command = sprintf("mv tmp.mp4 igwave_Omega%3.1f.mp4",Omega)
  system(command)

  command = sprintf("mv tmp_0041.png igwave_Omega%3.1f.png",Omega)
  system(command)

  system("rm tmp_????.png")
  system("rm dcl_????.png")
end
