#!/usr/bin/ruby

require "narray"
require "numru/dcl"
include NumRu

#-- help
if ARGV.size == 0 then
   puts '[usage] : dclplot [options] [filename]'
   puts '[options] : '
   puts '--title="string" : draw the title.'
   puts '--save-mode="string" : save file format'
   puts '                       "string" : "PS" => post script, "PNG" => png'
   puts '                                  default = "PNG"'
   puts '--undef="string" : set the undefined value.'
   puts '--set-viewx=[min:max] : set the x axis viewport.'
   puts '                    default = [0.2:0.8]'
   puts '--set-viewy=[min:max] : set the y axis viewport.'
   puts '                        default = [0.2:0.8]'
   puts '--set-xrange=[min:max] : set the x axis range.'
   puts '--set-yrange=[min:max] : set the y axis range.'
   puts '--with-xline=[colum numbers] : set the x coordinate value.'
   puts '                               ex. --with-xline=[1:2:3].'
   puts '--with-yline=[colum numbers] : set the y coordinate value.'
   puts '                               ex. --with-yline=[1:2:3].'
   puts '--with-xpoint=[colum numbers] : set the x coordinate value.'
   puts '                               ex. --with-xpoint=[1:2:3].'
   puts '--with-ypoint=[colum numbers] : set the y coordinate value.'
   puts '                               ex. --with-ypoint=[1:2:3].'
   puts '--set-xlog, --set-ylog : set logalithm coordinate.'
   puts '--with-clip : clipping the value out of window.'
   puts '--set-xtitle="string" : x axis title.'
   puts '--set-ytitle="string" : y axis title.'
   puts '--set-xsub="string" : x axis unit.'
   puts '--set-ysub="string" : y axis unit.'
   puts '--skip=number : line skip number.'
   puts '--split=type : split type.'
   puts '               ex. if you split by comma, --split=",".'
   puts '               except : if you split by [tab], --split="tab".'
   puts '[example] : dclplot --with-xline=[1] --with-yline=[2:3] --title="hoge" test.dat'
   puts '[NOTE] : --with-{x,y}line or --with-{x,y}point is neccesary.'
   puts '[file format] : 1st array => values name (character)'
   puts '                2nd array => values unit (character)'
   puts '                3rd arrayi and under => values'
   exit
end

#-- default value set
vxmin = 0.2
vxmax = 0.8
vymin = 0.2
vymax = 0.8
iws = 4
undeflag = false
autoscalew = true
autoscalev = true
undefc = '999.0'
title = ''
xlflag = false
ylflag = false
xpflag = false
ypflag = false
itr = 1
xtitle = ''
ytitle = ''
xsub = ''
ysub = ''
xa = ''
ya = ''
xaflag = false
yaflag = false
xsubflag = false
ysubflag = false
lskip = 0
splitype = ","

#-- argv analysis
counter = 0
for i in 0..ARGV.size-1
   tmp = ARGV[i]

   if tmp[0..6] == '--title' then
      tmpval = tmp.split("=")
      title = tmpval[1].gsub("'","")
      counter = counter + 1
   end

   if tmp[0..10] == '--save-mode' then
      tmpval = tmp.split("=")
      if tmpval[1].gsub("'","").gsub('"','') == 'PS' then
         iws = 2
      end
      counter = counter + 1
   end

   if tmp[0..6] == '--undef' then
      undeflag = true
      tmpval = tmp.split("=")
      undefc = tmpval[1]
      counter = counter + 1
   end

   if tmp[0..10] == '--set-viewx' then
      autoscalev = false
      tmpval = tmp.split("=")
      view_range = tmpval[1].split(":")
      vxmin = view_range[0].gsub("[","").to_f
      vxmax = view_range[1].gsub("]","").to_f
      counter = counter + 1
   end

   if tmp[0..10] == '--set-viewy' then
      autoscalev = false
      tmpval = tmp.split("=")
      view_range = tmpval[1].split(":")
      vymin = view_range[0].gsub("[","").to_f
      vymax = view_range[1].gsub("]","").to_f
      counter = counter + 1
   end

   if tmp[0..11] == '--set-xrange' then
      autoscalew = false
      tmpval = tmp.split("=")
      view_range = tmpval[1].split(":")
      xmin = view_range[0].gsub("[","").to_f
      xmax = view_range[1].gsub("]","").to_f
      counter = counter + 1
   end

   if tmp[0..11] == '--set-yrange' then
      autoscalew = false
      tmpval = tmp.split("=")
      view_range = tmpval[1].split(":")
      ymin = view_range[0].gsub("[","").to_f
      ymax = view_range[1].gsub("]","").to_f
      counter = counter + 1
   end

   if tmp[0..11] == '--with-xline' then
      xlflag = true
      tmpval = tmp.split("=")
      xline = tmpval[1].split(":")
      for i in 0..xline.size-1
         xline[i] = xline[i].gsub("[","").gsub("]","")
      end
      counter = counter + 1
   end

   if tmp[0..11] == '--with-yline' then
      ylflag = true
      tmpval = tmp.split("=")
      yline = tmpval[1].split(":")
      for i in 0..yline.size-1
         yline[i] = yline[i].gsub("[","").gsub("]","")
      end
      counter = counter + 1
   end

   if tmp[0..12] == '--with-xpoint' then
      xpflag = true
      tmpval = tmp.split("=")
      tmpval = tmp.split("=")
      xpoint = tmpval[1].split(":")
      for i in 0..xpoint.size-1
         xpoint[i] = xpoint[i].gsub("[","").gsub("]","")
      end
      counter = counter + 1
   end

   if tmp[0..12] == '--with-ypoint' then
      ypflag = true
      tmpval = tmp.split("=")
      ypoint = tmpval[1].split(":")
      for i in 0..ypoint.size-1
         ypoint[i] = ypoint[i].gsub("[","").gsub("]","")
      end
      counter = counter + 1
   end

   if tmp[0..9] == '--set-xlog' then
      if itr == 2 then
         itr = 4
      else
         itr = 3
      end
      counter = counter + 1
   end

   if tmp[0..9] == '--set-ylog' then
      if itr == 3 then
         itr = 4
      else
         itr = 2
      end
      counter = counter + 1
   end

   if tmp[0..10] == '--with-clip' then
      DCL.sglset('LCLIP',true)
      counter = counter + 1
   end

   if tmp[0..11] == '--set-xtitle' then
      xaflag = true
      tmpval = tmp.split("=")
      xa = tmpval[1].gsub("'","")
      counter = counter + 1
   end

   if tmp[0..11] == '--set-ytitle' then
      yaflag = true
      tmpval = tmp.split("=")
      ya = tmpval[1].gsub("'","")
      counter = counter + 1
   end

   if tmp[0..9] == '--set-xsub' then
      xsubflag = true
      tmpval = tmp.split("=")
      xsub = tmpval[1].gsub("'","")
      counter = counter + 1
   end

   if tmp[0..9] == '--set-ysub' then
      ysubflag = true
      tmpval = tmp.split("=")
      ysub = tmpval[1].gsub("'","")
      counter = counter + 1
   end

   if tmp[0..5] == '--skip' then
      tmpval = tmp.split("=")
      lskip = tmpval[1].gsub("'","").to_i
      counter = counter + 1
   end

   if tmp[0..6] == '--split' then
      tmpval = tmp.split("=")
      splitype = tmpval[1].gsub("'","").gsub('"','')
      if splitype == "tab" then
         splitype = "	"
      end
      counter = counter + 1
   end

end

fname = ARGV[counter]

#-- file reading part

f = open( fname, 'r' )

f_content = f.read

#-- Array separate

f_col = f_content.split("\n")
f_col_size = f_col.size

for i in lskip..f_col_size-1
   f_col[i] = f_col[i].split(splitype)
end

#-- gsub

for i in lskip..f_col[lskip].size-1
   f_col[lskip][i]=f_col[lskip][i].gsub("'","").gsub("  ","")
   f_col[lskip+1][i]=f_col[lskip+1][i].gsub("'","").gsub("  ","")
end

#-- undef convert

if undeflag == true then
   rmiss = DCL.glrget( 'RMISS' )
   DCL.gllset( 'LMISS', true )

   for j in 0..f_col[lskip].size-1
      for i in lskip..f_col_size-1
         if f_col[i][j]==undefc then
            f_col[i][j] = rmiss.to_s
         end
      end
   end
end

#-- convert Array to NArray
for j in 0..f_col[lskip].size-1
   for i in lskip..f_col_size-1
      f_col[i][j] = f_col[i][j].gsub("'","").gsub('"','').gsub(" ","").gsub('/','').gsub(':','')
   end
end

nval = NArray.to_na(f_col)
dimen = nval.shape
tate = dimen[1]  # Ԥο
yoko = dimen[0]  # ο
val = NArray.sfloat(yoko,tate-2)

for j in lskip..tate-3
   for i in 0..yoko-1
      val[i,j] = nval[i,j+2].to_f
   end
end

#-- dcl part

DCL.sgiset('IFONT', 2)
DCL.gropn( iws )
DCL.grfrm

if autoscalew == false then
   DCL.grswnd( xmin, xmax, ymin, ymax )
end

if autoscalev == false then
   DCL.grsvpt( vxmin, vxmax, vymin, vymax )
end

if xlflag == true and ylflag == true then
   if xline.size >= yline.size then
      for i in 0..xline.size-1
         DCL.usspnt( val[xline[i].to_i-1,lskip..tate-3], val[yline[0].to_i-1,lskip..tate-3] )
         xtitle = xtitle + ', ' + f_col[lskip][xline[i].to_i-1]
      end
      xtitle[0..1] = xtitle[0..1].gsub(', ','')
      ytitle = f_col[lskip][yline[0].to_i-1]
   else
      for i in 0..yline.size-1
         DCL.usspnt( val[xline[0].to_i-1,0..tate-3], val[yline[i].to_i-1,0..tate-3] )
         ytitle = ytitle + ', ' + f_col[lskip][yline[i].to_i-1]
      end
      xtitle = f_col[lskip][xline[0].to_i-1]
      ytitle[0..1] = ytitle[0..1].gsub(", ","")
   end
   if xsubflag == false then
      xsub = f_col[lskip+1][xline[0].to_i-1]
      ysub = f_col[lskip+1][yline[0].to_i-1]
   end
end

if xpflag == true and ypflag == true then
   if xpoint.size >= ypoint.size then
      for i in 0..xpoint.size-1
         DCL.usspnt( val[xpoint[i].to_i-1,0..tate-3], val[ypoint[0].to_i-1,0..tate-3] )
         xtitle = xtitle + ', ' + f_col[lskip][xpoint[i].to_i-1]
      end
      xtitle[0..1] = xtitle[0..1].gsub(", ","")
      ytitle = f_col[lskip][ypoint[0].to_i-1]
   else
      for i in 0..ypoint.size-1
         DCL.usspnt( val[xpoint[0].to_i-1,0..tate-3], val[ypoint[i].to_i-1,0..tate-3] )
         ytitle = ytitle + ', ' + f_col[lskip][ypoint[i].to_i-1]
      end
      xtitle = f_col[lskip][xpoint[0].to_i-1]
      ytitle[0..1] = ytitle[0..1].gsub(", ","")
   end
   xsub = f_col[lskip+1][xpoint[0].to_i-1]
   ysub = f_col[lskip+1][ypoint[0].to_i-1]
end

if xaflag == false then
   xa = xtitle
end

if yaflag == false then
   ya = ytitle
end

DCL.ussttl( xa, xsub, ya, ysub )
DCL.uspfit
DCL.grstrn( itr )
DCL.grstrf
DCL.usdaxs

DCL.uxmttl( 't', title, 0.0 )
if xlflag == true and ylflag == true then
   if xline.size >= yline.size then
      for i in 0..xline.size-1
         DCL.uulinz( val[xline[i].to_i-1,lskip..tate-3], val[yline[0].to_i-1,lskip..tate-3], itype=i/10 + 1, index=(i+1) * 10 )
      end
   else
      for i in 0..yline.size-1
         DCL.uulinz( val[xline[0].to_i-1,lskip..tate-3], val[yline[i].to_i-1,lskip..tate-3], itype=i/10 + 1, index=(i+1) * 10 )
      end
   end
end

if xpflag == true and ypflag == true then
   if xpoint.size >= ypoint.size then
      for i in 0..xpoint.size-1
         DCL.sgspmt( i + 2 )  # ߤξ¤ 17 ޤ
         DCL.sgpmu( val[xpoint[i].to_i-1,lskip..tate-3], val[ypoint[0].to_i-1,lskip..tate-3] )
      end
   else
      for i in 0..ypoint.size-1
         DCL.sgspmt( i + 2 )  # ߤξ¤ 17 ޤ
         DCL.sgpmu( val[xpoint[0].to_i-1,lskip..tate-3], val[ypoint[i].to_i-1,lskip..tate-3] )
      end
   end
end

DCL.grcls
