#!/usr/bin/env ruby
=begin
= mkfig-QBO.rb -- NCEP の QBO のお絵描きスクリプト
=end

##########  設定部分 ここから #################
year = [1980, 2001]
month = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]   # 初期値 
#month = ["03", "06", "09", "12"]   # 初期値 
var = "uwnd"
#vp = [0.15, 0.85, 0.25, 0.6]
vp = [0.15, 0.85, 0.35, 0.5]
##########  設定部分 ここまで #############

require "getopts"        # for option_parse
require "numru/netcdf"

class NArray # for deep_copy
  def self._load(o); to_na(*Marshal::load(o)).ntoh; end
  def _dump(limit); Marshal::dump([hton.to_s, typecode, *shape]); end
end

module NumRu
  class NetCDFVar
    def scaled_get(hash=nil)
      sf = att('scale_factor')
      ao = att('add_offset')
      if ( sf == nil && ao == nil ) then
	# no scaling --> just call get
	simple_get(hash)
      else
	if (sf != nil) 
	  csf = sf.get
	       if csf.is_a?(NArray) then  # --> should be a numeric
		 csf = csf[0]
	       elsif csf.is_a?(String)
		 raise NetcdfError, "scale_factor is not a numeric"
	       end
	  if(csf == 0) then; raise NetcdfError, "zero scale_factor"; end
	else
	  csf = 1.0      # assume 1 if not defined
	end
	if (ao != nil) 
	  cao = ao.get
	  if cao.is_a?(NArray) then  # --> should be a numeric
	    cao = cao[0]
	  elsif csf.is_a?(String)
	    raise NetcdfError, "add_offset is not a numeric"
	  end
	    else
	  cao = 0.0      # assume 0 if not defined
	end
	var = simple_get(hash)
	var = var.to_type( NArray::FLOAT )
	csf*var+cao
      end
    end  
    alias put scaled_put
    alias get scaled_get
  end
end

require "numru/ggraph"
include NumRu

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

## make compisit-GPhys object 
# make array which set the gphys objects.
va_array = []
axmonth = []
va = []
i = 0

year[0].upto(year[1]) do |y|
  month.each do |m|
    path = "../../UWND.NCEP/UWND.#{y}.NCEP/"
    name = "UWND_#{y}-#{m}_NCEP.nc"

    gphys = GPhys::NetCDF_IO.open(path+name, var)
    gphys = gphys.mean("lon")
    va_name = gphys.data.name

    $grid = gphys.grid_copy
    na = gphys.data.val 
    na = na.newdim!(-1)
    axmonth << i
    i += 1
    va = VArray.new(na).rename!(va_name)
    va_array << va
  end
end

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

va_array = NArray.to_na(va_array).newdim(0,0)
vacomposit = VArrayComposite.new(va_array)
grid = $grid.insert_axis!(1, axmonth)

gphys = GPhys.new(grid, vacomposit)
gphys.coord(2).set_att('long_name','month')
gphys.coord(2).set_att('units','month since 1990 JAN')

DCL.gropn(2)
DCL.sgpset('lcntl', false)   # 制御文字を解釈しない
DCL.sgpset('lfull',true)     # 全画面表示
DCL.uzfact(0.75)             # 座標軸の文字列サイズを 0.75 倍
DCL.sgpset('lfprop',true)    # プロポーショナルフォントを使う

#< GGraph による 描画 >
GGraph.set_fig('viewport'=>vp)    # set_*: ずっと有効な設定
GGraph.set_fig('itr'=>2 )   
# 1枚目
#gphys = gphys.cut('lat'=>0)
gphys = gphys.cut('lat'=>0)
GGraph.tone( gphys, true, 'ltone'=>false, 'exchange'=>true )        # ゼロ以下にシェーディング
GGraph.contour(gphys, false, 'exchange'=>true)


DCL.grcls
