#!/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      = "MixRtAll"
MixRtAll = GPhys::IO.open( ARGV[1], var )

var         = "MixRtBasicZ"
MixRtBasicZ = GPhys::IO.open( ARGV[0], 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 s in 1..3
	GGraph.line( MixRtBasicZ.cut('z'=>0..300000).cut('s'=>s).mean('x'), 
             true, 
	     'exchange'=>true,
             'title' => 'Mixing Ratio',
	     'annotate'=>false,
             'index'=> 152,
             'type' => 1 
#	     'max'=> ,
#	     'min'=> 0
            )
	for i in 1..5
		GGraph.line( MixRtAll.cut('t'=>i*deltime*2.0).mean('x').cut('s'=>s), 
	             false, 
		     'exchange'=>true,
	             'index'=> color[i], 
        	     'type' => 1
	            )	
	end		
end	


GGraph.set_fig( 'itr'=> 3)
for s in 1..3
	GGraph.line( MixRtBasicZ.cut('z'=>0..300000).cut('s'=>s).mean('x'), 
             true, 
	     'exchange'=>true,
             'title' => 'Mixing Ratio',
	     'annotate'=>false,
             'index'=> 152,
             'type' => 1 
#	     'max'=> ,
#	     'min'=> 0
            )
	for i in 1..5
		GGraph.line( MixRtAll.cut('t'=>i*deltime*2.0).mean('x').cut('s'=>s), 
	             false, 
		     'exchange'=>true,
	             'index'=> color[i], 
        	     'type' => 1
	            )	
	end		
end	


DCL.grcls


if ( $OPT_dump )

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

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

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

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

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

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

end	


