=begin
= Draw $B%b%8%e!<%k(B
rgtview.rb $B$+$i8F$S=P$9(B Draw $B%b%8%e!<%k$rDj5A$9$k%U%!%$%k(B.
$B$3$3$G$O(B, $BIA2h$K4X$9$k%a%=%C%I$d(B GGraph $B$N@_Dj$r(B, $BJQ?tKh$K@_Dj$7$F$$$k(B.
=end

module Draw
require "colorbar"

=begin

  def GGraph::annotate(str_ary)
    charsize = 0.5 * DCL.uzpget('rsizec1')
    dvx = 0.01
    dvy = charsize*1.5
    raise TypeError,"Array expected" if ! str_ary.is_a?(Array)
    vxmin,vxmax,vymin,vymax = DCL.sgqvpt
    vx = vxmax + dvx
    vy = vymax - charsize/2
    str_ary.each{|str|
      DCL::sgtxzv(vx,vy,str,charsize,0,-1,1)
      vy -= dvy
    }
    nil
  end
  
=end
  


  def GGraph::annotate(str_ary) # GGraph $B$N(B annotate $B%a%=%C%I$r:FDj5A(B
    lnum = 0
    str_ary.each{ |str|lnum += 1 }
    charsize = 0.7 * DCL.uzpget('rsizec1')
    dvx = 0.01
    dvy = charsize*1.5
    raise TypeError,"Array expected" if ! str_ary.is_a?(Array)
    vxmin,vxmax,vymin,vymax = DCL.sgqvpt
    vx = 0.65
    vy = 0.045 + (lnum-1)*dvy
    str_ary.each{|str|
      DCL::sgtxzv(vx,vy,str,charsize,0,-1,1)
      vy -= dvy
    }
    nil
  end



##########################################################
#                   $B?'9g$$(B, $BEyCM@~(B $B@_Dj(B                  #
##########################################################
##  $B40@.IJ(B -------- $B$3$3$+$i(B ---------

  def settei_U(obj) 
    GGraph.set_linear_contour_options('min'=>-50,'int'=>2.5, 'max'=>70)
    $levels = NArray[-1000, -15, -5, 0, 5, 15, 25, 1000]
    $patterns = NArray[30999, 35999, 40999, 55999, 70999, 75999, 85999]
    $cbar_conf = {
      "levels"=>$levels, 
      "colors"=>$patterns,
      "eqlev"=>true,
      "nobound"=>0,
      "tick1"=>20,"tick2"=>1
    }
  end

  def settei_V(obj)
    GGraph.set_linear_contour_options('int'=>0.5)
    $levels = NArray[-1000, -1.5, -0.5, 0, 0.5, 1.5, 1000]
    $patterns = NArray[30999,40999,55999,70999,75999,85999]
    $cbar_conf = {
      "levels"=>$levels, 
      "colors"=>$patterns,
      "eqlev"=>true,
      "nobound"=>0,
      "tick1"=>20,"tick2"=>1
    }
  end

  def settei_W(obj)
#    GGraph.set_linear_contour_options( 'int'=>0.005)
#    $levels = NArray[-10, -0.04, -0.02, -0.01, 0.0, 0.01, 0.02, 10]
    GGraph.set_linear_contour_options( 'int'=>0.05)
    $levels = NArray[-10, -0.4, -0.2, -0.1, 0.0, 0.1, 0.2, 10]
    $patterns = NArray[30999, 35999, 40999, 55999, 70999, 75999, 85999]
    $cbar_conf = {
      "levels"=>$levels, 
      "colors"=>$patterns,
      "eqlev"=>true,
      "nobound"=>0,
      "tick1"=>20,"tick2"=>1
    }
  end

  def settei_T
#    GGraph.set_linear_tone_options( 'min'=>180, 'max'=>330)
    GGraph.set_linear_contour_options( 'min'=>180, 'max'=>330, 'int'=>10)
    $levels = NArray.int(8).indgen!(0, 20)+180
    $levels[0] = 150
#    $patterns = NArray[30999, 35999, 40999, 45999, 50999, 55999, 60999, 65999, 70999, 75999, 80999, 85999, 90999]
#    $patterns = NArray[60999, 62999, 65999, 68999, 70999, 72999, 75999, 77999, 80999, 82999, 85999, 87999, 90999]
#    $patterns = NArray[25999, 30999, 40999, 50999, 60999, 70999, 74999, 78999, 85999]
#    $patterns = NArray[25999, 32999, 55999, 65999, 75999, 85999]
    $patterns = NArray[25999, 30999, 40999, 55999, 70999, 75999, 85999]
    $cbar_conf = {
      "levels"=>$levels, 
      "colors"=>$patterns,
      "eqlev"=>true,
      "tick1"=>20,"tick2"=>1
    }
  end

  def settei_CP
    $levels = NArray[0.0002, 0.0015, 0.002, 0.0025, 0.003, 0.0035, 1000]
    $patterns = NArray[35999, 45999, 60999, 70999,75999,85999]
    $cbar_conf = {
      "levels"=>$levels, 
      "colors"=>$patterns,
      "eqlev"=>true,
      "nobound"=>2,
      "tick1"=>7,"tick2"=>1
    }
  end


##  $B40@.IJ(B -------- $B$3$3$^$G(B ---------
## $BL$40@.IJ(B

  def settei_LSP
    $levels = NArray[0.0007, 0.0009, 0.0011, 0.0014, 0.0017, 0.002, 1000]
    $patterns = NArray[35999, 45999, 60999, 70999, 75999, 85999]
    $cbar_conf = {
      "levels"=>$levels, 
      "colors"=>$patterns,
      "eqlev"=>true,
      "nobound"=>2,
      "tick1"=>7,"tick2"=>1
    }
  end

  def settei_TP
    GGraph.set_linear_contour_options( 'int'=>0.1)
    $levels = NArray[0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 1000]
#    $levels = NArray[0.001, 0.0015, 0.002, 0.0025, 0.003, 0.0035, 1000]
    $patterns = NArray[35999, 45999, 60999, 70999, 75999, 85999]
    $cbar_conf = {
      "levels"=>$levels, 
      "colors"=>$patterns,
      "eqlev"=>true,
      "nobound"=>2,
      "tick1"=>7,"tick2"=>1
    }
  end

  def settei_PWS
#    GGraph.set_linear_contour_options( 'int'=>0.1)
#    $levels = NArray[1.0e-15, 1.0e-14, 1.0e-13, 1.0e-12, 1.0e-11, 1.0e-10, 1.0e-9]
#    $levels = NArray[1.0e-10, 1.0e-9, 5.0e-9, 1.0e-8, 1.0e-7, 5.0e-7, 1.0e-6]
    $levels = NArray[1.0e-13, 2.5e-12, 5.0e-11, 7.5e-10, 1.0e-9, 5.0e-9, 1.0e-8]
    $patterns = NArray[35999, 45999, 60999, 70999, 75999, 85999]
    $cbar_conf = {
      "levels"=>$levels, 
      "colors"=>$patterns,
      "eqlev"=>true,
      "nobound"=>2,
      "tick1"=>7,"tick2"=>1
    }
  end

  def settei_strm(obj)
    GGraph.set_linear_contour_options( 'interval'=>2.0e10 )  
    $levels = NArray[obj.val.min, 0.0, obj.val.max]
    $patterns = NArray[1201,1204]
    $cbar_conf = {
      "levels"=>$levels, 
      "colors"=>$patterns,
      "eqlev"=>true,
      "nobound"=>0,
      "tick1"=>7,"tick2"=>1
    }
  end


  def settei_Theta
    lev = [370, 400, 450, 500, 600, 700, 800] 
    $levels = NArray::to_na(NArray.int(16).indgen!(200, 10).to_a + lev)
    mj = 1; mn = 1
    $index = [mj, mn]
  end

  def settei_Available
    GGraph.set_linear_contour_options( 'nlev'=>30 )  
    $levels = NArray[-100, -0.05, 0, 0.05, 0.1, 100] 
    $patterns = NArray[35999, 45999, 60999, 70999, 85999]
    $cbar_conf = {
      "levels"=>$levels, 
      "colors"=>$patterns,
      "eqlev"=>true,
      "nobound"=>2,
      "tick1"=>7,"tick2"=>1
    }
  end


  def settei_native
    GGraph.set_linear_contour_options( 'nlev'=>10 )  
    GGraph.set_linear_tone_options( 'nlev'=>50)  
    $cbar_conf = {
      "tick1"=>25,"tick2"=>5
    }
  end


  def settei_strm_temp
    GGraph.set_linear_contour_options( 'nlev'=>15 )  
    GGraph.set_linear_tone_options( 'min'=>180, 'nlev'=>90, 'max'=>300 )  
  end

##########################################################
#                     $BIA2hJ}K!(B $B@_Dj(B                      #
##########################################################
  
  def mkwin
    # -f, --file $B%*%W%7%g%s$,M-8z$J$i$P(B dcl.ps $B$KJ]B8(B.
    if ($OPT_f) || ($OPT_file) then
      output = 2
    else
      output = 1  # $B%G%U%)%k%H$OI8=`=PNO$K(B.
    end
    # viewport $B$NBg$-$5(B, $B<4$N@_Dj(B
    GGraph.set_fig('viewport'=>[0.15,0.85,0.23,0.58])  # set_*: $B=D2#Hf$O(B 2 : 1
    GGraph.set_fig('itr'=> ($OPT_itr)) if ($OPT_itr)  
    GGraph.set_fig( "xrev"=>"units:mb,units:hPa,units:millibars",  
		   "yrev"=>"units:mb,units:hPa,units:millibars") 

    # $BAk3+$/(B
    DCL.gropn(output)
    # $B%U%)%s$H$N@_Dj(B
    DCL.sgpset('lcntl', false) ; DCL.uzfact(0.7)
    DCL.sgpset('lfull', true)  ; DCL.sgpset('lfprop',true) 
  end

  def line(obj)
    GGraph.line(obj, true, "annot"=>false, "exchange"=>($OPT_exchange))
    DCL.grcls       
  end

  def draw(obj, levels=nil, patterns=nil, colorbar=nil, map=nil, cont=true, tone=true, line=false)
    p obj
    if patterns && levels
      GGraph.tone(obj,
		  true, 
		  "lev"=>levels, 
		  "patterns"=>patterns, 
#		  "annot"=>false,
		  "title"=>$title+" (m)",
		  "exchange"=>($OPT_exchange)
		  )
    else
      GGraph.tone(obj,
		  true, 
		  "annot"=>false, 
		  "title"=>$title,
		  "exchange"=>($OPT_exchange)
		  ) if tone
    end
    if cont && tone
      GGraph.contour(obj, false, "exchange"=>($OPT_exchange))
    elsif cont && !tone
      mj = DCL.udpget('indxmj')
      mn = DCL.udpget('indxmn')
      GGraph.contour(obj, 
		     true, 
		     "annot"=>false, 
		     "title"=>$title,
		     "levels"=>levels,
		     "index" =>[mj,mn],
		     "label" =>true,
		     "exchange"=>($OPT_exchange)
		   ) 
    elsif line
      GGraph.line(obj)
    end

    DCL.draw_map(0.15,0.85,0.23,0.58) if map

    if colorbar
      DCL::Util::color_bar($cbar_conf)
    end
    DCL.grcls       
  end







  def draw_cont(obj)
    GGraph.contour(obj, true, "title"=>$title) # $B%3%s%?!<$r=E$M=q$-(B
    DCL.grcls       
  end

  def draw_tone(obj)
    GGraph.tone(obj)
    DCL.grcls       
  end

  def draw_strm_temp(strm, temp)
    GGraph.tone(temp)
    GGraph.contour(strm, false) # $B%3%s%?!<$r=E$M=q$-(B
    DCL.grcls       
  end

  def draw_map(vpt_xmin, vpt_xmax, vpt_ymin, vpt_ymax)
    # $BCO?^$r=E$M$k(B
    DCL::grfig                           # $B2~%Z!<%8$7$J$$(B
    DCL::grstrn(10)                      # $B@55w1_E{?^K!(B
    DCL::grswnd(0.0,360.0,-90.0,90.0)    # $B0^EY7PEY$r@_Dj(B
    DCL::grsvpt(vpt_xmin, vpt_xmax, vpt_ymin, vpt_ymax)
    DCL::umpfit                          # $B$"$H$O$*$^$+$;(B
    DCL::grstrf                          # $B3NDj(B
    DCL::umpmap("coast_world")           # $BCO?^$rIA$/(B
  end

  def draw_with_map(obj, vpt_xmin=0.15, vpt_xmax=0.85, vpt_ymin=0.2, vpt_ymax=0.55)
    NumRu::GGraph::tone(obj)
    DCL.draw_map(vpt_xmin, vpt_xmax, vpt_ymin, vpt_ymax)
    DCL.grcls       
  end


end

