# -*- coding: utf-8 -*-
# エネルギースペクトルの結果を一覧で表示する
require "numru/ggraph"
include NumRu

# ----- コマンドラインオプション -----
iws = ( ARGV[0] || 1 ).to_i     # 装置番号 (1,2,or 4)

# ----- 使用データ -----
file_res1_tro = './result1/eksp/tro_1deksp.nc'
file_res1_cli = './result1/eksp/cli_1deksp.nc'
file_res2_tro = './result2/tro_1deksp.nc'
file_res2_cli = './result2/cli_1deksp.nc'
file_res3_tro = './result3/tro_1deksp.nc'
file_res3_cli = './result3/cli_1deksp.nc'
file_res4_tro = './result4/tro_1deksp.nc'
file_res4_cli = './result4/cli_1deksp.nc'

t0 = 1800
t1 = 2000
t2 = 300
t3 = 500

data_res1_tro = (GPhys::IO.open file_res1_tro, 'tro_1deksp').cut("t"=>t0..t1).mean("t")
data_res1_cli = (GPhys::IO.open file_res1_cli, 'cli_1deksp').cut("t"=>t0..t1).mean("t")
data_res2_tro = (GPhys::IO.open file_res2_tro, 'tro_1deksp').cut("t"=>t2..t3).mean("t")
data_res2_cli = (GPhys::IO.open file_res2_cli, 'cli_1deksp').cut("t"=>t2..t3).mean("t")
data_res3_tro = (GPhys::IO.open file_res3_tro, 'tro_1deksp').cut("t"=>t2..t3).mean("t")
data_res3_cli = (GPhys::IO.open file_res3_cli, 'cli_1deksp').cut("t"=>t2..t3).mean("t")
data_res4_tro = (GPhys::IO.open file_res4_tro, 'tro_1deksp').cut("t"=>t2..t3).mean("t")
data_res4_cli = (GPhys::IO.open file_res4_cli, 'cli_1deksp').cut("t"=>t2..t3).mean("t")

data_res1_tot = data_res1_tro + data_res1_cli
data_res2_tot = data_res2_tro + data_res2_cli
data_res3_tot = data_res3_tro + data_res3_cli
data_res4_tot = data_res4_tro + data_res4_cli 

data_res1_tro.name = "barotropic"
data_res1_cli.name = "baroclinic"
data_res1_tot.name = "total"
data_res2_tro.name = "barotropic"
data_res2_cli.name = "baroclinic"
data_res2_tot.name = "total"
data_res3_tro.name = "barotropic"
data_res3_cli.name = "baroclinic"
data_res3_tot.name = "total"
data_res4_tro.name = "barotropic"
data_res4_cli.name = "baroclinic"
data_res4_tot.name = "total"

# ----- DCL設定 -----
def prep_dcl(iws=1) # iws : DCL出力デバイス．1,4:画面, 2:PS
  DCL.sgscmn(39)                # カラーマップ番号
  DCL.swpset("iwidth",1000)     # 画面の幅
  DCL.swpset("iheight",1000)    # 画面の高さ
  DCL.swpset("lalt",true)      # 裏で描画（パラパラアニメ用）   
   DCL.swpset("ifl",1)          # png で出力
#  DCL.swpset("lsysfnt",true)   # システムフォントを使用
#  DCL.swcset("fontname","Hiragino Kaku Gothic")
  DCL.sglset("lcntl",true)     # 上付き下付き文字を有効
  DCL.sgiset("ifont",2)
  DCL.gropn(iws)
#  DCL.sldiv("y",2,2)           # 画面分割 (描画順("y"oko/"t"ate),数:横,数:縦)
  DCL.sgpset('lfull',true)     # 全画面表示
#  DCL.sgpset('isub', 96)       # 下付き添字を表す制御文字を '_' から '`' に
  DCL.uzfact(0.5)             # 座標軸の文字列サイズを定数倍
  DCL.sgpset('lcorner',false)  # コーナーマークを書かない
  DCL.glpset('lmiss',true)     # DCLの欠損値処理を on に
end

case1 = DCL.csgi(131) + DCL.csgi(164) + "= 0"
case2 = DCL.csgi(131) + DCL.csgi(164) + "= -5.280" + DCL.csgi(194) + '10|-17"'
case3 = DCL.csgi(131) + DCL.csgi(164) + "= 5.280" + DCL.csgi(194) + '10|-17"'
case4 = DCL.csgi(131) + DCL.csgi(164) + "= 1.056" + DCL.csgi(194) + '10|-16"'

max = 5.0*10**0
min = 1.0*10**(-9)
# ytitle = 'E_tot"(k),' + 'E_' + DCL.csgi(174) + '"(k),' + 'E_' + DCL.csgi(170) + '"(k)'
ytitle = 'E(k)'

# k^{-5/3} の直線を描く
x1 = NArray.sfloat(150)
y1 = NArray.sfloat(150)
x1[0] = 20.0
y1[0] = 1.0*10**(-1)
for k in 0..148
  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(150)
y2 = NArray.sfloat(150)
x2[0] = 20.0
y2[0] = 1.0*10.0**(-1)
for k in 0..148
  x2[k+1] = x2[k] + 1.0
  y2[k+1] = (x2[k+1]/x2[0])**(-3)*y2[0]
end

# k^{-5/3} の直線を描く
x3 = NArray.sfloat(60)
y3 = NArray.sfloat(60)
x3[0] = 10.0
y3[0] = 1.0*10**(-1)
for k in 0..58
  x3[k+1] = x3[k] + 1.0
  y3[k+1] = (x3[k+1]/x3[0])**(-5/3)*y3[0]
end
# k^{-3} の直線を描く
x4 = NArray.sfloat(70)
y4 = NArray.sfloat(70)
x4[0] = 10.0
y4[0] = 1.0*10.0**(-1)
for k in 0..68
  x4[k+1] = x4[k] + 1.0
  y4[k+1] = (x4[k+1]/x4[0])**(-3)*y4[0]
end

vpt = NArray[0.15, 0.5, 0.15, 0.5]             # ビューポートサイズ (1:1)
vpt00 = ( vpt + ([0.0]*2 + [0.4]*2) ).to_a    # x,y方向にずらしてArray化
vpt01 = ( vpt + ([0.374]*2 + [0.4]*2) ).to_a    # x,y方向にずらしてArray化
vpt10 = ( vpt + ([0.0]*2 + [-0.02]*2) ).to_a    # x,y方向にずらしてArray化
vpt11 = ( vpt + ([0.374]*2 + [-0.02]*2) ).to_a    # x,y方向にずらしてArray化

prep_dcl(iws)
GGraph.set_fig("itr"=>4,'viewport'=>vpt00)
GGraph.set_axes('xunits'=>'','yunits'=>'','xtitle'=>'k','ytitle'=>ytitle,'xside'=>'b', 'yside'=>'l') 
DCL.uzpset('labelxb',true)
GGraph.line(data_res1_tot,true,'annot'=>false, 'titl'=>' ', "max"=>max,"min"=>min,"legend"=>false,"type"=>1,"index"=>283,"annotate"=>false,"legend_vx"=>0.3,"legend_vy"=>0.94)
# GGraph.line(data_res1_tro,false,'annot'=>false,'titl'=>' ',"legend"=>true,"type"=>1,"index"=>863,"annotate"=>false,"legend_vx"=>0.42,"legend_vy"=>0.94)
# GGraph.line(data_res1_cli,false,'annot'=>false,'titl'=>' ',"legend"=>true,"type"=>1,"index"=>503,"annotate"=>false,"legend_vx"=>0.58,"legend_vy"=>0.94)
DCL.uzpset('pad1',-2.0) ; DCL.uxsttl('t',case1,-1) ; DCL.uzpset('pad1',0.7)
DCL.uulinz(x1,y1,3,2)
DCL.uulinz(x2,y2,4,2)
DCL.sgstxs(0.02)
# DCL.sgtxzu(180,1.0*10**(-2),'k|-5/3"',0.8*DCL.uzpget('rsizec2'),0,0,3)
# DCL.sgtxzu(240,1.5*10**(-4),'k|-3"',0.8*DCL.uzpget('rsizec2'),0,0,3)

GGraph.set_fig("itr"=>4,'viewport'=>vpt01,'new_frame'=>false)
DCL.uzpset('labelyl',false)
GGraph.line(data_res2_tot,true,'annot'=>false, 'titl'=>' ', "max"=>max,"min"=>min,"legend"=>false,"type"=>1,"index"=>283,"annotate"=>false)
# GGraph.line(data_res2_tro,false,'annot'=>false,'titl'=>' ',"legend"=>false,"type"=>1,"index"=>863,"annotate"=>false)
# GGraph.line(data_res2_cli,false,'annot'=>false,'titl'=>' ',"legend"=>false,"type"=>1,"index"=>503,"annotate"=>false)
DCL.uzpset('pad1',-2.0) ; DCL.uxsttl('t',case2,-1) ; DCL.uzpset('pad1',0.7)
DCL.uulinz(x3,y3,3,2)
DCL.uulinz(x4,y4,4,2)
DCL.sgstxs(0.02)
# DCL.sgtxzu(100,1.5*10**(-3),'k|-5/3"',0.8*DCL.uzpget('rsizec2'),0,0,3)
# DCL.sgtxzu(90,2.0*10**(-4),'k|-3"',0.8*DCL.uzpget('rsizec2'),0,0,3)

GGraph.set_fig("itr"=>4,'viewport'=>vpt10,'new_frame'=>false)
DCL.uzpset('labelyl',true); DCL.uzpset('labelxb',true)
GGraph.line(data_res3_tot,true,'annot'=>false, 'titl'=>' ', "max"=>max,"min"=>min,"legend"=>false,"type"=>1,"index"=>283,"annotate"=>false)
# GGraph.line(data_res3_tro,false,'annot'=>false,'titl'=>' ',"legend"=>false,"type"=>1,"index"=>863,"annotate"=>false)
# GGraph.line(data_res3_cli,false,'annot'=>false,'titl'=>' ',"legend"=>false,"type"=>1,"index"=>503,"annotate"=>false)
DCL.uzpset('pad1',-2.0) ; DCL.uxsttl('t',case3,-1) ; DCL.uzpset('pad1',0.7)
DCL.uulinz(x3,y3,3,2)
DCL.uulinz(x4,y4,4,2)
DCL.sgstxs(0.02)
# DCL.sgtxzu(100,1.5*10**(-3),'k|-5/3"',0.8*DCL.uzpget('rsizec2'),0,0,3)
# DCL.sgtxzu(90,2.0*10**(-4),'k|-3"',0.8*DCL.uzpget('rsizec2'),0,0,3)

GGraph.set_fig("itr"=>4,'viewport'=>vpt11,'new_frame'=>false)
DCL.uzpset('labelyl',false)
GGraph.line(data_res4_tot,true,'annot'=>false, 'titl'=>' ', "max"=>max,"min"=>min,"legend"=>false,"type"=>1,"index"=>283,"annotate"=>false)
# GGraph.line(data_res4_tro,false,'annot'=>false,'titl'=>' ',"legend"=>false,"type"=>1,"index"=>863,"annotate"=>false)
# GGraph.line(data_res4_cli,false,'annot'=>false,'titl'=>' ',"legend"=>false,"type"=>1,"index"=>503,"annotate"=>false)
DCL.uzpset('pad1',-2.0) ; DCL.uxsttl('t',case4,-1) ; DCL.uzpset('pad1',0.7)
DCL.uulinz(x3,y3,3,2)
DCL.uulinz(x4,y4,4,2)
DCL.sgstxs(0.02)
#DCL.sgtxzu(100,1.5*10**(-3),'k|-5/3"',0.8*DCL.uzpget('rsizec2'),0,0,3)
#DCL.sgtxzu(90,2.0*10**(-4),'k|-3"',0.8*DCL.uzpget('rsizec2'),0,0,3)

DCL::sgtxzv(0.5,vpt00[3]+0.07,'Energy Spectra',
	    1.15*DCL.uzpget('rsizec2'),0,0,3)

DCL.grcls      # 描画終了処理
