# -*- coding: euc-jp -*-
require "numru/ggraph"
include NumRu

timestep  = ARGV[0].to_i

x1 = 0
x2 = 256000
z1 = 0
#z2 = 24000
#z2 = 30000
z2 = 48000
dz = 300
t1 = 0
t2 = 86400 * 50
#t2 = 86400 * 30
dt = 1800
deltime = 3600 * 6
tn = t2 / deltime

files  = "thermal-moist_H2O-l-Rain_FallFluxAtLB.nc"
gphys1 = GPhys::IO.open(files, 'H2O-l-Rain_FallFluxAtLB')
#files  = "thermal-moist_H2O-l-Rain_FallFluxAtLB_TET_CtlPress_atSfcFlux_x256km_z48km.nc"
#gphys2 = GPhys::IO.open(files, 'H2O-l-Rain_FallFluxAtLB')

#DCL.gropn(1)
DCL.gropn(2)
#DCL.sldiv('y',1,2)
DCL.sgpset('lfull',true)
DCL.sgpset('lcntl', false)
DCL.uzfact(0.7)
#DCL.uzfact(0.3)
#GGraph.set_fig( 'viewport'=>[0.25,0.7,0.15,0.6] )
# アスペクト比をそれっぽくする
GGraph.set_fig( 'viewport'=>[0.1,0.85,0.15,0.6] )
#GGraph.set_fig( 'viewport'=>[0.25,0.8,0.10,0.275] )

#xax = gphys1.coord(0)
#zax = gphys1.coord(2)

t1 = ( timestep - 1 ) * deltime
t2 = timestep * deltime

p "##########################################"
p timestep
p t1
p t2
p "##########################################"

gphysdisp0 = gphys1
gphysdisp0 = gphysdisp0.mean( 'x' )
gphysdisp0 = gphysdisp0.mean( 'y' )
gphysdisp0 = gphysdisp0.cut( 't'=>t1..t2 )
#gphysdisp0 = gphysdisp0.cut( 't'=>0..t2 )
#gphysdisp0 = gphysdisp0 * dt
#gphysdisp0 = gphysdisp0.sum( 't' )
gphysdisp1  = gphysdisp0

print "\n Total of H2O-l-Rain_FallFlux at Lower Boundary (no-TET) :\n"
p gphysdisp1
p ( gphysdisp1 * dt ).sum( 't' )
#p gphysdisp.val

#gphysdisp0 = gphys2
#gphysdisp0 = gphysdisp0.mean( 'x' )
#gphysdisp0 = gphysdisp0.mean( 'y' )
#gphysdisp0 = gphysdisp0.cut( 't'=>0..t2 )
##gphysdisp0 = gphysdisp0 * dt
##gphysdisp0 = gphysdisp0.sum( 't' )
#gphysdisp2  = gphysdisp0
#
#print "\n Total of H2O-l-Rain_FallFlux at Lower Boundary (TET) :\n"
#p gphysdisp2
#p ( gphysdisp2 * dt ).sum( 't' )
##p gphysdisp.val
#p "--------------------------------"

#GGraph.line( gphysdisp, true )
#GGraph.line( gphysdisp, true, 'min' => -9.0e-4, 'max' => 0.0, 'type'=>1 )
GGraph.line( gphysdisp1, true, 'min' => -9.0e-4, 'max' => 0.0, 'type'=>1 )
#GGraph.line( gphysdisp2, false, 'min' => -9.0e-4, 'max' => 0.0, 'type'=>2 )
#GGraph.line( gphysdisp, true )
#GGraph.tone( gphysdisp, true, 'transpose'=>true )
#GGraph.tone( gphysdisp, true, 'transpose'=>true, 
#            'lev'=>[0,0.5e-3,1.0e-3,1.5e-3,2.0e-3,2.5e-3,3.0e-3,3.5e-3,4.0e-3,4.5e-3,5.0e-3,5.5e-3,6.0e-3,6.5e-3,7.0e-3],
#             # レベル＆パターンを陽に指定
#             'pat'=>[10999,12999,15999,20999,25999,30999,35999,40999,55999,65999,70999,75999,80999,90999,95999]
#             'lev'=>[   0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60,  0.65,  0.70, 0.75, 0.80, 0.85,  0.90],
#             'pat'=>[10999,15999,20999,25999,30999,35999,40999,45999,50999,67999,70999,80999,85999,90999]
#)
#GGraph.color_bar
#GGraph.color_bar( 'vlength'=>0.07, 'inffact'=>0.5 )
#GGraph.color_bar( 'vlength'=>0.12, 'inffact'=>1.0 )

DCL.grcls

exit


#-- NetCDF ファイルへの書き込み --#

t_f = gphys.val

  #-- 軸の設定 --#
t_a = VArray.new( NArray.sfloat(tn+1).indgen(0,t2/tn),
                  {"long_name"=>"time","units"=>"sec"},
                    "t" )
t = Axis.new.set_pos(t_a)

  #-- データ用の配列を準備 --#
data = VArray.new( NArray.sfloat(tn+1),
                   {"long_name"=>"Integral of H2O-l-Rain_FallFluxAtLB in z", "units"=>"Kg.m-2.s-1"},
                   "Int-H2O-l-Rain_FallFluxAtLB" )
t_f_gphys = GPhys.new( Grid.new(t), data )

  #-- 値を配列に入れる --#
t_f_gphys[0..tn] = t_f[0..tn]

  #-- テスト出力 --#
p "-----------------------------"
p t_f
p t_f_gphys

  #-- netCDF ファイルの生成とファイルへの書き出し --#
outfile = NetCDF.create("thermal-moist_Int-H2O-l-Rain_FallFluxAtLB.nc")
GPhys::NetCDF_IO.write(outfile,t_f_gphys)

  #-- ファイルクローズ --#
outfile.close
