#!/usr/bin/env ruby
=begin
= $BHf<>1tD>J?6QCM;~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 == 5
	vx = 0.30
	vy = 0.12 - charsize/2
      end
  }
    nil
  end



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

shum_long_name = "Vertical mean specific humidity "
shum_nh_law_00_10_long_name = shum_long_name + " ( EQ - N10)"
shum_nh_law_10_20_long_name = shum_long_name + " (N10 - N20)"
shum_nh_law_20_30_long_name = shum_long_name + " (N20 - N30)"
shum_sh_law_00_10_long_name = shum_long_name + " ( EQ - S10)"
shum_sh_law_10_20_long_name = shum_long_name + " (S10 - S20)"
shum_sh_law_20_30_long_name = shum_long_name + " (S20 - S30)"

shum_nh_midlat_long_name = shum_long_name + " (N30 - N60)"
shum_sh_midlat_long_name = shum_long_name + " (S30 - S60)"

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

axmonth = []
i = 0

shum_name = ""
shum_unit = ""


shum_nh_law_00_10 = []
shum_nh_law_10_20 = []
shum_nh_law_20_30 = []
shum_sh_law_00_10 = []
shum_sh_law_10_20 = []
shum_sh_law_20_30 = []
shum_nh_mid = []
shum_sh_mid = []

year[0].upto(year[1]) do |y|
  month.each do |m|
    path = "../../../SHUM.NCEP/SHUM.#{y}.NCEP/SHUM_#{y}-#{m}_NCEP.nc" # specific humidity

    gp_shum = GPhys::NetCDF_IO.open(path, 'shum').average('level').mean('lon')
    cos_lat = cos( gp_shum.coord('lat')*2*PI/360 )

    shum_nh_law_00_10 << ( ( ( gp_shum * cos_lat ).cut( 'lat'=>  0..10  ) ).average('lat').val )
    shum_nh_law_10_20 << ( ( ( gp_shum * cos_lat ).cut( 'lat'=> 10..20  ) ).average('lat').val )
    shum_nh_law_20_30 << ( ( ( gp_shum * cos_lat ).cut( 'lat'=> 20..30  ) ).average('lat').val )
    shum_sh_law_00_10 << ( ( ( gp_shum * cos_lat ).cut( 'lat'=>  0..-10 ) ).average('lat').val )
    shum_sh_law_10_20 << ( ( ( gp_shum * cos_lat ).cut( 'lat'=>-10..-20 ) ).average('lat').val )
    shum_sh_law_20_30 << ( ( ( gp_shum * cos_lat ).cut( 'lat'=>-20..-30 ) ).average('lat').val )
    shum_nh_mid       << ( ( ( gp_shum * cos_lat ).cut( 'lat'=> 60.. 30 ) ).average('lat').val )
    shum_sh_mid       << ( ( ( gp_shum * cos_lat ).cut( 'lat'=>-30..-60 ) ).average('lat').val )

    shum_name =      gp_shum.data.name
    shum_unit =      gp_shum.data.get_att("units")

    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
  # $B29EY:9;~7ONs$r(B GPhys $B2=(B
  gp_shum_nh_law_00_10 = GPhys.new(grid, VArray.new(NArray.to_na(shum_nh_law_00_10)).rename!(shum_name)) 
  gp_shum_nh_law_10_20 = GPhys.new(grid, VArray.new(NArray.to_na(shum_nh_law_10_20)).rename!(shum_name)) 
  gp_shum_nh_law_20_30 = GPhys.new(grid, VArray.new(NArray.to_na(shum_nh_law_20_30)).rename!(shum_name)) 
  gp_shum_sh_law_00_10 = GPhys.new(grid, VArray.new(NArray.to_na(shum_sh_law_00_10)).rename!(shum_name)) 
  gp_shum_sh_law_10_20 = GPhys.new(grid, VArray.new(NArray.to_na(shum_sh_law_10_20)).rename!(shum_name)) 
  gp_shum_sh_law_20_30 = GPhys.new(grid, VArray.new(NArray.to_na(shum_sh_law_20_30)).rename!(shum_name)) 
  gp_shum_nh_mid = GPhys.new(grid, VArray.new(NArray.to_na(shum_nh_mid)).rename!(shum_name))
  gp_shum_sh_mid = GPhys.new(grid, VArray.new(NArray.to_na(shum_sh_mid)).rename!(shum_name))

  ## $B%G!<%?$NB0@-@_Dj(B
  gp_shum_nh_law_00_10.data.set_att('long_name', shum_nh_midlat_long_name)
  gp_shum_nh_law_10_20.data.set_att('long_name', shum_nh_midlat_long_name)
  gp_shum_nh_law_20_30.data.set_att('long_name', shum_nh_midlat_long_name)
  gp_shum_sh_law_00_10.data.set_att('long_name', shum_nh_midlat_long_name)
  gp_shum_sh_law_10_20.data.set_att('long_name', shum_nh_midlat_long_name)
  gp_shum_sh_law_20_30.data.set_att('long_name', shum_nh_midlat_long_name)
  gp_shum_nh_mid.data.set_att('long_name', shum_nh_midlat_long_name)
  gp_shum_sh_mid.data.set_att('long_name', shum_sh_midlat_long_name)
  gp_shum_nh_law_00_10.data.set_att('units', shum_unit)
  gp_shum_nh_law_10_20.data.set_att('units', shum_unit)
  gp_shum_nh_law_20_30.data.set_att('units', shum_unit)
  gp_shum_sh_law_00_10.data.set_att('units', shum_unit)
  gp_shum_sh_law_10_20.data.set_att('units', shum_unit)
  gp_shum_sh_law_20_30.data.set_att('units', shum_unit)
  gp_shum_nh_mid.data.set_att('units', shum_unit)
  gp_shum_sh_mid.data.set_att('units', shum_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(Kg/Kg)
max = 0.01
min = 0

# $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 

### 1 $BKgL\(B : $BKLH>5eDc0^EY;~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/10, max/5)
DCL::uyaxdv('R', max/10, max/5)

DCL::uxmttl('T', 'Vertical mean specific humidity (1979-2003)', 0.0)
DCL::uysttl('L', "specific humidity(#{shum_unit.to_s})", 0.0)
DCL::uxsttl('B', 'YEAR', 0.0)
# DCL::uscset('cyunit', shum_unit.to_s ) # y $B<4$NC10L(B

names, idxs = plot_line_main([ 
			       gp_shum_nh_law_00_10,
			       gp_shum_nh_law_10_20,
			       gp_shum_nh_law_20_30
                              ])

__show_line_index(names,idxs)

### 2 $BKgL\(B : $BFnH>5eDc0^EY;~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/10, max/5)
DCL::uyaxdv('R', max/10, max/5)

DCL::uxmttl('T', 'Vertical mean specific humidity (1979-2003)', 0.0)
DCL::uysttl('L', "specific humidity(#{shum_unit.to_s})", 0.0)
DCL::uxsttl('B', 'YEAR', 0.0)
# DCL::uscset('cyunit', shum_unit.to_s ) # y $B<4$NC10L(B

names, idxs = plot_line_main([ 
			       gp_shum_sh_law_00_10,
			       gp_shum_sh_law_10_20,
			       gp_shum_sh_law_20_30
                              ])

__show_line_index(names,idxs)

### 3 $BKgL\(B : $BCf0^EY;~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/10, max/5)
DCL::uyaxdv('R', max/10, max/5)

DCL::uxmttl('T', 'Vertical mean specific humidity (1979-2003)', 0.0)
DCL::uysttl('L', "specific humidity(#{shum_unit.to_s})", 0.0)
DCL::uxsttl('B', 'YEAR', 0.0)
# DCL::uscset('cyunit', shum_unit.to_s ) # y $B<4$NC10L(B

names, idxs = plot_line_main([ 
			       gp_shum_nh_mid, gp_shum_sh_mid
                              ])

__show_line_index(names,idxs)


DCL.grcls


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