# -*- coding: euc-jp -*-
#
# 3 年目の東西平均分布を描画するスクリプト
#
# 2012/01/10 Rain に対応した
# 2011/12/20 mode の分岐を廃止. OSR の描画を追記.
# 2011/09/08 drawzm.rb を元に作成
#

require "numru/ggraph"
include NumRu

win  = ARGV[0].to_i # window の指定. 2: ps (ファイル名を指定する必要あり), 4: X
file = ARGV[1].to_s # 画像の保存ファイル名


tar = 'figure'


dir    = '..'
vname1 = 'OLRA'
vname2 = 'OSRA'
vname3 = 'ulwrf'
vname4 = 'uswrf'
vname5 = 'dswrf'
vname3file = 'ulwrftop.mon.mean'
vname4file = 'uswrftop.mon.mean'
vname5file = 'dswrftop.mon.mean'

vname7 = 'Rain'
vname8 = 'prate'
vname8file = 'prate.mon.mean'


#
# OLRA, OSRA の データは 3 年目を使用する
#

ts = 2 * 365 * 4 + 1
te = 3 * 365 * 4

#
# NECP の データは 1988-2007 年の各月平均を使用する
#
ts2 = 17417760
te2 = 17592240


#
# 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/NCEP'+'/'+vname3file+".nc", vname3)
gphys4 = GPhys::NetCDF_IO.open('/home2/yukai-home2/NCEP'+'/'+vname4file+".nc", vname4)
gphys5 = GPhys::NetCDF_IO.open('/home2/yukai-home2/NCEP'+'/'+vname5file+".nc", vname5)
gphys6 = gphys5 - gphys4

gphys7 = GPhys::NetCDF_IO.open(dir+'/'+vname7+".nc", vname7)
gphys8 = GPhys::NetCDF_IO.open('/home2/yukai-home2/NCEP'+'/'+vname8file+".nc", vname8)

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

#
# DCLのオープンと設定 
#
DCL.gropn(win)
#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.set_axes('xlabelint'=>30)

#
# 描画
#


=begin
# OLR の描画
GGraph.set_fig('window'=>[-90,90,100,300])     # 軸の最大/最小値を設定. [x 軸最小, x 軸最大, y 軸最小, y 軸最大]

  # dcpam の OLRA
    GGraph.line( gphys1.average(0).cut('time'=>time[ts]..time[te]).mean('time'),
                 true,
                 'annot'=>false,
                 'titl'=>'zonal averaged annual annual mean OLR' )
  # NCEP 
    GGraph.line( gphys3.average(0).cut('time'=>ts2..te2).mean('time'),
                 false,
                 "type"=> 2,"index"=>2)
=end

=begin
# OSR の描画
GGraph.set_fig('window'=>[-90,90,0,400])     # 軸の最大/最小値を設定. [x 軸最小, x 軸最大, y 軸最小, y 軸最大]
GGraph.set_axis('ylabel'=>"- outgoing shortwave")

  # dcpam の OSRA
    gphys2 = gphys2.abs
    GGraph.line( gphys2.average(0).cut('time'=>time[ts]..time[te]).mean('time'),
                 true,
                 'annot'=>false,
                 'titl'=>'zonal averaged annual annual mean -OSR' )
  # NCEP の OSR
    GGraph.line( gphys6.average(0).cut('time'=>ts2..te2).mean('time'),
                 false,
                 "type"=> 2, "index"=>2 )

=end

# Rain の描画
  GGraph.set_fig('window'=>[-90,90,0,18e-5])   # 軸の最大/最小値を設定. [x 軸最小, x 軸最大, y 軸最小, y 軸最大]

  # dcpam の Rain
  
    gphys7       = gphys7 / 2.5e6
    gphys7.units = 'kg m-2 s-1'

    GGraph.line( gphys7.average(0).cut('time'=>time[ts]..time[te]).mean('time'), true,
                 'annot'=>false,
                 #'titl'=>'zonal averaged annual mean precipitation' )
                 'titl' =>'Annual zonal mean precipitation' )
  # NCEP の Rain
    GGraph.line( gphys8.average(0).cut('time'=>ts2..te2).mean('time'), false,
                 "type"=> 2, "index"=>2 )


DCL.grcls

#
# png 画像に変換して $tar ディレクトリへ格納
# ただし, ps 出力した場合のみ実行.

if win == 2 then

  com = sprintf("mv  dcl.ps ./" +tar+ "/"+file+ ".ps " ) # ps ファイルの名前を変えて,  figure へ移動
  system(com)

  com = sprintf("convert -rotate 90 " "./" +tar+ "/" +file+ ".ps " "./" +tar+ "/" +file+ ".png") # dcl.ps を png へコンバート
  system(com)

end


