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

 $local_path = '/work11/ape/yukiko/lib'
# $local_path = '/home/yukiko/tmp/ape-data/lib'
$: << $local_path

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

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

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

sstid = ["control"]
groupid = ["aguforape","agcm5_adj","agcm5_kuo"]
cumulusid = ["eml","ias","ksc","kuo","mca","non"]


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

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

set_dir_id  = groupid[0]

$cumulus = "non"
$rezol = "T39L48_#{$cumulus}"
$expID = "control"
set_dir(set_dir_id,host)


class Ape_mkfig
  # #{$groupid}_TR_control.nc

  def nc_tr_mono
    @data = Ape.new("#{$ncfile_path}#{$groupid}_TR_#{$expID}.nc")
    
    # x-t ダイヤグラム
    gphys = @data.gphys_open("tr_tppn")
    gphys = gphys.cut(true,0,true)[true,-401..-1]
    
    grid_0 = gphys.grid_copy.axis(0)
    grid_1 = Axis.new().set_pos(VArray.new(NArray.sfloat(401).indgen!/4).rename("time").put_att("units","days"))
    grid = Grid.new(grid_0,grid_1)
    gphys = GPhys.new(grid, gphys.data )
    mkfig_plot(gphys.lon_lotate.rename("tr_tppn_mono").set_lost_axes(""))
  end

  def nc_tr_closeup_T159_non
    
    puts "#{$ncfile_path}#{$groupid}_TR_#{$expID}_half2.nc"

    @data = Ape.new("#{$ncfile_path}#{$groupid}_TR_#{$expID}_half2.nc")        
    
    # x-t ダイヤグラム
    gphys = @data.gphys_open("tr_tppn")
    gphys = gphys.cut(true,0,true)[true,-401..-1]
    
    grid_0 = gphys.grid_copy.axis(0)
    grid_1 =
      Axis.new().
      set_pos(VArray.new(NArray.sfloat(401).indgen!/4).rename("time").
 put_att("units","days"))
        grid = Grid.new(grid_0,grid_1)
        gphys = GPhys.new(grid, gphys.data )

    gphys = gphys.lon_lotate

	mkfig_plot(gphys.rename("tr_tppn_mono").set_lost_axes(""))
#	mkfig_plot(gphys[0..239,-121..-1].rename("tr_tppn_30day_1_mono"))
	mkfig_plot(gphys[240..-1,-121..-1].rename("tr_tppn_30day_2_mono"))

  end


  def nc_tr_closeup_T319_non
    
    @data = Ape.new("#{$ncfile_path}#{$groupid}_TR_#{$expID}_0031.nc")
    
    # x-t ダイヤグラム
    gphys =   __tr_T319_marge("tr_tppn")
    gphys = gphys[true,-401..-1]

    grid_0 = gphys.grid_copy.axis(0)
    grid_1 =
      Axis.new().
      set_pos(VArray.new(NArray.sfloat(401).indgen!/4).rename("time").
 put_att("units","days"))
        grid = Grid.new(grid_0,grid_1)
        gphys = GPhys.new(grid, gphys.data )

    gphys = gphys.lon_lotate

#	mkfig_plot(gphys[0..479,-121..-1].rename("tr_tppn_30day_1_mono"))
	mkfig_plot(gphys.rename("tr_tppn_mono").set_lost_axes(""))
#	mkfig_plot(gphys[480..-1,-121..-1].rename("tr_tppn_30day_2_mono"))

  end
  
  def __tr_T319_marge(name)
    
    x_size = NArray.sfloat(960).indgen*360.0/960
    t_size = NArray.sfloat(1440).indgen*1.0/4.0 + 0.25
    
    # 軸
    x = VArray.new(x_size).rename("lon").put_att("units","degrees_east")
    t = VArray.new(t_size).rename("time").put_att("units","days since 0000-01-01")
    x = Axis.new().set_pos(x)
    t = Axis.new().set_pos(t)
    grid = Grid.new(x,t)
    
    data = NArray.sfloat(960,1440).indgen * 0.0
    
    file1 = Ape.new("#{$ncfile_path}#{$groupid}_TR_#{$expID}_0031.nc")
    file2 = Ape.new("#{$ncfile_path}#{$groupid}_TR_#{$expID}_0032.nc")
    file3 = Ape.new("#{$ncfile_path}#{$groupid}_TR_#{$expID}_0033.nc")
    file4 = Ape.new("#{$ncfile_path}#{$groupid}_TR_#{$expID}_0034.nc")
    file5 = Ape.new("#{$ncfile_path}#{$groupid}_TR_#{$expID}_0035.nc")    
    file6 = Ape.new("#{$ncfile_path}#{$groupid}_TR_#{$expID}_0036.nc")
    file7 = Ape.new("#{$ncfile_path}#{$groupid}_TR_#{$expID}_0037.nc")
    file8 = Ape.new("#{$ncfile_path}#{$groupid}_TR_#{$expID}_0038.nc")
    file9 = Ape.new("#{$ncfile_path}#{$groupid}_TR_#{$expID}_0039.nc")
    file10 = Ape.new("#{$ncfile_path}#{$groupid}_TR_#{$expID}_0040.nc")
    file11 = Ape.new("#{$ncfile_path}#{$groupid}_TR_#{$expID}_0041.nc")
    file12 = Ape.new("#{$ncfile_path}#{$groupid}_TR_#{$expID}_0042.nc")

    gphys = file1.gphys_open(name).cut(true,0,true)
    data[true,0..119] = gphys.data.val
    gphys = file2.gphys_open(name).cut(true,0,true)
    data[true,120..239] = gphys.data.val
    gphys = file3.gphys_open(name).cut(true,0,true)
    data[true,240..359] = gphys.data.val
    gphys = file4.gphys_open(name).cut(true,0,true)
    data[true,360..479] = gphys.data.val
    gphys = file5.gphys_open(name).cut(true,0,true)
    data[true,480..599] = gphys.data.val
    gphys = file6.gphys_open(name).cut(true,0,true)
    data[true,600..719] = gphys.data.val
    gphys = file7.gphys_open(name).cut(true,0,true)
    data[true,720..839] = gphys.data.val
    gphys = file8.gphys_open(name).cut(true,0,true)
    data[true,840..959] = gphys.data.val
    gphys = file9.gphys_open(name).cut(true,0,true)
    data[true,960..1079] = gphys.data.val
    gphys = file10.gphys_open(name).cut(true,0,true)
    data[true,1080..1199] = gphys.data.val
    gphys = file11.gphys_open(name).cut(true,0,true)
    data[true,1200..1319] = gphys.data.val
    gphys = file12.gphys_open(name).cut(true,0,true)
    data[true,1320..1439] = gphys.data.val
    
    data = VArray.new(data).rename("#{name}").
      put_att("units",gphys.data.get_att("units")).
      put_att("ape_name",gphys.data.get_att("ape_name"))
    data = GPhys.new( grid, data)

    return data
  end

  def nc_tr_wkplot_mono
    
    @data = Ape.new("/work11/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


$cumulus = "eml"
$rezol = "T39L48_#{$cumulus}"
$expID = "control"
set_dir(set_dir_id,host)

rezolid = ["T39L48_non", "T39L24_non","T39L96_non", "T79L48_non"]

rezolid.each { |item|
$cumulus = "eml"
$rezol = item
$expID = "control"
set_dir(set_dir_id,host)
#a.nc_sst_mono
a.nc_tr_mono
a.nc_tr_wkplot_mono


}

$rezol = "T159L48_non"
set_dir(set_dir_id,host)
a.nc_tr_closeup_T159_non
a.nc_tr_wkplot_mono

$rezol = "T319L48_non"
set_dir(set_dir_id,host)
a.nc_tr_closeup_T319_non


