# -*- coding: euc-jp -*-
#
# 2011/07/05 ulwrf の部分を修正
# 2011/07/04 drawgm.rb を元に作成
#

require "numru/ggraph"
include NumRu


dir    = '..'
vname1 = 'OLRA'
vname2 = 'OSRA'
vname3 = 'ulwrf'
vname3file = 'ulwrf.mon.mean'
vname4 = 'Rain'

#
# OLRA, OSRA の データは 19 - 20 年目を使用する
#
ts = 19 * 365 * 4 + 1
te = 20 * 365 * 4

#
# OLRA, OSRA にかける重み関数の読み込み
#
gphysw = GPhys::NetCDF_IO.open(dir+'/'+vname1+".nc", 'lat_weight')
weight = gphysw.val
weight = weight / weight.sum(0)

#p weight
#p weight.reshape!(1,gphysw.shape[0])

#
# 各 nc ファイルの読み込み
#
gphys1 = GPhys::NetCDF_IO.open(dir+'/'+vname1+".nc", vname1)
gphys2 = GPhys::NetCDF_IO.open(dir+'/'+vname2+".nc", vname2)
gphys3 = GPhys::NetCDF_IO.open('/home2/yukai-home2/NECP'+'/'+vname3file+".nc", vname3)


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


#
# DCLのオープンと設定 
#
DCL.gropn(4)
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.set_fig('viewport'=>[0.15,0.7,0.2,0.6])

#
# 描画
#

GGraph.line( ( gphys1 * weight.reshape!(1,gphysw.shape[0]) ).sum(1).cut('time'=>time[ts]..time[te]).mean('time'),true )
GGraph.line( ( gphys2 * weight.reshape!(1,gphysw.shape[0]) ).sum(1).cut('time'=>time[ts]..time[te]).mean('time'),true )
GGraph.line( gphys3.average(0).mean('time'),true )


DCL.grcls


#p vname1 + ': ' + gphys1.to_s
#p vname2 + ': ' + gphys2.to_s
#p vname3 + ': ' + gphys3.to_s


#fileunit = File.open("heatbudget-zm.txt", 'w')
#fileunit.puts vname1 + ': ' + gphys1.to_s
#fileunit.puts vname2 + ': ' + gphys2.to_s
#fileunit.puts vname3 + ': ' + gphys3.to_s
#fileunit.close


#p vname1 + ':' + ( gphys1 * weight.reshape!(1,gphysw.shape[0]) ).sum(1).mean(0).cut('time'=>time[ts]..time[te]).mean('time').to_s
#p vname2 + ':' + ( gphys2 * weight.reshape!(1,gphysw.shape[0]) ).sum(1).mean(0).cut('time'=>time[ts]..time[te]).mean('time').to_s
#p vname3 + ':' + ( gphys3 * weight.reshape!(1,gphysw.shape[0]) ).sum(1).mean(0).cut('time'=>time[ts]..time[te]).mean('time').to_s
