#!/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

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

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

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

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

var        = "PotTempAdv"
PotTempAdv = GPhys::IO.open( ARGV[0], var ) * DayTime

var        = "PotTempRad"
PotTempRad = GPhys::IO.open( ARGV[0], var ) * DayTime

var         = "PotTempCond"
PotTempCond = GPhys::IO.open( ARGV[0], var ) * DayTime

var         = "PotTempTurb"
PotTempTurb = GPhys::IO.open( ARGV[0], var ) * DayTime

var         = "PotTempDisp"
PotTempDisp = GPhys::IO.open( ARGV[0], var ) * DayTime

var         = "PotTempDiff"
PotTempDiff = GPhys::IO.open( ARGV[0], var ) * DayTime

var         = "PotTempDamp"
PotTempDamp = GPhys::IO.open( ARGV[0], var ) * DayTime

PotTempTend = PotTempAdv + PotTempRad + PotTempCond + PotTempTurb + PotTempDisp + PotTempDiff + PotTempDamp


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

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'=> 3)
GGraph.line( TempAll.cut('t'=>0).mean('x').cut('z'=>0..300000), 
             true, 
	     'exchange'=>true,
             'title' => 'potential temperature',
	     'annotate'=>false,
             'index'=> 1,
             'type' => 1,
	     'max'=> 470,
	     'min'=> 100
            )
for i in 1..5
	GGraph.line( TempAll.cut('t'=>i * deltime * 2.0).mean('x'), 
             false, 
	     'exchange'=>true,
             'index'=> color[i],
             'type' => 1
            )
end	


GGraph.set_fig( 'itr'=> 1)
GGraph.line( PotTempAll.cut('t'=>0).mean('x').cut('z'=>0..300000), 
             true, 
	     'exchange'=>true,
             'title' => 'potential temperature',
	     'annotate'=>false,
             'index'=> 1,
             'type' => 1,
	     'max'=> 465,
	     'min'=> 455
            )
for i in 1..5
	GGraph.line( PotTempAll.cut('t'=>i * deltime * 2.0).mean('x'), 
             false, 
	     'exchange'=>true,
             'index'=> color[i], 
             'type' => 1
            )
end	

GGraph.set_fig( 'itr'=> 1)
GGraph.line( VPotTempAll.cut('t'=>0).mean('x').cut('z'=>0..300000), 
             true, 
	     'exchange'=>true,
             'title' => 'potential temperature',
	     'annotate'=>false,
             'index'=> 152,
             'type' => 1,
	     'max'=> 465,
	     'min'=> 455
            )
for i in 1..5
	GGraph.line( VPotTempAll.cut('t'=>i * deltime * 2.0).mean('x'), 
             false, 
	     'exchange'=>true,
             'index'=> color[i], 
             'type' => 1
            )
end	


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

	#全体
	GGraph.line( PotTempTend.cut('t'=>time).cut('z'=>0..300000), 
             true, 
	     'exchange'=>true,
             'title' => 'potential temperature tendency',
	     'annotate'=>false,
             'index'=> 11,
             'type' => 1 ,
	     'max'=> 20,
	     'min'=> -20
            )
	#移流項(赤線)
	GGraph.line( PotTempAdv.cut('t'=>time), 
             false, 
	     'exchange'=>true,
             'index'=> 22,
             'type' => 1
            )
	#放射(緑線)	 	
	GGraph.line( PotTempRad.cut('t'=>time), 
             false, 
	     'exchange'=>true,
             'index'=> 32,
             'type' => 1
            )
	#凝縮加熱(青)
	GGraph.line( PotTempCond.cut('t'=>time), 
             false, 
	     'exchange'=>true,
             'index'=> 42,
             'type' => 1
            )
	#散逸加熱(シアン)
	GGraph.line( PotTempDisp.cut('t'=>time), 
             false, 
	     'exchange'=>true,
             'index'=> 92,
             'type' => 1
            )
	#乱流拡散 + 数値拡散 (ピンク)
	GGraph.line( PotTempTurb.cut('t'=>time) + PotTempDiff.cut('t'=>time), 
             false, 
	     'exchange'=>true,
             'index'=> 62,
             'type' => 1
            )
#	#拡散(数値拡散)
#	GGraph.line( PotTempDiff.cut('t'=>time), 
#             false, 
#	     'exchange'=>true,
#             'index'=> 72,
#             'type' => 1
#            )
#	# 
#	GGraph.line( PotTempDamp.cut('t'=>time), 
#             false, 
#	     'exchange'=>true,
#             'index'=> 52,
#             'type' => 1
#            )
end	

DCL.grcls


if ( $OPT_dump )

for i in 1..10
	time = i * deltime
	p time.class
	output = "~/work/deepconv/arare4/tools/dcmodel-dclps2png.rb dcl2.ps "+ARGV[2]+"_PotTempTendency_t"+i.to_s+".png"
	p output
	num = i + 3
	output2 = "psselect "+num.to_s+" dcl.ps > dcl2.ps"
	system( output2 )
	system( output )
end
	output = "~/work/deepconv/arare4/tools/dcmodel-dclps2png.rb dcl2.ps "+ARGV[2]+"_TempAll_Mean.png"
	p output
	system( "psselect 1 dcl.ps > dcl2.ps")
	system( output )

	output = "~/work/deepconv/arare4/tools/dcmodel-dclps2png.rb dcl2.ps "+ARGV[2]+"_PotTempAll_Mean.png"
	p output
	system( "psselect 2 dcl.ps > dcl2.ps")
	system( output )

	output = "~/work/deepconv/arare4/tools/dcmodel-dclps2png.rb dcl2.ps "+ARGV[2]+"_VPotTempAll_Mean.png"
	p output
	system( "psselect 3 dcl.ps > dcl2.ps")
	system( output )
end	


