#!/usr/bin/env ruby
=begin
= mkfig-STRM.rb -- 質量流線関数の振幅の時系列
=end

##########  設定部分 ここから #################
year = [1979, 2003]
month = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]   # 初期値 
var = "strm"
var_dir = "STRM"

# タイトル
fig_title = "Mass-stream function"

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
g_850 = []
g_200 = []
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)
    va_850 << gphys.cut("level"=>850).data
    va_200 << gphys.cut("level"=>200).data
    va_name = gphys.data.get_att("long_name")
    va_unit = gphys.data.get_att("units")

    ax_lon = gphys.axis("lon")
    ax_lat = gphys.axis("lat")
    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


grid = Grid.new(ax_lon, ax_lat, axmonth)

gphys_850 = GPhys.new(grid, VArrayComposit.new(va_850).rename!(fig_title).set_att("line_name", "Maximum "+fig_title) )
gphys_200 = GPhys.new(grid, VArrayComposit.new(va_200).rename!(fig_title).set_att("line_name", "Minimun "+fig_title) )

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

# 軸の属性設定
gphys_max.coord(2).set_att('long_name','month')
gphys_max.coord(2).set_att('units','month since 1990 JAN')
gphys_min.coord(2).set_att('long_name','month')
gphys_min.coord(2).set_att('units','month since 1990 JAN')



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

##
# 事前準備

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

##
# お絵描きメイン

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

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

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

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

