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

dircore = "../prog03_calc_rte/out"
dirtail = "-all"
dirtail = "-lw"
dirtail = "-sw"
fncore = "LW_Case25_Earth_Tropics_CO2-300ppmv"

dir = dircore  + "-dwn1e2" + dirtail
fn = fncore + "_Flux.nc"
path = dir + "/" + fn
gpupfl_dwn1e2 = GPhys::NetCDF_IO.open( path, 'RadUwFlux' )
gpdnfl_dwn1e2 = GPhys::NetCDF_IO.open( path, 'RadDwFlux' )
fn = fncore + "_Tendency.nc"
path = dir + "/" + fn
gptend_dwn1e2 = GPhys::NetCDF_IO.open( path, 'DTempDt' )

dir = dircore  + "-dwn1e1" + dirtail
fn = fncore + "_Flux.nc"
path = dir + "/" + fn
gpupfl_dwn1e1 = GPhys::NetCDF_IO.open( path, 'RadUwFlux' )
gpdnfl_dwn1e1 = GPhys::NetCDF_IO.open( path, 'RadDwFlux' )
fn = fncore + "_Tendency.nc"
path = dir + "/" + fn
gptend_dwn1e1 = GPhys::NetCDF_IO.open( path, 'DTempDt' )

dir = dircore  + "-dwn1e0" + dirtail
fn = fncore + "_Flux.nc"
path = dir + "/" + fn
gpupfl_dwn1e0 = GPhys::NetCDF_IO.open( path, 'RadUwFlux' )
gpdnfl_dwn1e0 = GPhys::NetCDF_IO.open( path, 'RadDwFlux' )
fn = fncore + "_Tendency.nc"
path = dir + "/" + fn
gptend_dwn1e0 = GPhys::NetCDF_IO.open( path, 'DTempDt' )

dir = dircore  + "-dwn1e-1" + dirtail
fn = fncore + "_Flux.nc"
path = dir + "/" + fn
gpupfl_dwn1em1 = GPhys::NetCDF_IO.open( path, 'RadUwFlux' )
gpdnfl_dwn1em1 = GPhys::NetCDF_IO.open( path, 'RadDwFlux' )
fn = fncore + "_Tendency.nc"
path = dir + "/" + fn
gptend_dwn1em1 = GPhys::NetCDF_IO.open( path, 'DTempDt' )

gptend_dwn1e2 = gptend_dwn1e2 * 86400.0
gptend_dwn1e1 = gptend_dwn1e1 * 86400.0
gptend_dwn1e0 = gptend_dwn1e0 * 86400.0
gptend_dwn1em1 = gptend_dwn1em1 * 86400.0
gptend_dwn1e2.units = "K day-1"
gptend_dwn1e1.units = "K day-1"
gptend_dwn1e0.units = "K day-1"
gptend_dwn1em1.units = "K day-1"

#gpupfl_dwn1e2.long_name = ""
#gpupfl_dwn1e1.long_name = ""
#gpupfl_dwn1e0.long_name = ""
#gpdnfl_dwn1e2.long_name = ""
#gpdnfl_dwn1e1.long_name = ""
#gpdnfl_dwn1e0.long_name = ""


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.15; svx2 = 0.6; svy1 = 0.2; svy2 = 0.8

def graph_flux( svx1, svx2, svy1, svy2, gpupfl_dwn1e2, gpupfl_dwn1e1, gpupfl_dwn1e0, gpupfl_dwn1em1, gpdnfl_dwn1e2, gpdnfl_dwn1e1, gpdnfl_dwn1e0, gpdnfl_dwn1em1, flaglog, flagdiff )

  longname = "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
    longname = "flux diff."
  end

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

  gpout = gpupfl_dwn1e2.copy
  gpout = gpout.cut('Press'=>y1..y2)
  gpout.long_name = longname
  GGraph.line( gpout, true , "exchange"=>true, "title"=>"", "index"=>20, "type"=>1, "title"=>"" )
  gpout = gpupfl_dwn1e1.copy
  gpout = gpout.cut('Press'=>y1..y2)
  GGraph.line( gpout, false , "exchange"=>true, "title"=>"", "index"=>20, "type"=>2, "title"=>"" )
  gpout = gpupfl_dwn1e0.copy
  gpout = gpout.cut('Press'=>y1..y2)
  GGraph.line( gpout, false , "exchange"=>true, "title"=>"", "index"=>20, "type"=>3, "title"=>"" )
  gpout = gpupfl_dwn1em1.copy
  gpout = gpout.cut('Press'=>y1..y2)
  GGraph.line( gpout, false , "exchange"=>true, "title"=>"", "index"=>20, "type"=>4, "title"=>"" )

  gpout = gpdnfl_dwn1e2.copy
  gpout = gpout.cut('Press'=>y1..y2)
  GGraph.line( gpout, false, "exchange"=>true, "title"=>"", "index"=>40, "type"=>1, "title"=>"" )
  gpout = gpdnfl_dwn1e1.copy
  gpout = gpout.cut('Press'=>y1..y2)
  GGraph.line( gpout, false, "exchange"=>true, "title"=>"", "index"=>40, "type"=>2, "title"=>"" )
  gpout = gpdnfl_dwn1e0.copy
  gpout = gpout.cut('Press'=>y1..y2)
  GGraph.line( gpout, false, "exchange"=>true, "title"=>"", "index"=>40, "type"=>3, "title"=>"" )
  gpout = gpdnfl_dwn1em1.copy
  gpout = gpout.cut('Press'=>y1..y2)
  GGraph.line( gpout, false, "exchange"=>true, "title"=>"", "index"=>40, "type"=>4, "title"=>"" )

end

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

def graph_tendency( svx1, svx2, svy1, svy2, gptend_dwn1e2, gptend_dwn1e1, gptend_dwn1e0, gptend_dwn1em1, flaglog, flagdiff )

  longname = "tendency"

  x1 = -5; x2 = 5; y1 = 1.05e5; y2 = 0
  itr = 1
  if flaglog then
    y2 = 1e1
    itr = 2
  end
  if flagdiff then
    x1 = -0.5; x2 = 0.5
    longname = "tendency diff."
  end

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

  gpout = gptend_dwn1e2.copy
  gpout = gpout.cut('Press'=>y1..y2)
  gpout.long_name = longname
  GGraph.line( gpout, true , "exchange"=>true, "title"=>"", "index"=>10, "type"=>1, "title"=>"" )
  gpout = gptend_dwn1e1.copy
  gpout = gpout.cut('Press'=>y1..y2)
  GGraph.line( gpout, false , "exchange"=>true, "title"=>"", "index"=>10, "type"=>2, "title"=>"" )
  gpout = gptend_dwn1e0.copy
  gpout = gpout.cut('Press'=>y1..y2)
  GGraph.line( gpout, false , "exchange"=>true, "title"=>"", "index"=>10, "type"=>3, "title"=>"" )
  gpout = gptend_dwn1em1.copy
  gpout = gpout.cut('Press'=>y1..y2)
  GGraph.line( gpout, false , "exchange"=>true, "title"=>"", "index"=>10, "type"=>4, "title"=>"" )

end

# flux (linear)
flaglog = false
flagdiff = false
graph_flux( svx1, svx2, svy1, svy2,
            gpupfl_dwn1e2, gpupfl_dwn1e1, gpupfl_dwn1e0, gpupfl_dwn1em1, gpdnfl_dwn1e2, gpdnfl_dwn1e1, gpdnfl_dwn1e0, gpdnfl_dwn1em1,
            flaglog, flagdiff )

# flux diff. (linear)
flaglog = false
flagdiff = true
graph_flux( svx1, svx2, svy1, svy2,
            gpupfl_dwn1e2-gpupfl_dwn1em1, gpupfl_dwn1e1-gpupfl_dwn1em1, gpupfl_dwn1e0-gpupfl_dwn1em1, gpupfl_dwn1em1-gpupfl_dwn1em1, gpdnfl_dwn1e2-gpdnfl_dwn1em1, gpdnfl_dwn1e1-gpdnfl_dwn1em1, gpdnfl_dwn1e0-gpdnfl_dwn1em1, gpdnfl_dwn1em1-gpdnfl_dwn1em1,
            flaglog, flagdiff )

# flux
flaglog = true
flagdiff = false
graph_flux( svx1, svx2, svy1, svy2,
            gpupfl_dwn1e2, gpupfl_dwn1e1, gpupfl_dwn1e0, gpupfl_dwn1em1, gpdnfl_dwn1e2, gpdnfl_dwn1e1, gpdnfl_dwn1e0, gpdnfl_dwn1em1,
            flaglog, flagdiff )

# flux diff.
flaglog = true
flagdiff = true
graph_flux( svx1, svx2, svy1, svy2,
            gpupfl_dwn1e2-gpupfl_dwn1em1, gpupfl_dwn1e1-gpupfl_dwn1em1, gpupfl_dwn1e0-gpupfl_dwn1em1, gpupfl_dwn1em1-gpupfl_dwn1em1, gpdnfl_dwn1e2-gpdnfl_dwn1em1, gpdnfl_dwn1e1-gpdnfl_dwn1em1, gpdnfl_dwn1e0-gpdnfl_dwn1em1, gpdnfl_dwn1em1-gpdnfl_dwn1em1,
            flaglog, flagdiff )

# tendency (linear)
flaglog = false
flagdiff = false
graph_tendency( svx1, svx2, svy1, svy2,
                gptend_dwn1e2, gptend_dwn1e1, gptend_dwn1e0, gptend_dwn1em1,
                flaglog, flagdiff )

# tendency diff. (linear)
flaglog = false
flagdiff = true
graph_tendency( svx1, svx2, svy1, svy2,
                gptend_dwn1e2-gptend_dwn1em1, gptend_dwn1e1-gptend_dwn1em1, gptend_dwn1e0-gptend_dwn1em1, gptend_dwn1em1-gptend_dwn1em1,
                flaglog, flagdiff )

# tendency
flaglog = true
flagdiff = false
graph_tendency( svx1, svx2, svy1, svy2,
                gptend_dwn1e2, gptend_dwn1e1, gptend_dwn1e0, gptend_dwn1em1,
                flaglog, flagdiff )

# tendency diff.
flaglog = true
flagdiff = true
graph_tendency( svx1, svx2, svy1, svy2,
                gptend_dwn1e2-gptend_dwn1em1, gptend_dwn1e1-gptend_dwn1em1, gptend_dwn1e0-gptend_dwn1em1, gptend_dwn1em1-gptend_dwn1em1,
                flaglog, flagdiff )




## flux linear
#graph_flux( svx1, svx2, svy1, svy2, gpupfl, gpdnfl, false, false )
#
## flux diff. linear
#graph_flux( svx1, svx2, svy1, svy2, gpupfl-ref_gpupfl, gpdnfl-ref_gpdnfl, false, true )
#
## flux log
#graph_flux( svx1, svx2, svy1, svy2, gpupfl, gpdnfl, true, false )
#
## flux diff. log
#graph_flux( svx1, svx2, svy1, svy2, gpupfl-ref_gpupfl, gpdnfl-ref_gpdnfl, true, true )


## tendency linear
#graph_tendency( svx1, svx2, svy1, svy2, gptend, false, false )
#
## tendency diff. linear
#graph_tendency( svx1, svx2, svy1, svy2, gptend-ref_gptend, false, true )
#
## tendency log
#graph_tendency( svx1, svx2, svy1, svy2, gptend, true, false )
#
## tendency diff. log
#graph_tendency( svx1, svx2, svy1, svy2, gptend-ref_gptend, true, true )

DCL.grcls
