#!/usr/bin/env ruby
=begin
= $B4%Ag@EE*%(%M%k%.!<%T!<%/CM;~7ONs:n?^%9%/%j%W%H(B($B@VF;(B, $BCf0^EY(B)

=end

require "getopts"        # for option_parse
require "numru/ggraph"
include NumRu
include Misc::EMath

## $BIA2h%a%=%C%I%a%$%s(B

def plot_line_main(gphys_array)
  
  line_index_ary = Array.new; name_ary = Array.new

  default_index = 12
  
  line_hash = { "index"=> default_index }
  line_index_ary.push(default_index)
  name_ary.push(gphys_array[0].data.get_att("long_name"))
  
  x = gphys_array[0].coord(0).val
  y = gphys_array[0].data.val
  DCL.uulinz(x, y, 1, default_index)
  gphys_array.size.times{ |num|
      unless num == 0
	line_index = (num*10 + default_index)
	line_index_ary.push(num*10 +default_index)
	name_ary.push(gphys_array[num].data.get_att("long_name"))
	x = gphys_array[num].coord(0).val
	y = gphys_array[num].data.val
	DCL.uulinz(x, y, 1, line_index)
      end
  }
  
  return name_ary, line_index_ary
  
end



##-----------------------
#  $B%i%$%s%$%s%G%C%/%9$N<oN`$rI=<((B

def __show_line_index(str_ary,line_index_ary, x=0.15, y=0.12) 
  charsize = 1.0 * DCL.uzpget('rsizec1')
  dvx = 0.01
  dvy = charsize*1.5
  raise TypeError,"Array expected" if ! str_ary.is_a?(Array)
  vxmin,vxmax,vymin,vymax = DCL.sgqvpt
  vx = x
  vy = y + charsize/2
  str_ary.size.times{|num|
    DCL::sgtxzv(vx,vy,"--- #{str_ary[num]}", 
		  charsize, 0, -1, line_index_ary[num])
      vy -= dvy
      if num == 2
	vx = 0.30
	vy = 0.12 - charsize/2
      end
  }
    nil
  end



################################################################
#                        make gphys 
################################################################
Rad = UNumeric.new(6.37E6, 'm')
G =   UNumeric.new(9.81  , 'm.s-2')

year = [1979, 2003]
month = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]   # $B=i4|CM(B 

# $B9g7W(B
dsefl_total_long_name = "Toatal dry static energy"
dsefl_total_tropical_long_name =  dsefl_total_long_name + " (S30 - N30)"
dsefl_total_nh_midlat_long_name = dsefl_total_long_name + " (N30 - N60)"
dsefl_total_sh_midlat_long_name = dsefl_total_long_name + " (S30 - S60)"
# $BBS>uJ?6Q(B
dsefl_bar_long_name = "Dry static energy contoributed by merdio. cir."
dsefl_bar_tropical_long_name =  dsefl_bar_long_name + " (S30 - N30)"
dsefl_bar_nh_midlat_long_name = dsefl_bar_long_name + " (N30 - N60)"
dsefl_bar_sh_midlat_long_name = dsefl_bar_long_name + " (S30 - S60)"
# $BBS>u>qMp(B
dsefl_dash_long_name =  "Dry static energy contoributed by zonal eddy"
dsefl_dash_tropical_long_name =  dsefl_dash_long_name + " (S30 - N30)"
dsefl_dash_nh_midlat_long_name = dsefl_dash_long_name + " (N30 - N60)"
dsefl_dash_sh_midlat_long_name = dsefl_dash_long_name + " (S30 - S60)"


## make compisit-GPhys object 
# make array which set the gphys objects.

axmonth = []
i = 0

dsefl_total_law = []; dsefl_total_nh_mid = []; dsefl_total_sh_mid = []
dsefl_bar_law = []; dsefl_bar_nh_mid = []; dsefl_bar_sh_mid = []
dsefl_dash_law = []; dsefl_dash_nh_mid = []; dsefl_dash_sh_mid = []

dsefl_unit = 'W'

year[0].upto(year[1]) do |y|
  month.each do |m|

    # NetCDFVar => GPhys 
    path_dsefl = "../../../DSEFL.NCEP/DSEFL.#{y}.NCEP/DSEFL_#{y}-#{m}_NCEP.nc" # $B9g7W(B

    gp_dsefl_total = GPhys::NetCDF_IO.open(path_dsefl, 'dsefl')
    gp_dsefl_bar   = GPhys::NetCDF_IO.open(path_dsefl, 'dsefl_bar')
    gp_dsefl_dash  = GPhys::NetCDF_IO.open(path_dsefl, 'dsefl_dash')

    # get cos
    lat     = gp_dsefl_total.coord('lat')
    grid    = Grid.new(gp_dsefl_total.axis('lat'))
    cos_lat = GPhys.new(grid, cos(lat*2*PI/360.0))
    
    # $B;~7ONs(B VArray $B:n$j(B
    ## dsefl_total
    dsefl_total_law    << ( ( ( gp_dsefl_total * cos_lat ).cut( 'lat'=> 60.. 30 ).integrate('level') /G * Rad * 2 * PI * 100).val ) 
    dsefl_total_nh_mid << ( ( ( gp_dsefl_total * cos_lat ).cut( 'lat'=> 60.. 30 ).integrate('level') /G * Rad * 2 * PI * 100).val ) 
    dsefl_total_sh_mid << ( ( ( gp_dsefl_total * cos_lat ).cut( 'lat'=>-30..-60 ).integrate('level') /G * Rad * 2 * PI * 100).val ) 
    ## dsefl_bar
    dsefl_bar_law    << ( ( ( gp_dsefl_bar * cos_lat ).cut( 'lat'=> 60.. 30 ).integrate('level') /G * Rad * 2 * PI * 100).val )
    dsefl_bar_nh_mid << ( ( ( gp_dsefl_bar * cos_lat ).cut( 'lat'=> 60.. 30 ).integrate('level') /G * Rad * 2 * PI * 100).val )
    dsefl_bar_sh_mid << ( ( ( gp_dsefl_bar * cos_lat ).cut( 'lat'=>-30..-60 ).integrate('level') /G * Rad * 2 * PI * 100).val )
    ## dsefl_dash
    dsefl_dash_law    << ( ( ( gp_dsefl_dash * cos_lat ).cut( 'lat'=> 60.. 30 ).integrate('level') /G * Rad * 2 * PI * 100).val )
    dsefl_dash_nh_mid << ( ( ( gp_dsefl_dash * cos_lat ).cut( 'lat'=> 60.. 30 ).integrate('level') /G * Rad * 2 * PI * 100).val )
    dsefl_dash_sh_mid << ( ( ( gp_dsefl_dash * cos_lat ).cut( 'lat'=>-30..-60 ).integrate('level') /G * Rad * 2 * PI * 100).val )

    axmonth << DCL::dateg3(year[0],1,1,y.to_i,m.to_i,1)
    i += 1
    p y if i%12 == 0
  end
end


### for intensity

  # $BF|IU<4:n@.(B
  namonth = NArray.to_na(axmonth)                 # $BF|IU<4$N(B NArray
  vamonth = VArray.new(namonth).rename!("month")  # VArray $B2=(B. $BL>A0$O(B "month"
  vamonth.set_att('long_name','month')            # $BB0@-@_Dj(B. long_name $B$H(B
  vamonth.set_att('units','month since 1979 JAN') #           units     $B$r@_Dj(B.
  grid = Grid.new( Axis.new(true).set_cell_guess_bounds(vamonth).set_pos_to_center )
                                                  # Axis $B2=$7$F(B, $B%;%k0LCV$rE,Ev$K7h$a$F(B Grid $B2=(B
  # dsefl_total $B$r(B GPhys $B2=(B
  ## VArray -> GPhys
  dsefl_total_name = "dsefl"
  gp_dsefl_total_law    = GPhys.new(grid, VArray.new(NArray.to_na(dsefl_total_law)).rename!(dsefl_total_name))
  gp_dsefl_total_nh_mid = GPhys.new(grid, VArray.new(NArray.to_na(dsefl_total_nh_mid)).rename!(dsefl_total_name))
  gp_dsefl_total_sh_mid = GPhys.new(grid, VArray.new(NArray.to_na(dsefl_total_sh_mid)).rename!(dsefl_total_name))
  ## $B%G!<%?$NB0@-@_Dj(B
  gp_dsefl_total_law.data.set_att('long_name', dsefl_total_tropical_long_name)
  gp_dsefl_total_nh_mid.data.set_att('long_name', dsefl_total_nh_midlat_long_name)
  gp_dsefl_total_sh_mid.data.set_att('long_name', dsefl_total_sh_midlat_long_name)
  gp_dsefl_total_law.data.set_att('units', dsefl_unit)
  gp_dsefl_total_nh_mid.data.set_att('units', dsefl_unit)
  gp_dsefl_total_sh_mid.data.set_att('units', dsefl_unit)
  # dsefl_bar $B$r(B GPhys $B2=(B
  ## VArray -> GPhys
  dsefl_bar_name = "dsefl_bar"
  gp_dsefl_bar_law    = GPhys.new(grid, VArray.new(NArray.to_na(dsefl_bar_law)).rename!(dsefl_bar_name))
  gp_dsefl_bar_nh_mid = GPhys.new(grid, VArray.new(NArray.to_na(dsefl_bar_nh_mid)).rename!(dsefl_bar_name))
  gp_dsefl_bar_sh_mid = GPhys.new(grid, VArray.new(NArray.to_na(dsefl_bar_sh_mid)).rename!(dsefl_bar_name))
  ## $B%G!<%?$NB0@-@_Dj(B
  gp_dsefl_bar_law.data.set_att('long_name', dsefl_bar_tropical_long_name)
  gp_dsefl_bar_nh_mid.data.set_att('long_name', dsefl_bar_nh_midlat_long_name)
  gp_dsefl_bar_sh_mid.data.set_att('long_name', dsefl_bar_sh_midlat_long_name)
  gp_dsefl_bar_law.data.set_att('units', dsefl_unit)
  gp_dsefl_bar_nh_mid.data.set_att('units', dsefl_unit)
  gp_dsefl_bar_sh_mid.data.set_att('units', dsefl_unit)
  # dsefl_dash $B$r(B GPhys $B2=(B
  dsefl_dash_name = "dash"
  ## VArray -> GPhys
  gp_dsefl_dash_law    = GPhys.new(grid, VArray.new(NArray.to_na(dsefl_dash_law)).rename!(dsefl_dash_name))
  gp_dsefl_dash_nh_mid = GPhys.new(grid, VArray.new(NArray.to_na(dsefl_dash_nh_mid)).rename!(dsefl_dash_name))
  gp_dsefl_dash_sh_mid = GPhys.new(grid, VArray.new(NArray.to_na(dsefl_dash_sh_mid)).rename!(dsefl_dash_name))
  ## $B%G!<%?$NB0@-@_Dj(B
  gp_dsefl_dash_law.data.set_att('long_name', dsefl_dash_tropical_long_name)
  gp_dsefl_dash_nh_mid.data.set_att('long_name', dsefl_dash_nh_midlat_long_name)
  gp_dsefl_dash_sh_mid.data.set_att('long_name', dsefl_dash_sh_midlat_long_name)
  gp_dsefl_dash_law.data.set_att('units', dsefl_unit)
  gp_dsefl_dash_nh_mid.data.set_att('units', dsefl_unit)
  gp_dsefl_dash_sh_mid.data.set_att('units', dsefl_unit)

################################################################
#                        $BIA2h%k!<%A%s(B
################################################################

##
# $B;vA0=`Hw(B

DCL.uscset('cyspos', 'B' )              # y $B<4$NC10L$N0LCV$r2<J}$X(B 
rsizel2 = DCL.uzrget('rsizel2')         # $B8=:_$N%i%Y%k%5%$%:$r<hF@(B
DCL.uzrset('rsizel2', rsizel2*0.42 )    # $B%i%Y%k%5%$%:$r%G%U%)%k%H$N(B 0.5 $BG\$K(B

##
# $B$*3(IA$-%a%$%s(B

# $B=D<4$N:GBgIA2hHO0O(B(W/m^2)
max = +8.0+15
min = -8.0+15

# $B%S%e!<%]!<%H@_Dj(B
vpt = NArray[0.15,0.85,0.23,0.58]

p idate = (year[0].to_s+"0101").to_i      # $BF|IU(B 0 $BF|(B. 19790101
ndate = (year[-1].to_s+"1201").to_i       # $BF|IU(B $B:G8e$NF|(B. 20031231
p nd = DCL.dateg1(idate,ndate)            # $BF|F|?t(B
#GGraph.set_axes("date?"=>["x", idate, nd])        
                                          # $BF|IU<4$rM-8z$K$9$k(B


DCL.swpset('lsep',  true)    # $B%Z!<%8KhJL!9$N%U%!%$%k$KMn$9(B
DCL.gropn(2)
DCL.sgpset('lcntl', false)   # 
DCL.sgpset('lfull',true)     # $B%U%k%9%/%j!<%s(B
DCL.uzfact(0.55)             # 
DCL.sgpset('lcorner',false)  # $B%3!<%J!<$r<h$CJ'$&(B 
DCL.sgpset('lfprop',true)    # 
DCL.udpset('lmsg',false)     # 
DCL.uscset('cyspos', 'B' )   # y $B<4$NC10L$N0LCV$r2<J}$X(B 


### $B;~7ONs(B

DCL::grfrm                   # $B%Z!<%83NDj(B 

DCL::grswnd(0.0, nd, min, max)
DCL::grsvpt(*vpt)
DCL::grstrn(1)
DCL::grstrf

DCL::ucxacl('B', idate, nd)
DCL::ucxacl('T', idate, nd)

DCL::uyaxdv('L', (max - min)/10, (max - min)/5)
DCL::uyaxdv('R', (max - min)/10, (max - min)/5)

DCL::uxmttl('T', 'Peak value of dry static energy (1979-2003)', 0.0)
DCL::uysttl('L', "dry static energy (#{dsefl_total_unit.to_s})", 0.0)
DCL::uxsttl('B', 'YEAR', 0.0)
# DCL::uscset('cyunit', dsefl_total_unit.to_s ) # y $B<4$NC10L(B

names, idxs = plot_line_main([ 
			       gp_dsefl_total_law, gp_dsefl_total_nh_mid, gp_dsefl_total_sh_mid,
			       gp_dsefl_bar_law, gp_dsefl_bar_nh_mid, gp_dsefl_bar_sh_mid,
			       gp_dsefl_dash_law, gp_dsefl_dash_nh_mid, gp_dsefl_dash_sh_mid
                              ])

__show_line_index(names,idxs)


DCL.grcls

##################################################
# $B2hA|%U%!%$%kL>$rJQ99(B
File.rename('dcl_001.ps', 'DSEFL_1973-2003_TIME-SERIES_NCEP.ps')
