#!/usr/bin/env ruby

require "numru/netcdf"
require "numru/dcl"
require "numru/ggraph"
 
require "getoptlong"
include NumRu

###
###$B0z?t=hM}(B
###
parser = GetoptLong.new
parser.set_options(
                   ###    global option   ###
                   ['--delt',         GetoptLong::REQUIRED_ARGUMENT],
                   ['--time',         GetoptLong::REQUIRED_ARGUMENT],
                   ['--num',          GetoptLong::REQUIRED_ARGUMENT],
                   ['--max',          GetoptLong::REQUIRED_ARGUMENT],
                   ['--min',          GetoptLong::REQUIRED_ARGUMENT],
                   ['--zlev',         GetoptLong::REQUIRED_ARGUMENT],
                   ['--spc',          GetoptLong::REQUIRED_ARGUMENT],
                   ['--aspect',       GetoptLong::REQUIRED_ARGUMENT],
                   ['--noinfo',       GetoptLong::NO_ARGUMENT],
                   ['--lin',          GetoptLong::NO_ARGUMENT],
                   ['--dump',         GetoptLong::NO_ARGUMENT]
                   )
begin
  parser.each_option do |name, arg|
    eval "$OPT_#{name.sub(/^--/, '').gsub(/-/, '_')} = '#{arg}'" 
  end
rescue
  exit(1)
end


###
### $B=i4|2=(B
###

deltime = $OPT_delt.to_f
time0   = $OPT_time.to_f
num     = $OPT_num.to_i

if ( $OPT_zlev )
  zlev = $OPT_zlev.to_f
else
  zlev = 300000.0
end

if ( $OPT_noinfo )
  infosw = false
else
  infosw = true
end

if ( $OPT_aspect )
  aspect = 850 / ($OPT_aspect).to_f
else
  aspect = 500
end


###
### $BCM$N<hF@(B
###

/.*_(.*)\.nc/ =~ ARGV[0]
H2O   = GPhys::IO.open(ARGV[0], $1).cut('z'=>0..zlev)

/.*_(.*)\.nc/ =~ ARGV[1]
NH4SH = GPhys::IO.open(ARGV[1], $1).cut('z'=>0..zlev)

/.*_(.*)\.nc/ =~ ARGV[2]
NH3   = GPhys::IO.open(ARGV[2], $1).cut('z'=>0..zlev)

 
if ( $OPT_max )
  maximum = $OPT_max.to_f
else
  maximum = 1.5e-3
end

if ( $OPT_min )
  minimum = $OPT_min.to_f
else
  minimum = 1.5e-5
end




###
### $B?^$N:n@.(B
###

DCL::swpset('IWIDTH',  850 )
DCL::swpset('IHEIGHT', aspect.to_i * 1.03)
#DCL.sgscmn(3)
DCL.sgscmn(5)

if ( $OPT_dump )
  DCL::swpset('LDUMP', true)
  DCL::swpset('LWAIT',false)
  DCL::swpset('LWAIT1',false)
  DCL::swpset('LALT',true)
  DCL::swpset('IPOSX', 50)
  DCL::swpset('IPOSY',50)
end

DCL.sgpset('lfull',true) 
DCL.sgpset('lfprop',true) 
DCL.sglset('lclip',true)
DCL.sgpset('lcntl', false) 
DCL.udpset('lmsg',false)

DCL.gropn( 4 )

GGraph.set_fig('viewport'=>[0.1, 0.85, 0.1, aspect*1e-3])

p aspect, aspect*1e-3

DCL.uzfact(0.8)
GGraph.set_axes('ytitle'=>'Height','xtitle'=>'')
GGraph.set_axes('xunits'=>'km','yunits'=>'km')
GGraph.set_fig( 'itr'=> 1)
DCL::slmgn(0.01, 0.01, 0.035, 0.01)

for i in 0..num
  time = time0 + i * deltime
  p time
  DCL::slsttl( "TIME = "+time.to_s,  'B', 0.0, -1.0, 0.02, 1)
  
  # H2O $B1+:.9gHf(B
  if ($OPT_lin) 
    GGraph.tone( H2O.cut('t'=>time), 
                 true, 
                 'title' => 'Rain Mixing Ratio [kg/kg]', 
                 'annotate' => infosw,
                 'nlev'=> 50 ,
                 'max' => maximum, 
                 'min' => minimum
                 )	
    GGraph.color_bar
  else
    GGraph.tone( H2O.cut('t'=>time), 
                 true, 
                 'title' => 'Rain Mixing Ratio [kg/kg]', 
                 'annotate' => infosw,
                 'levels'=>[1e-8,2e-8,5e-8,1e-7,2e-7,5e-7,1e-6,2e-6,5e-6,1e-5,2e-5,5e-5,1e-4,2e-4,5e-4,1e-3,2e-3,5e-3,1e-2]
                 )	
    GGraph.color_bar( 'constwidth' => true )
  end

  # NH4SH $B1+:.9gHf(B
  if ($OPT_lin) 
    GGraph.tone( NH4SH.cut('t'=>time), 
                 true, 
                 'title' => 'Rain Mixing Ratio [kg/kg]', 
                 'annotate' => infosw,
                 'nlev'=> 50,
                 'max' => maximum, 
                 'min' => minimum
                 )	
    GGraph.color_bar
  else  
    GGraph.tone( NH4SH.cut('t'=>time), 
                 true, 
                 'title' => 'Rain Mixing Ratio [kg/kg]', 
                 'annotate' => infosw,
                 'levels'=>[1e-8,2e-8,5e-8,1e-7,2e-7,5e-7,1e-6,2e-6,5e-6,1e-5,2e-5,5e-5,1e-4,2e-4,5e-4,1e-3,2e-3,5e-3,1e-2]
                 )	
    GGraph.color_bar( 'constwidth' => true )
  end

  # NH3 $B1+:.9gHf(B
  if ($OPT_lin) 
    GGraph.tone( NH3.cut('t'=>time), 
                 true, 
                 'title' => 'Rain Mixing Ratio [kg/kg]', 
                 'annotate' => infosw,
                 'nlev'=> 50,
                 'max' => maximum,
                 'min' => minimum
                 )	
    GGraph.color_bar
  else
    GGraph.tone( NH3.cut('t'=>time), 
                 true, 
                 'title' => 'Rain Mixing Ratio [kg/kg]', 
                 'annotate' => infosw,
                 'levels'=>[1e-8,2e-8,5e-8,1e-7,2e-7,5e-7,1e-6,2e-6,5e-6,1e-5,2e-5,5e-5,1e-4,2e-4,5e-4,1e-3,2e-3,5e-3,1e-2]
                 )	
    GGraph.color_bar( 'constwidth' => true )
  end
#  GGraph.color_bar( 'constwidth' => true, 
#                    'landscape' => true 
#                    )


end

DCL.grcls


if ( $OPT_dump )
  /(.*)_.*-s-(.*)\.nc/ =~ ARGV[1]
  info = $1+"_"+$2
  order = Math::log10(time0+deltime*num).to_i + 1 
  fmt = "%0"+(order.to_i).to_s+"d"  

  for i in 0..num
    time = sprintf(fmt, time0 + i * deltime)
    j = sprintf("%03d", i * 3 + 1 )
    k = sprintf("%03d", i * 3 + 2 )
    l = sprintf("%03d", i * 3 + 3 )

    if ($OPT_lin)
      outputfile = info+"_lin_t"+time.to_s+".png"  
    else
      outputfile = info+"_t"+time.to_s+".png"  
    end
    
    pngfile1 = "dcl_"+j+".png"
    pngfile2 = "dcl_"+k+".png"
    pngfile3 = "dcl_"+l+".png"

    pgmfile1 = "dcl_"+j+".pgm"
    pgmfile2 = "dcl_"+k+".pgm"
    pgmfile3 = "dcl_"+l+".pgm"

    convert1 = "convert "+pngfile1+" "+pgmfile1
    convert2 = "convert "+pngfile2+" "+pgmfile2
    convert3 = "convert "+pngfile3+" "+pgmfile3
    convert4 = "convert -channel Red "+pgmfile1+" -channel Green "+pgmfile2+" -channel Blue "+pgmfile3+" -combine "+outputfile
    del = "rm "+ pngfile1+" "+pngfile2+" "+pngfile3+" "+pgmfile1+" "+pgmfile2+" "+pgmfile3
    
    system( convert1 )
    system( convert2 )
    system( convert3 )
    system( convert4 )
    p convert4
    
#    system( del )
  end
end
