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

$local_path = '/home/yukiko/work/ape/yukiko/lib'
$: << $local_path
$fig_path = "/home/yukiko/work/ape/NetCDF/other_group/figs/tmp/"

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

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


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


END{

a = ape_mkfig_new(3)

#      "FRCGC" 
#      "UKMO_n48"
#      "UKMO_n96"
# pf "DWD", "ECMWF","GSFC","K1JAPAN","LASG","NCAR"
  
#  ["CGAM", "CSIRO_old", "CSIRO_standard", "DWD", "ECMWF", "GFDL" ,
#    "GSFC", "K1JAPAN", "LASG", "MIT", "MRI", "NCAR", 
#    "UKMO_n48", "UKMO_n96"].each{ |resol|
  
#  ["CGAM", "CSIRO_old", "CSIRO_standard", "DWD", "ECMWF", "GFDL" ,
#    "GSFC", "K1JAPAN", "LASG", "MIT", "MRI", "NCAR"].each{ |resol|
    ["AGCM5_kuo","AGCM5_adj"].each{ |resol|
#    ["AGCM5_kuo_old.2005","AGCM5_adj_old.2005"].each{ |resol|
#    ["AGCM5_adj_old.2005"].each{ |resol|

#  ["UKMO_n96"].each{ |resol|

#  ["ECMWF_07"].each{ |resol|
#  ["AGUforAPE","GFDL"].each{ |resol|

#   sstid = ["control"]

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

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

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

#    sstid = ["1keq","3keq","3kw1"]
#    sstid = ["3keq","3kw1"]
#    sstid = ["1keq"]
    sstid.each{ |item|
      $expID = item
      $rezol = resol
      set_directry

#      a.nc_sh_vor_anm         unless $expID == "control"
#          a.nc_ml_anm         unless $expID == "control"
#      a.nc_sh_anm        unless $expID == "control"
#      a.nc_gt_comparing_rezol(0)
#      a.nc_gt_comparing_rezol(1)
#      a.nc_gt_comparing_rezol(2)
#      a.nc_gt_comparing_rezol(3)
#      a.nc_sh_zonal_comparing_rezol(0)
#      a.nc_sh_zonal_comparing_rezol(1)
#      a.nc_sh_zonal_comparing_rezol(2)
#      a.nc_sh_zonal_comparing_rezol(3)

#    a.nc_gt_comparing_expid
#    a.nc_sh_zonal_comparing_expid

#      a.nc_sh_tnflux_level

      a.nc_pf
      a.nc_ml
      a.nc_sh
      a.nc_gt
      a.nc_tr
      a.nc_sh_zonal

#     a.nc_ml_tuom_anm
#     a.nc_sh_psuv_anm
#     a.nc_sh_tuv500_anm
#     a.nc_sh_phiuv250_anm
#     a.nc_sh_phiuv850_anm

#      a.nc_sh_zonal
#      a.nc_sh_zonal_anm  unless $expID == "control"

#      a.nc_ml
#      a.nc_sh
#      a.nc_tr
#      a.nc_gt

      p "#{item}............................................."

      #    a.nc_mf_anm
      #    a.nc_mf
      #    a.nc_tr_300day
      #    a.gr_tr_all_300day
      #    a.nc_gt_anm 
      #    a.nc_ml_anm         unless $expID == "control"


    }

    sstid = ["H1998con", "HS1986"]
    sstid.each{ |item|
      $expID = item
      $rezol = resol
      set_directry
      a.nc_ml_anm
      a.nc_sh_anm
      a.nc_sh_zonal_anm
      a.nc_sh_vor_anm
      a.nc_ml_tuom_anm
      a.nc_sh_psuv_anm
      a.nc_sh_tuv500_anm
      a.nc_sh_phiuv250_anm
      a.nc_sh_phiuv850_anm
    }

    $expID = "H1998pa"
    set_directry
    a.nc_ml_anm("H1998con")
    a.nc_sh_anm("H1998con")
    a.nc_sh_zonal_anm("H1998con")
    a.nc_sh_vor_anm("H1998con")
    a.nc_ml_tuom_anm("H1998con")
    a.nc_sh_psuv_anm("H1998con")
    a.nc_sh_tuv500_anm("H1998con")
    a.nc_sh_phiuv250_anm("H1998con")
    a.nc_sh_phiuv850_anm("H1998con")

    $expID = "flat3keq"
    set_directry
    a.nc_ml_anm("flat")
    a.nc_sh_anm("flat")
    a.nc_sh_zonal_anm("flat")
    a.nc_sh_vor_anm("flat")
    a.nc_ml_tuom_anm("flat")
    a.nc_sh_psuv_anm("flat")
    a.nc_sh_tuv500_anm("flat")
    a.nc_sh_phiuv250_anm("flat")
    a.nc_sh_phiuv850_anm("flat")

    $expID = "Qobs3keq"
    set_directry
    a.nc_ml_anm("Qobs")
    a.nc_sh_anm("Qobs")
    a.nc_sh_zonal_anm("Qobs")
    a.nc_sh_vor_anm("Qobs")
    a.nc_ml_tuom_anm("Qobs")
    a.nc_sh_psuv_anm("Qobs")
    a.nc_sh_tuv500_anm("Qobs")
    a.nc_sh_phiuv250_anm("Qobs")
    a.nc_sh_phiuv850_anm("Qobs")


  }

sstid = ["control","flat","qobs","control5n","1keq","3keq","3kw1","peaked"]
  sstid.each{ |item|
    $expID = item
    $rezol = "GSFC"
    set_directry
#    a.nc_sh_zonal_anm  unless $expID == "control" 
#    a.nc_ml
#    a.nc_ml_anm         unless $expID == "control"
  }


 sstid = ["control","peaked","flat","qobs","control5n","1keq","3keq","3kw1"]
  sstid.each{ |item|
    $expID = item
    $rezol = "NCAR"
    set_directry
#    a.nc_gt_anm 
    #a.nc_mf_anm         unless $expID == "control"
    #a.nc_mf

  }

$groupid = "MGO-02"
$expID = "control"
$rezol = "MGO"
set_directry
#  a.nc_sh_zonal_anm  unless $expID == "control"
#  a.nc_ml
#  a.nc_ml_anm         unless $expID == "control"
#    a.nc_tr
#    a.nc_ml_anm         unless $expID == "control"
#    a.nc_sh_anm 
#    a.nc_gt
#    a.nc_sh_zonal
#    a.nc_sh_zonal_anm  unless $expID == "control"
#    a.nc_sh
#    a.nc_gt_anm 
    #a.nc_mf_anm         unless $expID == "control"
    #a.nc_mf

  

$groupid = "NICAM-04"
$expID = "control"
$rezol = "FRCGC"
set_directry
#  a.nc_sh_zonal_anm  unless $expID == "control"
#  a.nc_ml
#  a.nc_ml_anm         unless $expID == "control"
#    a.nc_tr
#    a.nc_ml_anm         unless $expID == "control"
#    a.nc_sh_anm 
#    a.nc_gt
#    a.nc_sh_zonal
#    a.nc_sh_zonal_anm  unless $expID == "control"
#    a.nc_sh
#    a.nc_gt_anm 
    #a.nc_mf_anm         unless $expID == "control"
    #a.nc_mf
#  a.nc_sh_zonal_anm  unless $expID == "control"
#  a.nc_ml
#  a.nc_ml_anm         unless $expID == "control"
  
sstid = ["control5n","3keq","3kw1","control"]
  sstid.each{ |item|
    $expID = item
    $rezol = "UKMO_n96"
    set_directry
#    a.nc_sh_zonal_anm  unless $expID == "control"
#    a.nc_ml
#    a.nc_ml_anm         unless $expID == "control"
  }


sstid = ["control","peaked","flat","qobs","1keq"]
  sstid.each{ |item|
    $expID = item
    $rezol = "UKMO_n48"
    set_directry
#    a.nc_sh_zonal_anm  unless $expID == "control"
#    a.nc_ml
#    a.nc_ml_anm         unless $expID == "control"
  }

$groupid = "CCAM-05a"
$expID = "control"
$rezol = "CSIRO"
set_directry
#  a.nc_sh_zonal_anm  unless $expID == "control"
#  a.nc_ml
#  a.nc_ml_anm         unless $expID == "control"


sstid = ["control","peaked","flat","qobs","control5n","1keq","3keq","3kw1"]
  sstid.each{ |item|
    $expID = item
    $rezol = "LASG"
    set_directry
#    a.nc_sh_zonal_anm  unless $expID == "control"
#    a.nc_ml
#    a.nc_ml_anm         unless $expID == "control"
  }


$rezol = "K1JAPAN"
$expID = "control"
set_directry

sstid = ["control","flat","qobs","control5n","1keq","3keq","3kw1","peaked"]
  sstid.each{ |item|

    $rezol = "AGUforAPE"
    $expID = item
    set_directry
#    a.nc_gt_comparing_rezol
#    a.nc_sh_zonal_comparing_rezol
  }


rezol_ary = ["UKMO_n96","GSFC", "LASG", "NCAR", "K1JAPAN", "UKMO_n48"]
  rezol_ary.each{ |rezol| 

    $rezol = rezol
    $expID = "3keq"
    set_directry
#    a.nc_ml_anm
#    a.nc_ml
#    a.nc_sh_phiuv850_anm

    $rezol = rezol
    $expID = "1keq"
    set_directry
#    a.nc_ml_anm
#    a.nc_sh_phiuv850_anm

    $rezol = rezol
    $expID = "3kw1"
    set_directry
#    a.nc_ml_anm
#    a.nc_sh_phiuv850_anm

#    a.nc_gt_comparing_expid
#    a.nc_sh_zonal_comparing_expid
  }

rezol_ary = ["UKMO_n48"]
  rezol_ary.each{ |rezol| 

    $rezol = rezol
    $expID = "1keq"
    set_directry
#    a.nc_sh_phiuv850_anm

  }


#a.nc_ml_tuom_anm
#a.nc_sh_psuv_anm
#a.nc_sh_tuv500_anm
#a.nc_sh_phiuv250_anm
#a.nc_sh_phiuv850_anm


$rezol = "K1JAPAN"
$expID = "control"
set_directry

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


sstid.each{ |sst|

    $expID = sst
    $rezol = "UKMO_n96"
#    $rezol = "UKMO_n48"
    set_directry
    $groupid = "ukmo_03a"

#    a.nc_sh_zonal_anm  unless $expID == "control"

=begin
    a.nc_tr
    a.nc_ml
    a.nc_ml_anm         unless $expID == "control"
    a.nc_sh_anm 
    a.nc_gt
    a.nc_sh_zonal
    a.nc_sh_zonal_anm  unless $expID == "control"
    a.nc_sh
#    a.nc_gt_anm 
    #a.nc_mf_anm         unless $expID == "control"
    #a.nc_mf
    $groupid = "ukmo_03b"
#    a.nc_tr
    $groupid = "ukmo_03c"
#    a.nc_tr
    $groupid = "ukmo_03d"
#    a.nc_tr
    $groupid = "ukmo_03e"
#    a.nc_tr
#    $groupid = "ukmo_03f"
#    a.nc_tr
=end

    if $expID == "1keq" || $expID == "3keq" || $expID == "3kw1"
      #a.nc_sh_psuv_anm
      #  a.nc_ml_tuom_anm
      #a.nc_sh_tuv500_anm
      #a.nc_sh_phiuv250_anm
      #a.nc_sh_phiuv850_anm
    end
    #  a.nc_ml_tuom_anm
    #a.nc_sh_tuv500_anm
    #a.nc_sh_psuv_anm
    #a.nc_sh_phiuv250_anm
    #a.nc_sh_phiuv850_anm

    #    a.nc_sh_zonal_comparing_rezol
    #    a.nc_gt_comparing_rezol

  }


$groupid = "NSIPP-01"
$expID = "control"
$rezol = "GSFC"
set_directry

#sstid = ["control"]

#sstid = ["controln48"]

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

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

#sstid = ["control","flat","control5n","1keq","3keq","3kw1"]

sstid.each{ |sst|

$expID = sst

set_directry
#a.nc_gt_anm 

if $expID == "control"  ||  $expID == "controln48" 
else
#  a.nc_sh_anm   
end

#a.nc_tr
#a.nc_sh_anm 
#a.nc_sh_zonal
#a.nc_sh
#a.nc_ml
#a.nc_gt

#a.nc_ml_anm         unless $expID == "control"
#a.nc_mf_anm         unless $expID == "control"
#a.nc_mf

if $expID == "1keq" || $expID == "3keq" || $expID == "3kw1"
#a.nc_sh_psuv_anm
#  a.nc_ml_tuom_anm
#a.nc_sh_tuv500_anm
#a.nc_sh_phiuv250_anm
#a.nc_sh_phiuv850_anm
end
#  a.nc_ml_tuom_anm
#a.nc_sh_tuv500_anm
#a.nc_sh_psuv_anm
#a.nc_sh_phiuv250_anm
#a.nc_sh_phiuv850_anm


}

}

=begin

$groupid = "K1JAPAN-03a"
$expID = "control"
$rezol = "K1JAPAN"
set_directry

$groupid = "NSIPP-01"
$expID = "control"
$rezol = "GSFC"
set_directry

$groupid = "NCAR-03a"
$expID = "control"
$rezol = "NCAR"
set_directry

$groupid = "MGO-02"
$expID = "control"
$rezol = "MGO"
set_directry

$groupid = "NICAM-04"
$expID = "control"
$rezol = "FRCGC"
set_directry

$groupid = "ukmo_03a"
$expID = "controln48"
$rezol = "UKMO"
set_directry

$groupid = "ukmo_03a"
$expID = "control"
$rezol = "UKMO_n48"
set_directry


# --

$groupid = "CCAM-05a"
$expID = "control"
$rezol = "CSIRO"
set_directry


$groupid = "LASG-03a"
$expID = "control"
$rezol = "LASG"
set_directry



def hosakasst
a = ape_mkfig_new(3)
sstid = ["control","peaked","flat","qobs","control5n",
  "1keq","3keq","3kw1"]
  sstid.each{ |sst|

    $expID = sst
    
    set_directry
    
    if $expID == "1keq" || $expID == "3keq" || $expID == "3kw1"
      #a.nc_ml_tuom_anm
      #a.nc_sh_tuv500_anm
      #a.nc_sh_psuv_anm
      #a.nc_sh_phiuv250_anm
      a.nc_sh_phiuv850_anm
    end
  }
end

=end

class Ape_mkfig
  def nc_gt_comparing_expid

    if $rezol == "UKMO_n48" 
      id = ["control","peaked","flat","qobs","1keq"]
    elsif $rezol == "UKMO_n96" 
      id = ["control","control5n","3keq","3kw1"]
    else
      id = ["control","peaked","flat","qobs","control5n","1keq","3keq","3kw1"]
    end

    $file_label = "#{$rezol}"
    @data = Ape.new("#{$ncfile_path}#{$groupid}_GT_control.nc")
    @data.netcdf_open.var_names.each { |item| 
      gphys_ary = []
      unless item == "time"  || item == "gt_slwu"  || item == "lon" || item == "lat" || item == "vertical" || item == "site"  || item == "time_bnds" || item == "bounds_time" || item == "latitude" || item == "longitude" || item == "bounds_latitude" || item == "bounds_longitude" || item == "longitude_bnds"|| item == "latitude_bnds" || item =~ /vertical/


	id.each{ |expID|
	  $expID = expID
	  @data = Ape.new("#{$ncfile_path}#{$groupid}_GT_#{$expID}.nc")

	  if $rezol == "GSFC" || $rezol == "K1JAPAN" || $rezol == "MGO" || $rezol == "NCAR" || $rezol =~ /UKMO/ || $rezol == "CGAM" || $rezol == "DWD" || $rezol == "ECMWF" || $rezol == "GFDL" || $rezol == "LASG" || $rezol == "MIT"|| $rezol == "MRI"
	    if item == "gt_sw_toar" || item == "gt_sswr" || item == "gt_slwd" 
	      gphys = @data.gphys_open(item)*(-1)
	    else
	      gphys = @data.gphys_open(item)
	    end
	  else
	    gphys = @data.gphys_open(item)
	  end
	  gphys = gphys[0,0,true] if $rezol == "K1JAPAN" || $rezol == "MGO"
	  gphys = gphys[0,true] 	if $rezol == "UKMO_n96"

	  if $rezol == "UKMO_n48" && ($expID == "flat" ||$expID == "qobs" ||$expID == "1keq" ||$expID == "peaked")
	    gphys = gphys[0,true] 
	  end

	  if $rezol == "MGO" then 
	    grid_0 =
	      Axis.new().
	      set_pos(VArray.new(NArray.sfloat(1080).indgen!+180).rename("time").
		      put_att("units","days since 0000-01-01"))
	    grid = Grid.new(grid_0)
	    gphys = GPhys.new(grid, gphys.data )
	  end

	  if $rezol == "MGO"
	    gphys = gphys.set_att("ape_name", "#{gphys.data.get_att("long_name")}")
	  end
	  
	  if $rezol =~ /UKMO/
	    gphys = gphys.set_att("ape_name", "#{gphys.data.get_att("long_name")}")
	  end

	  gphys_ary.push(gphys.set_att("line_name",$expID) )
	}

	mkfig_plot(gphys_ary)
      end
    }

    # total_precipitation_flux
    gphys_ary = []
    id.each{ |expID|
      $expID = expID
      @data = Ape.new("#{$ncfile_path}#{$groupid}_GT_#{$expID}.nc")
      g1 = @data.gphys_open("gt_cppn")
      g2 = @data.gphys_open("gt_dppn")
      gphys = (g1 + g2).rename("gt_tppn").
	set_att("ape_name", "total_precipitation_flux")
      gphys = gphys[0,0,true] if $rezol == "K1JAPAN" || $rezol == "MGO"
      
      if $rezol == "UKMO_n48" && ($expID == "flat" ||$expID == "qobs" ||$expID == "1keq" ||$expID == "peaked")
	gphys = gphys[0,true] 
      end
      gphys = gphys[0,true] 	if $rezol == "UKMO_n96"
      
      if $rezol == "MGO" then 
	grid_0 =
	  Axis.new().
	  set_pos(VArray.new(NArray.sfloat(1080).indgen!+180).rename("time").
		  put_att("units","days since 0000-01-01"))
	grid = Grid.new(grid_0)
	gphys = GPhys.new(grid, gphys.data )
      end
      gphys_ary.push(gphys.
		     set_att("line_name",$expID) )
    }
    mkfig_plot(gphys_ary)
  end
 
  #  #{$groupid}_SH_control.nc@zonal_mean
  def nc_sh_zonal_comparing_expid

    if $rezol == "UKMO_n48" 
      id = ["control","peaked","flat","qobs","1keq"]
    elsif $rezol == "UKMO_n96" 
      id = ["control","control5n","3keq","3kw1"]
    elsif $rezol == "K1JAPAN"
      id = ["control","flat","control5n","1keq","3keq","3kw1"]
    else
      id = ["control","peaked","flat","qobs","control5n","1keq","3keq","3kw1"]
    end

    $file_label = "#{$rezol}"
    @data = Ape.new("#{$ncfile_path}#{$groupid}_SH_control.nc")
    @data.netcdf_open.var_names.each { |item| 
      gphys_ary = []
      unless item == "lon" || item == "lat"  || item == "time"  || item == "nv" || item == "bnd" || item == "wt_lat" || item == "wt_lon" || item == "lon_bnds" || item == "lat_bnds" || item == "wt_lat" || item == "wt_lat_u" || item == "lat_u"   || item == "lon_u"  || item == "lat_u_bnds"   || item == "lon_u_bnds" || item == "bounds_lat" || item == "bounds_lon"  || item == "longitude"  || item == "latitude"  || item == "gw" || item == "time_bnds" || item == "timebnd" || item == "lonbnd" || item == "latbnd"  || item == "latitude_73"  || item == "latitude_72" || item == "latitude_73_bnds" || item == "latitude_72_bnds"    || item == "bounds_time"  || item == "bounds_latitude" || item == "bounds_longitude"  || item == "bounds_latitude_73"  || item == "bounds_latitude_72" || item == "bounds_latitude_96" || item == "latitude_96" || item == "bounds_longitude_96" || item == "longitude_96" || item == "latitude_144" || item == "bounds_latitude_144" || item == "longitude_192" || item == "bounds_longitude_192"  || item == "longitude_bnds"|| item == "latitude_bnds"     
	puts item
	id.each{ |expID|
	  $expID = expID
	  @data = Ape.new("#{$ncfile_path}#{$groupid}_SH_#{$expID}.nc")

	  if $rezol == "GSFC" || $rezol == "K1JAPAN" || $rezol == "MGO" || $rezol == "NCAR" || $rezol == "UKMO_n48" || $rezol == "UKMO_n96" || $rezol == "CGAM"|| $rezol == "DWD" || $rezol == "ECMWF" || $rezol == "GFDL"  || $rezol == "LASG" || $rezol == "MIT"|| $rezol == "MRI"
	    if item == "sh_sw_toar" || item == "sh_sswr" || item == "sh_slwd" 
	      gphys = @data.gphys_open(item)*(-1)
	    else
	      gphys = @data.gphys_open(item)
	    end
	  else
	    gphys = @data.gphys_open(item)
	  end

	  if $rezol == "CSIRO" || $rezol == "GSFC" || $rezol == "LASG" || $rezol == "NCAR" || $rezol == "UKMO_n48" || $rezol == "UKMO_n96" || $rezol == "CGAM" || $rezol == "CSIRO_standard" || $rezol == "CSIRO_old" || $rezol == "DWD" || $rezol == "ECMWF" || $rezol == "GFDL" || $rezol == "MIT"|| $rezol == "MRI"
            if item == "sh_tauv" || item == "sh_tauu"
	      gphys = -gphys
            end
          end

	  gphys = gphys[true,true,0] if $rezol == "MGO"
	  if $rezol == "UKMO_n48" && ($expID == "flat" ||$expID == "qobs" ||$expID == "1keq" ||$expID == "peaked")
	    gphys = gphys[true,true,0] 
	  end
	  gphys = gphys[true,true,0] 	if $rezol == "UKMO_n96"
          gphys = gphys[true,true,0] if  $rezol == "ECMWF"
	  gphys = gphys.mean(0)

	  if $rezol == "MGO"
	    gphys = gphys.set_att("ape_name", "#{gphys.data.get_att("long_name")}")
	  end
	  if $rezol =~ /UKMO/
	    gphys = gphys.set_att("ape_name", "#{gphys.data.get_att("long_name")}")
	  end

  	  gphys_ary.push(gphys.rename("#{gphys.name}_zonal").
			 set_att("line_name",$expID) )
	}
	mkfig_plot(gphys_ary)
      end
    }
    # total_precipitation_flux
    gphys_ary = []
    id.each{ |expID|
      $expID = expID
      @data = Ape.new("#{$ncfile_path}#{$groupid}_SH_#{$expID}.nc")
      g1 = @data.gphys_open("sh_cppn")
      g2 = @data.gphys_open("sh_dppn")
      gphys = (g1 + g2).rename("sh_tppn_zonal").
	set_att("ape_name", "total_precipitation_flux")
      gphys = gphys[true,true,0] if $rezol == "MGO"
      if $rezol == "UKMO_n48" && ($expID == "flat" ||$expID == "qobs" ||$expID == "1keq" ||$expID == "peaked")
	gphys = gphys[true,true,0] 
      end
      gphys = gphys[true,true,0] 	if $rezol == "UKMO_n96"
      gphys = gphys[true,true,0] 	if $rezol == "ECMWF"

      gphys_ary.push(gphys.mean(0).
		     set_att("line_name",$expID) )
    }
    mkfig_plot(gphys_ary)
  end

  #  #{$groupid}_SH_control.nc@zonal_mean
  def nc_sh_zonal_comparing_rezol(num)
    $file_label = "APE_#{$expID}"

      if $expID == "control"
        if num == 0
          rezol = ["AGUforAPE", "CGAM", "CSIRO_old", "CSIRO_standard", "DWD", "ECMWF"]
        elsif num == 1 
        rezol = ["AGUforAPE", "FRCGC", "GFDL" , "GSFC", "K1JAPAN", "LASG"]
        elsif num == 2
          rezol = ["AGUforAPE", "MIT", "MRI", "NCAR", "UKMO_n48", "UKMO_n96"]
        elsif num == 3
          rezol = ["AGUforAPE", "AGCM5_kuo", "AGCM5_adj"]
        end

      elsif $expID == "flat" || $expID == "peaked" || $expID == "qobs" || $expID == "1keq" 
        if num == 0
          rezol = ["AGUforAPE", "CGAM", "CSIRO_old", "CSIRO_standard", "DWD", "ECMWF"]
        elsif num == 1 
        rezol = ["AGUforAPE", "GFDL" , "GSFC", "K1JAPAN", "LASG"]
        elsif num == 2
          rezol = ["AGUforAPE", "MIT", "MRI", "NCAR", "UKMO_n48"]
        elsif num == 3
          rezol = ["AGUforAPE", "AGCM5_kuo", "AGCM5_adj"]
        end
      elsif $expID == "control5n" || $expID == "3keq" || $expID == "3kw1" 
        if num == 0
          rezol = ["AGUforAPE", "CGAM", "CSIRO_old", "CSIRO_standard", "DWD", "ECMWF"]
        elsif num == 1 
        rezol = ["AGUforAPE", "GFDL" , "GSFC", "K1JAPAN", "LASG"]
        elsif num == 2
          rezol = ["AGUforAPE", "MIT", "MRI", "NCAR", "UKMO_n96"]
        elsif num == 3
          rezol = ["AGUforAPE", "AGCM5_kuo", "AGCM5_adj"]
        end
      end

    $ncfile_path = "/home/yukiko/work/ape/NetCDF/other_group/#{rezol[0].downcase}/"
    $groupid = $groupid_hash[rezol[0]]

    @data = Ape.new("#{$ncfile_path}#{$groupid}_SH_control.nc")
    @data.netcdf_open.var_names.each { |item| 
      gphys_ary = []
      unless item == "lon" || item == "lat"  || item == "sh_cldw" || item == "sh_cld_frac"

	rezol.each{ |rezol_item|
	  $rezol = rezol_item
	  $ncfile_path = "/home/yukiko/work/ape/NetCDF/other_group/#{$rezol.downcase}/"
	  $groupid = $groupid_hash[$rezol]
	  @data = Ape.new("#{$ncfile_path}#{$groupid}_SH_#{$expID}.nc")

	  if ( (item == "sh_dppn" || item == "sh_cppn" ) && $rezol == "FRCGC" ) || ( (item == "sh_sswr" || item == "sh_slwu" ) && $rezol =~ /UKMO/)
	  else
	    
            if $rezol == "GSFC" || $rezol == "K1JAPAN" || $rezol == "MGO" || $rezol == "NCAR" || $rezol == "UKMO_n48" || $rezol == "UKMO_n96" || $rezol == "CGAM"|| $rezol == "DWD" || $rezol == "ECMWF" || $rezol == "GFDL"  || $rezol == "LASG" || $rezol == "MIT"|| $rezol == "MRI"
	      if item == "sh_sw_toar" || item == "sh_sswr" || item == "sh_slwd" 
		gphys = @data.gphys_open(item)*(-1)
	      else
		gphys = @data.gphys_open(item)
	      end
	    else
	      gphys = @data.gphys_open(item)
	    end
	    
	    gphys = gphys[true,true,0] if $rezol == "MGO"
	    if $rezol == "UKMO_n48" && ($expID == "flat" ||$expID == "qobs" ||$expID == "1keq" ||$expID == "peaked")
	      gphys = gphys[true,true,0] 
	    end
	    gphys = gphys[true,true,0] 	if $rezol == "UKMO_n96"
            gphys = gphys[true,true,0] if  $rezol == "ECMWF"
	    gphys = gphys.mean(0)

	    if $rezol == "MGO"
	      gphys = gphys.set_att("ape_name", "#{gphys.data.get_att("long_name")}")
	    end
	    if $rezol =~ /UKMO/
	      gphys = gphys.set_att("ape_name", "#{gphys.data.get_att("long_name")}")
	    end

	  if $rezol == "CSIRO" || $rezol == "GSFC" || $rezol == "LASG" || $rezol == "NCAR" || $rezol == "UKMO_n48" || $rezol == "UKMO_n96" || $rezol == "CGAM" || $rezol == "CSIRO_standard" || $rezol == "CSIRO_old" || $rezol == "DWD" || $rezol == "ECMWF" || $rezol == "GFDL" || $rezol == "MIT"|| $rezol == "MRI"
	      if item == "sh_tauv" || item == "sh_tauu"
		gphys = -gphys
	      end
	    end

	    gphys_ary.push(gphys.rename("#{gphys.name}_zonal").
			   set_att("line_name",$rezol) )
	    puts $rezol
	  end
	}
	mkfig_plot(gphys_ary)
        `mv #{$fig_path}#{$file_label}_#{gphys_ary[0].name}.gif #{$fig_path}#{$file_label}_#{gphys_ary[0].name}_#{num}.gif`
      end
    }
    # total_precipitation_flux
    gphys_ary = []
    rezol.each{ |rezol_item|
      $rezol = rezol_item

      $ncfile_path = "/home/yukiko/work/ape/NetCDF/other_group/#{$rezol.downcase}/"
      $groupid = $groupid_hash[$rezol]
      @data = Ape.new("#{$ncfile_path}#{$groupid}_SH_#{$expID}.nc")

      if $rezol == "FRCGC"
	gphys = @data.gphys_open("sh_tppn").rename("sh_tppn_zonal").
	  set_att("ape_name", "total_precipitation_flux")
      else
	g1 = @data.gphys_open("sh_cppn")
	g2 = @data.gphys_open("sh_dppn")
	gphys = (g1 + g2).rename("sh_tppn_zonal").
	  set_att("ape_name", "total_precipitation_flux")
      end
      gphys = gphys[true,true,0] if $rezol == "MGO"
      if $rezol == "UKMO_n48" && ($expID == "flat" ||$expID == "qobs" ||$expID == "1keq" ||$expID == "peaked")
	gphys = gphys[true,true,0] 
      end
      gphys = gphys[true,true,0] 	if $rezol == "UKMO_n96"
      gphys = gphys[true,true,0] 	if $rezol == "ECMWF"
      gphys_ary.push(gphys.mean(0).
		     set_att("line_name",$rezol) )
    }
    mkfig_plot(gphys_ary)
    `mv #{$fig_path}#{$file_label}_#{gphys_ary[0].name}.gif #{$fig_path}#{$file_label}_#{gphys_ary[0].name}_#{num}.gif`
  end

  # #{$groupid}_GT_control.nc
  def nc_gt_comparing_rezol(num)
    $file_label = "APE_#{$expID}"

      if $expID == "control"
        if num == 0
          rezol = ["AGUforAPE", "CGAM", "CSIRO_old", "CSIRO_standard", "DWD", "ECMWF"]
        elsif num == 1 
        rezol = ["AGUforAPE", "FRCGC", "GFDL" , "GSFC", "K1JAPAN", "LASG"]
        elsif num == 2
          rezol = ["AGUforAPE", "MIT", "MRI", "NCAR", "UKMO_n48", "UKMO_n96"]
        elsif num == 3
          rezol = ["AGUforAPE", "AGCM5_kuo", "AGCM5_adj"]
        end
      elsif $expID == "flat" || $expID == "peaked" || $expID == "qobs" || $expID == "1keq" 
        if num == 0
          rezol = ["AGUforAPE", "CGAM", "CSIRO_old", "CSIRO_standard", "DWD", "ECMWF"]
        elsif num == 1 
        rezol = ["AGUforAPE", "GFDL" , "GSFC", "K1JAPAN", "LASG"]
        elsif num == 2
          rezol = ["AGUforAPE", "MIT", "MRI", "NCAR", "UKMO_n48"]
        elsif num == 3
          rezol = ["AGUforAPE", "AGCM5_kuo", "AGCM5_adj"]
        end
      elsif $expID == "control5n" || $expID == "3keq" || $expID == "3kw1" 
        if num == 0
          rezol = ["AGUforAPE", "CGAM", "CSIRO_old", "CSIRO_standard", "DWD", "ECMWF"]
        elsif num == 1 
        rezol = ["AGUforAPE", "GFDL" , "GSFC", "K1JAPAN", "LASG"]
        elsif num == 2
          rezol = ["AGUforAPE", "MIT", "MRI", "NCAR", "UKMO_n96"]
        elsif num == 3
          rezol = ["AGUforAPE", "AGCM5_kuo", "AGCM5_adj"]
        end
      end

    $ncfile_path = "/home/yukiko/work/ape/NetCDF/other_group/#{rezol[0].downcase}/"
    $groupid = $groupid_hash[rezol[0]]
    @data = Ape.new("#{$ncfile_path}#{$groupid}_GT_control.nc")
    @data.netcdf_open.var_names.each { |item| 
      gphys_ary = []
      unless item == "time"  || item == "gt_slwu" || item == "gt_cldw"  || item == "lon" || item == "lat" || item == "time_bnds" || item == "gt_cld_frac"
	rezol.each{ |rezol_item|
	  
	  $rezol = rezol_item
	  $ncfile_path = "/home/yukiko/work/ape/NetCDF/other_group/#{$rezol.downcase}/"
	  $groupid = $groupid_hash[$rezol]
          p "#{$ncfile_path}#{$groupid}_GT_control.nc"
          p item
	  @data = Ape.new("#{$ncfile_path}#{$groupid}_GT_#{$expID}.nc")

	  if (item == "gt_sswr" || item == "gt_sswi" ) && $rezol =~ /UKMO/
	  elsif ( item == "gt_cppn" ||  item == "gt_dppn") && $rezol == "FRCGC"
	  else
	    
	  if $rezol == "GSFC" || $rezol == "K1JAPAN" || $rezol == "MGO" || $rezol == "NCAR" || $rezol =~ /UKMO/ || $rezol == "CGAM" || $rezol == "DWD" || $rezol == "ECMWF" || $rezol == "GFDL" || $rezol == "LASG" || $rezol == "MIT"|| $rezol == "MRI"
	      if item == "gt_sw_toar" || item == "gt_sswr" || item == "gt_slwd" 
		gphys = @data.gphys_open(item)*(-1)
	      else
		gphys = @data.gphys_open(item)
	      end
	    else
	      gphys = @data.gphys_open(item)
	    end
	    
	    gphys = gphys[0,0,true] if $rezol == "K1JAPAN" || $rezol == "MGO"
	    gphys = gphys[0,true] 	if $rezol == "UKMO_n96"
	    
	    if $rezol == "UKMO_n48" && ($expID == "flat" ||$expID == "qobs" ||$expID == "1keq" ||$expID == "peaked")
	      gphys = gphys[0,true] 
	    end

            grid_0 =
              Axis.new().
	      set_pos(VArray.new(NArray.sfloat(gphys.val.length).indgen!+180).
			rename("time").
			put_att("units","days since 0000-01-01"))
	    grid = Grid.new(grid_0)
	    gphys = GPhys.new(grid, gphys.data )
	    gphys_ary.push(gphys.set_att("line_name",$rezol) )

		puts $rezol
	  end
	}

	mkfig_plot(gphys_ary)
        `mv #{$fig_path}#{$file_label}_#{gphys_ary[0].name}.gif #{$fig_path}#{$file_label}_#{gphys_ary[0].name}_#{num}.gif`
      end
    }

    # total_precipitation_flux
    gphys_ary = []
    rezol.each{ |rezol_item|
      $rezol = rezol_item
      $ncfile_path = "/home/yukiko/work/ape/NetCDF/other_group/#{$rezol.downcase}/"
      $groupid = $groupid_hash[$rezol]
      @data = Ape.new("#{$ncfile_path}#{$groupid}_GT_#{$expID}.nc")

      if $rezol == "FRCGC"
	gphys = @data.gphys_open("gt_tppn").rename("gt_tppn_zonal").
	  set_att("ape_name", "total_precipitation_flux")
      else
	g1 = @data.gphys_open("gt_cppn")
	g2 = @data.gphys_open("gt_dppn")
	gphys = (g1 + g2).rename("gt_tppn").
	  set_att("ape_name", "total_precipitation_flux")
	gphys = gphys[0,0,true] if $rezol == "K1JAPAN" || $rezol == "MGO"      
	if $rezol == "UKMO_n48" && ($expID == "flat" ||$expID == "qobs" ||$expID == "1keq" ||$expID == "peaked")
	  gphys = gphys[0,true] 
	end
	gphys = gphys[0,true] 	if $rezol == "UKMO_n96"
      end
      grid_0 =
	Axis.new().
	set_pos(VArray.new(NArray.sfloat(gphys.val.length).indgen!+180).
      rename("time").
      put_att("units","days since 0000-01-01"))
      grid = Grid.new(grid_0)
      gphys = GPhys.new(grid, gphys.data )
      gphys_ary.push(gphys.
		     set_att("line_name",$rezol) )
    }
    mkfig_plot(gphys_ary)
    `mv #{$fig_path}#{$file_label}_#{gphys_ary[0].name}.gif #{$fig_path}#{$file_label}_#{gphys_ary[0].name}_#{num}.gif`
  end

  #  #{$groupid}_SH_control.nc@zonal_mean
  def nc_ml_zonal_comparing_rezol
    $file_label = "#{$cumulus}_#{$expID}"
    rezol = ["T39L48_#{$cumulus}", "T39L24_#{$cumulus}","T39L96_#{$cumulus}", "T79L48_#{$cumulus}", "T159L48_#{$cumulus}"]

    # U の赤道鉛直断面
    gphys_ary = []
    rezol.each{ |rezol_item|
      $rezol = rezol_item
#      $ncfile_path = "/home/yukiko/tmp/ape-data/NetCDF/#{$rezol}/"
      $ncfile_path = "/home/yukiko/work/ape/NetCDF/#{$rezol}/"
      puts $ncfile_path
      @data = Ape.new("#{$ncfile_path}#{$groupid}_ML_#{$expID}.nc")
      gphys = @data.gphys_open("ml_u").cut(true,0,true).mean(0)
      gphys_ary.push(gphys.rename("#{gphys.name}_lat0_zonal").
		     set_att("line_name",$rezol) )
    }
    mkfig_plot(gphys_ary)
  end


  #  #{$groupid}_SH_control.nc@zonal_mean
  def nc_sh_anm_comparing_rezol

    rezol = ["T39L48_#{$cumulus}", "T39L24_#{$cumulus}","T39L96_#{$cumulus}", "T79L48_#{$cumulus}", "T159L48_#{$cumulus}"]

    expID_con_hash =  {
      "3keq" => "control", 
      "flat3keq" => "flat",
      "Qobs3keq" => "Qobs",
      "H1998pa" => "H1998con"
    }
    $file_label = "#{$cumulus}_#{$expID}"
    zonalcont = expID_con_hash[$expID]
    lost_axis = ["(diff) from (mean) zonal of #{zonalcont}"]

    gphys_ary_tppn = []
    gphys_ary_slh = []

    rezol.each{ |rezol_item|
      $rezol = rezol_item
      $ncfile_path = "/home/yukiko/work/ape/NetCDF/#{$rezol}/"
      @data_con = Ape.new("#{$ncfile_path}#{$groupid}_SH_#{zonalcont}.nc")
      @data     = Ape.new("#{$ncfile_path}#{$groupid}_SH_#{$expID}.nc")

      # 赤道上 降水量 (tppn) 
      g1 = @data_con.gphys_open("sh_cppn")
      g2 = @data_con.gphys_open("sh_dppn")
      gphys_con = (g1 + g2).rename("sh_tppn_zonal")
      g1 = @data.gphys_open("sh_cppn")
      g2 = @data.gphys_open("sh_dppn")
      gphys= (g1 + g2)
      gphys = (gphys - gphys_con.mean(0)).lon_lotate
      gphys =  gphys.cut(true,0).rename("sh_tppn_lat0_anm"). 
	set_att("ape_name", "total_precipitation_flux"). 
	set_att("line_name","#{$rezol}").
	add_lost_axes(lost_axis) 

      gphys_ary_tppn.push(gphys)
      
      # 赤道上 蒸発量 (slh) 
      gphys = @data.gphys_open("sh_slh")
      gphys_con = @data_con.gphys_open("sh_slh")
      gphys = (gphys - gphys_con.mean(0)).lon_lotate
      gphys = gphys.cut(true,0).rename("sh_slh_lat0_anm").
	set_att("line_name","#{$rezol}").
	add_lost_axes(lost_axis) 
      gphys_ary_slh.push(gphys)

    }
    mkfig_plot(gphys_ary_tppn)
    mkfig_plot(gphys_ary_slh)

  end

end

def hosakasst
a = ape_mkfig_new(3)
sstid = ["control","peaked","flat","qobs","control5n",
  "1keq","3keq","3kw1"]
  sstid.each{ |sst|

    $expID = sst
    
    set_directry
    
    if $expID == "1keq" || $expID == "3keq" || $expID == "3kw1"
      #a.nc_ml_tuom_anm
      #a.nc_sh_tuv500_anm
      #a.nc_sh_psuv_anm
      #a.nc_sh_phiuv250_anm
      #a.nc_sh_phiuv850_anm
    end
  }
end




