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

#$local_path = '/work11/ape/yukiko/lib'
$local_path = '/GFD_Dennou_Work2/yukiko/eva01/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
#  set_directry  #model comparison
#  set_directry_agcm5  # agcm5
  puts $rezol
#  $groupid = ""

  if File.exist?("#{$ncfile_path}#{$groupid}_TR_#{$expID}.nc")  
    
    t = ape_new("#{$ncfile_path}#{$groupid}_TR_#{$expID}.nc")

    gphys = t.go("tr_tppn")
    gphys_u = t.go("tr_mslp").set_lost_axes("")
    gphys = gphys.rename("tr_tppn_lat3030").set_lost_axes("")
    
    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)
    
    1.times{ |num|
      
      time = - 100 + num
#      t.plot( gphys[true,true,time].lon_lotate )
#      t.plot( gphys[true,true,time].cut(true,-33..33).lon_lotate )
      t.plot( gphys[true,true,time].cut(true,-15..15).lon_lotate )
#      t.plot_comp( gphys[true,true,time].cut(true,-33..33).lon_lotate,
#	     gphys_u[true,true,time].cut(true,-33..33).lon_lotate )
      
    }
    t.grcls
    mkgifanim

  end

  if File.exist?("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}.nc") then
    unless $rezol == "T159L48_eml" || $rezol == "T159L48_non"
    
      t = ape_new("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}.nc")
      gphys = t.go("tr_tppn")
      gphys = gphys.rename("tr_tppn_lat3030").set_lost_axes("")
      
      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)
      
      1.times{ |num|
	
#	time = - 100 + num
	time = - 1
#	t.plot( gphys[true,true,time].lon_lotate )
      t.plot( gphys[true,true,time].cut(true,-33..33).lon_lotate )
#      t.plot_comp( gphys[true,true,time].cut(true,-33..33).lon_lotate,
#	     gphys_u[true,true,time].cut(true,-33..33).lon_lotate )

	
      }
      t.grcls
      mkgifanim
    end
  end

  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_u = t.go("tr_mslp").set_lost_axes("")
	gphys = gphys.rename("tr_tppn_lat3030").set_lost_axes("")

	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)
	
	1.times{ |num|
	  
#	  time = - 100 + num
	  time = - 1
#	  t.plot( gphys[true,true,time].lon_lotate )
      t.plot( gphys[true,true,time].cut(true,-33..33).lon_lotate )

	}
	t.grcls
	mkgifanim	
      end
    end
  end

  if $rezol == "T159L48_non"
#    if File.exist?("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_half1.nc") then
    if File.exist?("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_half2.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_u = t.go("tr_mslp").set_lost_axes("")
      gphys = gphys.rename("tr_tppn_lat3030").set_lost_axes("")

      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)

      1.times{ |num|

        time = - 100 + num
	time = - 1
#        t.plot( gphys[true,true,time].lon_lotate )
#      t.plot( gphys[true,true,time].cut(true,-33..33).lon_lotate )
      t.plot( gphys[true,true,time].cut(true,-15..15).lon_lotate )
#      t.plot_comp( gphys[true,true,time].cut(true,-33..33).lon_lotate,
#	     gphys_u[true,true,time].cut(true,-33..33).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_u = t.go("tr_mslp").set_lost_axes("")
      gphys = gphys.rename("tr_tppn_lat3030").set_lost_axes("")

      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)

      1.times{ |num|

        time = - 100 + num
	time = -1
#        t.plot( gphys[true,true,time].lon_lotate )
      t.plot( gphys[true,true,time].cut(true,-33..33).lon_lotate )
#      t.plot_comp( gphys[true,true,time].cut(true,-33..33).lon_lotate,
#	     gphys_u[true,true,time].cut(true,-33..33).lon_lotate )


      }
      t.grcls
      mkgifanim

    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`
    `mv dcl_001.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")
  
  $groupid = $groupid_hash["AGUforAPE"]
  $gr2ncfile_path = "/GFD_Dennou_Work2/yukiko/eva01/work11/ape/yukiko/data/#{$rezol}_expID01/"
  $ncfile_path = "/GFD_Dennou_Work2/yukiko/eva01/work11/ape/NetCDF/#{$rezol}/"
  $file_label = "#{$rezol}_#{$expID}"
#  $gifname = "#{$file_label}_tr_tppn"
  $gifname = "#{$file_label}_tr_tppn_lat1515"

  puts $rezol, $expID

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

  end

end


def set_directry_agcm5
  $groupid = $groupid_hash[$rezol]
  $ncfile_path = "/work11/ape/NetCDF/#{$rezol}/"
  $file_label = "#{$rezol}_#{$expID}"
  $gifname = "#{$file_label}_tr_tppn"
#  $gifname = "#{$file_label}_tr_tppn_log"
end


def set_directry
  $groupid = $groupid_hash[$rezol]
  $ncfile_path = "/work11/ape/NetCDF/other_group/#{$rezol.downcase}/"
  $file_label = "#{$rezol}_#{$expID}"
  $gifname = "#{$file_label}_tr_tppn"
#  $gifname = "#{$file_label}_tr_tppn_log"
end


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

class Ape_mkfig

  def mkfig_plot_comp(gphys, gphys_comp)

    if @fig_num == 3 then
      @data.mkdump_comp(gphys, gphys_comp)
    elsif
      @data.mkps_comp(gphys, gphys_comp)
    else
      @data.plot_comp(gphys, gphys_comp)
    end

  end
end

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

class Ape

  def plot_comp(gphys, gphys_u=nil, gphys_v=nil)
    gropn(1) if $gropn == nil
    
    if gphys.class == Array
      plot_set(gphys[0]) 
    else 
      plot_set(gphys) 
    end

    plot_main_comp(gphys, gphys_u)

    # タイトルを標準出力へ
    if gphys.class == Array then
      if  gphys[0].data.get_att("ape_name") then
	puts "#{gphys[0].name} (#{gphys[0].data.get_att("ape_name")})"
      end
    elsif gphys.data.get_att("ape_name") then
      puts "#{gphys.name} (#{gphys.data.get_att("ape_name")})"
    else
      puts "#{gphys.name}"
    end
  end

  def mkdump_comp(gphys, gphys_u=nil, gphys_v=nil)
    gropn(3) if $gropn == nil

    plot_comp(gphys, gphys_u)
    
    file_name = "#{$file_label}_#{gphys_u.name}.gif"
    `rm dcl_* tmp.pnm`  
    plot(gphys_u)
    `for file in *.xwd;do xwdtopnm ${file} | pnmcut 2 2 904 654 > tmp.pnm;done`
    `ppmtogif tmp.pnm > #{$fig_path}#{file_name}`
    `rm dcl_* tmp.pnm`  

    $file_name = $pre_file

  end

  def mkps_comp(gphys, gphys_u=nil, gphys_v=nil)
    'rm dcl*.ps'
    gropn(2) if $gropn == nil

    plot_comp(gphys, gphys_u)

    file_name = "#{$file_label}_#{gphys_u.name}.ps"
    gphys = gphys[0]  if gphys.class == Array
#    file_name = "#{$file_label}_#{file_name}.ps"
    `mv dcl*.ps  #{$fig_path}#{file_name}` 

  end

  def plot_main_comp(gphys, gphys_u=nil)

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

    GGraph.set_fig($fig_set_hash)

    # 描画
    if gphys.axnames.size == 1 || gphys.name =~ "gt_"
      
      GGraph.line( gphys, true, $line_hash )
    elsif  $tone_flag == false
      GGraph.contour( gphys, true, $cont_hash )
    elsif $tone_hash['levels'][0] == $tone_hash['levels'][1] 
      GGraph.tone( gphys, true ) 
      GGraph.contour( gphys, false ) unless $cont_flag == false
    else
      GGraph.tone( gphys, true , $tone_hash ) 
      GGraph.contour( gphys, false, $cont_hash ) unless $cont_flag == false
    end

    plot_set(gphys_u) 
#    NumRu::DCL.udpset('LABEL',false)
#    $cont_hash = {'label'=>false}
    GGraph.contour( gphys_u, false, $cont_hash )

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

    $file_label = "#{@file}@#{gphys.name}"  if $file_label == "filename"

    # 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

    # トーンバー
    plot_set(gphys) 
    DCL::Util::color_bar($cbar_conf)  

  end

end
