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

#time  = ARGV[0].to_i
tn = ARGV[0].to_i
dt = ARGV[1].to_i
time = tn * dt

#x1 = 0
x1 = 250
#x2 = 500000
##x2 = 500 * 1000 - 500 * 10
x2 = 124750                       # エラーはいたので変えてみる
#z1 = 0
z1 = 100
#z2 = 30000
z2 = 29900                        # エラーはいたので変えてみる


#files = 'nccomb-restart/thermal-moist_restart.nc'
files = '../../netCDF/thermal-moist_restart.nc'
gphys10 = GPhys::IO.open(files, 'PTempBZ')
#files = /thermal-moist_PTemp_rank(\d\d\d\d\d\d).nc/
files = '../../netCDF/PTemp.nc'
gphys1  = GPhys::IO.open(files, 'PTemp')

#files = 'nccomb-restart/thermal-moist_restart.nc'
files = '../../netCDF/thermal-moist_restart.nc'
gphys20 = GPhys::IO.open(files, 'QMixBZ')
gphys20 = gphys20.cut( 's'=>1 )
#files = /thermal-moist_H2O-g_rank(\d\d\d\d\d\d).nc/
files = '../../netCDF/H2O-g.nc'
gphys2  = GPhys::IO.open(files, 'H2O-g')


###---- ひとまず二つでかくからコメントアウトしとく ---###

##files = 'nccomb-restart/thermal-moist_restart.nc'
#files = '../../netCDF/thermal-moist_restart.nc'
#gphys30 = GPhys::IO.open(files, 'QMixBZ')
#gphys30 = gphys30.cut( 's'=>2 )
#files = /thermal-moist_H2O-l-Cloud_rank(\d\d\d\d\d\d).nc/
#gphys3  = GPhys::IO.open(files, 'H2O-l-Cloud')

##files = 'nccomb-restart/thermal-moist_restart.nc'
#files = '../../netCDF/thermal-moist_restart.nc'
#gphys40 = GPhys::IO.open(files, 'QMixBZ')
#gphys40 = gphys40.cut( 's'=>3 )
#files = /thermal-moist_H2O-l-Rain_rank(\d\d\d\d\d\d).nc/
#gphys4  = GPhys::IO.open(files, 'H2O-l-Rain')

###---------------------------------------------------###

#DCL.gropn(1)
#DCL.gropn(2)
DCL.gropn(4)
#DCL.sldiv('y',1,4)
DCL.sldiv('y',1,2)
DCL.sgpset('lfull',true)
DCL.sgpset('lcntl', false)
DCL.uzfact(0.3)
#GGraph.set_fig( 'viewport'=>[0.25,0.7,0.15,0.6] )
GGraph.set_fig( 'viewport'=>[0.25,0.8,0.15,0.3] )

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

#vpy1 = 0.075
#vpy2 = 0.2
vpy1 = 0.05
vpy2 = 0.14

gphysdisp0 = gphys10.cut( 'x'=>x1..x2 ).cut( 'y'=>100 ).cut( 'z'=>z1..z2 ) + gphys1.cut( 't'=>time ).cut( 'x'=>x1..x2 ).cut( 'y'=>100 ).cut( 'z'=>z1..z2 )
#gphysdisp0 = gphysdisp0.mean( 'y' )

#gphysdispd = gphysdisp0

#gphysdispq = gphys20.cut( 'x'=>x1..x2 ).cut( 'z'=>z1..z2 ) + gphys2.cut( 't'=>time )
#gphysdispq = gphysdispq.mean( 'y' )
#gphysdisp0 = gphysdisp0 * ( 1.0 + ( 1.0 / 0.622 - 1.0 ) * gphysdispq )

#gphysdisp1 = gphysdisp0.copy
#gphysdisp0[true,1..-2] = ( gphysdisp1[true,2..-1] - gphysdisp1[true,0..-3] ) / ( 200.0 * 2 ) * 9.8 / gphysdisp1[true,1..-2]
#gphysdisp0[true,0] = ( gphysdisp1[true,1] - gphysdisp1[true,0] ) / ( 200.0 * 1 ) * 9.8 / gphysdisp1[true,0]
#gphysdisp0[true,-1] = ( gphysdisp1[true,-1] - gphysdisp1[true,-2] ) / ( 200.0 * 1 ) * 9.8 / gphysdisp1[true,-1]
#gphysdisp0.long_name = 'Static stability'
#gphysdisp0.units = 's-2'

#gphysdisp1 = gphysdispd.copy
#gphysdispd[true,1..-2] = ( gphysdisp1[true,2..-1] - gphysdisp1[true,0..-3] ) / ( 200.0 * 2 ) * 9.8 / gphysdisp1[true,1..-2]
#gphysdispd[true,0] = ( gphysdisp1[true,1] - gphysdisp1[true,0] ) / ( 200.0 * 1 ) * 9.8 / gphysdisp1[true,0]
#gphysdispd[true,-1] = ( gphysdisp1[true,-1] - gphysdisp1[true,-2] ) / ( 200.0 * 1 ) * 9.8 / gphysdisp1[true,-1]
#gphysdispd.long_name = 'Static stability'
gphysdisp0.long_name = 'Potential Temperature'
#gphysdispd.units = 's-2'
gphysdisp0.units = 'K'

#gphysdisp = gphysdisp0.mean( 'x' )
#gphysdisp.long_name = ''
##GGraph.next_fig( 'viewport'=>[0.1,0.2,vpy1,vpy2], 'window'=>[280,450,z1,z2] )
#GGraph.next_fig( 'viewport'=>[0.1,0.2,vpy1,vpy2], 'window'=>[-1e-3,1e-3,z1,z2] )
#GGraph.line( gphysdisp, true, 'exchange'=>true, 'annotate'=>false, 'title'=>'' )

#gphysdisp = gphysdispd.mean( 'x' )
#gphysdisp.long_name = ''
#GGraph.line( gphysdisp, false, 'exchange'=>true, 'annotate'=>false, 'title'=>'', 'type'=>2 )

gphysdisp = gphysdisp0
GGraph.fig( xax, zax, 'new_frame'=>false, 
            'viewport'=>[0.25,0.9,vpy1,vpy2], 'window'=>[x1,x2,z1,z2] )
#GGraph.tone( gphysdisp, false, 
#             'lev'=>[260,280,300,320,340,360,380,400,420,440,460],
#             # レベル＆パターンを陽に指定
#             'pat'=>[10999,15999,20999,25999,30999,40999,50999,60999,65999,70999,80999,90999]
#)
GGraph.set_tone_levels( 
             'lev'=>[300.0,350.0,400.0,450.0,500.0,550.0,600.0,650.0,700.0,750.0,800.0],
             'pat'=>[10999,15999,20999,25999,30999,40999,50999,65999,70999,80999,90999] )
GGraph.tone( gphysdisp )

##GGraph.tone( gphysdisp, false, 
##             'lev'=>[-10e-5,-8e-5,-6e-5,-4e-5,-2e-5,0,2e-5,4e-5,6e-5,8e-5,10e-5],
##             'lev'=>[0,1e-4,2e-4,3e-4,4e-4,5e-4,6e-4,7e-4,8e-4,9e-4,10e-4,11e-4],
#             'lev'=>[0,1e-4,2e-4,3e-4,4e-4,5e-4,6e-4,7e-4,8e-4,9e-4,10e-4,11e-4],
#             # レベル＆パターンを陽に指定
#             'pat'=>[1,10999,15999,20999,25999,30999,40999,50999,60999,65999,70999,80999,90999]
#)
#GGraph.axes( xax, zax )
#GGraph.title( gphysdisp.long_name )
GGraph.color_bar( 'vlength'=>0.07, 'inffact'=>0.5 )

GGraph.annotate( [ ( time / 3600.0 ).to_s + ' hours' ] )

#-----

gphysdisp0 = gphys20.cut( 'x'=>x1..x2 ).cut( 'y'=>100 ).cut( 'z'=>z1..z2 ) + gphys2.cut( 't'=>time ).cut( 'x'=>x1..x2 ).cut( 'y'=>100 ).cut( 'z'=>z1..z2 )
#gphysdisp0 = gphysdisp0.mean( 'y' )
gphysdisp0.long_name = 'Water vapor mixing ratio'

#gphysdisp = gphysdisp0.mean( 'x' )
#gphysdisp.long_name = ''
#GGraph.next_fig( 'viewport'=>[0.1,0.2,vpy1,vpy2], 'window'=>[0,20e-3,z1,z2] )
#GGraph.line( gphysdisp, true, 'exchange'=>true, 'annotate'=>false, 'title'=>'' )

gphysdisp = gphysdisp0
GGraph.fig( xax, zax, 'new_frame'=>false, 
            'viewport'=>[0.25,0.9,vpy1,vpy2], 'window'=>[x1,x2,z1,z2] )
#GGraph.tone( gphysdisp, false,
#             'lev'=>[0,2e-3,4e-3,6e-3,8e-3,10e-3,12e-3,14e-3,16e-3,18e-3,20e-3],
#             # レベル＆パターンを陽に指定
#             'pat'=>[1,10999,20999,30999,40999,50999,60999,65999,70999,80999,90999]
#)
GGraph.set_tone_levels(
   'lev'=>[0,0.000001,0.00015,0.00030,0.00045,0.00060,0.00075,0.00090,0.00105,0.00120,0.00135,0.0015],
   'pat'=>[1,10999,15999,20999,25999,30999,40999,50999,65999,70999,80999,90999] )

GGraph.tone( gphysdisp )

#GGraph.axes( xax, zax )
#GGraph.title( gphysdisp.long_name )
GGraph.color_bar( 'vlength'=>0.07, 'inffact'=>0.5 )

#-----

#
#gphysdisp0 = gphys30.cut( 'x'=>x1..x2 ).cut( 'z'=>z1..z2 ) + gphys3.cut( 't'=>time )
#gphysdisp0 = gphysdisp0.mean( 'y' )
#gphysdisp0.long_name = 'Cloud mixing ratio'
#
#gphysdisp = gphysdisp0.mean( 'x' )
#gphysdisp.long_name = ''
#GGraph.next_fig( 'viewport'=>[0.1,0.2,vpy1,vpy2], 'window'=>[0,10e-4,z1,z2] )
#GGraph.line( gphysdisp, true, 'exchange'=>true, 'annotate'=>false, 'title'=>'' )
#
#gphysdisp = gphysdisp0
#GGraph.fig( xax, zax, 'new_frame'=>false, 
#            'viewport'=>[0.25,0.9,vpy1,vpy2], 'window'=>[x1,x2,z1,z2] )
#GGraph.tone( gphysdisp, false, 
#             'lev'=>[0,1e-4,2e-4,3e-4,4e-4,5e-4,6e-4,7e-4,8e-4,9e-4,10e-4,11e-4,12e-4,13e-4,14e-4],
#             # レベル＆パターンを陽に指定
#             'pat'=>[1,10999,15999,20999,25999,30999,35999,40999,45999,50999,60999,65999,70999,80999,90999]
#)
#GGraph.axes( xax, zax )
#GGraph.title( gphysdisp.long_name )
#GGraph.color_bar( 'vlength'=>0.07, 'inffact'=>0.5 )
#

#-----

#
#gphysdisp0 = gphys40.cut( 'x'=>x1..x2 ).cut( 'z'=>z1..z2 ) + gphys4.cut( 't'=>time )
#gphysdisp0 = gphysdisp0.mean( 'y' )
#gphysdisp0.long_name = 'Rain mixing ratio'
#
#gphysdisp = gphysdisp0.mean( 'x' )
#gphysdisp.long_name = ''
#GGraph.next_fig( 'viewport'=>[0.1,0.2,vpy1,vpy2], 'window'=>[0,10e-4,z1,z2] )
#GGraph.line( gphysdisp, true, 'exchange'=>true, 'annotate'=>false, 'title'=>'' )
#
#gphysdisp = gphysdisp0
#GGraph.fig( xax, zax, 'new_frame'=>false, 
#            'viewport'=>[0.25,0.9,vpy1,vpy2], 'window'=>[x1,x2,z1,z2] )
#GGraph.tone( gphysdisp, false, 
##             'lev'=>[0,1e-4,2e-4,3e-4,4e-4,5e-4,6e-4,7e-4,8e-4,9e-4,10e-4,11e-4,12e-4,13e-4,14e-4],
#             'lev'=>[0,2e-4,4e-4,6e-4,8e-4,10e-4,12e-4,14e-4,16e-4,18e-4,20e-4,22e-4,24e-4,26e-4,28e-4],
#             # レベル＆パターンを陽に指定
#             'pat'=>[1,10999,15999,20999,25999,30999,35999,40999,45999,50999,60999,65999,70999,80999,90999]
#)
#GGraph.axes( xax, zax )
#GGraph.title( gphysdisp.long_name )
#GGraph.color_bar( 'vlength'=>0.07, 'inffact'=>0.5 )
#


DCL.grcls


