#!/usr/bin/ruby
require "numru/ggraph"
include NumRu

dirtail = '-all'
dirtail = '-dwn1e1-all'
#dirtail = '-dwn1e1-sw'
#dirtail = '-dwn1e1-lw'
#dirtail = ''
dir = "./out" + dirtail

sym = 'Test_Earth_Tropics_CO2-300ppmv_O3-0'
#sym = 'LW_Case25_Earth_Tropics_CO2-300ppmv'
#sym = 'Test_Earth_Iso300K_H2O-1e-3_CO2-100ppmv_O3-0'
#sym = 'Test_Earth_Iso300K_H2O-1e-3_CO2-300ppmv_O3-0'
#sym = 'Test_Earth_Tropics_CO2-100ppmv_O3-0'
#sym = 'Test_Earth_Tropics_CO2-300ppmv_O3-0'

fn = "flux.nc"
fn = sym + "_Flux.nc"
path = dir + "/" + fn
vname = 'RadUwFlux'
gpupfl = GPhys::NetCDF_IO.open( path, vname )
vname = 'RadDwFlux'
gpdnfl = GPhys::NetCDF_IO.open( path, vname )
#
fn = "tendency.nc"
fn = sym + "_Tendency.nc"
path = dir + "/" + fn
vname = 'DTempDt'
gptend = GPhys::NetCDF_IO.open( path, vname )

dir = "../prog08_calc_rte_check_tbl-x2/out" + dirtail
#
fn = sym + "_Flux.nc"
path = dir + "/" + fn
vname = 'RadUwFlux'
gpupfl2 = GPhys::NetCDF_IO.open( path, vname )
vname = 'RadDwFlux'
gpdnfl2 = GPhys::NetCDF_IO.open( path, vname )
#
fn = sym + "_Tendency.nc"
path = dir + "/" + fn
vname = 'DTempDt'
gptend2 = GPhys::NetCDF_IO.open( path, vname )

dir = "../prog08_calc_rte_check_tbl-x4/out" + dirtail
#
fn = sym + "_Flux.nc"
path = dir + "/" + fn
vname = 'RadUwFlux'
gpupfl3 = GPhys::NetCDF_IO.open( path, vname )
vname = 'RadDwFlux'
gpdnfl3 = GPhys::NetCDF_IO.open( path, vname )
#
fn = sym + "_Tendency.nc"
path = dir + "/" + fn
vname = 'DTempDt'
gptend3 = GPhys::NetCDF_IO.open( path, vname )

dir = "../prog08_calc_rte_check_tbl-x8/out" + dirtail
#
fn = sym + "_Flux.nc"
path = dir + "/" + fn
vname = 'RadUwFlux'
gpupfl4 = GPhys::NetCDF_IO.open( path, vname )
vname = 'RadDwFlux'
gpdnfl4 = GPhys::NetCDF_IO.open( path, vname )
#
fn = sym + "_Tendency.nc"
path = dir + "/" + fn
vname = 'DTempDt'
gptend4 = GPhys::NetCDF_IO.open( path, vname )


dir = "../prog03_calc_rte/out" + dirtail
dir = "../prog04.0_sort_ac_check/out" + "-" + dirtail.split("-")[2]
#
fn = sym + "_Flux.nc"
fn = "Flux-00-10.nc"
path = dir + "/" + fn
vname = 'RadUwFlux'
ref_gpupfl = GPhys::NetCDF_IO.open( path, vname )
vname = 'RadDwFlux'
ref_gpdnfl = GPhys::NetCDF_IO.open( path, vname )
#
fn = sym + "_Tendency.nc"
fn = "Tendency-00-10.nc"
vname = 'DTempDt'
path = dir + "/" + fn
ref_gptend = GPhys::NetCDF_IO.open( path, vname )


print "1: Display,  2: File\n"
citr = gets
citr = citr.chomp!
DCL.gropn(citr.to_i)
#DCL.gropn(1)
#DCL.gropn(2)

DCL.sldiv('y',4,2)
DCL.sgpset('lcntl',false)
DCL.sgpset('lfull',true)
DCL.uzfact(1.5)
DCL.sgpset('lfprop',true)
DCL.sglset('lclip',true)

svx1 = 0.2; svx2 = 0.6; svy1 = 0.2; svy2 = 0.8

def graph_flux( svx1, svx2, svy1, svy2, gpupfl1, gpdnfl1, gpupfl2, gpdnfl2, gpupfl3, gpdnfl3, gpupfl4, gpdnfl4, gpupfl5, gpdnfl5, flaglog, flagdiff )

  gpupfl1out = gpupfl1.copy
  gpdnfl1out = gpdnfl1.copy
  gpupfl2out = gpupfl2.copy
  gpdnfl2out = gpdnfl2.copy
  gpupfl3out = gpupfl3.copy
  gpdnfl3out = gpdnfl3.copy
  gpupfl4out = gpupfl4.copy
  gpdnfl4out = gpdnfl4.copy
  gpupfl5out = gpupfl5.copy
  gpdnfl5out = gpdnfl5.copy

  if flagdiff then
    gpupfl5out = gpupfl5out - gpupfl1out
    gpdnfl5out = gpdnfl5out - gpdnfl1out
    gpupfl4out = gpupfl4out - gpupfl1out
    gpdnfl4out = gpdnfl4out - gpdnfl1out
    gpupfl3out = gpupfl3out - gpupfl1out
    gpdnfl3out = gpdnfl3out - gpdnfl1out
    gpupfl2out = gpupfl2out - gpupfl1out
    gpdnfl2out = gpdnfl2out - gpdnfl1out
    gpupfl1out = gpupfl1out - gpupfl1out
    gpdnfl1out = gpdnfl1out - gpdnfl1out
  end

  gpupfl1out.long_name = "flux"
  gpdnfl1out.long_name = "flux"

  x1 = 0; x2 = 500; y1 = 1.05e5; y2 = 0
  x1 = 0; x2 = 1500; y1 = 1.05e5; y2 = 0
  itr = 1
  if flaglog then
    y2 = 1e1
    itr = 2
  end
  if flagdiff then
    x1 = -5; x2 = 5
    x1 = -10; x2 = 10
    x1 = -12; x2 = 12
    gpupfl2out.long_name = "flux diff."
    gpdnfl2out.long_name = "flux diff."
  end

  GGraph.set_fig 'itr'=>itr, 'viewport'=>[svx1,svx2,svy1,svy2], 'window'=>[x1,x2,y1,y2]

  if !flagdiff then
    itype = 3
    gpout = gpupfl1out.cut('Press'=>y1..y2)
    GGraph.line( gpout, true , "exchange"=>true, "index"=>20, "type"=>itype, "title"=>"" )
    gpout = gpdnfl1out.cut('Press'=>y1..y2)
    GGraph.line( gpout, false, "exchange"=>true, "index"=>40, "type"=>itype, "title"=>"" )
  end

  if !flagdiff then
    flagfirst = false
  else
    flagfirst = true
  end
  itype = 1
  gpout = gpupfl2out.cut('Press'=>y1..y2)
  GGraph.line( gpout, flagfirst, "exchange"=>true, "index"=>20, "type"=>itype, "title"=>"" )
  gpout = gpdnfl2out.cut('Press'=>y1..y2)
  GGraph.line( gpout, false, "exchange"=>true, "index"=>40, "type"=>itype, "title"=>"" )

  itype = 2
  gpout = gpupfl3out.cut('Press'=>y1..y2)
  GGraph.line( gpout, false, "exchange"=>true, "index"=>20, "type"=>itype, "title"=>"" )
  gpout = gpdnfl3out.cut('Press'=>y1..y2)
  GGraph.line( gpout, false, "exchange"=>true, "index"=>40, "type"=>itype, "title"=>"" )

  itype = 4
  gpout = gpupfl4out.cut('Press'=>y1..y2)
  GGraph.line( gpout, false, "exchange"=>true, "index"=>20, "type"=>itype, "title"=>"" )
  gpout = gpdnfl4out.cut('Press'=>y1..y2)
  GGraph.line( gpout, false, "exchange"=>true, "index"=>40, "type"=>itype, "title"=>"" )

  itype = 5
  gpout = gpupfl5out.cut('Press'=>y1..y2)
  GGraph.line( gpout, false, "exchange"=>true, "index"=>20, "type"=>itype, "title"=>"" )
  gpout = gpdnfl5out.cut('Press'=>y1..y2)
  GGraph.line( gpout, false, "exchange"=>true, "index"=>40, "type"=>itype, "title"=>"" )

end

def graph_tendency( svx1, svx2, svy1, svy2, gp1, gp2, gp3, gp4, gp5, flaglog, flagdiff )

  gp1out = gp1.copy
  gp2out = gp2.copy
  gp3out = gp3.copy
  gp4out = gp4.copy
  gp5out = gp5.copy

  if flagdiff then
    gp5out = gp5out - gp1out
    gp4out = gp4out - gp1out
    gp3out = gp3out - gp1out
    gp2out = gp2out - gp1out
    gp1out = gp1out - gp1out
  end

  gp1out = gp1out * 86400.0
  gp2out = gp2out * 86400.0
  gp3out = gp3out * 86400.0
  gp4out = gp4out * 86400.0
  gp5out = gp5out * 86400.0

  gp1out.units = "K (day)-1"
  gp1out.long_name = "tendency"

  x1 = -5; x2 = 5; y1 = 1.05e5; y2 = 0
  x1 = -20; x2 = 20; y1 = 1.05e5; y2 = 0
  itr = 1
  if flaglog then
    y2 = 1e1
    itr = 2
  end
  if flagdiff then
    x1 = -0.5; x2 = 0.5
    x1 = -1  ; x2 = 1
    x1 = -1.2; x2 = 1.2
    x1 = -5  ; x2 = 5
    gp2out.units     = "K (day)-1"
    gp2out.long_name = "tendency diff."
  end

  GGraph.set_fig 'itr'=>itr, 'viewport'=>[svx1,svx2,svy1,svy2], 'window'=>[x1,x2,y1,y2]

  if !flagdiff then
    itype = 3
    GGraph.line( gp1out, true , "exchange"=>true, "index"=>1, "title"=>"", "type"=>itype )
  end

  if !flagdiff then
    flagfirst = false
  else
    flagfirst = true
  end
  itype = 1
  GGraph.line( gp2out, flagfirst, "exchange"=>true, "index"=>1, "title"=>"", "type"=>itype )
  itype = 2
  GGraph.line( gp3out, false, "exchange"=>true, "index"=>1, "title"=>"", "type"=>itype )
  itype = 4
  GGraph.line( gp4out, false, "exchange"=>true, "index"=>1, "title"=>"", "type"=>itype )
  itype = 5
  GGraph.line( gp5out, false, "exchange"=>true, "index"=>1, "title"=>"", "type"=>itype )

end


# flux linear
graph_flux( svx1, svx2, svy1, svy2, ref_gpupfl, ref_gpdnfl, gpupfl, gpdnfl, gpupfl2, gpdnfl2, gpupfl3, gpdnfl3, gpupfl4, gpdnfl4, false, false )

# flux diff. linear
graph_flux( svx1, svx2, svy1, svy2, ref_gpupfl, ref_gpdnfl, gpupfl, gpdnfl, gpupfl2, gpdnfl2, gpupfl3, gpdnfl3, gpupfl4, gpdnfl4, false, true )

# flux log
graph_flux( svx1, svx2, svy1, svy2, ref_gpupfl, ref_gpdnfl, gpupfl, gpdnfl, gpupfl2, gpdnfl2, gpupfl3, gpdnfl3, gpupfl4, gpdnfl4, true, false )

# flux diff. log
graph_flux( svx1, svx2, svy1, svy2, ref_gpupfl, ref_gpdnfl, gpupfl, gpdnfl, gpupfl2, gpdnfl2, gpupfl3, gpdnfl3, gpupfl4, gpdnfl4, true, true )


# tendency linear
graph_tendency( svx1, svx2, svy1, svy2, ref_gptend, gptend, gptend2, gptend3, gptend4, false, false )

# tendency diff. linear
graph_tendency( svx1, svx2, svy1, svy2, ref_gptend, gptend, gptend2, gptend3, gptend4, false, true )

# tendency log
graph_tendency( svx1, svx2, svy1, svy2, ref_gptend, gptend, gptend2, gptend3, gptend4, true, false )

# tendency diff. log
graph_tendency( svx1, svx2, svy1, svy2, ref_gptend, gptend, gptend2, gptend3, gptend4, true, true )

DCL.grcls
