#!/usr/bin/env ruby
=begin
= mkfig.rb -- make figure for 20 years mean hgt(geppotential height)
=end

require "getopts"        # for option_parse
require "numru/netcdf_miss"
require "numru/ggraph"
require "libgphys-n"
include NumRu

def make_mean_gphys(season,year)
  phys = "HGT"
  src  = "NCEP"
  ary = []
  y1 = year[0]; y2 = year[1]
  path = "../../#{phys}.#{src}/"
  season.each do |m|
    y1.upto(y2) do |y|
      fn = path + "#{phys}.#{y}.#{src}/#{phys}_#{y}-#{m}_#{src}.nc"
      ary << fn
    end
  end
  return mean_gphys(ary, phys.downcase).cut('level'=>500)
end

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

GGraph.set_linear_contour_options( 'int'=>100)
level = NArray.int(8).indgen!(0, 200)+4800
level[0] = 0 
pattern = NArray[30999, 35999, 40999, 55999, 70999, 75999, 85999]


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

# 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)     # 

## four in one

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'=>itr)
GGraph.set_axes('xunits'=>'','yunits'=>'','xtitle'=>'') 
DCL.uzpset('labelxb',false)
GGraph.tone( mam, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
GGraph.contour( mam, false , 'annot'=>false, 'titl'=>'' )
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'=>itr)
GGraph.set_axes('ytitle'=>'')
DCL.uzpset('labelyl',false)
GGraph.tone( jja, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
GGraph.contour( jja, false , 'annot'=>false, 'titl'=>'' )
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','Summer(JJA)',-1) ; DCL.uzpset('pad1',0.7)

GGraph.set_fig('viewport'=>vpt10, 'new_frame'=>false, 'itr'=>itr)
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.contour( son, false , 'annot'=>false, 'titl'=>'' )
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'=>itr)
GGraph.set_axes('ytitle'=>'')
DCL.uzpset('labelyl',false)
GGraph.tone( djf, true , 'annot'=>false, 'titl'=>'', 'pat'=>pattern, 'lev'=>level )
GGraph.contour( djf, false , 'annot'=>false, 'titl'=>'' )
DCL.uzpset('pad1',0.2) ; DCL.uxsttl('t','Winter(DJF)',-1) ; DCL.uzpset('pad1',0.7)


DCL::sgtxzv(0.5,vpt00[3]+0.028,"50 years mean HGT (1948-2000)",
	        1.15*DCL.uzpget('rsizec2'),0,0,3)
DCL::sgtxzv(0.88,vpt00[3]+0.01,"(units:#{djf.data.units.to_s})",
	        0.9*DCL.uzpget('rsizec2'),0,0,3)
#DCL::sgtxzv(0.5,vpt10[2]-0.05,"CONTOUR INTERVAL : 8.000E+09",
#	        0.85*DCL.uzpget('rsizec2'),0,0,3)

DCL.grcls

=begin

## each picture
DCL.gropn(1)
DCL.swlset('lsep', true)     # 
DCL.sgpset('lcntl', false)   # 
DCL.sgpset('lfull',true)     # 
DCL.sgpset('lcorner',false)  # 
DCL.sgpset('lfprop',true)    # 

vpt = NArray[0.15,0.85,0.23,0.58]
GGraph.set_fig('viewport'=>vpt, 'itr'=>itr, 'new_frame'=>true)

#### annual mean
GGraph.tone( ann, true , 'annot'=>false, 'pat'=>pattern, 'lev'=>level )
GGraph.contour( ann, false , 'annot'=>false, 'titl'=>'50 years annual mean HGT(1948-2000)')

#### djf
GGraph.tone( djf, true , 'annot'=>false, 'pat'=>pattern, 'lev'=>level )
GGraph.contour( djf, false , 'annot'=>false, 'titl'=>'50 years winter(DJF) mean HGT(1948-2000)')

#### mam
GGraph.tone( mam, true , 'annot'=>false, 'pat'=>pattern, 'lev'=>level )
GGraph.contour( mam, false , 'annot'=>false, 'titl'=>'50 years Spring(MAM) mean HGT(1948-2000)')

#### jja
GGraph.tone( jja, true , 'annot'=>false, 'pat'=>pattern, 'lev'=>level )
GGraph.contour( jja, false , 'annot'=>false, 'titl'=>'50 years Summer(JJA) mean HGT(1948-2000)')

#### son
GGraph.tone( son, true , 'annot'=>false, 'pat'=>pattern, 'lev'=>level )
GGraph.contour( son, false , 'annot'=>false, 'titl'=>'50 years Fall(SON) mean HGT(1948-2000)')

DCL.grcls
=end
