#!/usr/bin/env ruby
=begin

=draw_thetatend.rb

  * $B?GCGNL=PNO%U%!%$%k$+$i290L$N<0$N3F9`(B($B0J2<;2>H(B)$B$N4sM?$rIA$/(B
    * $B0\N.9`(B
    * $BMpN.3H;69`(B($BCOI=LL%U%i%C%/%94^$`(B, $B?tCM3H;6$O4^$a$J$$(B)
    * $BJ|<MNd5Q9`(B
    * $B;60o2CG.9`(B
    * $B0J>e$N9g7W(B

== USAGE

  draw_thetatend.rb file

  * ARGUMENT
    * file: $B=PNO(B NetCDF $B%U%!%$%k(B($B>qMp>l%U%!%$%k(B)

=end

require "numru/ggraph"
require "numru/dcl"

include NumRu

Starttime = 82600     
Endtime = 86400

#file = ARGV[0]  # NetCDF file
file = "./nc/arare_diag-odaka1998-dx100-20051130-2.nc"  # NetCDF file

Daytime = UNumeric.new(86400.0, "s/day")

# open file and initialize GPhys object

gp_adv = GPhys::IO.open(file, "PotTempTendAdvZ")
gp_src = GPhys::IO.open(file, "PotTempTendSrcZ")
gp_turb = GPhys::IO.open(file, "PotTempTendTurbZ")
gp_rad = GPhys::IO.open(file, "PotTempTendRadZ")
gp_dis = GPhys::IO.open(file, "PotTempTendDisZ")
gp_sfc = GPhys::IO.open(file, "PotTempTendSfcZ")

# unit convertion

gp_adv = gp_adv * Daytime
gp_src = gp_src * Daytime
gp_turb = gp_turb * Daytime
gp_rad = gp_rad * Daytime
gp_dis = gp_dis * Daytime
gp_sfc = gp_sfc * Daytime

# cut

gp_adv = gp_adv.cut('t'=>Starttime..Endtime)
gp_src = gp_src.cut('t'=>Starttime..Endtime)
gp_turb = gp_turb.cut('t'=>Starttime..Endtime)
gp_rad = gp_rad.cut('t'=>Starttime..Endtime)
gp_dis = gp_dis.cut('t'=>Starttime..Endtime)
gp_sfc = gp_sfc.cut('t'=>Starttime..Endtime)

# mean

gp_adv = gp_adv.mean('t')
gp_src = gp_src.mean('t')
gp_turb = gp_turb.mean('t')
gp_rad = gp_rad.mean('t')
gp_dis = gp_dis.mean('t')
gp_sfc = gp_sfc.mean('t')

# total tendency calculation

gp_diff = gp_turb + gp_sfc
#gp_adv =  gp_adv + gp_src
gp_total = gp_adv + gp_turb + gp_rad + gp_dis + gp_sfc + gp_src

# drawing

DCL.gropn(4)
DCL.sgpset('lcntl', false)
DCL.uzfact(0.7)
GGraph.line( gp_total, true, 'exchange'=>true, 'title'=>'POTENTIAL TEMPARETURE TENDENCY',
             'max'=>150, 'min'=>-150, 'index'=>12 )
GGraph.line( gp_adv, false, 'exchange'=>true, 'index'=>22 )
GGraph.line( gp_src, false, 'exchange'=>true, 'index'=>92 )
GGraph.line( gp_rad, false, 'exchange'=>true, 'index'=>32 )
#GGraph.line( gp_turb, false, 'exchange'=>true, 'index'=>42 )
GGraph.line( gp_dis, false, 'exchange'=>true, 'index'=>62 )
#GGraph.line( gp_sfc, false, 'exchange'=>true, 'index'=>72 )
GGraph.line( gp_diff, false, 'exchange'=>true, 'index'=>72 )
DCL.grcls
