#!/usr/bin/ruby
# -*- coding: utf-8 -*-
#
# =begin
# == 概要
#
# 夜半球の横軸に温度, 縦軸に鉛直座標をとった図を作成する
#
# == 更新履歴
#
# * 2015/01/26 増田 和孝 新規作成
#=end

require "numru/ggraph"
require 'fileutils'
include NumRu

DATA_HOME_LOCAL = '/GFD_Dennou_Work11/masuda/'
rplanets=[0.5,1.0,1.25,1.46,2.0,4.0,6.0,8.0,12.0,16.0]
#rplanet=1.0
na_rplanets=NArray.to_na(rplanets)
nrplanet = rplanets.size
TMPFILE = "dcl.ps"

TIME_START = 725
TIME_END = 1095

VAL_MAX = 180
VAL_MIN = 0

WSN = 2

#YNAME = 'Temperature'
YNAME = 'western wind'
#vars = 'OLR'
#for v in vars
#  p var = v['name']
var = 'U'
  index=[15,25,35,45,55,65,75.85,95,105,155]
  for i in 0..nrplanet-1
    p rplanet = na_rplanets[i]
    file = var + '.nc'
    dir = 'E_T42L26_sync_R' + rplanet.to_s
    
    path = File.join(DATA_HOME_LOCAL,dir,file)
    gphys = GPhys::IO.open(path,var)
    #  p gphys
    na_lon = GPhys::IO.open(path, 'lon').val
    na_lat = GPhys::IO.open(path, 'lat').val
    na_sig = GPhys::IO.open(path,'sig').val
    na_lat_weight = GPhys::IO.open(path, 'lat_weight').val
    na_sig_weight = GPhys::IO.open(path, 'sig_weight').val
    nlon = na_lon.size
    nlat = na_lat.size
    nsig = na_sig.size

    # time average
    gphys = gphys.cut('time'=>TIME_START..TIME_END).mean('time')
  
    # Horizontal Mean
#    gphys = gphys * na_lat_weight.reshape(1, nlat) / na_lat_weight.sum
    # sigma Mean
    
    # all, west, east : NArray
#    gphys = gphys.sum('lat').mean('lon')
    gphys = gphys.cut('lat'=>0,'lon'=>180..360).mean('lon')
    #  p gphys
    
    nindex = index[i]
    legend = 'R' + rplanet.to_s
    if i == 0 then
      DCL.gropn(WSN)
      DCL.sgpset('lcntl',false)
      GGraph.set_fig( 'itr'=> 1, 'viewport'=>[0.2,0.8,0.3,0.6] )
#      GGraph.line( gphys, true,'exchange'=>true,'max'=>VAL_MAX,'min'=>VAL_MIN,'annotate'=>false,'index'=>nindex)
      GGraph.line( gphys, true,'exchange'=>true,'max'=>VAL_MAX,'min'=>VAL_MIN,'annotate'=>false,'index'=>nindex)
    else
      GGraph.line( gphys, false,'exchange'=>true,'index'=>nindex)
    end
  end
  
  psfn = 'RPlanets_night_lonmean-lat0' + var +'.ps'
  epsfn = 'RPlanets_night_lonmean-lat0' + var +'.eps'
  DCL.grcls
  if WSN == 2 then
#    if File.exist?(epsfn) then
#      puts("MESSAGE: #{epsfn} is exist. skip making eps")
#    else
      if File.exist?(TMPFILE) then
        FileUtils.mv(TMPFILE,psfn)
        puts("MESSAGE: after processing ... #{epsfn} will be generate.")
        `dclpsrmcm #{psfn} | dclpsrot | dclpsmargin > #{epsfn}`
        File.delete(psfn)
        
        exit
      end
#    end
  end
#end
