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

# ----- コマンドラインオプション -----
file = "ek-analysis.nc" # 第1引数はスペクトルを取り出してくるファイル名(必須)
iws = ( ARGV[0] || 1 ).to_i                    # 装置番号 (1,2,or 4)
tsleep = [ ( ARGV[1] || 0.0 ).to_f, 0.0 ].max  # 描画間隔(秒)
wait = ( tsleep <= 0.0 )            #->true/false; 0以下(true)ならマウスクリックを待つ

# ----- 使用データ -----
tro_flux1    = (GPhys::IO.open file, 'tro_flux1')
tro_flux2    = (GPhys::IO.open file, 'tro_flux2')
cli_flux1    = (GPhys::IO.open file, 'cli_flux1')
cli_flux2    = (GPhys::IO.open file, 'cli_flux2')
cli_flux3    = (GPhys::IO.open file, 'cli_flux3')

# ----- 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.gropn(iws)
  DCL.sglset("lcntl",true)     # 上付き下付き文字を有効
  DCL.sgiset("ifont",2)
  DCL.sgpset('isub', 96)       # 下付き添字を表す制御文字を '_' から '`' に
  DCL.glpset('lmiss',true)     # DCLの欠損値処理を on に
end

flux = tro_flux1 + tro_flux2 + cli_flux1 + cli_flux2 + cli_flux3
flux.name = "energy flux"

t0=300
t1=500

title = DCL.csgi(131) + DCL.csgi(164) + "= -5.280" + DCL.csgi(194) + '10|-17"'

x1 = NArray.sfloat(170)
y1 = NArray.sfloat(170)
y1.fill!(0.0)
x1[0] = 1.0
for k in 0..168
    x1[k+1] = x1[k] + 1.0
end

x2 = NArray.sfloat(141)
y2 = NArray.sfloat(141)
y2.fill!(0.0)
x2[0] = 30.0
for k in 0..139
    x2[k+1] = x2[k] + 1.0
end

# ----- 描画 -----
prep_dcl(iws,wait)
GGraph.set_fig("itr"=>3,"viewport"=>[0.20, 0.7, 0.25, 0.7])
GGraph.set_axes("ytitle"=>" ","xtitle"=>'k','xside'=>'b', 'yside'=>'l')
GGraph.line flux.cut("t"=>t0..t1).mean('t'),true,"title"=>title,"legend"=>false,"type"=>1, "max"=>1.5*10**(-2), "min"=>-1.5*10**(-2),"index"=>3,"annotate"=>false
DCL.uulinz(x1,y1,3,2)
GGraph.line flux.cut("t"=>t0..t1,"n"=>30..341).mean('t'),true,"title"=>title,"legend"=>false,"type"=>1, "max"=>8.0*10**(-4), "min"=>-1.0*10**(-4),"index"=>3,"annotate"=>false
DCL.uulinz(x2,y2,3,2)

DCL.grcls      # 描画終了処理
