#!/usr/bin/env ruby
=begin
= mkfig_25time_series.rb -- make time-series figure of prate

=end

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


################################################################
#                        make gphys 
################################################################

## set condition

L = UNumeric.new(2.5008e6, 'J.Kg-1')

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

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

axmonth = []
i = 0

pratename = ""  # $B2<5-$N%$%F%l!<%?Cf$GJQ?tL>$r<hF@$9$k(B. $B%9%3!<%W$N4X78>e$3$3$G@k8@(B.
prateunit = ""  # 

prate = []

year[0].upto(year[1]) do |y|
  month.each do |m|
    path = "../../../PRATE.NCEP/PRATE.#{y}.NCEP/PRATE_#{y}-#{m}_NCEP.nc" # zonal wind
p    gp_prate = GPhys::NetCDF_IO.open(path, 'prate')*L
p    gp_climat = GPhys::NetCDF_IO.open("../../25MEANS.NCEP/25MEANS.1979-2003.NCEP/GAUSSIAN_PHYS_1979-2003_#{m}_NCEP.nc", 'prate')
    
    va_prate = (gp_prate.mean('lon') - gp_climat).data.copy
                     
    prate << va_prate.reshape!(1, *va_prate.shape)
		                           
    pratename =      gp_prate.data.name
    prateunit =      gp_prate.data.get_att("units")

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


  namonth = NArray.to_na(axmonth)
  axmonth = Axis.new(true).set_cell_guess_bounds(VArray.new(namonth).rename!("month")).set_pos_to_center

    
### for width

  va_prate = VArrayComposite.new( NArray.to_na( prate ) )
  va_prate.set_att('long_name', 'Precipitation  (1979-2003)')

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



## set variable param 

tone_nlev = 50
#tone_max  = nil
#tone_min  = 0
#level   = NArray[0, 10, 20, 30, 40, 50, 60]*4
# level   = NArray[0, 10, 20, 30, 40, 50]
#pattern = NArray[30999, 35999, 40999, 55999, 70999, 75999, 85999]

# get coord info
uwndpath = "../../../PRATE.NCEP/PRATE.2000.NCEP/PRATE_2000-01_NCEP.nc"
gp_lat  = GPhys::NetCDF_IO.open(uwndpath, 'lat')
missval = GPhys::NetCDF_IO.open(uwndpath, 'prate').data.get_att('missing_value')[0]

before = DCLExt.gl_set_params('lmiss'=>true,'rmiss'=>missval)

ymax = gp_lat.data.val.max
ymin = gp_lat.data.val.min

###  200 hPa $BLL;~7ONs(B
  DCL::grfrm                   # $B%Z!<%83NDj(B 

  DCL::grswnd(0.0, nd, ymin, ymax)
  DCL::grsvpt(*vpt)
  DCL::grstrn(1)
  DCL::grstrf

  ## $B%H!<%s(B, $B%3%s%?!<IA2h(B
#  DCLExt::ue_set_tone(level, pattern)
#  DCLExt::ud_set_linear_levs(va_prate.val, 'nlev'=>tone_nlev, 'min'=>tone_min, 'max'=>tone_max)
  DCLExt::ue_set_linear_levs(va_prate.val, 'nlev'=>tone_nlev)
  DCL::uetone(va_prate.val[true, -1..0])

  ## $B<4IA2h(B

  ### $BF|IU<4(B
  DCL::ucxacl('B', idate, nd)
  DCL::ucxacl('T', idate, nd)
  ### y $B<4%i%Y%k@_Dj(B
  DCL::uyaxdv('L', 10, 20)
  DCL::uyaxdv('R', 10, 20)
  ### $B%?%$%H%k(B
#  DCL::uxsttl('T', "(#{va_prate.units.to_s})", 1)
  DCL::uxsttl('T', "(W.m-2)", 1)
  DCL::uxmttl('T', va_prate.get_att('long_name'), 0.0)
  DCL::uysttl('L', "Latitude", 0.0)
  DCL::uxsttl('B', 'YEAR', 0.0)

  ## color bar
#  DCLExt::color_bar('constwidth'=>true)
  DCLExt::color_bar('tickintv'=>0)

  DCL::ueitlv

DCL.grcls

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