# -*- coding: utf-8 -*-
# エネルギースペクトルを図に描く
require "numru/ggraph"
include NumRu

# ----- コマンドラインオプション -----
tro_file = ARGV[0].to_s         # 第1引数は順圧モードのスペクトルを取り出してくるファイル名(必須)
cli_file = ARGV[1].to_s         # 第2引数は傾圧モードのスペクトルを取り出してくるファイル名(必須)
iws = ( ARGV[2] || 1 ).to_i     # 装置番号 (1,2,or 4)
t0 = ( ARGV[3] || 300 ).to_i      # 第3引数は最初の時刻
t1 = ( ARGV[4] || 500 ).to_i       # 第4引数は最後の時刻
tsleep = [ ( ARGV[4] || 0.0 ).to_f, 0.0 ].max  # 第5引数は描画間隔(秒)
wait = ( tsleep <= 0.0 )            #->true/false; 0以下(true)ならマウスクリックを待つ

# ----- 使用データ -----
tro_1deksp = GPhys::IO.open tro_file, 'tro_1deksp'
cli_1deksp = GPhys::IO.open cli_file, 'cli_1deksp'

tro_1deksp = tro_1deksp.copy
tro_1deksp.name = "barotropic"
cli_1deksp = cli_1deksp.copy
cli_1deksp.name = "baroclinic"
total = tro_1deksp + cli_1deksp
total.name = "total"

# k^{-5/3} の直線を描く
x1 = NArray.sfloat(50)
y1 = NArray.sfloat(50)
x1[0] = 6.0
y1[0] = 1.0*10**(0)
for k in 0..48
  x1[k+1] = x1[k] + 1.0
  y1[k+1] = (x1[k+1]/x1[0])**(-5/3)*y1[0]
end
# k^{-3} の直線を描く
x2 = NArray.sfloat(50)
y2 = NArray.sfloat(50)
x2[0] = 6.0
y2[0] = 1.0*10.0**(0)
for k in 0..48
  x2[k+1] = x2[k] + 1.0
  y2[k+1] = (x2[k+1]/x2[0])**(-3)*y2[0]
end

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

title = DCL.csgi(131) + DCL.csgi(164) + "= -5.280" + DCL.csgi(194) + '10|-17"'
prep_dcl(iws,wait)
# ----- エネルギースペクトルの図 -----
GGraph.set_fig("itr"=>4,"viewport"=>[0.15,0.7,0.15,0.75])
GGraph.set_axes("xtitle"=>"k","ytitle"=>" ",'xside'=>'b', 'yside'=>'l')
GGraph.line total.cut("t"=>t0..t1).mean("t"),true,"title"=>title,"max"=>5.0*10**0,"min"=>5.0*10**(-6),"legend"=>true,"type"=>1,"index"=>3,"annotate"=>false
GGraph.line tro_1deksp.cut("t"=>t0..t1).mean("t"),false,"legend"=>true,"type"=>2,"index"=>2
GGraph.line cli_1deksp.cut("t"=>t0..t1).mean("t"),false,"legend"=>true,"type"=>3,"index"=>2
DCL.uulinz(x1,y1,3,1)
DCL.uulinz(x2,y2,4,1)

DCL.sgstxs(0.02)
DCL.swpset("lsysfnt",false)   # システムフォントを使用
DCL.sgtxu(80,1.0*10**(-2),'k|-5/3"')
DCL.sgtxu(70,1.0*10**(-3),'k|-3"')

DCL.grcls      # 描画終了処理
