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

dir = "./out"
#dir = "./out-dwn1e1-LW-noSW"
iBand = 17
iLoop = 9

dirref = "/work02/yot/work/radmodels/icrccm/mkncfiles/out"
#dir = "/home/yot/work/radmodels-2017-04-12-validation-allwn-highreso/icrccm/mkncfiles/out"
sym    = "LW_Case25_Earth_Tropics_CO2-300ppmv"
symref = "Case25"

#fn = "Case19_Earth_MidLatSummer_H2O-Only-X1.00_Flux.nc"
#fn = "Case20_Earth_MidLatSummer_H2O-Only-X1.00-WOCont_Flux.nc"
#fn = "Case25_Earth_Tropics_CO2-300ppmv_Flux.nc"
fn = sym + "_Flux.nc"
#fn = "flux.nc"
path = dir + "/" + fn
vname = 'RadUwFlux'
gpupfl = GPhys::NetCDF_IO.open( path, vname )
vname = 'RadDwFlux'
gpdnfl = GPhys::NetCDF_IO.open( path, vname )

#fn = "Case19_Earth_MidLatSummer_H2O-Only-X1.00_Tendency.nc"
#fn = "Case20_Earth_MidLatSummer_H2O-Only-X1.00-WOCont_Tendency.nc"
#fn = "Case25_Earth_Tropics_CO2-300ppmv_Tendency.nc"
fn = sym + "_Tendency.nc"
#fn = "tendency.nc"
path = dir + "/" + fn
vname = 'DTempDt'
gptend = GPhys::NetCDF_IO.open( path, vname )

#dir = "out"
#fn = "Case19flux.nc"
#fn = "Case20flux.nc"
#fn = "Case25flux.nc"
fn = symref + "flux.nc"
#fn = "Test_Earth_Iso300K_H2O-1e-3_CO2-300ppmv_Flux.nc"
dir = dirref
path = dir + "/" + fn
vname = 'upflux'
#vname = 'RadUwFlux'
ref_gpupfl = GPhys::NetCDF_IO.open( path, vname )
vname = 'downflux'
#vname = 'RadDwFlux'
ref_gpdnfl = GPhys::NetCDF_IO.open( path, vname )
#fn = "Case19hr.nc"
#fn = "Case20hr.nc"
#fn = "Case25hr.nc"
fn = symref + "hr.nc"
#fn = "Test_Earth_Iso300K_H2O-1e-3_CO2-300ppmv_Tendency.nc"
vname = 'hr'
#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.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.20; svx2 = 0.60; svy1 = 0.2; svy2 = 0.8

def graph_flux( svx1, svx2, svy1, svy2, gpupfl, gpdnfl, flaglog, flagdiff )

  gpupflout = gpupfl.copy
  gpdnflout = gpdnfl.copy
  gpupflout.long_name = "flux"
  gpdnflout.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
    gpupflout.long_name = "flux diff."
    gpdnflout.long_name = "flux diff."
  end

  GGraph.set_fig 'itr'=>itr, 'viewport'=>[svx1,svx2,svy1,svy2], 'window'=>[x1,x2,y1,y2]
  gpout = gpupflout.cut('Press'=>y1..y2)
  GGraph.line( gpout, true , "exchange"=>true, "index"=>20, "title"=>"" )
  gpout = gpdnflout.cut('Press'=>y1..y2)
  GGraph.line( gpout, false, "exchange"=>true, "index"=>40, "title"=>"" )

end

def graph_tendency( svx1, svx2, svy1, svy2, gp, flaglog, flagdiff )

  gpout = gp.copy

  gpout = gpout * 86400.0
  gpout.units = "K (day)-1"
  gpout.long_name = "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
    gpout.long_name = "tendency diff."
  end

  GGraph.set_fig 'itr'=>itr, 'viewport'=>[svx1,svx2,svy1,svy2], 'window'=>[x1,x2,y1,y2]
  GGraph.line( gpout, true , "exchange"=>true, "index"=>1, "title"=>"" )

end


# 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
