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

=end

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

def cal_correlation(x, y) # $BAj4X78?t$r5a$a$k(B
  xa = x - x.mean(0)           # x $B$NJP:9(B
  ya = y - y.mean(0)           # y $B$NJP:9(B
  xstddev = x.stddev           # x $B$NI8=`JP:9(B($BJ,;6$NJ?J}:,(B)
  ystddev = y.stddev           # y   ::
  
  crc = (( xa*ya ).mean(0))/(xstddev*ystddev) # $BAj4X78?t(B 
  return crc
end


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

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

prate_long_name = "Precipitation"
prate_law_10_10_long_name = prate_long_name + " (N10 - S10)"
prate_law_s5_15_long_name = prate_long_name + " (S5 - S15)"
prate_law_n5_15_long_name = prate_long_name + " (N5 - N15)"
prate_law_30_30_long_name = prate_long_name + " (N30 - S30)"
prate_nh_midlat_long_name = prate_long_name + " (N30 - N60)"
prate_sh_midlat_long_name = prate_long_name + " (S30 - S60)"


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

##  $B%,%&%9=E$_(B
path_gw = "../../../GAUSSIAN_WEIGHT/gaussian_weight.nc"
gp_gw   = GPhys::NetCDF_IO.open(path_gw, 'gw')

axmonth = []
i = 0

prate_name = ""; prate_unit = ""

prate_law_10_10 = []
prate_law_s5_15 = []
prate_law_n5_15 = []
prate_law_30_30 = []
prate_nh_mid = []
prate_sh_mid = []


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

    # NetCDFVar => GPhys 
    path_prate = "../../../PRATE.NCEP/PRATE.#{y}.NCEP/PRATE_#{y}-#{m}_NCEP.nc"
    gp_prate =  GPhys::NetCDF_IO.open(path_prate, 'prate').mean('lon')*L

    # $B;~7ONs(B VArray $B:n$j(B
    ## prate
    prate_law_10_10 << ( ( ( ( gp_prate ) * gp_gw ).cut( 'lat'=>-10..10 ) ).sum.val )
    prate_law_30_30 << ( ( ( ( gp_prate ) * gp_gw ).cut( 'lat'=>30..-30 ) ).sum.val )
#    if m == "02"
      prate_law_s5_15 << ( ( ( ( gp_prate ) * gp_gw ).cut( 'lat'=>-5..-15 ) ).sum.val )
#    elsif m == "08"
      prate_law_n5_15 << ( ( ( ( gp_prate ) * gp_gw ).cut( 'lat'=>5..15 ) ).sum.val )
#    end

    prate_nh_mid << ( ( ( ( gp_prate ) * gp_gw ).cut( 'lat'=> 60.. 30 ) ).sum.val )
    prate_sh_mid << ( ( ( ( gp_prate ) * gp_gw ).cut( 'lat'=>-30..-60 ) ).sum.val )

    prate_name = gp_prate.data.name
    prate_unit = gp_prate.data.units.to_s

    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 )

  # prate $B$r(B GPhys $B2=(B
  ## VArray -> GPhys

  gp_prate_nh_mid       = GPhys.new(grid, VArray.new(NArray.to_na(prate_nh_mid)).rename!(prate_name)) / gp_gw.cut( 'lat'=> 60..30  ).sum.val 
  gp_prate_sh_mid       = GPhys.new(grid, VArray.new(NArray.to_na(prate_sh_mid)).rename!(prate_name)) / gp_gw.cut( 'lat'=>-30..-60 ).sum.val 

#NArray.to_na(prate_law_10_10)).rename!(prate_name)) / gp_gw.cut( 'lat'=> 10..-10  ).sum.val 
  na_prate_law_s5_15 = NArray.to_na(prate_law_s5_15) / gp_gw.cut( 'lat'=> -5..-15  ).sum.val 
  na_prate_law_n5_15 = NArray.to_na(prate_law_n5_15) / gp_gw.cut( 'lat'=>  5..15  ).sum.val 
  na_prate_law_30_30 = NArray.to_na(prate_law_30_30) / gp_gw.cut( 'lat'=> -30..30  ).sum.val 
                    


############      stream function        #########################

strm_nh_hadley_long_name = "hadley circulation intencity (NH)"
strm_sh_hadley_long_name = "hadley circulation intencity (SH)"

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

axmonth = []
i = 0

strm_name = ""
strm_unit = ""

strm_hadley_north = []
strm_hadley_south = []

year[0].upto(year[1]) do |y|
  month.each do |m|
    path = "../../../STRM.NCEP/STRM.#{y}.NCEP/STRM_#{y}-#{m}_NCEP.nc" # stream function
    gp_climat = GPhys::NetCDF_IO.open("../../25MEANS.NCEP/25MEANS.1979-2003.NCEP/100hPa_PHYS_1979-2003_#{m}_NCEP.nc", 'strm')
    gp_strm =   GPhys::NetCDF_IO.open(path, 'strm').cut('level'=>100..1000) 

    gp_strm = GPhys::NetCDF_IO.open(path, 'strm')
#    if m == "02"
      strm_hadley_north <<  gp_strm.cut( 'lat'=>  0..30  ).data.val.max - gp_climat.cut( 'lat'=>  0..30  ).data.val.max
#    elsif m == "08"
      strm_hadley_south <<  gp_strm.cut( 'lat'=>  0..-30 ).data.val.min - gp_climat.cut( 'lat'=>  0..-30 ).data.val.min
#    end

    strm_name =      gp_strm.data.name
    strm_unit =      gp_strm.data.get_att("units")

  end
end


### for intensity

  na_strm_hadley_north = NArray.to_na(strm_hadley_north)
  na_strm_hadley_south = NArray.to_na(strm_hadley_south)

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

DCL.swpset('lsep',  true)    # $B%Z!<%8KhJL!9$N%U%!%$%k$KMn$9(B
DCL.gropn(2)
DCL.sldiv('T', 2, 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 

## y $B<4HO0O(B

axx = NArray[-5e10, 5e10] # x $B<4HO0O(B. -5e10 $B$+$i(B 5e10
axy = NArray[100, 200]        # y $B<4HO0O(B. 0 $B$+$i(B 200 W/m^2


### 1 $BKgL\(B : $BKLH>5e%O%I%l!<=[4D6/EY(B & $B9_?e(B (s5 - s15)

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

x = na_strm_hadley_north
y = na_prate_law_s5_15


DCL::usspnt(axx, axy)
DCL::uspfit
DCL::grstrf

DCL::ussttl('Mass stream function', strm_unit, 'precipitation (S5-S15) ', prate_unit )
DCL::usdaxs
DCL::uxmttl('T', 'Hadley intensity (NH) & Precipitation ', 0.0)

DCL::uusmkt(2)
DCL::uumrk(x, y)

print "$BKLH>5e%O%I%l!<=[4D6/EY$H9_?e(B(N5-N15)", cal_correlation(x, y), "\n"

### 2 $BKgL\(B : $BFnH>5e%O%I%l!<=[4D6/EY(B & $B9_?e(B (n5 - n15)

axy = NArray[150, 250]        # y $B<4HO0O(B. 0 $B$+$i(B 200 W/m^2

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

x = na_strm_hadley_south
y = na_prate_law_n5_15

DCL::usspnt(axx, axy)
DCL::uspfit
DCL::grstrf

DCL::ussttl('Mass stream function', strm_unit, 'precipitation (N5-N15)', prate_unit )
DCL::usdaxs
DCL::uxmttl('T', 'Hadley intensity (SH) & Precipitation (N5 - N15)', 0.0)

DCL::uusmkt(2)
DCL::uumrk(x, y)

print "$BFnH>5e%O%I%l!<=[4D6/EY$H9_?e(B(S5-S15)", cal_correlation(x, y), "\n"

### 3 $BKgL\(B : $BKLH>5e%O%I%l!<=[4D6/EY(B & $BFnKL(B ITCZ $B9_?e:9(B

axy = NArray[-150, 150]        # y $B<4HO0O(B. -100 $B$+$i(B 200 W/m^2


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

x = na_strm_hadley_north
y = na_prate_law_s5_15 - na_prate_law_n5_15



DCL::usspnt(axx, axy)
DCL::uspfit
DCL::grstrf

DCL::ussttl('Mass stream function', strm_unit, 'precipitation ', prate_unit )
DCL::usdaxs
DCL::uxmttl('T', 'Hadley intensity (NH) & Precipitation anomaly [(S5-S15) - (N5-N15)]', 0.0)

DCL::uusmkt(2)
DCL::uumrk(x, y)

print "$BKLH>5e%O%I%l!<=[4D6/EY$HFnKL9_?e:9(B[(S5-S15) - (N5-N15)]", cal_correlation(x, y), "\n"

### 4 $BKgL\(B : $BFnH>5e%O%I%l!<=[4D6/EY(B & $BFnKL(B ITCZ $B9_?e:9(B

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

x = na_strm_hadley_south
y = na_prate_law_n5_15 - na_prate_law_s5_15

DCL::usspnt(axx, axy)
DCL::uspfit
DCL::grstrf

DCL::ussttl('Mass stream function', strm_unit, 'precipitation ', prate_unit )
DCL::usdaxs
DCL::uxmttl('T', 'Hadley intensity (SH) & Precipitation anomaly [(N5-N15) - (S5-S15)]', 0.0)

DCL::uusmkt(2)
DCL::uumrk(x, y)

print "$BFnH>5e%O%I%l!<=[4D6/EY$HFnKL9_?e:9(B[(N5-N15) - (S5-S15)]", cal_correlation(x, y), "\n"



=begin
# $B%^!<%+!<>pJs5-=R(B
charsize = 1.0 * DCL.uzpget('rsizec1')
dvx = 0.01
dvy = charsize*1.5
vxmin,vxmax,vymin,vymax = DCL.sgqvpt
vx = 0.12
vy = 0.1 + charsize/2
DCL::sgpmu([vx], [vy])
DCL::sgtxzv(vx+dvx,vy,uwnd_nh_20_40_long_name, 
	    charsize, 0, -1, 1)
=end
DCL.grcls

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