#!/usr/bin/env ruby

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

###
###引数処理
###
parser = GetoptLong.new
parser.set_options(
                   ###    global option   ###
                   ['--delt',         GetoptLong::REQUIRED_ARGUMENT],
                   ['--dump',         GetoptLong::NO_ARGUMENT]
                   )
begin
  parser.each_option do |name, arg|
    eval "$OPT_#{name.sub(/^--/, '').gsub(/-/, '_')} = '#{arg}'" 
  end
rescue
  exit(1)
end

###
### 初期化
###

deltime = $OPT_delt.to_f
color   = [12, 22, 32, 42, 62, 92]
DayTime = 86400.0

###
### 値の取得
###

#gpview --overplot 3  $2@Stab,t=1e6  $2@StabTemp,t=1e6 $2@StabMolWt,t=1e6  --mean x --wsn 2 --exch  --aspect 1 --range 0:3e-5

var      = "Stab"
Stab     = GPhys::IO.open( ARGV[1], var )

var      = "StabTemp"
StabTemp = GPhys::IO.open( ARGV[1], var )

var      = "StabMolWt"
StabMolWt= GPhys::IO.open( ARGV[1], var )


###
### 図の作成
###

DCL::swpset('IHEIGHT', 600 )
DCL::swpset('IWIDTH',  600 )
#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) 

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

DCL.uzfact(0.75)
#DCL.sldiv('y',2,1)   
GGraph.set_axes('ytitle'=>'','xtitle'=>'')


GGraph.set_fig( 'itr'=> 1)
for i in 0..20
  time = i * deltime
  p time

  #全体
  GGraph.line( Stab.cut('t'=>time).cut('z'=>0..300000).mean('x'), 
               true, 
               'exchange'=>true,
               'title' => 'Static Stability',
               'annotate'=>false,
               'index'=> 11,
               'type' => 1 ,
               'max'=> 3e-5,
               'min'=> 0
               )
  #温度の効果(緑線)
  GGraph.line( StabTemp.cut('t'=>time).mean('x'), 
               false, 
               'exchange'=>true,
               'index'=> 32,
               'type' => 1
               )
  #分子量効果(緑線)	 	
  GGraph.line( StabMolWt.cut('t'=>time).mean('x'), 
               false, 
               'exchange'=>true,
               'index'=> 42,
               'type' => 1
               )
end	

DCL.grcls


if ( $OPT_dump )

  for i in 0..20
    time = i * deltime

    output = "~/work/deepconv/arare4/tools/dcmodel-dclps2png.rb dcl2.ps "+ARGV[2]+"_Stab_t"+i.to_s+".png"

    num = i + 1
    output2 = "psselect "+num.to_s+" dcl.ps > dcl2.ps"
    system( output2 )
    system( output )
  end
  
end

