#!/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],
                   ['--spc',          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 = 2.0
end

if ( $OPT_min )
  minimum = $OPT_min.to_f
else
  minimum = -2.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
###

/.*_(.*)\.nc/ =~ ARGV[0]
MixRt = GPhys::IO.open(ARGV[0], $1).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:.9gHf(B
  GGraph.tone( MixRt.cut('t'=>time), 
               true, 
               'title' => $1+' Mixing Ratio [kg/kg]', 
               'annotate' => infosw,
               'levels'=>[1e-8,2e-8,5e-8,1e-7,2e-7,5e-7,1e-6,2e-6,5e-6,1e-5,2e-5,5e-5,1e-4,2e-4,5e-4,1e-3,2e-3,5e-3,1e-2]
               )	

  GGraph.color_bar( 'constwidth' => true, 
                    'vlength'=>0.275
                    )

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	
    p time

    pngfile  = info+"_t"+time.to_s+".png"
    psselect = 'psselect '+j.to_s+' dcl.ps > dcl1.ps' 
    output   = convert+" dcl1.ps "+pngfile

    system( psselect )
    system( output )
  end
end
