# coding: utf-8
# -*- coding: utf-8 -*-
require "numru/ggraph"
include NumRu

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

# ----- 使用データ -----
tro_ektot  = GPhys::IO.open 'tro_ektot.nc', 'tro_ektot'
cli_ektot  = GPhys::IO.open 'cli_ektot.nc', 'cli_ektot'

total = tro_ektot + cli_ektot
total = total.copy
total.name = "total"
tro_ektot = tro_ektot.copy
tro_ektot.name = "barotropic"
cli_ektot = cli_ektot.copy
cli_ektot.name = "baroclinic"

x1 = NArray.sfloat(15)
y1 = NArray.sfloat(15)
x1[0] = 10.0
y1[0] = 5.0*10**(-3)
for k in 0..13
  x1[k+1] = x1[k] + 1.0
  y1[k+1] = y1[k]*1.0*10**(0.205)
end

x2 = NArray.sfloat(30)
y2 = NArray.sfloat(30)
x2[0] = 10.0
y2[0] = 5.0*10**(-3)
for k in 0..28
  x2[k+1] = x2[k] + 1.0
  y2[k+1] = y2[k]*1.0*10**(0.049)
end

# ----- DCL設定 -----
def prep_dcl(iws=1,wait=false) # iws : DCL出力デバイス．1,4:画面, 2:PS
#  DCL.swpset("iwidth",1200)     # 画面の幅
#  DCL.swpset("iheight",700)    # 画面の高さ
  DCL.swpset("iwidth",800)     # 画面の幅
  DCL.swpset("iheight",600)    # 画面の高さ
  DCL.swpset("lwait",wait)     # 次の描画の前にマウスクリックを待つ
  DCL.swpset("lalt",true)      # 裏で描画（パラパラアニメ用）
  DCL.swpset("ifl",1)          # png で出力
#  DCL.swpset("lsysfnt",true)   # システムフォントを使用
#  DCL.swcset("fontname","Hiragino Kaku Gothic")
  DCL.sgscmn(39) # カラーマップ番号の指定
  DCL.gropn(iws)
  DCL.sgpset('lfull',true)     # 全画面表示
  DCL.sglset("lcntl",true)     # 上付き下付き文字を有効
  DCL.sgiset("ifont",2)
  DCL.uzfact(0.9)              # 座標軸の文字列サイズを定数倍
#  DCL.sgpset('isub', 96)      # 下付き添字を表す制御文字を '_' から '`' に
  DCL.glpset('lmiss',true)     # DCLの欠損値処理を on に
end

ytitle = 'E_tot",' + 'E_' + DCL.csgi(150) + '", E_' + DCL.csgi(170) + '"'

prep_dcl(iws,wait)
# GGraph.set_fig("itr"=>1,"viewport"=>[0.2,0.78,0.12,0.55])
# GGraph.set_axes("xtitle"=>"t","ytitle"=>ytitle,'xside'=>'b', 'yside'=>'l')
# # ----- 全エネルギー -----
# GGraph.line(total,true,"title"=>" ","legend"=>true,"type"=>1,"max"=>170,"min"=>0,"index"=>283)
# # ----- 順圧エネルギー -----
# GGraph.line(tro_ektot,false,"legend"=>true,"type"=>1,"index"=>863)
# # ----- 傾圧エネルギー -----
# GGraph.line(cli_ektot,false,"legend"=>true,"type"=>1,"index"=>503)

# log スケール
GGraph.set_fig("itr"=>2,"viewport"=>[0.12,0.78,0.15,0.7])
GGraph.set_axes("xtitle"=>"t","ytitle"=>ytitle,'xside'=>'b', 'yside'=>'l')
# ----- 全エネルギー -----
GGraph.line(total.cut("t"=>0..250),true,"title"=>" ","legend"=>true,"type"=>1,"max"=>120,"min"=>5.0*10**(-4),"index"=>283)
# ----- 順圧エネルギー -----
GGraph.line(tro_ektot.cut("t"=>0..250),false,"legend"=>true,"type"=>1,"index"=>863)
# ----- 傾圧エネルギー -----
GGraph.line(cli_ektot.cut("t"=>0..250),false,"legend"=>true,"type"=>1,"index"=>503)
# ----- 成長率の傾き -----
DCL.uulinz(x1,y1,3,3)
DCL.uulinz(x2,y2,4,3)
DCL.sgstxs(0.02)
DCL.sglset("lcntl",true)     # 上付き下付き文字を有効   
DCL.swpset("lsysfnt",false)   # システムフォントを使用しない
DCL.sgiset("ifont",2)
char1 = DCL.csgi(152) + '_max"'
DCL.sgtxu(28,5.0*10**(0),char1)
char2 = DCL.csgi(152) + '_k=' + DCL.csgi(210) + '2"'
DCL.sgtxu(35,2.0*10**(-1),char2)

DCL.grcls      # 描画終了処理
