#!/usr/bin/env ruby
=begin
= mkfig-MID_JET.rb -- 中緯度ジェットの強さの時系列
=end

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

# タイトル
fig_title = "Midlatitude jet stream at Troposphere"
# y 軸タイトル
y_axis_title = "Westerly Wind velocity"

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
gsouth = []
gnorth = []
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).cut("level"=>150..400) # 100 - 300 mb間を切り取り
    gsouth << gphys.cut("lat"=>-60..-20).mean("lon").data.max            # 南半球
    gnorth << gphys.cut("lat"=>20..60).mean("lon").data.max              # 北半球
    va_name = gphys.data.get_att("long_name")
    va_unit = gphys.data.get_att("units")

    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(axmonth)

gphys_north = GPhys.new(grid, VArray.new(NArray.to_na(gnorth)).rename!(y_axis_title).set_att("line_name", "Maximum "+ fig_title+" at Northern Hemisphere") )
gphys_south = GPhys.new(grid, VArray.new(NArray.to_na(gsouth)).rename!(y_axis_title).set_att("line_name", "Maximum "+ fig_title+" at Southern Hemisphere") )

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

# 軸の属性設定
gphys_north.coord(0).set_att('long_name','month')
gphys_north.coord(0).set_att('units','month since 1980 JAN')
gphys_south.coord(0).set_att('long_name','month')
gphys_south.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 倍に
$OPT_f = true                           # ps に落す 

##
# お絵描きメイン

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

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

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

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

