# coding: cp932
# = 2012年1月の北半球の総観場(NCEP再解析)
# 使用法:
#   ruby sample_synop.rb [オプション]
# オプション（スペースで区切って並べる）:
#   1番目: DCLの装置番号 (default: 1)
#   2番目: 描画間隔(秒) (default: 0.5) 正ならアニメ，0以下ならクリックで次を表示
#   3番目: 最初の日付(1月何日か) (default: 1)
#   4番目: 最後の日付(1月何日か) (default: 31)
# 使用例:
#    ruby sample_synop.rb
#    ruby sample_synop.rb 4 1
#    ruby sample_synop.rb 4 0 21 30

require "numru/ggraph"
include NumRu

#< コマンドラインオプション >
iws = ( ARGV[0] || 1 ).to_i    # 第1引数は装置番号 (1,2,or 4)
tsleep = [ ( ARGV[1] || 0.5 ).to_f, 0.0 ].max  # 第2引数は描画間隔(秒)
wait = ( tsleep <= 0.0 )       #->true/false; 0以下(true)ならマウスクリックを待つ
fday = ( ARGV[2] ||  1 ).to_i  # 第3引数は最初の日付(1月何日か)
lday = ( ARGV[3] || 31 ).to_i  # 第4引数は最後の日付(1月何日か)

#< 使用データ >
tmp = GPhys::IO.open 'air.2012-01.nc', 'air'
hgt = GPhys::IO.open 'hgt.2012-01.nc', 'hgt'

#< DCL設定(メソッド) >
def prep_dcl(iws=1,wait=false)
  DCL.sgscmn(4)              # カラーマップ番号指定
  DCL.swpset("iwidth",900)   # 画面の幅
  DCL.swpset("iheight",450)  # 画面の高さ
  DCL.swpset("lwait",wait)   # 次の描画の前にマウスクリックを待つ
  DCL.swpset("lalt",true)    # 裏で描画（パラパラアニメ用）
  DCL.gropn(iws)             # DCL描画装置初期化 (iws=1 or 2)
  DCL.sldiv("y",2,1)         # 画面分割 (描画順("y"oko/"t"ate),数:横,数:縦)
  DCL.sgpset('isub', 96)     # 下付き添字を表す制御文字を '_' から '`' に
                             # (アンダースコアはよく使われるので，エラー防止のため)
  DCL.glpset('lmiss',true)   # DCLの欠損値処理を on に．
end

#< 描画 >
prep_dcl(iws,wait)
for d in fday..lday
  time = Date.new(2012,1,d)
  #<fig 1>
  GGraph.set_fig "itr"=>32,"viewport"=>[0.05,0.81,0.12,0.88]  # 32:正距方位図法
  GGraph.set_map "coast_world"=>true
  GGraph.tone tmp.cut("level"=>850,"time"=>time),true,"title"=>"T & Z",
     "min"=>230,"max"=>300,"int"=>5
  GGraph.contour hgt.cut("level"=>850,"time"=>time),false,"int"=>50
  GGraph.color_bar
  #<fig 2>
  GGraph.set_fig "itr"=>1,"viewport"=>[0.16,0.81,0.2,0.85]
  GGraph.set_axes "xmaplabel"=>"lon", "xlabelint"=>90, "xtickint"=>30
  GGraph.tone tmp.cut("lat"=>45,"time"=>time,"level"=>1000..100).eddy(0),true,
     "title"=>"T' & Z'","min"=>-24,"max"=>24,"int"=>4
  GGraph.contour hgt.cut("lat"=>45,"time"=>time,"level"=>1000..100).eddy(0),
     false,"int"=>100
  GGraph.color_bar
  sleep tsleep
end
DCL.grcls      # 描画終了処理．
