module NumRu
  module DCL
    module Util

      Color_bar_opts = {

      "levels"     => nil,
      "colors"     => nil,
      "vx0"        => 0.8,
      "vy0"        => 0.02,
      "vxlength"   => 0.18,
      "vylength"   => 0.02,
      "portrait"   => false,
      "tick1"      => 1,
      "tick2"      => 2,
      "label_size" => 0.01,
      "log"        => false
      }

      def color_bar_options
	p Color_bar_opts
      end
      module_function :color_bar_options

      def color_bar(options={},default=Color_bar_opts)

	if options.type!=Hash then
	  print "options must be Hash\n"
	  exit
	end
	options.each_key{|key|
	  if !default.member?(key) then
	    print key," option is not exist\n"
	    exit
	  end
	}
	options = default.dup.update(options)

	levels = options['levels']
	colors = options['colors']

	if (levels.nil? && !colors.nil?) || (!levels.nil? && colors.nil?) then
	  print "levels and colors must set at same time\n"
	  exit
	end

	portrait  = options["portrait"]

	miss = DCL::glrget("rmiss")

	if !levels.nil? then

	  nlevel = levels.length
	  if levels.type==Array then levels = NArray.to_na(levels) end
	  if colors.length != (nlevel-1) then
	    print "colors.length must be equal levels.length - 1\n"
	    exit
	  end

	  DCL::ueitlv
	  DCL::uestln(levels,colors)

	else

	  nton = DCL::ueqntl
	  if nton==0 then
	    print "no tone patern was set\n"
	    exit
	  end
	  lev1 = Array.new
	  lev2 = Array.new
	  for n in 0..nton-1
	    tlev1,tlev2,ipat = DCL::ueqtlv(n+1)
	    lev1.push(tlev1)
	    lev2.push(tlev2)
	  end

	  levels = lev1+lev2
	  levels = levels.uniq.sort
	  levels.delete(miss)
	  levels = NArray.to_na(levels)
	  if levels.ne(levels.sort).any? then
	    print "levels is not in order\n"
	    exit
	  end

	end


	vxmin = options["vx0"]
	vxlen = options["vxlength"]
	vymin = options["vy0"]
	vylen = options["vylength"]

	vxmax = vxmin + vxlen
	vymax = vymin + vylen

	tick1 = Array.new
	tick2 = Array.new
	for i in 0..levels.length-1
	    tick1.push(levels[i]) if i%options["tick1"]==0
	    tick2.push(levels[i]) if i%options["tick2"]==0
	end

	lsize = options["label_size"]

	min = levels[levels.ne(miss).where].min
	max = levels[levels.ne(miss).where].max

	nbar =100
	bar = NArray.float(nbar,2)
	for i in 0..nbar-1
	  bar[i,true] = min + (max-min).to_f/(nbar-1)*i
	end

	xb = DCL::uzlget("labelxb")
	yl = DCL::uzlget("labelyl")
	if portrait then
	  xmin = 0.0
	  xmax = 1.0
	  ymin = min
	  ymax = max
	  DCL::uzlset("labelxb",false)
	  DCL::uzlset("labelyl",true)
	  bar = bar.transpose(-1,0)
	  DCL::uwsgxa([0,1])
	  DCL::uwsgya(bar[0,true])
	else
	  xmin = min
	  xmax = max
	  ymin = 0.0
	  ymax = 1.0
	  DCL::uzlset("labelxb",true)
	  DCL::uzlset("labelyl",false)
	  DCL::uwsgxa(bar[true,0])
	  DCL::uwsgya([0,1])
	end

	type = 1
	if options["log"] then
	  type +=1
	  type +=1 if !portrait
	end

	DCL::grfig
	DCL::grsvpt(vxmin,vxmax,vymin,vymax)
	DCL::grswnd(xmin,xmax,ymin,ymax)
	DCL::grstrn(type)
	DCL::grstrf

	DCL::uetone(bar)

	rsizel = DCL::uzrget("rsizel1")
	DCL::uzrset("rsizel1",lsize)
	rsizet = DCL::uzrget("rsizet1")

	if portrait then
	  DCL::uzrset("rsizet1",vxlen)
	  DCL::uxaxdv("t",1,1)
	  DCL::uxaxdv("b",1,1)
	  DCL::uyaxnm("l",tick1,tick2)
	  DCL::uyaxnm("r",tick1,tick2)
	else
	  DCL::uzrset("rsizet1",vylen)
	  DCL::uxaxnm("t",tick1,tick2)
	  DCL::uxaxnm("b",tick1,tick2)
	  DCL::uyaxdv("l",1,1)
	  DCL::uyaxdv("r",1,1)
	end

	DCL::uzrset("rsizel1",rsizel)
	DCL::uzrset("rsizet1",rsizet)

	DCL::uzlset("labelxb",xb)
	DCL::uzlset("labelyl",yl)

      end
      module_function :color_bar
    end
  end
end
