#!/usr/bin/env ruby
=begin
= mkfig.rb -- make figure for zonal mean for 25 means

relative:25MEANS.1979-2003.NCEP/ $B0J2<$N(B netCDF $B%U%!%$%k$+$i(B
25 $BG/J?6QCM$r<h$j=P$7%W%m%C%H(B. 

$B8=:_(B mkfig_25years.rb $B$K5!G=$r%^!<%8$7$?$?$a(B, $BL$;HMQ(B.

100hPa*.nc:   shum
300hPa*.nc:   strm uwnd t dse dse_bar dse_dash lhe lhe_bar lhe_dash
GAUSSIAN*.nc: nswrs nlwrs nswrf ulsrf lhtfl shtfl lheat

$B3F?^$NJ*M}NL$O(B

   (a)$B%*%$%i!<J?6Q<ANLN.@~4X?t(B,
   (b)$BEl@>Iw(B,
   (c)$B29EY(B,
   (d)$BHf<>(B,
   (e)$BJ|<M<};Y(B($B9u@~(B:$BBg5$>eC<@5L#C;GH%U%i%C%/%9(B,
               $B@V@~(B:$BBg5$>eC<>e8~$-D9GH%U%i%C%/%9(B,
               $BNP@~(B:$BCOI=LL@5L#C;GH%U%i%C%/%9(B,
               $B@D@~(B:$BCOI=LL@5L#D9GH%U%i%C%/%9(B),
   (f)$BG.<};Y(B($B9u@~(B:$BCOI=LL@5L#@xG.%U%i%C%/%9(B,
             $B@V@~(B:$BCOI=LL@5L#82G.%U%i%C%/%9(B,
             $BNP@~(B:$B9_?e(B),
   (g)$BFnKL4%Ag@EE*%(%M%k%.!<M"AwNL(B
      ($B9u@~(B:$B9g7W(B, $B@V@~(B:$BBS>uJ?6Q@.J,(B, $BNP@~(B:$BBS>u>qMp@.J,(B),
   (h)$BFnKL@xG.M"AwNL(B
      ($B9u@~(B:$B9g7W(B, $B@V@~(B:$BBS>uJ?6Q@.J,(B, $BNP@~(B:$BBS>u>qMp@.J,(B),
   (i)EP $B%U%i%C%/%9$H$=$NH/;6(B
      ($BLp1)(B: EP $B%U%i%C%/%9(B, $B%H!<%s(B:$BH/;6(B),
   (j)$B;D:9=[4D$N<ANLN.@~4X?t(B $B$r0lKg$K$^$H$a$??^(B.

=end

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

## enable to the option min, max 
module NumRu
  module GGraph

    module_function

    def vector_for_dcchart(fx, fy, newframe=true, options=nil)  
      if ! defined?(@@vector_for_dcchart_options)
        @@vector_for_dcchart_options = Misc::KeywordOptAutoHelp.new(
          ['newfig', true, 'if false, do not cleared before figure setting.'],
          ['title', nil, 'Title of the figure(if nil, internally determined)'],
          ['annotate', true, 'if false, do not put texts on the right margin even when newframe==true'],
          ['transpose', false, 'if true, exchange x and y axes'],
          ['xintv', 1, '(Effective only if flow_vect) interval of data sampling in x'],
          ['yintv', 1, '(Effective only if flow_vect) interval of data sampling in y'],
          ['xfact', 1, 'scale factor for x'],
          ['yfact', 1, 'scale factor for y'],
          ['itr',   1, 'log or linear'],
          ['unit_vect', false, 'Show the unit vector'],
          ['max_unit_vect', false, '(Effective only if flow_vect && unit_vect) 
            If true, use the maximum arrows to scale the unit vector; otherwise, normalize in V coordinate.']
        )
      end
      opts = @@vector_for_dcchart_options.interpret(options)
      fx = fx.first2D.copy
      fy = fy.first2D.copy
      sh = fx.shape
      if sh != fy.shape
        raise ArgumentError, "shapes of fx and fy do not agree with each other"
      end
      fx = fx.transpose(1,0) if opts['transpose']
      fy = fy.transpose(1,0) if opts['transpose']
      if ((xi=opts['xintv']) >= 2)
        idx = NArray.int(sh[0]/xi).indgen!*xi     # [0,xi,2*xi,..]
        fx = fx[idx, true]
        fy = fy[idx, true]
      end
      if ((yi=opts['yintv']) >= 2)
        idx = NArray.int(sh[1]/yi).indgen!*yi     # [0,yi,2*yi,..]
        fx = fx[true, idx]
        fy = fy[true, idx]
      end
      xax = fx.coord(0)
      yax = fy.coord(1)
      if newframe
	nextfig = @@next_fig.dup  if ( @@next_fig != nil ) # backup next_fig
        fig(xax, yax, {'itr'=>itr}) 
        axes(xax, yax)
        if opts['title']
          ttl = opts['title']
        else
          fxnm = fx.data.get_att('long_name') || fx.name
          fynm = fy.data.get_att('long_name') || fy.name
          ttl =   '('+fxnm+','+fynm+')'
        end
        title( ttl )
        annotate(fx.lost_axes) if opts['annotate']
	@@next_fig = nextfig      if ( @@next_fig != nil )
      end
      fig(xax, yax, {'new_frame'=>false, 'itr'=>1, 'yreverse'=>false}) \
                                                          if (opts['newfig'])
      DCL.uwsgxa(xax.val) 
      DCL.uwsgya(yax.val)
      xfact = opts["xfact"]; yfact = opts["yfact"]
      before2=DCLExt.ug_set_params( {'LNRMAL'=>false, 'LMSG'=>false,
					 'XFACT1'=>xfact, 'YFACT1'=>yfact} )
#      before=DCLExt.ug_set_params({'lunit'=>true}) if opts['unit_vect']
      DCL.ugvect(fx.val[true, -1..0], fy.val[true, -1..0])
      DCLExt.unit_vect(xfact, yfact, nil, nil, "vxuoff"=>0.01, "vyuoff"=>-0.1) if opts['unit_vect']
#      DCLExt.ug_set_params(before) if opts['unit_vect']
      nil
    end
  end
end



  def GGraph::line(gphys, newframe=true, options=nil)
    if newframe!=true && newframe!=false
      raise ArgumentError, "2nd arg (newframe) must be true or false"
    end
    if ! defined?(@@line_options)
      @@line_options = Misc::KeywordOptAutoHelp.new(
						    ['title', nil, 'Title of the figure(if nil, internally determined)'],
						    ['annotate', true, 'if false, do not put texts on the right margin even when newframe==true'],
						    ['exchange', false, 'whether to exchange x and y axes'],
						    ['index', 1, 'line/mark index'],
						    ['type', 1, 'line type'],
						    ['label', nil, 'if a String is given, it is shown as the label'],
						    ['max', nil, 'maximam value for draw line'],
						    ['min', nil, 'minimam value for draw line']
						    )
    end
    opts = @@line_options.interpret(options)
    gp = gphys.first1D
    if !opts['exchange']
      x = gp.coord(0)
      y = gp.data
      xax = x
      if opts['min']
	ymin = opts['min'].to_f
      else
	ymin = gp.data.val.min
      end
      if opts['max']
	ymax = opts['max'].to_f
      else
	ymax = gp.data.val.max
      end
      yax = VArray.new(NArray[ymin, ymax], gp.data, gp.data.name)      
    else
      y = gp.coord(0)
      x = gp.data
      yax = y
      if opts['min']
	xmin = opts['min'] 
      else
	xmin = gp.data.val.min
      end
      if opts['max']
	xmax = opts['max'] if opts['max']
      else
	xmax = gp.data.val.max
      end
      xax = VArray.new(NArray[xmin, xmax], gp.data, gp.data.name)
    end
    if newframe
      fig(xax, yax)
      axes(xax, yax)
      title( (opts['title'] || gp.data.get_att('long_name')) )
      annotate(gp.lost_axes) if opts['annotate']
    end
    if opts['label']
      lcharbk = DCL.sgpget('lchar')
      DCL.sgpset('lchar',true)
      DCL.sgsplc(opts['label'])
      end
    DCL.uulinz(x.val, y.val, opts['type'], opts['index'])
    DCL.sgpset('lchar',lcharbk) if opts['label']
    nil
  end


##-----------------------
#  $B%i%$%s%a%=%C%I:FDj5A(B?

def plot_line_main(gphys_array, line_opts={})
  
  line_index_ary = Array.new; name_ary = Array.new

  default_index = 12
  
  line_hash = { "index"=> default_index }.update(line_opts)
  line_index_ary.push(default_index)
  name_ary.push(gphys_array[0].data.get_att("long_name"))
  
  GGraph.line( gphys_array[0], true, line_hash )
  gphys_array.size.times{ |num|
      unless num == 0
	line_hash = { "index"=> (num*10 + default_index)}
	line_index_ary.push(num*10 +default_index)
	name_ary.push(gphys_array[num].data.get_att("long_name"))
	GGraph.line( gphys_array[num], false, line_hash ) 
      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.1) 
    charsize = 0.85 * DCL.uzpget('rsizec1')
    dvx = 0.01
    dvy = charsize*1.6
    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 == 6
	vx = 0.30
	vy = 0.12 - charsize/2
      end
  }
    nil
  end

##-----------------------
## 25 $BG/J?6Q$N%*%V%8%'%/%H$r:n@.(B

def make_mean_gphys(phys, src, season, year, var=phys)
  pathary = []
  y1 = year[0]; y2 = year[1]
  path = "../../../../#{phys}.#{src}/"
  season.each do |m|
    y1.upto(y2) do |y|
      y = y-1 if m == "12"
      fn = path + "#{phys}.#{y}.#{src}/#{phys}_#{y}-#{m}_#{src}.nc"
      pathary << fn
    end
  end
#  gp     = mean_gphys_date_weight(pathary, phys.downcase)
  gp     = mean_gphys(pathary, var.downcase)
  return gp
end

## GGraph $B$N(B margin_info $B$r;29M$K(B, $BM>Gr$N>e2<$K$D$1$k$h$&$K$7$?(B. 
def margin_info(program=nil, data_source=nil, char_height=nil, date=nil,
		xl=0.0, xr=0.0, yb=nil, yt=nil)

  program = File.expand_path($0)       if !program
  if date
    program = program + '  ' + Date.today.to_s
  else
    program = program
    if date.nil?
      program = program + '  ' + Date.today.to_s
    end
  end
  if !data_source
    data_source = Dir.pwd
    data_source = '' if data_source == program.sub(/\/[^\/]+$/,'')
  end                
  
  sz = 1.0/( program.length + data_source.length )
  char_height = [0.008, sz].min        if !char_height
  yb = 2.0 * char_height               if !yb
  yt = 2.0 * char_height               if !yt
  
  DCL.slmgn(xl, xr, yb, yt)
  # $B>e$K$D$1$k(B
  DCL.slsttl(program,     't', -1.0,  1.0, char_height, 1)
  DCL.slsttl(data_source, 't',  1.0,  1.0, char_height, 2)
  # $B2<$K$D$1$k(B
  DCL.slsttl(program,     'b', -1.0,  1.0, char_height, 3)
  DCL.slsttl(data_source, 'b',  1.0,  1.0, char_height, 4)
end


##################################################################

unless getopts(
               "", 
	       "range:",
               "kikan:"          # "annual", "winter", "spring", "summer", "fall" $B$r;XDj(B
               )
  print "#{$0}:illegal options. please exec this program with -h/--help. \n"
  exit 1
end

## define Constants
Infty = 1.0/0.0
L = UNumeric.new(2.5008e6, 'J.Kg-1')
Rad = UNumeric.new(6.37E6, 'm')
G =   UNumeric.new(9.81  , 'm.s-2')

# make gphys objects

## set kikan
winter = "DJF";  spring = "MAM"
summer = "JJA";  fall = "SON"
annual = "ANNUAL"


case $OPT_kikan
when "annual"
  kikan = annual
when "winter"
  kikan = winter
when "spring"
  kikan = spring
when "summer"
  kikan = summer
when "fall"
  kikan = fall
else
  raise "#{$OPT_kikan} is invalid value."
end


##### $B5$8uCM<hF@(B

## $B<ANLN.@~4X?t(B
  strm_climat = GPhys::IO.open("../25MEANS.1979-2003.NCEP/100hPa_PHYS_1979-2003_#{kikan}_NCEP.nc", "strm")
## $BEl@>IwB.(B
  uwnd_climat = GPhys::IO.open("../25MEANS.1979-2003.NCEP/100hPa_PHYS_1979-2003_#{kikan}_NCEP.nc", "uwnd")
## $B29EY>l(B
  temp_climat = GPhys::IO.open("../25MEANS.1979-2003.NCEP/100hPa_PHYS_1979-2003_#{kikan}_NCEP.nc", "t")
## $BHf<>(B
  shum_climat = GPhys::IO.open("../25MEANS.1979-2003.NCEP/300hPa_PHYS_1979-2003_#{kikan}_NCEP.nc", "shum")

## $BJ|<M(B
  nswrf_climat = GPhys::IO.open("../25MEANS.1979-2003.NCEP/GAUSSIAN_PHYS_1979-2003_#{kikan}_NCEP.nc", "uswrf")
  ulwrf_climat = GPhys::IO.open("../25MEANS.1979-2003.NCEP/GAUSSIAN_PHYS_1979-2003_#{kikan}_NCEP.nc", "ulwrf")
  nlwrs_climat = GPhys::IO.open("../25MEANS.1979-2003.NCEP/GAUSSIAN_PHYS_1979-2003_#{kikan}_NCEP.nc", "nlwrs")
  nswrs_climat = GPhys::IO.open("../25MEANS.1979-2003.NCEP/GAUSSIAN_PHYS_1979-2003_#{kikan}_NCEP.nc", "nswrs")
    
## $BG.(B
#### $B@xG.%U%i%C%/%9(B
  lhtfl_climat = GPhys::IO.open("../25MEANS.1979-2003.NCEP/GAUSSIAN_PHYS_1979-2003_#{kikan}_NCEP.nc", "lhtfl")

#### $B82G.%U%i%C%/%9(B
  shtfl_climat = GPhys::IO.open("../25MEANS.1979-2003.NCEP/GAUSSIAN_PHYS_1979-2003_#{kikan}_NCEP.nc", "shtfl")

#### $B9_?e(B($B@xG.$K49;;$7$?$b$N(B)
  lheat_climat = GPhys::IO.open("../25MEANS.1979-2003.NCEP/GAUSSIAN_PHYS_1979-2003_#{kikan}_NCEP.nc", "prate")

## $B4%Ag@EE*%(%M%k%.!<(B
  dse_climat  =      GPhys::IO.open("../25MEANS.1979-2003.NCEP/100hPa_PHYS_1979-2003_#{kikan}_NCEP.nc", "dsefl")
  dse_bar_climat   = GPhys::IO.open("../25MEANS.1979-2003.NCEP/100hPa_PHYS_1979-2003_#{kikan}_NCEP.nc", "dsefl_bar")
  dse_dash_climat  = GPhys::IO.open("../25MEANS.1979-2003.NCEP/100hPa_PHYS_1979-2003_#{kikan}_NCEP.nc", "dsefl_dash")
  
  ## $B@xG.M"AwNL(B
  lhe_climat  =      GPhys::IO.open("../25MEANS.1979-2003.NCEP/100hPa_PHYS_1979-2003_#{kikan}_NCEP.nc", "lhefl")
  lhe_bar_climat   = GPhys::IO.open("../25MEANS.1979-2003.NCEP/100hPa_PHYS_1979-2003_#{kikan}_NCEP.nc", "lhefl_bar")
  lhe_dash_climat  = GPhys::IO.open("../25MEANS.1979-2003.NCEP/100hPa_PHYS_1979-2003_#{kikan}_NCEP.nc", "lhefl_dash")

## EP $B%U%i%C%/%9(B
  epflx_phi_climat = GPhys::IO.open("../25MEANS.1979-2003.NCEP/100hPa_PHYS_1979-2003_#{kikan}_NCEP.nc", "epflx_phi")
  epflx_p_climat   = GPhys::IO.open("../25MEANS.1979-2003.NCEP/100hPa_PHYS_1979-2003_#{kikan}_NCEP.nc", "epflx_p")
  epflx_div_climat = GPhys::IO.open("../25MEANS.1979-2003.NCEP/100hPa_PHYS_1979-2003_#{kikan}_NCEP.nc", "epflx_div")
## $B;D:9=[4D(B
  strm_rmean_climat = GPhys::IO.open("../25MEANS.1979-2003.NCEP/100hPa_PHYS_1979-2003_#{kikan}_NCEP.nc", "strm_rmean")
                   
##### ------------   $B3FG/(B
## set kikan
year  =   $OPT_range.split(/:/).collect{|y| y.to_i }

winter = ["12", "01", "02"];  spring = ["03", "04", "05"]
summer = ["06", "07", "08"];  fall = ["09", "10", "11"]
annual = winter + spring + summer + fall

case $OPT_kikan
when "annual"
  kikan = annual
when "winter"
  kikan = winter
when "spring"
  kikan = spring
when "summer"
  kikan = summer
when "fall"
  kikan = fall
else
  raise "#{$OPT_kikan} is invalid value."
end


## $B<ANLN.@~4X?t(B
  strm = make_mean_gphys('STRM', 'NCEP', kikan, year).cut( 'level'=>1000..100 )
## $BEl@>IwB.(B
  uwnd = make_mean_gphys('UWND', 'NCEP', kikan, year).mean('lon').cut( 'level'=>1000..100 )
## $B29EY>l(B
  temp = make_mean_gphys('T',    'NCEP', kikan, year).mean('lon').cut( 'level'=>1000..100 )
## $BHf<>(B
  shum = make_mean_gphys('SHUM', 'NCEP', kikan, year).mean('lon').cut( 'level'=>1000..100 )

## $BJ|<M(B
  dswrf = make_mean_gphys('DSWRF', 'NCEP', kikan, year).mean('lon')
  uswrf = make_mean_gphys('USWRF', 'NCEP', kikan, year).mean('lon')
  nswrf = uswrf - dswrf                                   # $B@5L#$NC;GHJ|<M%U%i%C%/%9(B
  nswrf.data.set_att('long_name',  'net short wave radiation flux at nominal top of atmosphere')
  ulwrf = make_mean_gphys('ULWRF', 'NCEP', kikan, year).mean('lon')
  ulwrf.data.set_att('long_name',  'upward long wave radiation flux at nominal top of atmosphere')
  nlwrs = make_mean_gphys('NLWRS', 'NCEP', kikan, year).mean('lon')  # $B@5L#$ND9GHJ|<M%U%i%C%/%9(B
  nlwrs.data.set_att('long_name',  'net long wave radiation flux at surface')
  nswrs = make_mean_gphys('NSWRS', 'NCEP', kikan, year).mean('lon')  # $BCOI=LL@5L#$NC;GHJ|<M%U%i%C%/%9(B
  nswrs.data.set_att('long_name',  'net short wave radiation flux at surface')

    
## $BG.(B
#### $B@xG.%U%i%C%/%9(B
  lhtfl = make_mean_gphys('LHTFL', 'NCEP', kikan, year).mean('lon')
  lhtfl.data.set_att('long_name', 'net latent heat flux at surface')
#### $B82G.%U%i%C%/%9(B
  shtfl = make_mean_gphys('SHTFL', 'NCEP', kikan, year).mean('lon')
  shtfl.data.set_att('long_name', 'net sensible heat flux at surface')
#### $B9_?e(B($B@xG.$K49;;$7$?$b$N(B)
  prate = make_mean_gphys('PRATE', 'NCEP', kikan, year).mean('lon')
  lheat = prate*L
  lheat.data.set_att('long_name', 'latent heat with precipitation')


## $B4%Ag@EE*%(%M%k%.!<(B
  dsefl  =      make_mean_gphys('DSEFL', 'NCEP', kikan, year, 'dsefl')
  dsefl_bar   = make_mean_gphys('DSEFL', 'NCEP', kikan, year, 'dsefl_bar')
  dsefl_dash  = make_mean_gphys('DSEFL', 'NCEP', kikan, year, 'dsefl_dash')
  
  ## $B@xG.M"AwNL(B
  lhefl  =      make_mean_gphys('LHEFL', 'NCEP', kikan, year, 'lhefl')
  lhefl_bar   = make_mean_gphys('LHEFL', 'NCEP', kikan, year, 'lhefl_bar')
  lhefl_dash  = make_mean_gphys('LHEFL', 'NCEP', kikan, year, 'lhefl_dash')

  # get cos
  lat     = lhefl.coord('lat')
  grid    = Grid.new(lhefl.axis('lat'))
  cos_lat = GPhys.new(grid, cos(lat*2*PI/360.0))


  dse =      dsefl.integrate('level') /G * Rad * cos_lat * 2 * PI * 100
  dse_bar =  dsefl_bar.integrate('level') /G * Rad * cos_lat * 2 * PI * 100
  dse_dash = dsefl_dash.integrate('level') /G * Rad * cos_lat * 2 * PI * 100
  dse.data.set_att('units', 'W')
  dse_bar.data.set_att('units', 'W')
  dse_dash.data.set_att('units', 'W')  



  lhe =      lhefl.integrate('level') /G * Rad * cos_lat * 2 * PI * 100
  lhe_bar =  lhefl_bar.integrate('level') /G * Rad * cos_lat * 2 * PI * 100
  lhe_dash = lhefl_dash.integrate('level') /G * Rad * cos_lat * 2 * PI * 100
  lhe.data.set_att('units', 'W')
  lhe.data.set_att('long_name', 'latent heat energy transport (total)')
  lhe_bar.data.set_att('units', 'W')
  lhe_dash.data.set_att('units', 'W')



## EP $B%U%i%C%/%9(B
  epflx_phi = make_mean_gphys('EPFLX', 'NCEP', kikan, year, 'epflx_phi')
  epflx_p   = make_mean_gphys('EPFLX', 'NCEP', kikan, year, 'epflx_p')
  epflx_div = make_mean_gphys('EPFLX', 'NCEP', kikan, year, 'epflx_div')
## $B;D:9=[4D(B
  strm_rmean = make_mean_gphys('STRM_RMEAN', 'NCEP', kikan, year)

##### ------------  $B:9J,(B

## $B<ANLN.@~4X?t(B	   
  strm = strm - strm_climat 
## $BEl@>IwB.(B	   
  uwnd = uwnd - uwnd_climat
## $B29EY>l(B	   
  temp = temp - temp_climat
## $BHf<>(B		   
  shum = shum - shum_climat
		   
## $BJ|<M(B		   
  nswrf = nswrf - nswrf_climat
  ulwrf = ulwrf - ulwrf_climat
  nlwrs = nlwrs - nlwrs_climat
  nswrs = nswrs - nswrs_climat

  radiation = [ nswrf, ulwrf, nswrs, nlwrs ] 
                 		   
## $BG.(B		   
#### $B@xG.%U%i%C%/%9(B
  lhtfl = lhtfl - lhtfl_climat
		   
#### $B82G.%U%i%C%/%9(B
  shtfl = shtfl - shtfl_climat
		   
#### $B9_?e(B($B@xG.$K49(B 
  lheat = lheat - lheat_climat

  heat = [ lhtfl, shtfl, lheat ]
		   
## $B4%Ag@EE*%(%M%k%.(B
  dse   = dse - dse_climat 
  dse_bar = dse_bar - dse_bar_climat   
  dse_dash = dse_dash - dse_dash_climat  

  dry_ene = [ dse, dse_bar, dse_dash ]
  		   
  ## $B@xG.M"AwNL(B	   
  lhe = lhe - lhe_climat      
  lhe_bar = lhe_bar - lhe_bar_climat   
  lhe_dash = lhe_dash - lhe_dash_climat  

  lat_ene = [ lhe, lhe_bar, lhe_dash ]
		   
## EP $B%U%i%C%/%9(B   
  epflx_phi = epflx_phi - epflx_phi_climat 
  epflx_p =   epflx_p   - epflx_p_climat   
  epflx_div = epflx_div - epflx_div_climat 
## $B;D:9=[4D(B	   
  strm_rmean = strm_rmean - strm_rmean_climat
                   
#############################################################################

# $B%^!<%8%s$K:\$;$kG/7n>pJs(B
mgn_str = {
  'annual' => 'ANNUAL', 
  'winter' => 'DJF', 
  'spring' => 'MAM',
  'summer' => 'JJA',
  'fall'   => 'SON'
}

# set User Path for dcldatabase
DCL.glcset('DUPATH','/home/daktu32/.dcldir/')     


DCL.swpset('lsep',  true)   # $B%Z!<%8KhJL!9$N%U%!%$%k$KMn$9(B
DCL.gropn(-2)
margin_info($0, mgn_str[$OPT_kikan]+" MEAN (#{year.uniq})")
DCL.sgpset('lcntl', false)   # 
DCL.uzfact(0.75)             # 
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 
DCL.sgpset('lfull',true)     # $B%U%k%9%/%j!<%s(B

DCL.sldiv('y',2,5)

##############################################################################

##--------------------------------------
## HEAT BALANCE (seasonal)

# $B%S%e!<%]!<%H@_Dj(B
# vpt00 = NArray[0.2, 0.8, 0.14, 0.44]
# vpt01 = NArray[0.2, 0.8, 0.14, 0.44]
vpt00 = NArray[0.12, 0.88, 0.14, 0.5]
vpt01 = NArray[0.11, 0.87, 0.14, 0.5]
vpt10 = vpt00
vpt11 = vpt01
vpt20 = vpt00
vpt21 = vpt01
vpt30 = vpt00
vpt31 = vpt01
vpt40 = vpt00
vpt41 = vpt01

itr = 2

# dcl $B$K7gB;CM>pJs$r65$($k(B
before = DCLExt.gl_set_params('lmiss'=>true,'rmiss'=>temp.data.get_att("missing_value")[0])

# $B<ANLN.@~4X?t(B($B:8>e$N3((B)
level = [-1.0e12, 0, +1.0e12]; pattern = [35999, 35999, 70999, 70999]
GGraph.next_linear_contour_options('int'=>2.5e9)
## draw
GGraph.set_fig('viewport'=>vpt00, 'itr'=>itr)
GGraph.set_axes('xunits'=>'', 'yunits'=>'', 'xtitle'=>'', 'ytitle'=>'') 
DCL.uzpset('labelxb',false)
GGraph.tone( strm, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
GGraph.contour( strm, false , 'annot'=>false, 'titl'=>'' )
GGraph::color_bar('constwidth'=>true)
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t',"(a) Mass Stream Function(#{strm.data.units})", -1) ; DCL.uzpset('pad1',0.7)

# $BEl@>IwB.(B($B1&>e$N3((B)
## set param
GGraph.next_linear_contour_options('min'=>-10,'int'=>0.5, 'max'=>10)
level   = [-5, -2.5, 0, 2.5, 5]
pattern = [30999, 35999, 40999, 70999, 75999, 85999]
## draw
GGraph.set_fig('viewport'=>vpt01, 'new_frame'=>true, 'itr'=>itr)
GGraph.set_axes('ytitle'=>'')
GGraph.tone( uwnd, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
GGraph.contour( uwnd, false , 'annot'=>false, 'titl'=>'' )
GGraph::color_bar('constwidth'=>true)
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t',"(b) Zonal Wind(#{uwnd.data.units})", -1) ; DCL.uzpset('pad1',0.7)

# $B29EY>l(B($B:82<$N3((B)
## set param
GGraph.next_linear_contour_options( 'min'=>-5, 'max'=>5, 'int'=>0.5)
level   = [-5, -2.5, 0, 2.5, 5]
pattern = NArray[25999, 30999, 40999, 70999, 75999, 85999]
## draw
GGraph.set_fig('viewport'=>vpt10, 'new_frame'=>true, 'itr'=>itr)
GGraph.set_axes('ytitle'=>'','xtitle'=>nil)
GGraph.tone( temp, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
GGraph.contour( temp, false , 'annot'=>false, 'titl'=>'' )
GGraph::color_bar('constwidth'=>true)
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t',"(c) Temperature(#{temp.data.units})", -1) ; DCL.uzpset('pad1',0.7)

# $BHf<>(B($B1&2<$N3((B)
## set param
GGraph.next_linear_contour_options('int'=>5.0e-5)
level =   NArray[0.006, 0.008, 0.01, 0.012, 0.014, 0.016]*0.1 - 0.001
pattern = NArray[25999, 30999, 35999, 40999, 70999, 75999, 85999]
## draw
GGraph.set_fig('viewport'=>vpt11, 'new_frame'=>true, 'itr'=>itr)
GGraph.set_axes('ytitle'=>'')
GGraph.tone( shum, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
GGraph.contour( shum, false , 'annot'=>false, 'titl'=>'' )
GGraph::color_bar('constwidth'=>true)
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t',"(d) Specific Humidity(#{shum.data.units})", -1) ; DCL.uzpset('pad1',0.7)

itr = 1

# $BJ|<M(B($B:82<$N3((B)
## draw
GGraph.set_fig('viewport'=>vpt20, 'new_frame'=>true, 'itr'=>itr)
GGraph.set_axes('ytitle'=>'','xtitle'=>nil)
names, idxs = plot_line_main(radiation, 'annot'=>false, 
			     'titl'=>'', 
			     'min'=>-20, 'max'=>20)
__show_line_index(names,idxs, x=0.22) 
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t',"(e) Radiation Budget(#{radiation[0].data.units})", -1) ; DCL.uzpset('pad1',0.7)

# $BG.(B($B1&2<$N3((B)
GGraph.set_fig('viewport'=>vpt21, 'new_frame'=>true, 'itr'=>itr)
GGraph.set_axes('ytitle'=>'')
names, idxs = plot_line_main(heat, 'annot'=>false,
			     'titl'=>'',  
			     'min'=>-20, 'max'=>20)
__show_line_index(names,idxs)
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t',"(f) Heat Budget(#{heat[0].data.units})", -1) ; DCL.uzpset('pad1',0.7)

# $B4%Ag@EE*%(%M%k%.!<%U%i%C%/%9(B($B:82<$N3((B)
GGraph.set_fig('viewport'=>vpt30, 'new_frame'=>true, 'itr'=>itr)
GGraph.set_axes('ytitle'=>'','xtitle'=>nil)
names, idxs = plot_line_main(dry_ene, 'annot'=>false, 
			     'titl'=>'',  
			     'min'=>-10.0e14, 'max'=>+10.0e14)
__show_line_index(names,idxs, x=0.3) 
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','(g) Dry Static Energy Transport(W)',-1) ; DCL.uzpset('pad1',0.7)

# $BFnKL%(%M%k%.!<%U%i%C%/%9(B($B:82<$N3((B)
GGraph.set_fig('viewport'=>vpt31, 'new_frame'=>true, 'itr'=>itr)
GGraph.set_axes('ytitle'=>'')
names, idxs = plot_line_main(lat_ene, 'annot'=>false, 
			     'titl'=>'',  
			     'min'=>-8.0e14, 'max'=>+8.0e14)
__show_line_index(names,idxs)
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','(h) Latent Heat Transport(W)',-1) ; DCL.uzpset('pad1',0.7)

itr = 2

# EP $B%U%i%C%/%9(B($B:82<$N3((B)
## settings
level = NArray[-5.0e1, -1.0e1, 0, 1.0e1, 5.0e1]*1.0e-6
pattern = NArray[30999, 35999, 40999, 55999, 75999, 85999]
vect_ratio = NArray[1.0, 100.0]*8e-12
vect_fact = 50.0
## draw
GGraph.set_fig('viewport'=>vpt40, 'new_frame'=>true, 'itr'=>itr)
GGraph.set_axes('ytitle'=>'','xtitle'=>nil)
DCL.uzpset('labelxb',true)
GGraph.tone( epflx_div, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
GGraph::vector_for_dcchart(epflx_phi, - epflx_p, false,
			   'xfact'=>vect_ratio[0]*vect_fact, 'yfact'=>vect_ratio[-1]*vect_fact, 
			   'unit'=>true, 'annot'=>false, 'itr'=>2,
			   'xintv'=>2
				    )
GGraph::color_bar('constwidth'=>true)
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','(i) EP Flux(kg.s-2), Div F(m.s-2)',-1) ; DCL.uzpset('pad1',0.7)

# $B;D:9=[4D(B($B:82<$N3((B)
## settings
level = [-1.0e12, 0, +1.0e12]; pattern = [35999, 35999, 70999, 70999]
GGraph.next_linear_contour_options('int'=>2.5e9)
## draw
GGraph.set_fig('viewport'=>vpt41, 'new_frame'=>true, 'itr'=>itr)
GGraph.set_axes('ytitle'=>'')
GGraph.tone( strm_rmean, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
GGraph::color_bar('constwidth'=>true)
GGraph.contour( strm_rmean, false , 'annot'=>false, 'titl'=>'' )
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','(j) Residual Mass Stream Function(kg/s)',-1) ; DCL.uzpset('pad1',0.7)

DCL.grcls

#######################################################

