# -*- coding: euc-jp -*-
require "numru/ggraph"
include NumRu
include NMath

ncfn_add = [
#  nil,
  "../prog01.0_mkprofile/out/LW_Case25_Earth_Tropics_CO2-300ppmv.nc",
]

dira = [
  "./out",
]
idir = 0
ncfna = [Dir.glob(dira[idir]+"/profile*.nc").sort[-1]]
for idir in 0+1..(dira.size-1)
  ncfna << Dir.glob(dira[idir]+"/profile*.nc").sort[-1]
end
if ncfn_add != nil then
  for ifile in 0..(ncfn_add.size-1)
    ncfna << ncfn_add[ifile]
  end
end

for ifile in 0..(ncfna.size-1)
  print ifile, ":", ncfna[ifile], "\n"
end

vara = ['Temp', 'VMR', 'VMR']

for ivar in 0..2-1
  for ifile in 0..ncfna.size-1

    ncfn = ncfna[ifile]
    var  = vara[ivar]

    gp  = GPhys::IO.open( ncfn, var )

#    gp = gp.copy

    if ivar > 0 then
      gp = gp.cut('MolNum'=>ivar-1)
    end

    gpp = gp.coord('Press')


#    puts var
#    for k in 0..gp.val.size-1
#      puts gpp.val[k].to_s + ', ' + gp.val[k].to_s
#    end


    if ( ivar == 0 ) && ( ifile == 0 ) then

      print "1: Display,  2: File\n"
      citr = gets
      citr = citr.chomp!
      DCL.gropn(citr.to_i)
#      DCL.gropn(1)
      DCL.sldiv('y',3,1)           # 2x2に画面分割, 'y'=yoko: 左上→右上→左下...
      DCL.sgpset('lcntl', false)   # 制御文字を解釈しない
      DCL.sgpset('lfull',true)     # 全画面表示
      DCL.uzfact(0.7)             # 座標軸の文字列サイズを 0.75 倍
      DCL.sgpset('lfprop',true)    # プロポーショナルフォントを使う

      DCL.sgpset('lclip', true)

      #DCL.glpset('lmiss',true)
      #DCL.glpset('rmiss',rmiss)
    end

    if ifile == 0 then
      case ivar
      when 0 then
        x1 = 150; x2 = 350
        itr = 2
      when 1 then
        x1 = 1e-6; x2 = 1.0e-1
        x1 = 1e-6; x2 = 1.0e0
        itr = 4
      when 2 then
        x1 = 1e-8; x2 = 1.0e-4
        itr = 4
      end
      #< GGraph による 描画 >
      vpx1 = 0.1; vpx2 = 0.4; vpy1 = 0.15; vpy2 = 0.9
#      y1 = 1050.0; y2 = 1.0e-4
      y1 = 1050.0e2; y2 = 1.0e1
      #
      GGraph.set_fig 'itr'=>itr, 'viewport'=>[vpx1,vpx2,vpy1,vpy2], 'yrev'=>'units:Pa', 'window'=>[x1,x2,y1,y2]
      #GGraph.set_axes('xlabelint'=>30)
    end
    if ifile == 0 then
      flag_first = true
    else
      flag_first = false
    end
    line_index = ifile * 100 + 1
    line_type  = 1
    case ifile
    when 0 then
      line_index = ifile * 100 + 1
    when 1 then
      line_index = 9 * 100 + 1 # red
    when 2 then
      line_index = 5 * 100 + 1 # green
    when 3 then
      line_index = 2 * 100 + 1 # blue
    when 4 then
      line_index = 1 * 100 + 1 # pink
    when 5 then
      line_index = 0 * 100 + 1 # black
      line_type  = 2
    end
#    if ( ifile <= 1 ) || ( 5 <= ifile ) then
    if ivar == 0 then
      GGraph.line gp, flag_first, 'annotate'=>false, 'exchange'=>true, 'title'=>"", 'index'=>line_index, 'type'=>line_type, 'legend'=>ncfn, 'legend_vx'=>0.1
    else
      GGraph.line gp, flag_first, 'annotate'=>false, 'exchange'=>true, 'title'=>"", 'index'=>line_index, 'type'=>line_type
    end
  end

end

DCL.grcls
