#!/usr/bin/env ruby

=begin

表題: AGUforAPE GrADS to NetCDF

履歴: 2004/02/13 yukiko@ep.sci.hokudai.ac.jp


=end

END{

 $rezol = "T159L48_non"
# $rezol = "T79L48_non"
# $rezol = "T39L24_non"
 Ape_mknetcdf.new.mknetcdf


}


# ----------------------------------------------
# ps, gif ファイルの格納場所

id = ["control","flat","peaked","1keq","3keq","3kw1","Qobs","control-5N"]

# ----------------------------------------------
# local load path

$local_path = '/work11/ape/yukiko/lib'
$: << $local_path

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

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


# -----------------------------------------------
# Ape Grads -- NetCDF コンバータ

class Ape_mknetcdf

  def mknetcdf
  
#    grfile_path = "/work11/ape/GrADS/#{$rezol}_expID01/"

#    grfile_path = "/work11/ape/GrADS/T159L48_non_expID07/mean/"
    grfile_path = "/work13/yukiko/ape/GrADS/T159L48_non_expID07/mean/"
#    grfile_path = "/work13/yukiko/ape/GrADS/T319L48_non_expID01/mean/"
    gr2ncfile_path = "/work11/ape/yukiko/data/"

    pwd_path = Dir.pwd

    file = NetCDF.create("#{gr2ncfile_path}/AGUforAPE-03a_SH_3keq.nc")
#    file = NetCDF.create("#{gr2ncfile_path}/AGUforAPE-03a_SH_control.nc")



# grid =========================================================


    gphys = GPhys::GrADS_IO.open("#{grfile_path}/meanOSRD.ctl", "OSRD")

    g0 = VArray.new(gphys.grid_copy.axis(0).pos.val ).rename("lon")
    g0 = g0.set_att("standard_name","longitude")
    g0 = g0.set_att("units","degrees_east")
    g0 = Axis.new().set_pos(g0)

    g1 = VArray.new(gphys.grid_copy.axis(1).pos.val ).rename("lat")
    g1 = g1.set_att("standard_name","latitude")
    g1 = g1.set_att("units","degrees_north")
#    g1[-1..0] = g1[0..-1] # 緯度軸の反転
    g1 = Axis.new().set_pos(g1)

    grid = Grid.new(g0,g1)

# data =========================================================


    cell_methods = "time: mean (accumulation at every time-step comment: averaged over 3-year period following 6-month spin-up)"

    # sh_sw_toai
    ctlitem = "OSRD"
    ncitem  = "sh_sw_toai"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]
    gphys = gphys.rename(ncitem).
      set_att("ape_name","toa_incoming_shortwave_flux").
      set_att("units","W m-2"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # sh_sw_toar
    ctlitem = "OSRU"
    ncitem  = "sh_sw_toar"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]
    gphys = gphys.rename(ncitem).
      set_att("ape_name","toa_outgoing_shortwave_flux").
      set_att("units","W m-2"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # sh_lw_toa
    ctlitem = "OLR"
    ncitem  = "sh_lw_toa"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]
    gphys = gphys.rename(ncitem).
      set_att("ape_name","toa_outgoing_longwave_flux").
      set_att("units","W m-2"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # sh_cld_frac
    ctlitem = "CCOVER"
    ncitem  = "sh_cld_frac"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]
    gphys = gphys.rename(ncitem).
      set_att("ape_name","cloud_area_fraction").
      set_att("units","1"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # sh_cldw
    ctlitem = "VICLDW"
    ncitem  = "sh_cldw"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]
    gphys = gphys.rename(ncitem).
      set_att("ape_name","atmosphere_cloud_condensed_water_co").
      set_att("units","kg m-2"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # sh_cppn
    ctlitem = "PRCPC"
    ncitem  = "sh_cppn"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]
    gphys = gphys.rename(ncitem).
      set_att("ape_name","convective_precipitation_flux").
      set_att("units","kg m-2 s-1"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # sh_dppn
    ctlitem = "PRCPL"
    ncitem  = "sh_dppn"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]
    gphys = gphys.rename(ncitem).
      set_att("ape_name","large_scale_precipitation_flux").
      set_att("units","kg m-2 s-1"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # sh_sswi
    ctlitem = "SSRD"
    ncitem  = "sh_sswi"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]
    gphys = gphys.rename(ncitem).
      set_att("ape_name","surface_downwelling_shortwave_flux").
      set_att("units","W m-2"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # sh_sswr
    ctlitem = "SSRU"
    ncitem  = "sh_sswr"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]
    gphys = gphys.rename(ncitem).
      set_att("ape_name","surface_upwelling_shortwave_flux").
      set_att("units","W m-2"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # sh_slwd
    ctlitem = "SLRD"
    ncitem  = "sh_slwd"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]
    gphys = gphys.rename(ncitem).
      set_att("ape_name","surface_downwelling_longwave_flux").
      set_att("units","W m-2"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # sh_slwu
    ctlitem = "SLRU"
    ncitem  = "sh_slwu"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]
    gphys = gphys.rename(ncitem).
      set_att("ape_name","surface_upwelling_longwave_flux").
      set_att("units","W m-2"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # sh_slh
    ctlitem = "EVAP"
    ncitem  = "sh_slh"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]
    gphys = gphys.rename(ncitem).
      set_att("ape_name","surface_upward_latent_heat_flux").
      set_att("units","W m-2"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # sh_evap
    ctlitem = "EVAP"
    ncitem  = "sh_evap"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]*0.4*10**(-6)
    gphys = gphys.rename(ncitem).
      set_att("ape_name","evaporation_flux").
      set_att("units","kg m-2 s-1"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # sh_ssh
    ctlitem = "SENS"
    ncitem  = "sh_ssh"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]
    gphys = gphys.rename(ncitem).
      set_att("ape_name","surface_upward_sensible_heat_flux").
      set_att("units","W m-2"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # sh_tauu
    ctlitem = "TAUX"
    ncitem  = "sh_tauu"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]
    gphys = gphys.rename(ncitem).
      set_att("ape_name","surface_downward_eastward_stress").
      set_att("units","Pa"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # sh_tauv
    ctlitem = "TAUY"
    ncitem  = "sh_tauv"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]
    gphys = gphys.rename(ncitem).
      set_att("ape_name","surface_downward_northward_stress").
      set_att("units","Pa"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # sh_ps
    ctlitem = "PS"
    ncitem  = "sh_ps"
    print "GrADS to NetCDF [ #{ctlitem}.ctl -- #{ncitem} ] \n"
    @data = Ape.new("#{grfile_path}/mean#{ctlitem}.ctl")
    gphys = @data.gphys_open(ctlitem)[true,true,0,0]
    gphys = gphys.rename(ncitem).
      set_att("ape_name","surface_air_pressure").
      set_att("units","Pa"). 
      set_att("cell_methods", cell_methods)
    gphys = GPhys.new(grid,gphys.data)
    GPhys::NetCDF_IO.write(file, gphys)

    # 大域属性
    file.put_att("Conventions", "CF-1.0")
#    file.put_att("title",'Aqua Planet: Single-level 2-D Means from \'control\' Experiment')
    file.put_att("title",'Aqua Planet: Single-level 2-D Means from \'3keq\' Experiment')
    file.put_att("history", "Original data produced: 2005/02/05 on ES; time-mean calculated for APE SH standard output" )
    file.put_att("institution", "AGU for APE; AFES Working Team, GFD Dennou Club, University of Tokyo")
    file.put_att("source","AFES v1.15")
    file.close

  end

end

