# -*- coding: euc-jp -*-

require "numru/ggraph"
include NumRu

MAX = 400
MIN = 0.0
 
xdim = 96
ydim = 48

dim    = 25     # Array size

# Case A: Newton Crater
LON = 202.5
LAT = -42.6776
Offset = 13.0

# Case B: Nili Fossae
#LON = 75.0
#LAT = 20.4112
#Offset = 5.0

# Case C: Isdis Planitia
#LON = 86.25
#LAT = 9.27785
#Offset = 5.75


# Starting time
TimeA = 62928.0
TimeB = 62616.0
TimeC = 64944.0
TimeD = 71160.0

TimeA_s = TimeA - Offset
TimeB_s = TimeB - Offset
TimeC_s = TimeC - Offset
TimeD_s = TimeD - Offset

sigma = 5.670373e-8 # Stefan Boltsmann Constat

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

SurfTemp_All = GPhys::IO.open('SurfTemp.nc', 'SurfTemp')
SurfTemp_local = SurfTemp_All.cut('lon'=>LON, 'lat'=>LAT)
SLRA_All     = GPhys::IO.open('SLRA.nc', 'SLRA')
SLRA_local   = SLRA_All.cut('lon'=>LON, 'lat'=>LAT)

Ls_All = GPhys::IO.open('PlanetLonFromVE_rank000000.nc', 'PlanetLonFromVE')

# Set local time to Axis variable
lt = VArray.new(NArray.float(dim).indgen!,
                {"long_name"=>"local time","unit"=>"hour"}, "lt")
lta = Axis.new.set_pos(lt)
grid = Grid.new(lta)

#DCL.swlset('ldump', true )
DCL.swlset('lwnd', false )

DCL.gropn(4)
#DCL.sldiv('y', 3, 1)
DCL.sgpset('lcntl', false)
DCL.uzfact(0.7)

GGraph.set_linear_tone_options('min'=>MIN, 'max'=>MAX )

# Set Array for loop
time = NArray.float(4)
time = [TimeA_s, TimeB_s, TimeC_s, TimeD_s]

# Set Work Array
ST    = NArray.float(xdim,ydim)  # Surface temperature
STFLUX= NArray.float(xdim,ydim)  # 

for k in 0..3
for i in 0..6

  t_start = time[k] + i * 360.0 - 1.0 # set from LT=00:00
  t_end = t_start + dim.to_f

  SurfTemp = SurfTemp_All.cut('time'=>t_start..t_end)
  SLRA     = SLRA_All.cut('time'=>t_start..t_end)
  Ls_val = Ls_All.cut('time'=>t_start..t_end).val.to_i
  Ls_str = Ls_val[0].to_s

  STFLUX = sigma * SurfTemp**4
  STFLUX.units='W.m-2'

  RadLDwFlux = STFLUX - SLRA
  AirTemp = (RadLDwFlux.abs / sigma)**0.25
  AirTemp.units='K'

  t = AirTemp.axis(0).pos
  lt = t - t_start
  lt.long_name = "local time"
  AirTemp.axis(0).set_pos(lt)

# label makeing

#  t_start = time[k] + i * 360.0 - 5.0 * 24.0
#  t_end = time[k] + i * 360.0 + 5.0 * 24.0

#  Ls_val = Ls_All.cut('time'=>t_start..t_end).val.to_i
#  Ls_str1 = Ls_val[0].to_s
#  Ls_str2 = Ls_val[-1].to_s
#  label  ='Ls='+Ls_str+'('+Ls_str1+'-'+Ls_str2+')'

#  label  ='Net SLR'+'(Ls='+Ls_str+')'
  MAX = 400 ; MIN = 0.0
#  GGraph.set_linear_tone_options('min'=>MIN, 'max'=>MAX )
#  GGraph.tone( SLRA , true, \
#             'title'=>label )
#  GGraph.color_bar

#  label  ='Upward SLR'+'(Ls='+Ls_str+')'
#  GGraph.set_linear_tone_options('min'=>MIN, 'max'=>MAX )
#  GGraph.tone( STFLUX , true, \
#             'title'=>label )
#  GGraph.color_bar

  label  ='Downward SLR'+'(Ls='+Ls_str+')'
  MAX = 100 ; MIN = 0.0
  GGraph.set_linear_tone_options('min'=>MIN, 'max'=>MAX )
  GGraph.tone( RadLDwFlux, true, \
             'title'=>label )
  GGraph.color_bar

end
end 


DCL.grcls
