#!/usr/bin/env ruby
=begin
= mkfig_25time_series.rb -- make time-series figure of zonal wind 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 
################################################################
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 

uwnd_nh_20_40_long_name = "mean zonal wind speed (N20 - N40)"
uwnd_nh_40_60_long_name = "mean zonal wind speed (N40 - N60)"
uwnd_sh_20_40_long_name = "mean zonal wind speed (S20 - S40)"
uwnd_sh_40_60_long_name = "mean zonal wind speed (S40 - S60)"

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

axmonth = []
i = 0

uwnd_name = ""
uwnd_unit = ""

uwnd_north_20_40 = []
uwnd_north_40_60 = []
uwnd_south_20_40 = []
uwnd_south_40_60 = []

year[0].upto(year[1]) do |y|
  month.each do |m|
    path = "../../../UWND.NCEP/UWND.#{y}.NCEP/UWND_#{y}-#{m}_NCEP.nc" # stream function
    gp_uwnd = GPhys::NetCDF_IO.open(path, 'uwnd').cut('level'=>100..1000).mean('lon')
    gp_climat = GPhys::NetCDF_IO.open("../../25MEANS.NCEP/25MEANS.1979-2003.NCEP/100hPa_PHYS_1979-2003_#{m}_NCEP.nc", 'uwnd')
#    if m == "02"
      uwnd_north_20_40 <<  (gp_uwnd).cut('level'=>200,  'lat'=>20..40  ).mean('lat').val - (gp_climat).cut('level'=>200,  'lat'=>20..40  ).mean('lat').val
      uwnd_north_40_60 <<  (gp_uwnd).cut('level'=>200,  'lat'=>40..60  ).mean('lat').val - (gp_climat).cut('level'=>200,  'lat'=>40..60  ).mean('lat').val
#    elsif m == "08"
      uwnd_south_20_40 <<  (gp_uwnd).cut('level'=>200,  'lat'=>-20..-40).mean('lat').val - (gp_climat).cut('level'=>200,  'lat'=>-20..-40).mean('lat').val
      uwnd_south_40_60 <<  (gp_uwnd).cut('level'=>200,  'lat'=>-40..-60).mean('lat').val - (gp_climat).cut('level'=>200,  'lat'=>-40..-60).mean('lat').val
#    end
    uwnd_name =      gp_uwnd.data.name
    uwnd_unit =      gp_uwnd.data.get_att("units")
  end
end


### for intensity

  na_uwnd_north_20_40 = NArray.to_na(uwnd_north_20_40)
  na_uwnd_north_40_60 = NArray.to_na(uwnd_north_40_60)
  na_uwnd_south_20_40 = NArray.to_na(uwnd_south_20_40)
  na_uwnd_south_40_60 = NArray.to_na(uwnd_south_40_60)


############      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] # y $B<4HO0O(B. -7m/s $B$+$i(B + 7m/s
axy = NArray[-7, 7]       # y $B<4HO0O(B. -7m/s $B$+$i(B + 7m/s


### 1 $BKgL\(B : $BKLH>5e(B (N20 - N40)

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

x = na_strm_hadley_north
y = na_uwnd_north_20_40

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

DCL::ussttl('Deviation of mass stream function (NH)', strm_unit, 'Deviation of zonal wind ', uwnd_unit )
DCL::usdaxs
DCL::uxmttl('T', 'Zonal Wind at 200hPa (N20 - N40)', 0.0)

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

print "$BKLH>5e%O%I%l!<=[4D6/EY$H(B200 hPa $BLLEl@>Iw(B(N20-N40)", cal_correlation(x, y), "\n"


### 2 $BKgL\(B : $BKLH>5e(B (N40 - N60)

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

x = na_strm_hadley_north
y = na_uwnd_north_40_60

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

DCL::ussttl('Deviation of mass stream function (NH)', strm_unit, 'Deviation of zonal wind ', uwnd_unit )
DCL::usdaxs
DCL::uxmttl('T', 'Zonal Wind at 200hPa (N40 - N60)', 0.0)

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

print "$BKLH>5e%O%I%l!<=[4D6/EY$H(B200 hPa $BLLEl@>Iw(B(N40-N60)", cal_correlation(x, y), "\n"

### 3 $BKgL\(B : $BFnH>5e(B (S20 - S40)

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

x = na_strm_hadley_south
y = na_uwnd_south_20_40

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

DCL::ussttl('Deviation of mass stream function (SH)', strm_unit, 'Deviation of zonal wind ', uwnd_unit )
DCL::usdaxs
DCL::uxmttl('T', 'Zonal Wind at 200hPa (S20 - S40)', 0.0)

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

print "$BFnH>5e%O%I%l!<=[4D6/EY$H(B200 hPa $BLLEl@>Iw(B(S20-S40)", cal_correlation(x, y), "\n"

### 4 $BKgL\(B : $BFnH>5e(B (S40 - S60)

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

x = na_strm_hadley_south
y = na_uwnd_south_40_60

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

DCL::ussttl('Deviation of mass stream function (SH)', strm_unit, 'Deviation of zonal wind ', uwnd_unit )
DCL::usdaxs
DCL::uxmttl('T', 'Zonal Wind at 200hPa (S40 - S60)', 0.0)

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

print "$BFnH>5e%O%I%l!<=[4D6/EY$H(B200 hPa $BLLEl@>Iw(B(S40-S60)", 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', 'UWND_1973-2003_TIME-SERIES_NCEP_DEVIATION_area_mean_sanpuzu.ps')
