require "numru/ggraph"
include NumRu

ncfn  = "out.nc"
vname = "QPassTrace"
time  = 0
time  = ( 669*9 + 56*9 ) * 24

ncfn_topog  = "../../data_Mars/sp_for_Mars_T021_MGS.nc"
vname_topog = "topog"
ncfn_ls     = "../PlanetLonFromVE_rank000000.nc"
vname_ls    = "PlanetLonFromVE"

#gphys = GPhys::IO.open( ncfn, vname )
url = ncfn + "@" + vname
gphys = GPhys::IO.open_gturl( url )

url = ncfn_topog + "@" + vname_topog
gp_topog = GPhys::IO.open_gturl( url )

url = ncfn_ls + "@" + vname_ls
gp_ls = GPhys::IO.open_gturl( url )

na_time = gphys.coord('time').val
na_time -= na_time[0]
va_time = VArray.new( na_time,
                      { "long_name"=>gphys.coord('time').long_name, 
                        "units"=>gphys.coord('time').units.to_s },
                      "time" )
gphys.axis('time').set_pos(va_time)
#
gp_ls.axis('time').set_pos(va_time)

iws = (ARGV[0] || (puts ' WORKSTATION ID (I)  ? ;'; DCL::sgpwsn; gets)).to_i
DCL.gropn(iws)

#DCL.sldiv('y',1,4)
DCL.sgpset('isub', 96)     # control character of subscription: '_' --> '`'
DCL.sgpset('lfull',true)
DCL.uzfact(0.6)            # font size
DCL.sgpset('lclip', true)  # clip graph
#DCL.udlset('LMSG', false)  # flag for message of contour interval
DCL.glpset('lmiss',true)   # missing value

#DCL.sgscmn(4)  # blue-cyan-white-yellow-red
#DCL.sgscmn(5)  # gray-scale
#DCL.sgscmn(14) # blue-white-red

levels = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,2.0,3.0,4.0,5.0,10.0]
patterns = [1,10999,15999,20999,25999,30999,35999,40999,50999,60999,65999,70999,75999,80999,90999,95999]
levels = [0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,2.0,3.0,4.0,5.0,10.0,20.0,30.0]
patterns = [1,10999,15999,20999,25999,30999,35999,40999,50999,60999,65999,70999,75999,80999,85999,90999,95999]

vpx1 = 0.1
vpx2 = 0.9
vpy1 = 0.2
vpy2 = 0.55
GGraph.set_fig 'itr'=> 10, 'viewport'=>[vpx1,vpx2,vpy1,vpy2], 'window'=>[0,360,-90,90]
GGraph.set_map "coast_world"=>false, 'grid'=>false
GGraph.set_axes('xlabelint'=>30)
GGraph.set_axes('ylabelint'=>30)

for it in 0..(na_time.size-1)
  gpout = gphys.cut('time'=>na_time[it])
  GGraph.tone gpout, true,
              "annotate"=>false,
              "map_axes"=>true,
              'min'=>0, 'max'=>0.05, 'nlev'=>50
#            'lev'=>levels, 'pat'=>patterns
#GGraph.contour gphys, false, 'lev'=>levels
  GGraph.color_bar
  GGraph.contour gp_topog, false, 'lev'=>[0]
  #
  # time information
  rsize = DCL.sgqtxs()
  iind  = DCL.sgqtxi()
  DCL.sgstxs( rsize*0.25 )
  DCL.sgstxi(3)
  DCL.sgstxc(-1)
  DCL.sglset('LCLIP', false)
  label = "time"
  DCL.sgtxv( vpx1+(vpx2-vpx1)*0.7, vpy2*1.10, label )
  label = ": " + na_time[it].to_i.to_s.rjust(4) + " hrs"
  DCL.sgtxv( vpx1+(vpx2-vpx1)*0.76, vpy2*1.10, label )
  label = "Ls"
  DCL.sgtxv( vpx1+(vpx2-vpx1)*0.7, vpy2*1.05, label )
  label = ": " + gp_ls.cut('time'=>na_time[it]).val[0].to_i.to_s.rjust(4) + " degrees"
  DCL.sgtxv( vpx1+(vpx2-vpx1)*0.76, vpy2*1.05, label )
  DCL.sglset('LCLIP', true)
  DCL.sgstxi(iind)
  DCL.sgstxs( rsize )
end

#GGraph.line gphys, true, 'exchange'=>false

DCL.grcls
