#!/usr/bin/env ruby
=begin
= mkfig.rb -- make figure for 25 years mean prate
=end

require "getopts"        # for option_parse
require "numru/netcdf_miss"
require "numru/ggraph"
require "libgphys-n"
include NumRu

## enable to the option min, max 

  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


year = [1979, 2003]
winter = ["12", "01", "02"];  spring = ["03", "04", "05"]
summer = ["06", "07", "08"];  fall = ["09", "10", "11"]
itr = 1
min = 0; max = 0.001

#level = NArray[0.00005, 0.00006, 0.00007, 0.00008, 0.00009, 0.0001, 1000000]
#pattern = NArray[35999, 45999, 60999, 70999, 75999, 85999]

def make_mean_gphys(season,year)
  ary = []
  y1 = year[0]; y2 = year[1]
  path = "../../NCEP/PRATE.NCEP/"
  season.each do |m|
    y1.upto(y2) do |y|
      fn = path + "PRATE.#{y}.NCEP/PRATE_#{y}-#{m}_NCEP.nc"
      ary << fn
    end
  end
  return mean_gphys(ary, "prate").mean('lon')
end

djf = make_mean_gphys(winter,year)
mam = make_mean_gphys(spring,year)
jja = make_mean_gphys(summer,year)
son = make_mean_gphys(fall,year)

# set User Path for dcldatabase
DCL.glcset('DUPATH','/home/daktu32/.dcldir/')     
DCL.gropn(2)
DCL.sgpset('lcntl', false)   # 
DCL.sgpset('lfull',true)     # 
DCL.sgpset('lcorner',false)  # 
DCL.uzfact(0.35)             # 
DCL.sgpset('lfprop',true)    # 
DCL.udpset('lmsg',false)     # 

vpt = NArray[0.05, 0.45, 0.05, 0.25]             # 
vpt00 = ( vpt + ([0.050]*2 + [0.32]*2) ).to_a    # 
vpt01 = ( vpt + ([0.474]*2 + [0.32]*2) ).to_a    # 
vpt10 = ( vpt + ([0.050]*2 + [0.10]*2) ).to_a    # 
vpt11 = ( vpt + ([0.474]*2 + [0.10]*2) ).to_a    # 



GGraph.set_fig('viewport'=>vpt00, 'itr'=>itr)
GGraph.set_axes('xunits'=>'','yunits'=>'','xtitle'=>'') 
DCL.uzpset('labelxb',false)
#GGraph.tone( mam, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
#GGraph.contour( mam, false , 'annot'=>false, 'titl'=>'' )
GGraph.line( mam, false , 'annot'=>false, 'titl'=>'','max'=>0.005 )
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','Spring(MAM)', -1) ; DCL.uzpset('pad1',0.7)

GGraph.set_fig('viewport'=>vpt01, 'new_frame'=>false, 'itr'=>itr)
GGraph.set_axes('ytitle'=>'')
DCL.uzpset('labelyl',false)
#GGraph.tone( jja, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
#GGraph.contour( jja, false , 'annot'=>false, 'titl'=>'' )
GGraph.line( jja, false , 'annot'=>false, 'titl'=>'' )
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','Summer(JJA)',-1) ; DCL.uzpset('pad1',0.7)

GGraph.set_fig('viewport'=>vpt10, 'new_frame'=>false, 'itr'=>itr)
GGraph.set_axes('ytitle'=>nil,'xtitle'=>nil)
DCL.uzpset('labelyl',true); DCL.uzpset('labelxb',true)
#GGraph.tone( son, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
#GGraph.contour( son, false , 'annot'=>false, 'titl'=>'' )
GGraph.line( son, false , 'annot'=>false, 'titl'=>'' )
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','Autumn(SON)',-1) ; DCL.uzpset('pad1',0.7)

GGraph.set_fig('viewport'=>vpt11, 'new_frame'=>false,  'itr'=>itr)
GGraph.set_axes('ytitle'=>'')
DCL.uzpset('labelyl',false)
#GGraph.tone( djf, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
#GGraph.contour( djf, false , 'annot'=>false, 'titl'=>'' )
GGraph.line( djf, false , 'annot'=>false, 'titl'=>'' )
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','Winter(DJF)',-1) ; DCL.uzpset('pad1',0.7)


DCL::sgtxzv(0.5,vpt00[3]+0.028,"25 years mean Precipitation Rate (1979-2003)",
	        1.15*DCL.uzpget('rsizec2'),0,0,3)
DCL::sgtxzv(0.88,vpt00[3]+0.01,"(units:#{djf.data.units.to_s})",
	        0.9*DCL.uzpget('rsizec2'),0,0,3)
#DCL::sgtxzv(0.5,vpt10[2]-0.05,"CONTOUR INTERVAL : 8.000E+09",
#	        0.85*DCL.uzpget('rsizec2'),0,0,3)
DCL.grcls

