# -*- coding: euc-jp -*-
#
# 2011/07/13 x 軸の設定を 30 度間隔に変更
# 2011/07/11 切り出し時刻を編集, 描画するデータを引数で与えるように設定
# 2011/07/08 軸の設定追加. window の設定を引数で与えるよう変更. 画像格納作業を追加
# 2011/07/07 NECP と OLRA を重ね書きするように編集
# 2011/07/05 ulwrf の部分を修正
# 2011/07/04 drawgm.rb を元に作成
#

require "numru/ggraph"
include NumRu

mode = ARGV[0].to_s
win  = ARGV[1].to_i
file = ARGV[2].to_s 


tar = 'figure'


dir    = '..'
vname1 = 'OLRA'
vname2 = 'OSRA'
vname3 = 'ulwrf'
vname4 = 'uswrf'
vname5 = 'dswrf'
vname3file = 'ulwrftop.mon.mean'
vname4file = 'uswrftop.mon.mean'
vname5file = 'dswrftop.mon.mean'

#
# OLRA, OSRA の データは 11 - 20 年目を使用する
#

ts = 10 * 365 * 4 + 1
te = 20 * 365 * 4

#
# NECP の データは 1988-2007 年度の各月平均を使用する
#
ts2 = 17417760
te2 = 17592240


#
# OLRA, OSRA にかける重み関数の読み込み
#
gphysw = GPhys::NetCDF_IO.open(dir+'/'+vname1+".nc", 'lat_weight')
weight = gphysw.val
weight = weight / weight.sum(0)

#p weight
#p weight.reshape!(1,gphysw.shape[0])

#
# 各 nc ファイルの読み込み
#
gphys1 = GPhys::NetCDF_IO.open(dir+'/'+vname1+".nc", vname1)
gphys2 = GPhys::NetCDF_IO.open(dir+'/'+vname2+".nc", vname2)
gphys3 = GPhys::NetCDF_IO.open('/home2/yukai-home2/NECP'+'/'+vname3file+".nc", vname3)
gphys4 = GPhys::NetCDF_IO.open('/home2/yukai-home2/NECP'+'/'+vname4file+".nc", vname4)
gphys5 = GPhys::NetCDF_IO.open('/home2/yukai-home2/NECP'+'/'+vname5file+".nc", vname5)

gphys6 = gphys5 - gphys4

#
# 変数 time の定義 
#
#tm = gphys3.shape[2] # number of elements for time dimension
time = gphys1.coord('time').val
#time2 = gphys3.coord('time').val

#
# DCLのオープンと設定 
#
DCL.gropn(win)
#DCL.sldiv('y',2,2)           # 2x2に画面分割, 'y'=yoko: 左上→右上→左下...
DCL.sgpset('lcntl', false)   # 制御文字を解釈しない
DCL.sgpset('lfull',true)     # 全画面表示
DCL.uzfact(0.75)             # 座標軸の文字列サイズを 0.75 倍
DCL.sgpset('lfprop',true)    # プロポーショナルフォントを使う

GGraph.set_fig('viewport'=>[0.15,0.7,0.2,0.6]) # 描画領域の設定
GGraph.set_axes('xlabelint'=>30)

#
# 描画
#



   GGraph.set_fig('window'=>[-90,90,140,300])     # 軸の最大/最小値を設定. [x 軸最小, x 軸最大, y 軸最小, y 軸最大]

if mode == "c" then
  
      GGraph.line( gphys1.average(0).cut('time'=>time[ts]..time[te]).mean('time'),true )

elsif mode == "o"

      GGraph.line( gphys3.average(0).cut('time'=>ts2..te2).mean('time'),true, "type"=> 2,"index"=>2)

else

      GGraph.line( gphys1.average(0).cut('time'=>time[ts]..time[te]).mean('time'),true )
      GGraph.line( gphys3.average(0).cut('time'=>ts2..te2).mean('time'),false, "type"=> 2,"index"=>2)

   end

=begin

   GGraph.set_fig('window'=>[-90,90,0,350])     # 軸の最大/最小値を設定. [x 軸最小, x 軸最大, y 軸最小, y 軸最大]

   if mode == "c" then

      GGraph.line( gphys2.average(0).cut('time'=>time[ts]..time[te]).mean('time'),true )

   elsif mode == "o"

      GGraph.line( gphys4.average(0).cut('time'=>ts2..te2).mean('time'),true, "type"=> 2,"index"=>2)

   else

      GGraph.line( (gphys2.average(0).cut('time'=>time[ts]..time[te]).mean('time')).abs,true )
      GGraph.line( gphys6.average(0).cut('time'=>ts2..te2).mean('time'),false, "type"=> 2,"index"=>2)

   end

=end



DCL.grcls

#
# png 画像に変換して $tar ディレクトリへ格納
# ただし, ps 出力した場合のみ実行.


if win == 2 then

  com = sprintf("convert -rotate 90 dcl.ps " +file+".png")
  system(com)

  com = sprintf("mv " +file+".png  ./" +tar+ "")
  system(com)

end


