#!/usr/bin/env ruby
=begin
= mkfig-PRATE_EQ_LON.rb -- 単位面積／単位時間あたりの熱帯域降雨量の経度-時間図
=end

##########  設定部分 ここから #################
year = [1980, 2000]
range = [nil, nil]

month = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]   # 初期値 
var = "prate"
var_dir = "PRATE"

lat_max = 30
lat_min = -30

# ps に落す 
$OPT_f = true

# タイトル
fig_title = "CMAP Precipitation rate at Equatorial Surface"

# y 軸タイトル
y_axis_title = "Precipitation rate"

vp = [0.15, 0.85, 0.2, 0.55]

if ARGV == 4
  year = ARGV[0..1]
  range = ARGV[2..3]
end

##########  設定部分 ここまで #############

require "getopts"        # for option_parse
require "numru/netcdf"
require "numru/ggraph"
include NumRu
require "libdraw-n"
include Draw
require "libgphys-n"


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




################################################################
#                        make gphys 
################################################################


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

year[0].upto(year[1]) do |y|
  month.each do |m|
    path = "../../#{var_dir}.CMAP/"
    name = "#{var_dir}_#{y}-#{m}_CMAP.nc"

    gphys = GPhys::NetCDF_IO.open(path+name, var)                        

    gphys = gphys.cut("lat"=>lat_max..lat_min).mean("lat")

    va_name = gphys.data.get_att("long_name")
    va_unit = gphys.data.get_att("units")

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

    axmonth << DCL::dateg3(year[0],1,1,y.to_i,m.to_i,1)
    i += 1
  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_array2 = NArray.to_na(va_array).newdim(0)
vacomposit = VArrayComposite.new(va_array2)
grid = $grid.insert_axis!(0, axmonth)

gphys = GPhys.new(grid, vacomposit.rename!(y_axis_title).set_att("line_name", fig_title))

## データの属性設定
gphys.data.set_att('units',va_unit)

# 軸の属性設定
#gphys.coord(0).set_att('long_name','month')
#gphys.coord(0).set_att('units','month since 1980 JAN')


################################################################
#                        描画ルーチン
################################################################

##
# 事前準備

DCL.uscset('cyspos', 'B' )              # y 軸の単位の位置を下方へ 
rsizel2 = DCL.uzrget('rsizel2')         # 現在のラベルサイズを取得
DCL.uzrset('rsizel2', rsizel2*0.42 )    # ラベルサイズをデフォルトの 0.5 倍に

##
# お絵描きメイン

Draw::mkwin(vp)                         # ウィンドウオープン

idate = (year[0].to_s+"0101").to_i      # 日付 0 日. 
ndate = (year[-1].to_s+"1201").to_i     # 日付 最後の日. 
nd = DCL.dateg1(idate,ndate)            # 日日数
GGraph.set_axes("date?"=>["x", idate, nd])        
                                        # 日付軸を有効にする

 levels = NArray[0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 1000]*10e-6
 patterns = NArray[25999, 35999, 45999, 40999, 55999, 60999, 65999, 70999, 80999, 85999, 90999]
 GGraph.tone(gphys,
	    true,
	    "lev"=>levels,
	    "patterns"=> patterns,
	    "exchange"=>true
	    )

DCL::uxsttl("b", "YEAR", 0)             # x 軸タイトルを "YEAR" に 
DCL::uxmttl("t", fig_title, -1)         # タイトル

DCL::Util::color_bar(
		       "levels"=>levels,
		       "colors"=>patterns,
		       "eqlev"=>true,
		       "tick1"=>12,"tick2"=>1,
		       "nobound"=>2
		     )

Draw::clwin                             # ウィンドウクローズ

