# -*- coding: euc-jp -*-
#
# (dcpam 用) ある年における惑星表面の物理量の東西平均時間変化を描画する
#
# usage: draw_ctl_ts.rb を実行する.
#
# * 2012/01/05 完成
# * 2012/01/04 drawzm_clim_1panel.rb を基に作成
#
#
require "numru/ggraph"
include NumRu

##############################################################################
def prepare_gphys( gphys )

  lon  = gphys.coord(0).val
  lon_a = VArray.new( lon,
                      {  "long_name"=>gphys.coord('lon').get_att('long_name'),
                         "units"=>gphys.coord('lon').get_att('units') },
                         "lon" )
  lon = Axis.new.set_pos(lon_a)
  #p lon

  lat  = gphys.coord(1).val
  lat_a = VArray.new( lat,
                      {  "long_name"=>gphys.coord('lat').get_att('long_name'),
                         "units"=>gphys.coord('lat').get_att('units') },
                         "lat" )
  lat = Axis.new.set_pos(lat_a)
  #p lat

  time = NArray.int(365)

  for i in 0..364
    time[i] = i+1
  end

  time_a = VArray.new(time, 
                      {  "long_name"=>gphys.coord('time').get_att('long_name'),
                         "units"=>"day"},
                         "time" )
  time = Axis.new.set_pos(time_a)
  #p time

  im = gphys.shape[0]
  jm = gphys.shape[1]
  tm = 365
  #p im,jm,tm

  data_a = NArray.sfloat(im,jm,tm).fill(-999.0)
  #p data_a

  data = VArray.new(data_a,
                     {  "long_name"=>gphys.get_att('long_name'),
                        "units"=>gphys.get_att('units')},
                        gphys.name )
  #p data 

  gphystmp = GPhys.new( Grid.new(lon,lat,time), data )
  return gphystmp
end

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

dir    = ARGV[0]
vname1 = ARGV[1]

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

exp = ARGV[2]

#if data == "dcpam" then

  gphys1 = GPhys::NetCDF_IO.open(dir+'/'+vname1+".sfc.4Xday.1981-2010.ltm.nc", vname1) 
  time = gphys1.coord('time').val
  time = time.to_i
  p time

  gphystmp = prepare_gphys( gphys1 )


=begin
elsif data == "NCEP" then

  gphys1 = GPhys::NetCDF_IO.open(dir+'/'+vname1_longname + ".mon.mean"+".nc", vname1) 
  gphys2 = GPhys::NetCDF_IO.open(dir+'/'+vname2+".nc", vname2)
  #gphys3 = GPhys::NetCDF_IO.open(dir+'/'+vname3+".nc", vname3)

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

end 
=end

#< 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_axes('xlabelint'=>90,'ylabelint'=>30) # x 軸に値を打つ間隔


# 日平均値を計算し, 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

ts = 0     # 描画開始年 1 月 1 日のデータ番号

dayi = daysofyear - 1 

for i in 0..dayi
  te = ts + 3
  gphystmp[true,true,i] = gphys1.cut('time'=>time[ts]..time[te]).mean('time')
  ts = te + 1
  i = i + 1
end

if vname1 == 'SurfTemp'

# first panel
  
  gphystmp = gphystmp.mean('lon')
  GGraph.tone( gphystmp, true,
               'exchange'=>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,
                'annot'=>false,
               'title'=>gphys1.get_att('long_name')+' ('+exp+')' )

  GGraph.contour( gphystmp, false,
                 'exchange'=>true, 
                 'lev'=>[200,210,220,230,240,250,260,270,280,290,300], 'index'=>3 )      # 参考まで
  GGraph.color_bar('vcent'=>0.3,'units'=>'('+gphystmp.units.to_s+')')

elsif vname1 == 'prate'

  # second panel
#  gphystmp       = gphystmp / 2.5e6   # /s
#  gphystmp       = gphystmp / 2.5e6 * 18600.0e0 * 365.0e0   # /y への換算
  gphystmp.units = 'kg m-2 s-1'

  GGraph.tone( gphystmp.mean('lon'), true, 
               'exchange'=>true,
               'lev'=>[0,1e-5,2e-5,4e-5,8e-5,12e-5,16e-5,20e-5,24e-5,28e-5,32e-5], # /s の場合のレベル
#               'lev'=>[0,1e1,2e1,4e1,8e1,12e1,16e1,20e1,24e1,28e1,32e1],  # /y の場合のレベル
               # レベル＆パターンを陽に指定
               'pat'=>[1,20999,30999,35999,40999,50999,60999,65999,70999,75999,80999,85999], 
               # パタンの方が1つ多→±∞まで
               'map_axes'=>true,
               'annot'=>false,
               'title'=>gphys1.get_att('long_name')+' ('+exp+')')
  #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('vcent'=>0.3,'units'=>'('+gphystmp.units.to_s+')')

elsif vname1 == 'SoilMoist'

  # third panel

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

  #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('vcent'=>0.3,'units'=>'('+gphystmp.units.to_s+')')

elsif vname1 == 'SurfSnow'

  # forth panel
  GGraph.tone( gphystmp.mean('lon'), true,
               'exchange'=>true, 
               'lev'=>[0,50,100,150,200,250,300,350,400,450,500,550,600,650,700,750],
               # レベル＆パターンを陽に指定
               'pat'=>[0,15999,20999,25999,30999,35999,40999,45999,50999,55999,60999,65999,70999,75999,80999,85999,90999], 
               # パタンの方が1つ多→±∞まで
               'map_axes'=>true,
               'annot'=>false,
               'title'=>gphys1.get_att('long_name')+' ('+exp+')')

#  GGraph.contour( gphystmp, false,
#                  'exchange'=>true,
#                  'lev'=>[0,200,400,600], 'index'=>3 )      # 参考まで
  GGraph.color_bar('vcent'=>0.3,'units'=>'('+gphystmp.units.to_s+')')

=begin
elsif vname1 == '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 vname1 == '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

end


DCL.grcls

