#!/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","3kw1","Qobs","control-5N","3keq"]
sstid_all = ["control","flat","peaked","1keq","3keq","3kw1","Qobs","control-5N","flat3keq","Qobs3keq","H1998con","H1998pa"]
sstid_nocon = ["flat","peaked","1keq","3keq","3kw1","Qobs","control-5N"]
sstid_tmp = ["control","flat","Qobs","3keq","flat3keq","Qobs3keq",
  "H1998con","H1998pa"]
groupid = ["aguforape","agcm5_adj","agcm5_kuo"]
cumulusid = ["eml","ias","ksc","kuo","mca","non"]

 rezolid = ["T39L48_non", "T39L24_non","T39L96_non", "T79L48_non", "T159L48_non"]
# rezolid = ["T39L48_non", "T39L24_non","T39L96_non", "T79L48_non"]
#rezolid = ["T39L48_eml", "T39L24_eml","T39L96_eml", "T79L48_eml", "T159L48_eml"]
#rezolid = ["T39L48_eml", "T39L24_eml","T39L96_eml", "T79L48_eml"]

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


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

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

set_dir_id  = groupid[0]

$iryu = -7.0


END{


  rezolid = ["T39L48_non", "T159L48_non"]
  rezolid.each{ |item|
    $expID = "control"
    $cumulus = "non"
    $rezol = item
    set_dir(set_dir_id,host)
#  a.nc_tr_wkplot_mono
      a.nc_tr_wkplot
  }


  cumulusid = ["eml","ias","ksc","kuo","mca","non"]
  sstid_all = ["control","flat","peaked","1keq","3keq","3kw1","Qobs","control-5N","flat3keq","Qobs3keq","H1998con","H1998pa"]

  sstid_all.each{ |sst|
    cumulusid.each{ |item|
      $expID = sst
      $cumulus = item
      $rezol = "T39L48_#{item}"
      set_dir(set_dir_id,host)
      #  a.nc_tr_wkplot_mono
#      a.nc_tr_wkplot
    }
  }

  rezolid = ["T39L24_non","T39L96_non", "T79L48_non", "T159L48_non"]
  sstid.each{ |sst|
    rezolid.each{ |item|
      
      $expID = sst
      $cumulus = "non"
      $rezol = item
      set_dir(set_dir_id,host)
#  a.nc_tr_wkplot_mono
#      a.nc_tr_wkplot
    }

  }

#sstid = ["control-5N","3keq","Qobs"]
  rezolid = ["T79L48_eml"]#,"T39L96_eml", "T79L48_eml"]
  sstid.each{ |sst|
    rezolid.each{ |item|
      
      $expID = sst
      $cumulus = "eml"
      $rezol = item
      set_dir(set_dir_id,host)
#  a.nc_tr_wkplot_mono
#      a.nc_tr_wkplot
    }

  }



  rezolid = ["T39L24_eml","T39L96_eml", "T79L48_eml"]
  rezolid = ["T79L48_eml"]
sstid = ["control","flat","peaked","1keq","3kw1","Qobs","control-5N","3keq"]
  sstid.each{ |sst|
    rezolid.each{ |item|
#  $cumulus = "eml"
#  $rezol = "T159L48_eml"

	$expID = sst
	$cumulus = "eml"
	$rezol = item
	set_dir(set_dir_id,host)
#  a.nc_tr_wkplot_mono
#  a.nc_tr_wkplot
}
  }


  ["AGCM5_adj","AGCM5_kuo","AGCM5_kuo-nosc"].each{ |item|
  #cumulusid.each{ |item|
    sstid_all.each{ |sst|
  #    $cumulus = item
  #    $rezol = "T39L48_#{item}"
    $rezol = item
      $expID = sst
#      set_dir(set_dir_id,host)
    set_directry_agcm5
      #  a.nc_tr_wkplot_mono
#      a.nc_tr_wkplot
    }
  }


#sstid = ["control"]
#sstid = ["controln48"]
#sstid = ["peaked","flat","qobs","control_5n","1keq","3keq","3kw1"]
#sstid = ["control","flat","control5n","1keq","3keq","3kw1"]

sstid = ["control","peaked","flat","qobs","control5n",
  "1keq","3keq","3kw1"]
  sstid.each{ |sst|
    $rezol = "GSFC"
    $expID = sst
    set_directry
#    a.nc_tr_wkplot    
  }

sstid = ["control","flat","control5n","1keq","3keq","3kw1"]
  sstid.each{ |sst|
    $rezol = "K1JAPAN"
    $expID = sst
    set_directry
#    a.nc_tr_wkplot    
  }


sstid = ["control","peaked","flat","qobs","control5n",
  "1keq","3keq","3kw1"]
sstid = ["control5n"]
  sstid.each{ |sst|
    $rezol = "NCAR"
    $expID = sst
    set_directry
#    a.nc_tr_wkplot    
  }

sstid = ["control"]
  sstid.each{ |sst|
    $rezol = "MGO"
    $expID = sst
    set_directry
#    a.nc_tr_wkplot    
  }

sstid = ["control","peaked","flat","qobs","1keq"]
  sstid.each{ |sst|
    $rezol = "UKMO_n48"
    $expID = sst
    set_directry
#    a.nc_tr_wkplot    
  }

sstid = ["3keq","3kw1","control","control5n"]
  sstid.each{ |sst|
    $rezol = "UKMO_n96"
    $expID = sst
    set_directry
#    a.nc_tr_wkplot    
  }

}



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


def set_directry_agcm5
  $groupid = $groupid_hash[$rezol]
#  $ncfile_path = "/work11/ape/NetCDF/#{$rezol}/"
  $ncfile_path = "/home/yukiko/eva01/work11/ape/NetCDF/#{$rezol}/"
  $file_label = "#{$rezol}_#{$expID}"
end

class Ape_mkfig

  def nc_tr_wkplot
    
#    @data = Ape.new("/work11/ape/yukiko/data/wk-stspct/#{$rezol}_#{$expID}_wk_40smooth.nc")
    @data = Ape.new("/home/yukiko/work/ape/yukiko/data/wk-stspct/#{$rezol}_#{$expID}_wk_40smooth.nc")
    @data.netcdf_open.var_names.each { |name|
      unless name == "wvn" || name == "freq" || name == "freq0" || name == "noname" 
	if name =~ /lw_toa|mslp|ps/
	  lost_axis = []
	elsif name =~ /250/
	  lost_axis = ["plev=25000 Pa"]
	elsif name =~ /500/
	  lost_axis = ["plev=50000 Pa"]
	end
	
	# 時空間スペクトル
	gphys = @data.gphys_open(name). 
	  add_lost_axes(lost_axis)
	dim1 = gphys.coord(0).shape_current.to_s.to_i
	dim2 = gphys.coord(1).shape_current.to_s.to_i

	if name =~ /_asym_org_spct/
	  gphys = gphys.rename(name.sub("asym", "sym"))
	elsif name =~ /_sym_org_spct/
	  gphys = gphys.rename(name.sub("sym", "asym"))
	end

	gphys = gphys.rename(name.sub("ps", "mslp") )

	  puts name
	if name =~ /bg_spct/ # || name =~ /org_spct/
	  mkfig_plot(gphys[((dim1+1)/2-31)..((dim1+1)/2+29),0..80].log.
		   add_lost_axes("logarithm of data") )
	else
	  mkfig_plot(gphys[((dim1+1)/2-31)..((dim1+1)/2+29),0..80])
	end

      end
    }
    
  end

  def nc_tr_wkplot_mono
    
#    @data = Ape.new("/work11/ape/yukiko/data/wk-stspct/#{$rezol}_#{$expID}_wk_40smooth.nc")
    @data = Ape.new("/home/yukiko/eva/work/ape/yukiko/data/wk-stspct/#{$rezol}_#{$expID}_wk_40smooth.nc")
	
    # 時空間スペクトル
    gphys = @data.gphys_open("tr_tppn_sym_spct").
      rename("tr_tppn_sym_spct_mono")
    dim1 = gphys.coord(0).shape_current.to_s.to_i
    dim2 = gphys.coord(1).shape_current.to_s.to_i

    mkfig_plot(gphys[((dim1+1)/2-31)..((dim1+1)/2+29),0..80])
    
  end

end



=begin


  sstid_tmp.each{ |item|
    $cumulus = "eml"
    $rezol = "T39L48_#{$cumulus}"
    $expID = item
    set_dir(set_dir_id,host)
    a.nc_tr_wkplot    
    $cumulus = "ias"
    $rezol = "T39L48_#{$cumulus}"
    $expID = item
    set_dir(set_dir_id,host)
    a.nc_tr_wkplot    
    $cumulus = "ksc"
    $rezol = "T39L48_#{$cumulus}"
    $expID = item
    set_dir(set_dir_id,host)
    a.nc_tr_wkplot    
    $cumulus = "kuo"
    $rezol = "T39L48_#{$cumulus}"
    $expID = item
    set_dir(set_dir_id,host)
    a.nc_tr_wkplot    
    $cumulus = "mca"
    $rezol = "T39L48_#{$cumulus}"
    $expID = item
    set_dir(set_dir_id,host)
    a.nc_tr_wkplot    
  }

  $cumulus = "non"
  $rezol = "T159L48_#{$cumulus}"
  $expID = "3kw1"
  set_dir(set_dir_id,host)

  sstid.each{ |item|
    $cumulus = "non"
    $rezol = "T159L48_#{$cumulus}"
    $expID = item
    set_dir(set_dir_id,host)
#    a.nc_tr_wkplot
    
  }
    
  sstid.each{ |item|
    $cumulus = "non"
    $rezol = "T79L48_#{$cumulus}"
    $expID = item
    set_dir(set_dir_id,host)
#    a.nc_tr_wkplot
    $cumulus = "eml"
    $rezol = "T79L48_#{$cumulus}"
    $expID = item
    set_dir(set_dir_id,host)
#    a.nc_tr_wkplot
    
  }
  
  sstid.each{ |item|
    $cumulus = "non"
    $rezol = "T39L96_#{$cumulus}"
    $expID = item
    set_dir(set_dir_id,host)
#    a.nc_tr_wkplot
    $cumulus = "eml"
    $rezol = "T39L96_#{$cumulus}"
    $expID = item
    set_dir(set_dir_id,host)
#    a.nc_tr_wkplot
    
  }
  

  sstid.each{ |item|
    $cumulus = "non"
    $rezol = "T39L24_#{$cumulus}"
    $expID = item
    set_dir(set_dir_id,host)
#    a.nc_tr_wkplot
    $cumulus = "eml"
    $rezol = "T39L24_#{$cumulus}"
    $expID = item
    set_dir(set_dir_id,host)
#    a.nc_tr_wkplot

  }
  
  sstid_all.each{ |item|
    $cumulus = "non"
    $rezol = "T39L48_#{$cumulus}"
    $expID = item
    set_dir(set_dir_id,host)
#    a.nc_tr_wkplot
    $cumulus = "eml"
    $rezol = "T39L48_#{$cumulus}"
    $expID = item
    set_dir(set_dir_id,host)
#    a.nc_tr_wkplot    
  }


=end






