#
# 自転軸傾斜各 4 種類の年平均・東西平均降水分布を重ね書きする
#
# usage: draw_ctl7.rb を実行する
#
# * 2011/12/30 drawzm_clim_1panel.rb を基に作成
#
#
require "numru/ggraph"
include NumRu

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

def clim_mean( iyrs, iyre, 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


     ts = (iyrs-1) * daysofyear * dataNumOfDay + 1
     te =  iyre    * daysofyear * dataNumOfDay
 
=begin
     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
=end

     gphystmp = gphys.cut('time'=>time[ts]..time[te]).mean('time')

     return gphystmp

end

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

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

dir    = ARGV[0]

vname = ARGV[1]

vname1 = vname + "-00"
vname2 = vname + "-23"
vname3 = vname + "-60"
vname4 = vname + "-90"


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

  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_fig 'viewport'=>[0.15,0.7,0.1,0.5] # 描画領域 (xmin, xmax, ymin, ymax)

GGraph.set_fig 'window'=>[-90,90,0,1.5e-4]
GGraph.set_axes('xlabelint'=>30) # x 軸に値を打つ間隔
GGraph.set_axes('ylabelint'=>2e-5) # y 軸に値を打つ間隔

gphystmp1 = clim_mean( iyrs, iyre, gphys1 ).mean('lon')
gphystmp2 = clim_mean( iyrs, iyre, gphys2 ).mean('lon')
gphystmp3 = clim_mean( iyrs, iyre, gphys3 ).mean('lon')
gphystmp4 = clim_mean( iyrs, iyre, gphys4 ).mean('lon')

if vname == 'SurfTemp'

# first panel
  GGraph.line( gphystmp )
#  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 )      # 参考まで

elsif vname == 'Rain'

  # second panel
  gphystmp1       = gphystmp1 / 2.5e6   
  gphystmp1.units = 'kg m-2 s-1'

  gphystmp2       = gphystmp2 / 2.5e6
  gphystmp2.units = 'kg m-2 s-1'

  gphystmp3       = gphystmp3 / 2.5e6
  gphystmp3.units = 'kg m-2 s-1'

  gphystmp4       = gphystmp4 / 2.5e6
  gphystmp4.units = 'kg m-2 s-1'

  GGraph.line( gphystmp1,true,
               'annot'=>false,
               'title'=>'zonally averaged annual mean precipitation')
  GGraph.line( gphystmp2,false,
               'annot'=>false,
               'type'=>2 )
  GGraph.line( gphystmp3,false,
               'annot'=>false,
               'type'=>3 )
  GGraph.line( gphystmp4,false,
               'annot'=>false,
               'type'=>4 )

#  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 )      # 参考まで

elsif vname == 'SoilMoist'


  # third panel
  GGraph.line( gphystmp )
#  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 )      # 参考まで

elsif vname == 'SurfSnow'

  # forth panel
  GGraph.line( gphystmp )
#  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 )      # 参考まで

elsif vname == 'ulwrf' || vname1 == 'OLRA'

  # forth panel

  GGraph.set_fig('window'=>[-90,90,140,300])     # 軸の最大/最小値を設定. [x 軸最小, x 軸最大, y 軸最小, y 軸最大]
  GGraph.line( gphystmp )
#  GGraph.tone( gphystmp, true, 
#             'lev'=>[100,120,140,160,180,200,220,240,260,280,300],
#             # レベル＆パターンを陽に指定
#             'pat'=>[10999,15999,20999,25999,30999,35999,40999,50999,60999,70999,80999,90999],
#             # パタンの方が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 )      # 参考まで

elsif vname == 'OSR'

  # forth panel

   GGraph.set_fig('window'=>[-90,90,0,350])     # 軸の最大/最小値を設定. [x 軸最小, x 軸最大, y 軸最小, y 軸最大]
  GGraph.line( gphystmp )
#  GGraph.tone( gphystmp, true, 
#             'lev'=>[100,120,140,160,180,200,220,240,260,280,300],
#             # レベル＆パターンを陽に指定
#             'pat'=>[10999,15999,20999,25999,30999,35999,40999,50999,60999,70999,80999,90999],
#             # パタンの方が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 )      # 参考まで

end

#GGraph.color_bar

DCL.grcls

