require "numru/ggraph"
include NumRu

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

def clim_mean( iyrs, iyre, imon, gphys )

  dataNumOfDay = 4
  daysOfMonth  = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

  daysofyear = 0
  daysOfMonth.each do |i|
    daysofyear += i
  end

  time = gphys.coord('time').val

  for iyr in iyrs..iyre
    t = daysofyear*(iyr-1)
    for i in 1..imon-1
      t = t + daysOfMonth[i-1]
    end

    ts = t
    te = t + daysOfMonth[imon-1]

    ts = ts * dataNumOfDay
    te = te * dataNumOfDay - 1

    if iyr == iyrs
      gphystmp = gphys.cut('time'=>time[ts]..time[te]).mean('time')
    else
      gphystmp = gphystmp + gphys.cut('time'=>time[ts]..time[te]).mean('time')
    end
  end
  gphystmp = gphystmp / ( iyre - iyrs + 1 )

  return gphystmp

end

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

iyrs = ARGV[1].to_i
iyre = ARGV[2].to_i

imon = ARGV[3].to_i

title = ARGV[4]

dir    = ARGV[0]
vname1 = 'SurfTemp'
vname2 = 'Rain'
vname3 = 'SoilMoist'
vname4 = 'SurfSnow'

gphys1 = GPhys::NetCDF_IO.open(dir+'/'+vname1+".nc", vname1)
gphys2 = GPhys::NetCDF_IO.open(dir+'/'+vname2+".nc", vname2)
gphys3 = GPhys::NetCDF_IO.open(dir+'/'+vname3+".nc", vname3)
gphys4 = GPhys::NetCDF_IO.open(dir+'/'+vname4+".nc", vname4)


tm = gphys1.shape[2] # number of elements for time      dimension
time = gphys1.coord('time').val



#< DCLのオープンと設定 >
DCL.gropn(2)
DCL.sldiv('y',2,2)           # 2x2に画面分割, 'y'=yoko: 左上→右上→左下...
DCL.sgpset('lcntl', false)   # 制御文字を解釈しない
DCL.sgpset('lfull',true)     # 全画面表示
DCL.uzfact(0.75)             # 座標軸の文字列サイズを 0.75 倍
DCL.sgpset('lfprop',true)    # プロポーショナルフォントを使う

#< GGraph による 描画 >
GGraph.set_fig 'itr'=>10, 'viewport'=>[0.15,0.85,0.1,0.6]
GGraph.set_map 'coast_world'=>true, 'grid'=>false


# first panel
#gphystmp = gphys1.cut('time'=>time[ts]..time[te]).mean('time')

gphystmp = clim_mean( iyrs, iyre, imon, gphys1 )

GGraph.tone( gphystmp, true, 
             'lev'=>[210,220,230,240,250,260,270,280,290,300],
             # レベル＆パターンを陽に指定
             'pat'=>[20999,30999,40999,45999,50999,55999,60999,70999,80999,85999,90999], 
             # パタンの方が1つ多→±∞まで
             'map_axes'=>true )
#GGraph.contour( gphystmp, false, 'lev'=>[200,210,220,230,240,250,260,270,280,290,300], 'index'=>3 )      # 参考まで
GGraph.color_bar


DCL::uxmttl('T', ' ', 1.0)
DCL::uxmttl('T', ' ', 1.0)
DCL::uxmttl('T', title, -1.0)


# second panel
#gphystmp = gphys2.cut('time'=>time[ts]..time[te]).mean('time')

gphystmp = clim_mean( iyrs, iyre, imon, gphys2 )

#GGraph.tone( gphystmp, true, 
#             'lev'=>[0,20,50,100,200,300,400,500,600,700,800],
#             # レベル＆パターンを陽に指定
#             'pat'=>[1,20999,30999,35999,40999,50999,60999,65999,70999,75999,80999,85999], 
#             # パタンの方が1つ多→±∞まで
#             'map_axes'=>true )
gphystmp = gphystmp / 2.5e6
GGraph.tone( gphystmp, true, 
             'lev'=>[0,1e-5,2e-5,4e-5,8e-5,12e-5,16e-5,20e-5,24e-5,28e-5,32e-5],
             # レベル＆パターンを陽に指定
             'pat'=>[1,20999,30999,35999,40999,50999,60999,65999,70999,75999,80999,85999], 
             # パタンの方が1つ多→±∞まで
             'map_axes'=>true )
#GGraph.contour( gphystmp, false, 'lev'=>[0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150], 'index'=>3 )      # 参考まで
GGraph.color_bar

# third panel
#gphystmp = gphys3.cut('time'=>time[ts]..time[te]).mean('time')

gphystmp = clim_mean( iyrs, iyre, imon, gphys3 )

GGraph.tone( gphystmp, true, 
             'lev'=>[0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150],
             # レベル＆パターンを陽に指定
             'pat'=>[15999,20999,25999,30999,35999,40999,45999,50999,55999,60999,65999,70999,75999,80999,85999,90999,95999],
             # パタンの方が1つ多→±∞まで
             'map_axes'=>true )
#GGraph.contour( gphystmp, false, 'lev'=>[0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150], 'index'=>3 )      # 参考まで
GGraph.color_bar

# forth panel
#gphystmp = gphys4.cut('time'=>time[ts]..time[te]).mean('time')

gphystmp = clim_mean( iyrs, iyre, imon, gphys4 )

GGraph.tone( gphystmp, true, 
             'lev'=>[0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150],
             # レベル＆パターンを陽に指定
             'pat'=>[15999,20999,25999,30999,35999,40999,45999,50999,55999,60999,65999,70999,75999,80999,85999,90999,95999], 
             # パタンの方が1つ多→±∞まで
             'map_axes'=>true )
#GGraph.contour( gphystmp, false, 'lev'=>[0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150], 'index'=>3 )      # 参考まで
GGraph.color_bar


DCL.grcls

