#!/usr/bin/env ruby

require "numru/netcdf"
require "numru/dcl"
require "numru/ggraph"
 
require "getoptlong"
include NumRu

###
###$B0z?t=hM}(B
###
parser = GetoptLong.new
parser.set_options(
                   ###    global option   ###
                   ['--delt',         GetoptLong::REQUIRED_ARGUMENT],
                   ['--time',         GetoptLong::REQUIRED_ARGUMENT],
                   ['--num',          GetoptLong::REQUIRED_ARGUMENT],
                   ['--max',          GetoptLong::REQUIRED_ARGUMENT],
                   ['--min',          GetoptLong::REQUIRED_ARGUMENT],
                   ['--zlev',         GetoptLong::REQUIRED_ARGUMENT],
                   ['--noinfo',       GetoptLong::NO_ARGUMENT],
                   ['--dump',         GetoptLong::NO_ARGUMENT]
                   )
begin
  parser.each_option do |name, arg|
    eval "$OPT_#{name.sub(/^--/, '').gsub(/-/, '_')} = '#{arg}'" 
  end
rescue
  exit(1)
end


###
### $B=i4|2=(B
###

deltime = $OPT_delt.to_f
time0   = $OPT_time.to_f
num     = $OPT_num.to_i

if ( $OPT_max )
  maximum = $OPT_max.to_f
else
  maximum = 50.0
end

if ( $OPT_min )
  minimum = $OPT_min.to_f
else
  minimum = -50.0
end

if ( $OPT_zlev )
  zlev = $OPT_zlev.to_f
else
  zlev = 300000.0
end

if ( $OPT_noinfo )
  infosw = false
else
  infosw = true
end

p infosw


### $BCM$N<hF@(B
###

var  = "VelX"
VelX = GPhys::IO.open(ARGV[0],var).cut('z'=>0..zlev)


###
### $B?^$N:n@.(B
###

DCL::swpset('IHEIGHT', 400 )
DCL::swpset('IWIDTH',  800 )
#DCL.sgscmn(3)
#DCL.sgscmn(5)

if ( $OPT_dump )
  DCL.gropn( 2 )
else
  DCL.gropn( 4 )
end


DCL.sgpset('lfull',true) 
DCL.sgpset('lfprop',true) 
DCL.sglset('lclip',true)
DCL.sgpset('lcntl', false) 
DCL.udpset('lmsg',false)

GGraph.set_fig('viewport'=>[0.1,0.85,0.1,0.4])

DCL.uzfact(0.8)
#DCL.sldiv('y',2,2)   
GGraph.set_axes('ytitle'=>'Height','xtitle'=>'')
GGraph.set_axes('xunits'=>'km','yunits'=>'km')
GGraph.set_fig( 'itr'=> 1)


for i in 0..num
  time = time0 + i * deltime
  p time
  
  #$B?eJ?B.EY(B
  GGraph.tone( VelX.cut('t'=>time), 
               true, 
               'annotate'=>infosw,
               'levels' =>[-1000,minimum],
               'pat'=>[14999]
               )
  GGraph.tone( VelX.cut('t'=>time), 
               false, 
               'annotate'=>infosw,
               'levels' =>[maximum,1000],
               'pat'=>[96999]
               )
  GGraph.tone( VelX.cut('t'=>time), 
               false, 
               'title' => 'Horizontal Velocity',
               'annotate'=>infosw,
               'nlev' => 50,
               'max' => maximum,
               'min' => minimum
               )
  GGraph.color_bar('vlength'=>0.275)
#  GGraph.color_bar( 'landscape' => true )
#  GGraph.color_bar

end

DCL.grcls


if ( $OPT_dump )
  /(.*)\.nc/ =~ ARGV[0]
  info = $1
  convert = '~sugiyama/work/deepconv/arare4/tools/dcmodel-dclps2png.rb'
  order = Math::log10(time0+deltime*num).to_i + 1 
  fmt = "%0"+(order.to_i).to_s+"d"  

  for i in 0..num
    time = sprintf(fmt, time0 + i * deltime)
    j = i + 1	
    
    pngfile = info+"_t"+time.to_s+".png"
#    epsfile = info+"_t"+time.to_s+".eps"
#    tiffile = info+"_t"+time.to_s+".tif"
#    bmpfile = info+"_t"+time.to_s+".bmp"
    output1 = 'psselect '+j.to_s+' dcl.ps > dcl2.ps' 
#    output2 = convert+" dcl2.ps "+pngfile+" --eps "+epsfile+" --tif "+tiffile+" --bmp "+bmpfile
    output2 = convert+" dcl2.ps "+pngfile

    system( output1 )
    system( output2 )

  end
end

