#!/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
$heat = 0

# 加熱幅 0: control, 1: 4times X half-width
$width = 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)  
  
  gphys.grid_copy.axis("t").pos.val.size.times{ |num|
    
    $file_label = "time = #{num*0.4}"
    
    print "#{num+1}: "
    t.plot(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 set_filename

  heat_ary = ["linear","positive"]
  width_ary = ["","-4width"]

  $filename = 
    "#{$ncfile}wavecisk-#{$layer}layer-#{heat_ary[$heat]}#{width_ary[$width]}.nc"

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

  $gifname = 
    "wavecisk-#{$layer}layer-#{heat_ary[$heat]}#{width_ary[$width]}-xyz1-#{$variable}"

  
  heat_ary = ["linear","positive_only"]
  
  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=1, eta1 = 1.5, eta2 = -1.5", 
  ]
  
  $lost_axis.push("init-heat: 4 times X-width") if $width == 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 --no-loop dcl_*.gif > #{$gifname}-anim.gif`
  `mv dcl_001.xwd.gif #{$gifname}-t0.gif`
  if File.size?("dcl_151.xwd.gif")
    `mv dcl_151.xwd.gif #{$gifname}-t60.gif`
  else
    `mv dcl_076.xwd.gif #{$gifname}-t30.gif`
  end
  `rm dcl_* tmp.pnm`
  
end

def setopt

  num = 0

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

  # 加熱型 0:linear, 1:positive
  $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")

  # 変数 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
  -width num   加熱幅 0: control, 1: 4times X half-width
  -height      出力変数 (height)
  -u           出力変数 (uvel)
  -v           出力変数 (vvel)\n\n"

    exit 1

  end

end




