#!/usr/bin/env ruby
=begin
= mkfig-PRATE_SYDNEY.rb -- 単位面積／単位時間あたりのシドニー付近降雨量の月変動 (数十年分の平均値)
=end

##########  設定部分 ここから #################
year = [1980, 2001]
month = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]   # 初期値 
lat = -33
lon = 152
var = "prate"
var_dir = "PRATE"

# ps に落す 
$OPT_f = true

# タイトル
fig_title = "Monthly Precipitation valiability at SYDNEY-AREA"

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

vp = [0.15, 0.85, 0.2, 0.55]

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

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
gprate = NArray.float(12).fill!(0.0)
va_name = ""
va_unit = ""

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

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

    gphys = gphys.cut("lat"=>lat).cut("lon"=>lon).val[0]
    gprate[m.to_i-1] += gphys 
  end
    i += 1
end

axmonth = NArray[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

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

grid = Grid.new(axmonth)
p i
gphys = GPhys.new(grid, VArray.new(gprate/i).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])        
                                        # 日付軸を有効にする
Draw::plot_line_main([gphys])

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

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

