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

dir   = "../prog03_calc_rte/out"
fn    = "Spectral_Flux.nc"

press1 = 0.0
press2 = 1e5
molnum = 2
wns = 10; wne = 50000
wns = 1.0/(3.0*1e-6)*1e-2; wne = 1.0/(0.2*1e-6)*1e-2


################################################

def prepare_vars( dir, fn, press1, press2, vname )

  path = dir + "/" + fn
  gp = GPhys::NetCDF_IO.open( path, vname )

  gp1 = gp.cut("Press"=>press1)
  gp2 = gp.cut("Press"=>press2)

  gp1 = gp1.copy
  gp2 = gp2.copy

  namelev = "WaveNum"
  wn = gp.coord(namelev)

#  gp1 = gp1 * wn
#  gp2 = gp2 * wn

  namelev = "WaveNum"
  z = gp.axis(namelev).pos.convert_units( Units['cm-1'] )
  z.long_name = 'wavenumber'
  gp1.axis(namelev).set_pos(z)
  gp2.axis(namelev).set_pos(z)
  gp1.long_name = "flux"
  gp2.long_name = "flux"

  gp1 = gp1 * 1e2
  gp2 = gp2 * 1e2
  gp1.units = "Wm-2(cm-1)-1"
  gp2.units = "Wm-2(cm-1)-1"


  gpwl1 = gp.cut("Press"=>press1)
  gpwl2 = gp.cut("Press"=>press2)

  gpwl1 = gpwl1.copy
  gpwl2 = gpwl2.copy
  wl = 1.0/wn

  gpwl1 = gpwl1 / wl**2
  gpwl2 = gpwl2 / wl**2

#  gpwl1 = gpwl1 * wl
#  gpwl2 = gpwl2 * wl

#  wl = wl.convert_units( Units['micron'] )
  wl.long_name = "wavelength"
  vawl = wl.convert_units( Units['micron'] )
#  vawl = VArray.new( wl.val, { "long_name"=>'wavelength', "units"=>wl.units.to_s }, namelev )
  gpwl1.axis(namelev).set_pos(vawl)
  gpwl2.axis(namelev).set_pos(vawl)

  gpwl1.long_name = "flux"
  gpwl2.long_name = "flux"

  gpwl1 = gpwl1 * 1e-6
  gpwl2 = gpwl2 * 1e-6
  gpwl1.units = "Wm-2(micron)-1"
  gpwl2.units = "Wm-2(micron)-1"

  return gp1, gp2, gpwl1, gpwl2
end
################################################

vname = 'RadUwFlux'
gpuwwn1, gpuwwn2, gpuwwl1, gpuwwl2 = prepare_vars( dir, fn, press1, press2, vname )
vname = 'RadDwFlux'
gpdwwn1, gpdwwn2, gpdwwl1, gpdwwl2 = prepare_vars( dir, fn, press1, press2, vname )

DCL.gropn(1)
DCL.sldiv('y',2,2)
DCL.sgpset('lcntl',false)
DCL.sgpset('lfull',true)
#DCL.uzfact(1.5)
DCL.sgpset('lfprop',true)

svx1 = 0.15; svx2 = 0.9; svy1 = 0.2; svy2 = 0.6
x1 = wns; x2 = wne; y1 = 0; y2 = 1e3
x1 = wns; x2 = wne; y1 = 0; y2 = 2.5e3

itr = 1
#itr = 2
#itr = 3

GGraph.set_fig 'itr'=>itr, 'viewport'=>[svx1,svx2,svy1,svy2], 'window'=>[x1,x2,y1,y2]
GGraph.line( gpuwwn1, true, "title"=>"", "annotate"=>false, "index"=>10 )
GGraph.line( gpuwwn2, false, "title"=>"", "annotate"=>false, "index"=>20 )

GGraph.set_fig 'itr'=>itr, 'viewport'=>[svx1,svx2,svy1,svy2], 'window'=>[1.0/(x2*1e2)*1e6,1.0/(x1*1e2)*1e6,y1,y2]
GGraph.line( gpuwwl1, true, "title"=>"", "annotate"=>false, "index"=>10 )
GGraph.line( gpuwwl2, false, "title"=>"", "annotate"=>false, "index"=>20 )

GGraph.set_fig 'itr'=>itr, 'viewport'=>[svx1,svx2,svy1,svy2], 'window'=>[x1,x2,y1,y2]
GGraph.line( gpdwwn1, true, "title"=>"", "annotate"=>false, "index"=>10 )
GGraph.line( gpdwwn2, false, "title"=>"", "annotate"=>false, "index"=>20 )

GGraph.set_fig 'itr'=>itr, 'viewport'=>[svx1,svx2,svy1,svy2], 'window'=>[1.0/(x2*1e2)*1e6,1.0/(x1*1e2)*1e6,y1,y2]
GGraph.line( gpdwwl1, true, "title"=>"", "annotate"=>false, "index"=>10 )
GGraph.line( gpdwwl2, false, "title"=>"", "annotate"=>false, "index"=>20 )

DCL.grcls
