#
# dcpam の hoge 年目の値について東西平均・月平均を描画する ctl スクリプト
#
# useage: このスクリプトを実行する.
#         (or draw_ctl.rb でも実行できるが, xy 分布も描画されるので数時間かかる)
#
# * 2011/12/22 iyr の指定の方法を修正. eps ファイルの作成を追記
# * 2011/12/05 for ループを修正 
# * 2011/12/04 画像変換を追記
#

require "numru/ggraph"
include NumRu

dir = '..'

# 実験の指定
# * スクリプト実行時に引数で与える.
#   * 引数なし(Earth 実験), 00, 23.44, 60, 90 のいずれか

exp = ARGV[0]

if exp == nil then

  exp = "Earth"

end
p 'exp: ' + exp.to_s 


if exp == "Earth" then

  iyrs = 3
  iyre = 3
 
elsif exp == "00" || exp == "23.44" || exp == "60" || exp == "90" then

  iyrs = 10
  iyre = 10
  
end

#p iyrs, iyre
 
gphys = GPhys::NetCDF_IO.open( dir + '/'+'Ps'+'.nc', 'Ps' )
tm = gphys.shape[2] # number of elements for time      dimension

num = 1 * 10
#num = 4 * 10

DataNumOfDay = 4
DaysOfMonth  = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

daysOfYear = 0
DaysOfMonth.each do |i|
  daysOfYear += i
end
#p daysOfYear

figdir = 'figs_zm_lin_mon'
Dir::mkdir( figdir )
Dir::mkdir( figdir+'/eps' )


for iyr in iyrs..iyre
  for imon in 1..12
    t = daysOfYear*(iyr-1)

    for i in 1..imon-1
      t = t + DaysOfMonth[i-1]
    end

    ts = t
    te = t + DaysOfMonth[imon-1]

    ts = ts * DataNumOfDay
    te = te * DataNumOfDay - 1

    if ts > tm then
      p 'ts is in appropriate'
    end

    if te > tm then
      p 'te is in appropriate'
    end

    p 'Year: ' + iyr.to_s + ', Month: ' + imon.to_s + ', t = ' + ts.to_s + ' to '+ te.to_s

    case imon
    when 1
      title = 'JAN.'
    when 2
      title = 'FEB.'
    when 3
      title = 'MAR.'
    when 4
      title = 'APR.'
    when 5
      title = 'MAY'
    when 6
      title = 'JUN.'
    when 7
      title = 'JUL.'
    when 8
      title = 'AUG'
    when 9
      title = 'SEP.'
    when 10
      title = 'OCT.'
    when 11
      title = 'NOV.'
    when 12
      title = 'DEC.'
    end

    system('ruby drawzm_lin.rb ' + dir + ' ' + ts.to_s + ' ' + te.to_s + ' ' + title+ ' '+ exp.to_s )
 
# gif ファイルの作成

    com = sprintf( "convert -rotate 90 dcl.ps dcl_mon%0#{2}d.gif" , imon )
    system(com)

# eps ファイルの作成

    com = sprintf( "convert -rotate 90 dcl.ps dcl_mon%0#{2}d.eps" , imon )
    system(com)

    if imon == 12 then

       com = sprintf( "gifsicle *gif --loopcount=20 >"+"clim_zm_mon.gif" )
       system(com)

       com = sprintf( "mv *.gif "+figdir )
       system(com)

       com = sprintf( "mv *.eps "+figdir+"/eps/" )
       system(com)

    end
  end
end
