# -*- coding: euc-jp -*-
#
# 2011/07/13 x 軸の設定を 30 度間隔に変更
# 2011/07/11 切り出し時刻を編集, 描画するデータを引数で与えるように設定
# 2011/07/08 軸の設定追加. window の設定を引数で与えるよう変更. 画像格納作業を追加
# 2011/07/07 NECP と OLRA を重ね書きするように編集
# 2011/07/05 ulwrf の部分を修正
# 2011/07/04 drawgm.rb を元に作成
#

require "numru/ggraph"
include NumRu
#########################################################################

def annual_mean( iyrs, iyre, gphys, data )

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

 if data == "dcpam" then
 
   ts = (iyrs - 1) * 365 * 4 + 1
   te = iyre * 365 * 4
 
 elsif data == "NCEP"   

   ts = (iyrs - 1948) * 12 
   te = (iyre - 1948 + 1 ) * 12 - 1

 end
   p time[ts], time[te]

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

 return gphystmp

end
#########################################################################

dirD  = ARGV[0]
dirN  = ARGV[1]

# 描画に使う年の指定
iyrsD = ARGV[4].to_i
iyreD = ARGV[5].to_i
iyrsN = ARGV[6].to_i
iyreN = ARGV[7].to_i

# 実験名
exp = ARGV[8].to_s


# 変数の設定と gphys の準備

## dcpam 計算結果用

vnameD = ARGV[2]
gphysD = GPhys::NetCDF_IO.open(dirD+'/'+vnameD+".nc", vnameD)

## NCEP データ用

if exp == "Earth" then
if vnameD == "OLRA" || vnameD == "OSRA"  # 放射フラックスの場合

   vnameN1 = ARGV[3]
   vnameN1_longname = vnameN1 + "top"

   vnameN2 = 'uswrf'
   vnameN2_longname = vnameN2 + "top"

   gphysN1 = GPhys::NetCDF_IO.open(dirN+'/'+vnameN1_longname + ".mon.mean"+".nc", vnameN1)
   gphysN2 = GPhys::NetCDF_IO.open(dirN+'/'+vnameN2_longname + ".mon.mean"+".nc", vnameN2)

  if vnameD ==  "OSRA"

      gphysD = - gphysD
      gphysN = gphysN1 - gphysN2
   else
      gphysD = gphysD
      gphysN = gphysN1
   end

else                                     # 放射フラックス以外の場合

   vnameN = ARGV[3]
   gphysN = GPhys::NetCDF_IO.open(dirN+'/'+vnameN+ ".mon.mean"+".nc", vnameN)

end

end

#
# DCLのオープンと設定 
#
DCL.gropn(1)
#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.1,0.5]  # 描画領域の設定
GGraph.set_axes('xlabelint'=>30)

#
# 物理量の年平均, 東西平均
#

data = "dcpam"
gphystmpD = annual_mean( iyrsD, iyreD, gphysD, data ).mean('lon')

if exp == "Earth" then

  data = "NCEP"
  gphystmpN = annual_mean( iyrsN, iyreN, gphysN, data ).mean('lon')

end

if vnameD == '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 vnameD == 'Rain'

  # second panel

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

  gphystmpD       = gphystmpD / 2.5e6
  gphystmpD.units = 'kg m-2 s-1'
  GGraph.line(gphystmpD,true,
              'annot'=>false,
              'title' => ' ' )
              #'title'=>gphystmpD.get_att('long_name')+' ('+exp.to_s+')' )
              #'title'=>'Annual zonal mean Pricipitation')

  if exp == "Earth" then
    GGraph.line( gphystmpN,false,"type"=> 2,"index"=>2 )
  end

#  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 vnameD == '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 vnameD == '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 vnameD == 'OLRA'

  # forth panel

  GGraph.set_fig('window'=>[-90,90,100,300])     # 軸の最大/最小値を設定. [x 軸最小, x 軸最大, y 軸最小, y 軸最大]
  GGraph.line( gphystmpD,true,
               'annot'=>false,
               'title'=>' ' )

  if exp == "Earth" then
    GGraph.line( gphystmpN, false, "type"=> 2,"index"=>2 )
  end

#  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 vnameD == 'OSRA'

  # forth panel

   gphystmpD = - gphystmpD

   GGraph.set_fig('window'=>[-90,90,0,400])     # 軸の最大/最小値を設定. [x 軸最小, x 軸最大, y 軸最小, y 軸最大]
   GGraph.line( gphystmpD,true,
                'annot'=>false,
                'title'=>' ' )

   if exp == "Earth" then
     GGraph.line( gphystmpN, false, "type"=> 2,"index"=>2 )
   end

#  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

#DCL::uxmttl('T', ' ', 1.0)
#DCL::uxmttl('T', ' ', 1.0)
DCL::uxmttl('T', '('+exp.to_s+')', 1.0)

DCL.grcls
