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

LDUMP=true
# LDUMP=false

gtURL="gravcurrent1.nc@rho"

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

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.25,0.45])
for it in 0..nt-1
  if LDUMP then  
    GGraph.tone rho[true,true,it].cut('x'=>0..60), true, 'min'=>1.0001, 'max'=>1.001, 'inf_min'=>true, 'inf_max'=>true, 'annotate'=>false
  else
    GGraph.tone rho[true,true,it].cut('x'=>0..60), true, 'min'=>1.0001, 'max'=>1.001, '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 > gravcurrent_%04d.png", it+1, it+1)
    system(command)
  end
  system('ffmpeg -r 10 -i gravcurrent_%04d.png -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -r 10 gravcurrent.mp4')
  system("mv gravcurrent_0020.png gravcurrent.png")
  system("rm gravcurrent_????.png")
  system("rm dcl_????.png")
end
