#!/usr/bin/env ruby
# ----------------------------------------------
# local load path

 $local_path = '/work11/ape/yukiko/lib'
# $local_path = '/home/yukiko/tmp/ape-data/lib'
$: << $local_path

# ----------------------------------------------
# 必要なライブラリ, モジュールの読み込み

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


# gif, netcdf の格納場所
$fig_path = "/work11/yukiko/waveCISK/figs/tmp/"
$ncfile_path = "/work11/yukiko/spmodel-yukiko/data/"


#----------------------------------------------
# ユーザ設定

# 鉛直層 
$layer = 4

# 加熱型 0:linear, 1:positive, 2: noheat
$heat = 0

# 加熱幅 0: control, 1: 4times X half-width
$width = 0

# 加熱タイプ 0: eta1=1.5,eta2=-1.5, 1: eta1=1.5, eta2=0
$eta = 0

# β項 0: beta=0, 1:beta=1
$beta = 1

# 次元 0: 2dim, 1: 1dim(初期値1次元問題)
$dim = 0

# 加熱源の中心位置 0: y=0, 1 y=1
$y1shift = "0"

# 変数 height, u, v
$variable = "height"


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


END{

  setopt
  set_filename
  t = ape_new($filename)
  gphys = t.go($variable)

#  DCL.swpset('LDUMP', true) 
  DCL.swpset('LWAIT',false) ; DCL.swpset('LWAIT0',false) 
  DCL.swpset('LWAIT1',false)
#  DCL.swpset('IPOSX', 50) ; DCL.swpset('IPOSY',50)  
  DCL.swpset('LSEP',true)
  t.gropn(2)
  
  gphys.grid_copy.axis("t").pos.val.size.times{ |num|
    
    $file_label = "time = #{num*0.4}"
    
    print "#{num+1}: "

    if $variable == "vvel" 
      plot_def(gphys[true,-51..-11,true,num])
    else
      plot_def(gphys.cut(true,0,true,true)[true,true,num])
    end
    t.plot_main(gphys[true,true,0,num].
	   set_att("ape_name","#{gphys.data.get_att("long_name")}").
	   set_lost_axes($lost_axis))
    
  }

$tonesetof_flag == true

  t.grcls

  print "gif convert start\n"
  mkgifanim

}



# トーン, コンターパターンのデフォルト設定
def plot_def(gphys)
  
  # トーンパターンのデフォルト
  patterns = NArray[30999, 35999, 40999, 55999, 70999, 75999, 85999]
  
  # トーンレベルのデフォルト: ((最大値 - 最小値) / 8 )
=begin
  levels = Array.new
  dx = (gphys.max - gphys.min)/7 ;  x  = gphys.min 
  8.times{ |num| ; levels.push(x) ;  x = dx + x }
  levels[7] = levels[7] + dx ; levels[0] = levels[0] - dx
  levels = NArray.to_na(levels)
  7.times{ |num| 
    if levels[num] < 0 && levels[num+1] > 0 
      if levels[num].abs < levels[num+1].abs
	levels = levels - levels[num]
      else
	levels = levels - levels[num+1]
      end
    end
  }
=end

  dx = (gphys.max - gphys.min)/7
  levels = NArray[-100*dx, -2*dx, -dx, 0.0, dx, 2*dx, 3*dx,100*dx]


  # カラーバーセットのデフォルト: colorbar.rb 参照
  $cbar_conf = {
    "levels"=>levels, 
    "colors"=>patterns,
    "eqlev"=>true, 
    "nobound"=>0, 
    "tick1"=>20,"tick2"=>1
  }
  
  cont_lev, line_type, label  = cont_lev_set(levels)
  
  # コンター, トーンセット
  $tone_hash = { 'patterns'=> patterns, 'levels'=>levels }
  $cont_hash = {'levels' => cont_lev, 'index'=> [2,1], 'label'=> label, 
    'line_type'=> line_type }
  
  # ラインセット
  $line_hash = { 'exchange'=>false }
  
  # window セット
  $fig_set_hash = { "window" => nil }
  
end


def cont_lev_set(levels)
  # コンターラベルのデフォルト: (トーンレベル/2 )
  # GPhys のコンター決め打ちオプションが言うこと聞いてくれないので, 
  # なんだか無茶なことをしてる. 
  x  = levels[0]
  dx = levels[3] -levels[2]
  cont_lev  = Array.new
  line_type = Array.new
  (levels.size*2).times{ |num|
    if num % 2 == 0 
      cont_lev.push(levels[num/2])
    else
      cont_lev.push(levels[num/2]+dx/2)
    end
    if cont_lev[num] < 0
      line_type.push(3) 
    else
      line_type.push(1)
    end
  }
  
  label = Array.new
  cont_lev.size.times{ |num|
    label[num] = format("%#.3g", cont_lev[num]).to_s 
    label[num] = "" unless num % 2 == 0
  }
  return cont_lev, line_type, label
end

#----------------------------------------------
# 設定

def set_filename

  heat_ary = ["linear","positive","noheat"]
  width_ary = ["","-4width"]
  eta_ary = ["","-eta1"]
  beta_ary = ["-beta0",""]
  dim_ary = ["","-1dim"]
  y1shift_ary  = ["","-y1shift"]

  $filename = 
    "wavecisk-#{heat_ary[$heat]}#{eta_ary[$eta]}#{beta_ary[$beta]}#{width_ary[$width]}#{y1shift_ary[$y1shift]}#{dim_ary[$dim]}.nc"
#    "#{$ncfile}wavecisk-#{$layer}layer-#{heat_ary[$heat]}#{width_ary[$width]}.nc"

  print "#{$filename}@#{$variable}\n"

  $gifname = 
    "wavecisk-#{heat_ary[$heat]}#{eta_ary[$eta]}#{beta_ary[$beta]}#{width_ary[$width]}#{y1shift_ary[$y1shift]}#{dim_ary[$dim]}-xyz1-#{$variable}" 
#    "wavecisk-#{$layer}layer-#{heat_ary[$heat]}#{width_ary[$width]}-xyz1-#{$variable}"

  heat_ary = ["linear","positive_only","no"]
  eta_ary = [
    "eta1 = 1.5, eta2 = -1.5",
    "eta1 = 1.5, eta2 = 0.0"
  ]
  beta_ary = ["beta=0","beta=1"]

  sign = "YU.YAMADA #{Time.now.strftime("%Y/%m/%d %H:%M:%S JST")}"
  
  $lost_axis = [
    sign,
    "z=1", 
    "rezol: k=l=64, z=#{$layer}", 
    "#{heat_ary[$heat]}_heat, linear_equation", 
    "#{beta_ary[$beta]}, #{eta_ary[$eta]}"
  ]
  
  $lost_axis.push("init-heat: 4 times X-width") if $width == 1
  $lost_axis.push("init-heat: uniform along the Y-axis") if $dim == 1
  $lost_axis.push("init-heat: shift center position to y0=1") if $y1shift == 1
end


# gif アニメ作成シェルスクリプト
def mkgifanim

#  `for file in *.xwd ; do xwdtopnm ${file} |  pnmcut 2 2 904 654 > tmp.pnm; ppmtogif tmp.pnm > ${file}.gif; done >& /dev/null `
#  `gifsicle --delay=50 --loopcount=100 dcl_*.gif > #{$gifname}-anim.gif`
  `mv dcl_001.ps #{$gifname}-t0.ps`
  if File.size?("dcl_151.ps")
    `mv dcl_151.ps #{$gifname}-t60.ps`
  else
    `mv dcl_076.ps #{$gifname}-t30.ps`
  end
  `rm dcl_*`
  
end

def setopt

  num = 0

  $layer = ARGV[num+1].to_i if num = ARGV.index("-lay")

  # 加熱型 0:linear, 1:positive 2:noheat
  $heat = ARGV[num+1].to_i if num = ARGV.index("-heat")

  # 加熱幅 0: control, 1: 4times X half-width
  $width = ARGV[num+1].to_i if num = ARGV.index("-width")

  # 加熱タイプ 0: eta1=1.5,eta2=-1.5, 1: eta1=1.5, eta2=0
  $eta = ARGV[num+1].to_i if num = ARGV.index("-eta")

  # β項 0: beta=0, 1:beta=1
  $beta = ARGV[num+1].to_i if num = ARGV.index("-beta")

  # 次元 0: 2dim, 1: 1dim(初期値1次元問題)
  $dim = ARGV[num+1].to_i if num = ARGV.index("-dim")

  # 加熱源の中心位置 0: y=0, 1: y=1
  $y1shift = ARGV[num+1].to_i if num = ARGV.index("-y1shift")

  # 変数 height, u, v
  if ARGV.index("-height")
    $variable = "height"
  elsif ARGV.index("-u")
    $variable = "uvel"
  elsif ARGV.index("-v")
    $variable = "vvel"
  end

  if ARGV.index("-h") || ARGV.index("-help") 
    print "
  -lay     num   鉛直層数
  -heat    num   加熱型 0:linear, 1:positive, 2:noheat
  -width   num   加熱幅 0: control, 1: 4times X half-width
  -eta     num   加熱モード 0: eta1=1.5,eta2=-1.5, 1: eta1=1.5, eta2=0
  -beta    num   β項 0: beta=0, 1:beta=1
  -dim     num   次元 0: 2dim, 1: 1dim(初期値1次元問題)
  -y1shift num   加熱源の中心位置 0: y=0, 1: y=1
  -height        出力変数 (height)
  -u             出力変数 (uvel)
  -v             出力変数 (vvel)\n\n"

    exit 1

  end

end




