#!/usr/bin/ruby
require "numru/ggraph"
include NumRu

b_nband = NArray.to_na([8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40])
i_nip   = NArray.to_na((1..(40*2*8)).to_a)
nnband = b_nband.size
nnip   = i_nip.size
bi_rmse = NArray.float(nnband,nnip)


rmiss = -999.0
for iband in 0..(nnband-1)
  for iip in 0..(nnip-1)
    bi_rmse[iband,iip] = rmiss
  end
end


for iband in 0..(nnband-1)

  file_name = '../dir_optimize_ip_v2-'+b_nband[iband].to_s.rjust(2,'0')+'x2-08/out/loss_lbl.txt'
  File.open( file_name, mode = "rt"){|f|
    iline = 0
    f.each_line{|line|
      if ( iline > 0 ) then
        a_line = line.split
        iip = a_line[1].to_i-1
        bi_rmse[iband,iip] = a_line[2].to_f
      end
      iline += 1
    }
  }

end


va_nband = VArray.new( b_nband,
                  {"long_name"=>"number of band", "units"=>"1"},
                  "nband" )
ax_nband = Axis.new.set_pos(va_nband)

va_nip = VArray.new( i_nip,
                  {"long_name"=>"total number of IP","units"=>"1"},
                  "nip" )
ax_nip = Axis.new.set_pos(va_nip)

va_data = VArray.new( bi_rmse,
                 {"long_name"=>"RMSE with respect to the LBL model", "units"=>"W m-2"},
                 "rmse" )
gphys = GPhys.new( Grid.new(ax_nband,ax_nip), va_data )


iws = (ARGV[0] || (puts ' WORKSTATION ID (I)  ? ;'; DCL::sgpwsn; gets)).to_i
DCL.gropn(iws)

#DCL.sldiv('y',1,4)
#DCL.sgpset('isub', 96)     # control character of subscription: '_' --> '`'
DCL.sglset('LCNTL', true)
DCL.sgpset('lfull',true)
DCL.uzfact(0.6)            # font size
DCL.sgpset('lclip', true)  # clip graph
#DCL.udlset('LMSG', false)  # flag for message of contour interval
DCL.glpset('lmiss',true)   # missing value
DCL.glpset('rmiss',rmiss)

#DCL.sgscmn(4)  # blue-cyan-white-yellow-red
#DCL.sgscmn(5)  # gray-scale
#DCL.sgscmn(14) # blue-white-red

#levels = [110,120,130,140,150,160,170,180,190,200,210,220,230,240]
#patterns = [10999,15999,20999,25999,30999,35999,40999,50999,60999,65999,70999,75999,80999,90999,95999]

GGraph.set_fig 'itr'=> 1, 'viewport'=>[0.25,0.7,0.15,0.6]#, 'window'=>[0,45,0,600]

GGraph.tone gphys, true, 'min'=>1.0, 'max'=>5, 'nlev'=>40, 'tonc'=>true  #, 'lev'=>levels, 'pat'=>patterns
#GGraph.contour gphys, false, 'min'=>1.0, 'max'=>5, 'nlev'=>40 # 'lev'=>[0.6,0.7,0.8,0.9,1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0] #, 'lev'=>levels
GGraph.color_bar

#GGraph.line gphys, true, 'exchange'=>false


# y=a*x+b
#   y1=a*x1+b, y2=a*x2+b
#   y2-y1=a*(x2-x1)
#   a=(y2-y1)/(x2-x1)
#    =(140-40)/(40-10)=100/30=10/3~3.3
#   b=y1-(y2-y1)/(x2-x1)*x1
#    =y1*(x2-x1)/(x2-x1)-(y2-y1)*x1/(x2-x1)
#    =y1*x2/(x2-x1)-y1*x1/(x2-x1)-y2*x1/(x2-x1)+y1*x1/(x2-x1)
#    =(x2*y1-x1*y2)/(x2-x1)
#    =(40*40-10*140)/(40-10)=200/30=20/3~6.7
#   y=10/3*x+20/3
na_x    = NArray.to_na([10,40])
na_data = NArray.to_na([40,140])
va_x = VArray.new( na_x,
                  {"long_name"=>"number of band", "units"=>"1"},
                  "nband" )
ax_x = Axis.new.set_pos(va_x)
va_data = VArray.new( na_data,
                 {"long_name"=>"RMSE with respect to the LBL model", "units"=>"W m-2"},
                 "rmse" )
gphys = GPhys.new( Grid.new(ax_x), va_data )
#GGraph.line gphys, false, 'exchange'=>false


DCL.grcls
