#!/usr/bin/env ruby

load "#{$local_path}/lib-ape-base.rb"
load "#{$local_path}/lib-ape-view.rb"

# --------------------------------------------------------------------

class Ape

  def plot_xteq(gphys)

    gropn(2) if $gropn == nil 
    plot_spct_set(gphys)

    # attribute の long_name を消す (タイトル描画位置の都合)
    if gphys.data.get_att("long_name")
      gphys = gphys.set_att("long_name","")
    end

      GGraph.tone( gphys, true , $tone_hash ) 
#      GGraph.contour( gphys, false, $cont_hash ) unless $cont_flag == false

    # タイトル
    tani = "(#{gphys.data.get_att("units")})"
    DCL::uxsttl("t", tani , -1.0 )
    DCL::uxmttl("t", gphys.data.get_att("ape_name").gsub("_", " "), -1.0 ) 

    # nc ファイル名
    if $gropn == 2
      charsize = 0.79 - $file_label.size * 0.0115 
      DCL::sgtxzv(charsize,0.62,$file_label,0.8 * DCL.uzpget('rsizec1'),0,-1,1)
    else
      charsize = 0.81 - $file_label.size * 0.0095 
      DCL::sgtxzv(charsize,0.6,$file_label,0.8 * DCL.uzpget('rsizec1'),0,-1,1) 
    end

    # トーンバー
    unless gphys.axnames.size == 1
      DCL::Util::color_bar($cbar_conf)  unless $tone_flag == false
    end

  end

  def plot_spct_bunsan(gphys)

    gropn(1) if $gropn == nil 
    plot_spct_set(gphys)
    plot_set(gphys)

#    $fig_set_hash = { "window" => 
#      [gphys.axis(0).pos.val.min, gphys.axis(0).pos.val.max, 0.0 ,0.5] }
#
#     GGraph.set_fig($fig_set_hash)
    
    # attribute の long_name を消す (タイトル描画位置の都合)
    if gphys.data.get_att("long_name")
      gphys = gphys.set_att("long_name","")
    end

      GGraph.tone( gphys, true , $tone_hash ) 
#      GGraph.contour( gphys, false, $cont_hash ) unless $cont_flag == false


    # 分散曲線
    x_size = gphys.grid_copy.axis(0).pos.val.size
    x = gphys.grid_copy.axis(0).pos.val[x_size/2..-1]
    h_array = NArray.to_na([ 12, 25, 50, 100, 200 ])

    omega = 2*3.1415/24/60/60
    beta =  2*(omega)/(4e+7) * ( 60*60*24*(6370e+3) )  # (2*Omega/a)*cos(0)


    for i in (0..4)
      
      h = h_array[i]
      c_g = ((h*9.8)**0.5)*60*60*24/(4e+7)    # 重力波速度
      
      kelvin = x*c_g
      rossby = -x/(x**2/beta +(2+1)/c_g )
      grav = -(c_g*( c_g*x**2 + beta*(2+1) ) )**0.5

      DCL::uuslnt(1)
      ii=(i)*20+4
      ii = 5*20+4 if ii == 2*20+4
      ii = 720+4 if ii == 4*20+4
      DCL::uuslni(ii)
      DCL::uulin(x, kelvin)
      DCL::uulin(-x, -rossby)
      DCL::uulin(-x, -grav)
      DCL::uulin(x, -grav)

    end

    name_ary = ["h = 12 ", "h = 25 ","h = 50 ","h =100", "h =200 "]
    line_index_ary = [4,24,104,64,724]

    __line_index(name_ary,line_index_ary) 

    # タイトル
    tani = "(#{gphys.data.get_att("units")})"
    DCL::uxsttl("t", tani , -1.0 )
    DCL::uxmttl("t", gphys.data.get_att("ape_name").gsub("_", " "), -1.0 ) 

    # nc ファイル名
    if $gropn == 2
      charsize = 0.79 - $file_label.size * 0.0115 
      DCL::sgtxzv(charsize,0.62,$file_label,0.8 * DCL.uzpget('rsizec1'),0,-1,1)
    else
      charsize = 0.81 - $file_label.size * 0.0095 
      DCL::sgtxzv(charsize,0.6,$file_label,0.8 * DCL.uzpget('rsizec1'),0,-1,1) 
    end

    # トーンバー
    unless gphys.axnames.size == 1
      DCL::Util::color_bar($cbar_conf)  unless $tone_flag == false
    end

  end  
  
  def __line_index(str_ary,line_index_ary) 
    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.15
    vy = 0.12 - charsize/2
    str_ary.size.times{|num|
      DCL::sgtxzv(vx,vy,"--- #{str_ary[num]}", 
                  charsize, 0, -1,line_index_ary[num])
      vy -= dvy
      if num == 2
        vx = 0.30
        vy = 0.12 - charsize/2
      end
    }
    nil
  end

  def plot_spct_set(gphys)
    
    # 初期化
    $fig_set_hash = {"window" => nil}
    $line_hash = { 'exchange'=>false }
    
    if gphys.name == "rain_spct"
      levels = NArray[1e+1, 1e+2, 1e+3, 1e+4, 1e+5,1e+6]
      patterns = NArray[25999,35999, 45999, 55999,70999]
      $tone_hash = { 'patterns'=> patterns, 'levels'=>levels }
      $cont_hash = {'int'=>10}
      $cbar_conf = {
        "levels"=>levels, 
        "colors"=>patterns,
        "eqlev"=>true,
        "nobound"=>2,
        "tick1"=>7,"tick2"=>1
      }

    elsif gphys.name == "u_spct"
      levels = NArray[0.001, 0.01, 0.1, 1, 10,100]
      patterns = NArray[25999,35999, 45999, 55999,70999]
      $tone_hash = { 'patterns'=> patterns, 'levels'=>levels }
      $cont_hash = {'int'=>10}
      $cbar_conf = {
        "levels"=>levels, 
        "colors"=>patterns,
        "eqlev"=>true,
        "nobound"=>2,
        "tick1"=>7,"tick2"=>1
      }


    elsif gphys.name == "rain_xteq"
      levels = NArray[50, 250, 450, 650, 850, 1050, 1250,10000]
      patterns = NArray[25999,35999, 45999, 55999,70999,75999,85999]
      $tone_hash = { 'patterns'=> patterns, 'levels'=>levels }
      $cont_hash = {'int'=>10}
      $cbar_conf = {
        "levels"=>levels, 
        "colors"=>patterns,
        "eqlev"=>true,
        "nobound"=>2,
        "tick1"=>7,"tick2"=>1
      }

    elsif gphys.name == "u_xteq"
      levels = NArray[-1000,-10,-6,-2,2,6,10,1000]
      patterns = NArray[25999,35999, 45999, 55999,70999,75999,85999]
      $tone_hash = { 'patterns'=> patterns, 'levels'=>levels }
      $cont_hash = {'int'=>10}
      $cbar_conf = {
        "levels"=>levels, 
        "colors"=>patterns,
        "eqlev"=>true,
        "nobound"=>0,
        "tick1"=>7,"tick2"=>1
      }

    else
      plot_def(gphys)
    end
    
  end


end

# --------------------------------------------------------------------

=begin

t = ape_new 1
g = t.go("u")

  # rain
  file=${figs}rain-spct-eq-${name}-gt3
  command="gtcont tone=,10,100,1000,10000,100000,1000000 pat=25999,35999,45999,5599
9,70999,75999,85999 rain.sum.y32.spct title:rain,s-t,pwspct  cont=5E+1 -sg:lcorner=
.false."
  sub_mkfig



  # u z13
  file=${figs}u-spct-z13-eq-${name}-gt3
  command="gtcont tone=,0.001,0.01,0.1,1,10,100 pat=25999,35999,45999,55999,70999,7
5999,85999 title:u,s-t,pwspct u.z13.out.y32.spct cont=2E-1 -sg:lcorner=.false."
  sub_mkfig

    DCL::grswnd(-30.0,30.0,0.0,0.5)
#    DCL::grsvpt(0.13,0.83,0.25,0.60)
#    DCL::grstrn(1)



#      k_x = NArray.to_na(k_x)
#      kelvin2 = kelvin[0..k_x.size-1]

#      DCL::grfrm


    x =  VArray.new(x).rename("")
    x = Axis.new().set_pos(x)
    x = Grid.new(x)
    kelvin = VArray.new(kelvin).rename("kelvin")
    kelvin = GPhys.new(x,kelvin)
    rossby = VArray.new(rossby).rename("rossby")
    rossby = GPhys.new(x,rossby)
    grav   = VArray.new(grav).rename("gravity")
    grav   = GPhys.new(x,grav)
    
    $line_hash = { "index"=> (i*20 +1)}
    GGraph.line( kelvin, false, $line_hash )


=end
