#!/usr/bin/env ruby
=begin
= mkfig-allinone.rb

== 概要

EP フラックスおよび発散の各季節平均値を一枚の PS ファイルにプロ
ットした図を作成する.

== 使用方法

下記の ｢設定｣ の項目を適宜編集し実行. ファイル名や実行は Makefile
にて設定すること.

=end

#############              設定              ##################

# 平均期間. [はじめの年, 終りの年] の形で指定
year = [1980, 2001]

# 絵のタイトル
fig_title = "20 years mean EP flux (1980-2001)"

# ep flux のコンター間隔およびトーンパターン
level = NArray[-1.0e8, -5.0e1, -1.0e1, 0, 1.0e1, 5.0e1, 1.0e8]*1.0e-6
pattern = NArray[30999, 35999, 40999, 55999, 75999, 85999]
# ベクトル矢羽の V 座標における単位長さ
xfact = 4e-11; yfact=1.5e-8 

# 単位名. 図の上部に表示する. 単位自体は NetCDF の属性を元に GPhys を経由して取得.
unit_name = "divF units:"

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

## -- method definition --
def make_mean_gphys(season,year,varname,physname)
  ary = []
  y1 = year[0]; y2 = year[1]
  path = "../../NCEP/#{physname}.NCEP/"
  season.each do |m|
    y1.upto(y2) do |y|
      fn = path + "#{physname}.#{y}.NCEP/#{physname}_#{y}-#{m}_NCEP.nc"
      ary << fn
    end
  end
  return mean_gphys(ary, varname) # bin/libgphys-n.rb 内で定義.
end


## -- require libraries -- 
require "getopts"        
require "numru/ggraph"
require "colorbar"
require "libgphys-n"
include NumRu

## -- file config -- 
physname = "EPFLX"
var2 =  "epflx_y"
var3 =  "epflx_z"
var1 =  "epflx_div"

winter = ["12", "01", "02"];  spring = ["03", "04", "05"]
summer = ["06", "07", "08"];  fall = ["09", "10", "11"]

## other figure factor
# xfact = 1e-11; yfact=2.5e-8 

djf = make_mean_gphys(winter,year,var1,physname)
mam = make_mean_gphys(spring,year,var1,physname)
jja = make_mean_gphys(summer,year,var1,physname)
son = make_mean_gphys(fall,year,var1,physname)

djf1 = make_mean_gphys(winter,year,var2,physname)
mam1 = make_mean_gphys(spring,year,var2,physname)
jja1 = make_mean_gphys(summer,year,var2,physname)
son1 = make_mean_gphys(fall,year,var2,physname)

djf2 = make_mean_gphys(winter,year,var3,physname)
mam2 = make_mean_gphys(spring,year,var3,physname)
jja2 = make_mean_gphys(summer,year,var3,physname)
son2 = make_mean_gphys(fall,year,var3,physname)


# set User Path for dcldatabase
DCL.glcset('DUPATH','/home/daktu32/.dcldir/')     


DCL.gropn(2)
DCL.sgpset('lcntl', false)   # 
DCL.sgpset('lfull',true)     # 
DCL.sgpset('lcorner',false)  # 
DCL.uzfact(0.35)             # 
DCL.sgpset('lfprop',true)    # 
DCL.udpset('lmsg',false)     # 

vpt = NArray[0.05, 0.45, 0.05, 0.25]             # 
vpt00 = ( vpt + ([0.050]*2 + [0.32]*2) ).to_a    # 
vpt01 = ( vpt + ([0.474]*2 + [0.32]*2) ).to_a    # 
vpt10 = ( vpt + ([0.050]*2 + [0.10]*2) ).to_a    # 
vpt11 = ( vpt + ([0.474]*2 + [0.10]*2) ).to_a    # 

before = DCLExt.gl_set_params('lmiss'=>true,'rmiss'=>djf.data.get_att("missing_value")[0])

GGraph.set_fig('viewport'=>vpt00, 'itr'=>2)
GGraph.set_axes('xunits'=>'','yunits'=>'','xtitle'=>'') 
DCL.uzpset('labelxb',false)
GGraph.tone( mam, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
GGraph::vector_for_dcchart(mam1, mam2, false,
				  'xfact'=>xfact, 'yfact'=>yfact, 
                                  'unit'=>false, 'annot'=>false, 'itr'=>2
				    )
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','Spring(MAM)', -1) ; DCL.uzpset('pad1',0.7)

GGraph.set_fig('viewport'=>vpt01, 'new_frame'=>false, 'itr'=>2)
GGraph.set_axes('ytitle'=>'')
DCL.uzpset('labelyl',false)
GGraph.tone( jja, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','Summer(JJA)',-1) ; DCL.uzpset('pad1',0.7)
GGraph::vector_for_dcchart(jja1, jja2, false,
				  'xfact'=>xfact, 'yfact'=>yfact, 
                                  'unit'=>false, 'annot'=>false, 'itr'=>2
				    )

GGraph.set_fig('viewport'=>vpt10, 'new_frame'=>false, 'itr'=>2)
GGraph.set_axes('ytitle'=>nil,'xtitle'=>nil)
DCL.uzpset('labelyl',true); DCL.uzpset('labelxb',true)
GGraph.tone( son, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
GGraph::vector_for_dcchart(son1, son2, false,
				  'xfact'=>xfact, 'yfact'=>yfact, 
                                  'unit'=>false, 'annot'=>false, 'itr'=>2
				    )
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','Autumn(SON)',-1) ; DCL.uzpset('pad1',0.7)

GGraph.set_fig('viewport'=>vpt11, 'new_frame'=>false,  'itr'=>2)
GGraph.set_axes('ytitle'=>'')
DCL.uzpset('labelyl',false)
GGraph.tone( djf, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
GGraph::vector_for_dcchart(djf1, djf2, false,
				  'xfact'=>xfact, 'yfact'=>yfact, 
                                  'unit'=>true, 'annot'=>false, 'itr'=>2
				    )
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','Winter(DJF)',-1) ; DCL.uzpset('pad1',0.7)

## -- colorbar --
DCL::Util::color_bar({
		       "levels"=>level, 
		       "colors"=>pattern,
		       "eqlev"=>true,
		       "nobound"=>0,
		       "tick1"=>10,"tick2"=>1,
		       "vx0"=>0.3,
		       "vxlength"=>0.4,
		       "vy0"=>0.08,
		       "vylength"=>0.02 
		     })

## -- extra information --

# 絵のタイトル
DCL::sgtxzv(0.5,vpt00[3]+0.028, fig_title,
	        1.15*DCL.uzpget('rsizec2'),0,0,3)
# 単位
DCL::sgtxzv(0.85,vpt00[3]+0.01, "(#{unit_name}: #{djf.data.units.to_s})",
	        0.9*DCL.uzpget('rsizec2'),0,0,3)


DCL.grcls

