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

$local_path = '/work11/ape/yukiko/lib'
$: << $local_path

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

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

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

#sstid = ["control","flat","peaked","1keq","3keq","3kw1","Qobs","control-5N"]

sstid = ["control","peaked","flat","Qobs","control-5N",
  "1keq","3keq","3kw1","flat3keq","Qobs3keq",
  "H1998con","H1998pa","HS1986"]

rezolid = ["T39L48_eml", "T39L24_eml","T39L96_eml", "T79L48_eml", 
  "T39L48_ias","T39L48_ksc","T39L48_kuo","T39L48_mca","T39L48_non"]

# rezolid = ["T159L48_eml","T319L48_eml"]

host = "eva01"
 a = Ape_mkfig.new 3
# a = Ape_mkfig.new 1


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

END{

  setopt
  puts $rezol

  if File.exist?("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}.nc") then
    unless $rezol == "T159L48_eml"
   
      t = ape_new("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}.nc")

      gphys = t.go("tr_tppn")
#      gphys = gphys.rename("tr_tppn_blue")
      
      DCL.swpset('LDUMP', true) 
      DCL.swpset('LWAIT',false) ; DCL.swpset('LWAIT0',false) 
      DCL.swpset('LWAIT1',false)
      DCL.swpset('IPOSX', 50) ; DCL.swpset('IPOSY',50)  
      t.gropn(1)
      
      100.times{ |num|
	
	time = - 100 + num
	t.plot( gphys[true,true,time].lon_lotate )
	
      }
      t.grcls
      mkgifanim
    end
  end

  if $rezol == "T159L48_non"
    if File.exist?("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_half1.nc") then
      
      file_name = []
      file_name.push("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_half1.nc")
      file_name.push("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_half2.nc")

	t = Ape.new(file_name)
      
      gphys = t.go("tr_tppn")    
      #	gphys = gphys.rename("tr_tppn_blue")
      DCL.swpset('LDUMP', true) 
      DCL.swpset('LWAIT',false) ; DCL.swpset('LWAIT0',false) 
      DCL.swpset('LWAIT1',false)
      DCL.swpset('IPOSX', 50) ; DCL.swpset('IPOSY',50)  
      t.gropn(1)
      
      100.times{ |num|
	
	time = - 100 + num
	t.plot( gphys[true,true,time].lon_lotate )
	
      }
      t.grcls
      mkgifanim	
      
    end
  end

  if $rezol == "T319L48_non"
    if File.exist?("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_0031.nc") then
      
      file_name = []
      file_name.push("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_0031.nc")
      file_name.push("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_0032.nc")
      file_name.push("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_0033.nc")
      file_name.push("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_0034.nc")
      file_name.push("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_0035.nc")
      file_name.push("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_0036.nc")
      file_name.push("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_0037.nc")
      file_name.push("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_0038.nc")
      file_name.push("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_0039.nc")
      file_name.push("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_0040.nc")
      file_name.push("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_0041.nc")
      file_name.push("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_0042.nc")

      t = Ape.new(file_name)
      
      gphys = t.go("tr_tppn")    
      #	gphys = gphys.rename("tr_tppn_blue")
      DCL.swpset('LDUMP', true) 
      DCL.swpset('LWAIT',false) ; DCL.swpset('LWAIT0',false) 
      DCL.swpset('LWAIT1',false)
      DCL.swpset('IPOSX', 50) ; DCL.swpset('IPOSY',50)  
      t.gropn(1)
      
      100.times{ |num|
	
	time = - 100 + num
	t.plot( gphys[true,true,time].lon_lotate )
	
      }
      t.grcls
      mkgifanim	
      
    end
  end


=begin
  if $expID == "control" then 

    if $rezol == "T159L48_eml" || $rezol == "T319L48_eml" then

      file_name = []
      
      if File.exist?("#{$gr2ncfile_path}") then
	Dir.foreach($gr2ncfile_path) { |item|
	  file_name.push("#{$gr2ncfile_path}/#{item}") if item =~ /.nc$/
	}
	t = Ape.new(file_name)

	gphys = t.go("tr_tppn")    
#	gphys = gphys.rename("tr_tppn_blue")
	DCL.swpset('LDUMP', true) 
	DCL.swpset('LWAIT',false) ; DCL.swpset('LWAIT0',false) 
	DCL.swpset('LWAIT1',false)
	DCL.swpset('IPOSX', 50) ; DCL.swpset('IPOSY',50)  
	t.gropn(1)
	
	100.times{ |num|
	  
	  time = - 100 + num
	  t.plot( gphys[true,true,time].lon_lotate )
	  
	}
	t.grcls
	mkgifanim	
      end
    end
  end
=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`
  if File.size?("dcl_100.xwd.gif")
    `mv dcl_100.xwd.gif #{$gifname}-t0.gif`
  end
  `rm dcl_* tmp.pnm`
  
end


def setopt

  num = 0

  $rezol = "T39L48_eml"
  $expID = "control"

  $rezol = ARGV[num+1]  if num = ARGV.index("-resol")
  $expID = ARGV[num+1]  if num = ARGV.index("-sst")
  
  $gr2ncfile_path = "/work11/ape/yukiko/data/#{$rezol}_expID01/"
  $ncfile_path = "/work11/ape/NetCDF/#{$rezol}/"
  $file_label = "#{$rezol}_#{$expID}"
  $gifname = "#{$file_label}_tr_tppn"
#  $gifname = "#{$file_label}_tr_tppn_blue"

  puts $rezol, $expID

  if ARGV.index("-h") || ARGV.index("-help") 
    print "
  -resol name   解像度 + 積雲パラメタ (T39L48_eml)
  -sst   name   SST (control)\n"
    exit 1

  end

end




=begin
rezolid.each { |rezol|

    $rezol = rezol
    $ncfile_path = "/work11/ape/NetCDF/#{$rezol}/"
    $ncfile_write_path = "/work11/ape/ohfuchi/#{$rezol}/"
    puts $rezol

    sstid.each{ |item|
      
      $expID = item

      if File.exist?("#{$ncfile_path}AGUforAPE-03a_ML_#{$expID}.nc") then
	mkanim
      end
    
    }
  }


=end



