#!/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   ###
                   ['--num',         GetoptLong::REQUIRED_ARGUMENT],
                   ['--range',       GetoptLong::REQUIRED_ARGUMENT],
                   ['--aspect',      GetoptLong::REQUIRED_ARGUMENT],
                   ['--int',         GetoptLong::REQUIRED_ARGUMENT],
                   ['--T',           GetoptLong::REQUIRED_ARGUMENT],
                   ['--X',           GetoptLong::REQUIRED_ARGUMENT],
                   ['--dump',         GetoptLong::NO_ARGUMENT],
                   ['--nocont',      GetoptLong::NO_ARGUMENT]
                   )
begin
  parser.each_option do |name, arg|
    eval "$OPT_#{name.sub(/^--/, '').gsub(/-/, '_')} = '#{arg}'" 
  end
rescue
  exit(1)
end

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

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

var    = "TempBasicZ"
gphysT0 = GPhys::IO.open( ARGV[0], var )

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

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

var    = "MixRt"
gphys1 = GPhys::IO.open( ARGV[0], var )

var    = "MixRtBasicZ"
gphys2 = GPhys::IO.open( ARGV[0], var )


var    = "PotTemp"
gphysA1 = GPhys::IO.open( ARGV[0], var )

var    = "PotTemp"
gphysA2 = GPhys::IO.open( ARGV[0], var )

var    = "PotTempBasicZ"
gphysA3 = GPhys::IO.open( ARGV[0], var )



time = $OPT_T.to_f
x    = $OPT_X.to_f

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) 


#DCL.sldiv('y',2,1)           # 2x1に画面分割, 'y'=yoko: 左上→右上→左下...
DCL.uzfact(0.75)             # 座標軸の文字列サイズを 0.75 倍

GGraph.set_fig( 'itr'=> 3)
GGraph.set_fig('viewport'=>[0.1,0.8,0.1,0.8])

gphysA1 = 0.0
gphysA2 = 0.0
gphysA3 = 0.0

gphysA1 = gphysAll.cut('s'=>1) + gphysAll.cut('s'=>4) + gphysAll.cut('s'=>5) 
gphysA2 = gphysAll.cut('s'=>2) + gphysAll.cut('s'=>3) + gphysAll.cut('s'=>6) + gphysAll.cut('s'=>7) + gphysAll.cut('s'=>8) + gphysAll.cut('s'=>9) 
gphysA3 = gphys2.cut('s'=>2) + gphys2.cut('s'=>3)



GGraph.line( gphysAll.cut('t'=>$OPT_T.to_f).cut('s'=>1).mean('x'), 
             true, 
	     'exchange'=>true,
             'title' => 'H2O Mixing Ratio',
	     'annotate'=>false,
             'max'=> 0.015,
             'min'=> 1.0e-10,	
             'index'=> 23,
             'type' => 1
            )
GGraph.line( gphys2.cut('s'=>1).mean('x'), 
             false, 
	     'exchange'=>true,
             'index'=> 11,
             'type' => 3
            )
GGraph.line( gphysSat.cut('t'=>$OPT_T.to_f).cut('s'=>1).mean('x'), 
             false, 
	     'exchange'=>true,
             'index'=> 31,
             'type' => 3
            )


GGraph.set_fig( 'itr'=> 1)
GGraph.line( gphysAll.cut('t'=>$OPT_T.to_f).cut('s'=>1).mean('x'), 
             true, 
	     'exchange'=>true,
             'title' => 'H2O Mixing Ratio',
	     'annotate'=>false,
             'max'=> 0.015,
             'index'=> 23,
             'type' => 1
            )
GGraph.line( gphys2.cut('s'=>1).mean('x'), 
             false, 
	     'exchange'=>true,
             'index'=> 11,
             'type' => 3
            )
GGraph.line( gphysSat.cut('t'=>$OPT_T.to_f).cut('s'=>1).mean('x'), 
             false, 
	     'exchange'=>true,
             'index'=> 31,
             'type' => 3
            )

GGraph.set_fig( 'itr'=> 1)
GGraph.line( gphysAll.cut('t'=>$OPT_T.to_f).cut('s'=>2).mean('x'), 
             true, 
	     'exchange'=>true,
             'title' => 'NH3 Mixing Ratio',
	     'annotate'=>false,
             'max'=> 0.004,
             'index'=> 23,
             'type' => 1
            )
GGraph.line( gphys2.cut('s'=>2).mean('x'), 
             false, 
	     'exchange'=>true,
             'index'=> 11,
             'type' => 3
            )
GGraph.line( gphysSat.cut('t'=>$OPT_T.to_f).cut('s'=>2).mean('x'), 
             false, 
	     'exchange'=>true,
             'index'=> 31,
             'type' => 3
            )

GGraph.set_fig( 'itr'=> 1)
GGraph.line( gphysAll.cut('t'=>$OPT_T.to_f).cut('s'=>3).mean('x'), 
             true, 
	     'exchange'=>true,
             'title' => 'H2S Mixing Ratio',
	     'annotate'=>false,
             'max'=> 0.0012,
             'index'=> 23,
             'type' => 1
            )
GGraph.line( gphys2.cut('s'=>3).mean('x'), 
             false, 
	     'exchange'=>true,
             'index'=> 11,
             'type' => 3
            )


GGraph.line( gphysT.cut('t'=>$OPT_T.to_f).mean('x'), 
             true, 
	     'exchange'=>true,
             'title' => 'Temperature',
	     'annotate'=>false,
             'index'=> 23,
             'type' => 1
            )
GGraph.line( gphysT0.mean('x'), 
             false, 
	     'exchange'=>true,
             'title' => 'Temperature',
	     'annotate'=>false,
             'index'=> 11,
             'type' => 3
            )




GGraph.line( gphysA1.cut('t'=>$OPT_T.to_f).mean('x'), 
             true, 
	     'exchange'=>true,
             'title' => 'All H20 Mixing Ratio',
	     'annotate'=>false,
	     'max' => 0.018,
             'index'=> 23,
             'type' => 1
            )
GGraph.line( gphys2.cut('s'=>1).mean('x'), 
             false, 
	     'exchange'=>true,
             'index'=> 11,
             'type' => 3
            )

GGraph.line( gphysA2.cut('t'=>$OPT_T.to_f).mean('x'), 
             true, 
	     'exchange'=>true,
             'title' => 'All NH3 + H2S Mixing Ratio',
	     'annotate'=>false,
	     'max'=> 0.01,
             'index'=> 23,
             'type' => 1
            )
#GGraph.line( gphys2.cut('s'=>2).mean('x'), 
GGraph.line( gphysA3.mean('x'), 
             false, 
	     'exchange'=>true,
             'index'=> 11,
             'type' => 3
            )



DCL.grcls
