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

$local_path = '/home/yukiko/work/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 $rezol == "T639L24_eml"
#    if File.exist?("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_0042.nc") then
      
=begin
      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")
=end


#      file_name.push("#{$ncfile_path}AGUforAPE-03a_TR_#{$expID}_0042.nc")
  if $rezol == "T639L24_eml"
    file_name = "/home/yukiko/work/ape/GrADS/T639L24_eml_expID01/APE_T639L24_eml_expID01_cr_eft0.1d_6-16d_PRCP.nc"
  elsif $rezol == "T319L48_eml"
    file_name= "/home/yukiko/work/ape/yukiko/data/T319L48_eml_expID01/T319L48_eml_expID01_0042.nc"
  elsif $rezol == "T159L48_eml"
    file_name= "/home/yukiko/work/ape/yukiko/data/T159L48_eml_expID01/T159L48_eml_expID01_0014.nc"
  end

  t = Ape.new(file_name)
  if $rezol == "T639L24_eml"
    gphys = t.go("PRCP")          
  else
    gphys = t.go("tr_tppn")    
    gphys = gphys.rename("tr_tppn_visual")
  end

  DCL.swpset('LDUMP', true) 
  DCL.swpset('LWAIT',false) ; DCL.swpset('LWAIT1',false) 
  DCL.swpset('IPOSX', 50) ; DCL.swpset('IPOSY',50)  
      
#      DCL::sglset('LSOFTF', false)
#      DCL::grfrm
      
      GGraph.set_fig('viewport'=>[0.10,0.80,0.28,0.63]) # 縦横比は 2 : 1  
      DCL.uzfact(0.6)                                   # 文字の大きさ倍

      DCL.sgpset('lcorner',false)    # コーナーマークはつけない
      DCL.slmgn(0.0, 0.0, 0.0, 0.0)  # 描画マージン指定
      DCL.sgpset('lfprop',true)      # プロポーショナルフォントを使う
      DCL.sgpset('lfull',true)       # 全画面表示
      DCL.sgpset('lcntl', false)     # アンダーバーは普通に表示
      DCL.uscset('cyspos', 'B' )      # y 軸単位を書く位置
      
      DCL.sgscmn(9)

      t.gropn(1)

# colormap_03
#      patterns = NArray[7999, 60999, 65999, 75999, 80999, 85999, 99999]
#      levels = NArray[-1,0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 1000]

#colormap_09_sono1
      patterns =  NArray[70999, 60999, 35999, 30999, 25999, 20999, 15999, 10001]
      levels = exp(NArray[0.0,  3.0, 3.6, 4.2, 4.8, 5.4, 6.0, 6.6, 20.0])*0.000001
      levels[0] = -0.01

#colormap_09_sono2
#      patterns = NArray[70999, 35999, 30999, 25999, 20999, 15999, 10001]
#      levels = NArray[-1,0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 1000]

      $tone_hash = { 'patterns'=> patterns, 'levels'=>levels }

      patterns =  NArray[70999]
      levels =    NArray[-0.01,exp(20.0)*0.00001]
      $tone_hash_0 = { 'patterns'=> patterns, 'levels'=>levels }

#      patterns =  NArray[60999, 35999, 30999, 25999, 20999, 15999, 12999, 10001]
#      levels = exp(NArray[3.0, 3.6, 4.2, 4.8, 5.4, 6.0, 6.6, 30.0])*0.000001
      rmiss = DCL.glpget('rmiss')
      patterns =  NArray[60999, 40999, 35999, 20999, 15999, 12999]
      levels = NArray[10.0**(-6), 10.0**(-5.5), 10.0**(-5), 10.0**(-4.5), 10.0**(-4), 10.0**(-3.5), rmiss]

      $tone_hash_1 = { 'patterns'=> patterns, 'levels'=>levels }


      100.times{ |num|
#      240.times{ |num|
#      1.times{ |num|

	DCL::sglset('LSOFTF', false)	
	DCL::grfrm
	
	DCL::sgstrn(30)
	DCL::sgswnd(0, 360, -90, 90)
	DCL::sgsvpt(0.1, 0.9, 0.1, 0.9)
	DCL::sgssim(0.2, -0.25, 0.0)
	DCL::sgsmpl(0.0, 20.0, -20.0)
	DCL::sgstxy(-180.0, 180.0, 0.0, 90.0)
	DCL::sgstrf

	time = - 100 + num
#	time = num
	GGraph.tone( gphys[true,true,time].lon_lotate, false , $tone_hash_0 ) 
	GGraph.tone( gphys[true,true,time].lon_lotate, false , $tone_hash_1 ) 
#	DCL::umpgrd
	
      }
      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`
#  `gifsicle --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 = "T319L48_eml"
  $rezol = "T159L48_eml"
#  $rezol = "T639L24_eml"
  $expID = "control"

  $rezol = ARGV[num+1]  if num = ARGV.index("-resol")
  $expID = ARGV[num+1]  if num = ARGV.index("-sst")
  
  $gr2ncfile_path = "/home/yukiko/work/ape/yukiko/data/#{$rezol}_expID01/"
  $ncfile_path = "/home/yukiko/work/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



