#!/usr/bin/env ruby

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


def WindowOpenDump( aspect, dump, color)
  
  DCL::swpset('IWIDTH',  850 )
  DCL::swpset('IHEIGHT', aspect.to_i * 1.03 )
  unless ( color )
    DCL.sgscmn(5)
  end
  
  if ( 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])
  
  DCL::uzfact(0.75)
  DCL::slmgn(0.01, 0.01, 0.035, 0.01)
    
end


def WindowOpen( aspect, dump, color)
  
  DCL::swpset('IWIDTH',  850 )
  DCL::swpset('IHEIGHT', aspect.to_i * 1.03 )
  unless ( color )
    DCL.sgscmn(5)
  end
  
  DCL.sgpset('lfull',true) 
  DCL.sgpset('lfprop',true) 
  DCL.sglset('lclip',true)
  DCL.sgpset('lcntl', false) 
  DCL.udpset('lmsg',false)
  
  if ( dump )
    DCL.gropn( 2 )
  else
    DCL.gropn( 4 )
  end

  GGraph.set_fig('viewport'=>[0.1, 0.85, 0.1, aspect*1e-3])
  
  DCL::uzfact(0.75)
  DCL::slmgn(0.01, 0.01, 0.035, 0.01)
    
end


def DumpPS( time0, deltime, num )
  
  /(.*)\.nc/ =~ ARGV[0]
  info = $1
  convert = '~sugiyama/work/deepconv/arare4/tools/dcmodel-dclps2png.rb'
  order = Math::log10(time0+deltime*num).to_i + 1 
  fmt = "%0"+(order.to_i).to_s+"d"  

  system('/usr/bin/dclpsseparate dcl.ps')
  
  for i in 0..num
    time = sprintf(fmt, time0 + i * deltime)
    j = sprintf("%03d", i + 1)

    if ($OPT_lin)
      pngfile = info+"_lin_t"+time.to_s+".png"
    else
      pngfile = info+"_t"+time.to_s+".png"
    end
    output = convert+" dcl_"+j+".ps "+pngfile
    
    system( output )
  end
  
  system('/bin/rm dcl*ps')
end


def DumpPS2( time0, deltime, num, name )
  
  /(.*)\.nc/ =~ ARGV[0]
  info = $1
  convert = '~sugiyama/work/deepconv/arare4/tools/dcmodel-dclps2png.rb'
  order = Math::log10(time0+deltime*num).to_i + 1 
  fmt = "%0"+(order.to_i).to_s+"d"  

  system('/usr/bin/dclpsseparate dcl.ps')
  
  for i in 0..num
    time = sprintf(fmt, time0 + i * deltime)
    j = sprintf("%03d", i + 1)
    
    pngfile = info+"_"+name+"_t"+time.to_s+".png"
    output = convert+" dcl_"+j.to_s+".ps "+pngfile

    system( output )
  end

#  system('/bin/rm dcl*ps')
end


def DumpPS3( time0, deltime, num, names )
  
  /(.*)\.nc/ =~ ARGV[0]
  info = $1
  convert = '~sugiyama/work/deepconv/arare4/tools/dcmodel-dclps2png.rb'
  order = Math::log10(time0+deltime*num).to_i + 1 
  fmt = "%0"+(order.to_i).to_s+"d"  
  
  system('/usr/bin/dclpsseparate dcl.ps')

  for i in 0..num
    time = sprintf(fmt, time0 + i * deltime)
    j = sprintf("%03d", i + 1)
    
    pngfile = info+"_"+names[i]+".png"
    output = convert+" dcl_"+j+".ps "+pngfile
    
    system( output )
  end

  system('/bin/rm dcl*ps')
end


def DumpPNG( time0, deltime, num )
  
  /(.*)\.nc/ =~ ARGV[0]
  info = $1
  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 + 1 )

    if ($OPT_lin)
      pngfile = info+"_lin_t"+time.to_s+".png"
    else
      pngfile = info+"_t"+time.to_s+".png"
    end
    output = "mv dcl_"+j+".png "+pngfile
    p pngfile
    
    system( output )
  end
end


def DumpPNG2( time0, deltime, num, name )
  
  /(.*)\.nc/ =~ ARGV[0]
  info = $1
  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 + 1 )
    
    pngfile = info+"_"+name+"_t"+time.to_s+".png"
    output = "mv dcl_"+j+".png "+pngfile
    
    system( output )
  end
end


def DumpCompPNG( time0, deltime, num )
  
  /(.*)_.*-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"
    
#    if ($OPT_lin)
      convert1 = "convert -negate "+pngfile1+" "+pgmfile1
      convert2 = "convert -negate "+pngfile2+" "+pgmfile2
      convert3 = "convert -negate "+pngfile3+" "+pgmfile3
#    else
#      convert1 = "convert "+pngfile1+" "+pgmfile1
#      convert2 = "convert "+pngfile2+" "+pgmfile2
#      convert3 = "convert "+pngfile3+" "+pgmfile3
#    end
    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


def DumpCompPS( time0, deltime, num )
  /(.*)_.*-s-(.*)\.nc/ =~ ARGV[1]
  info = $1+"_"+$2
  convert = '~sugiyama/work/deepconv/arare4/tools/dcmodel-dclps2png.rb'
  order = Math::log10(time0+deltime*num).to_i + 1 
  fmt = "%0"+(order.to_i).to_s+"d"  

  system('/usr/bin/dclpsseparate dcl.ps')
  
  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

    output1 = convert+' dcl_'+j+'.ps dcl1.png'
    output2 = convert+' dcl_'+k+'.ps dcl2.png'
    output3 = convert+' dcl_'+l+'.ps dcl3.png'
    
    system( output1 )
    p output1
    system( output2 )
    system( output3 )
      
    convert1 = "convert -negate dcl1.png dcl1.pgm"
    convert2 = "convert -negate dcl2.png dcl2.pgm"
    convert3 = "convert -negate dcl3.png dcl3.pgm"
    convert4 = "convert -channel Red dcl1.pgm -channel Green dcl2.pgm -channel Blue dcl3.pgm -combine "+outputfile
    
    system( convert1 )
    p convert1
    system( convert2 )
    system( convert3 )
    system( convert4 )
    p convert4
    
  end
  system('/bin/rm dcl*.png dcl*.ps dcl*.pgm')
end


def DumpCompPS2( time0, deltime, num, name )
  /(.*)_.*-g\.nc/ =~ ARGV[1]
  info = $1+"_"+name
  convert = '~sugiyama/work/deepconv/arare4/tools/dcmodel-dclps2png.rb'
  order = Math::log10(time0+deltime*num).to_i + 1 
  fmt = "%0"+(order.to_i).to_s+"d"  

  system('/usr/bin/dclpsseparate dcl.ps')
  
  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

    output1 = convert+' dcl_'+j+'.ps dcl1.png'
    output2 = convert+' dcl_'+k+'.ps dcl2.png'
    output3 = convert+' dcl_'+l+'.ps dcl3.png'
    
    system( output1 )
    p output1
    system( output2 )
    system( output3 )
      
    convert1 = "convert -negate dcl1.png dcl1.pgm"
    convert2 = "convert -negate dcl2.png dcl2.pgm"
    convert3 = "convert -negate dcl3.png dcl3.pgm"
    convert4 = "convert -channel Red dcl1.pgm -channel Green dcl2.pgm -channel Blue dcl3.pgm -combine "+outputfile
    
    system( convert1 )
    p convert1
    system( convert2 )
    system( convert3 )
    system( convert4 )
    p convert4
    
  end
  system('/bin/rm dcl*.png dcl*.ps dcl*.pgm')
end


###
###引数処理
###
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],
                   ['--xlev',         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

    
###
### 初期化
###

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_xlev )
  xlev = $OPT_zlev.to_f
else
  xlev = 512000.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
###
var  = 'H2O-g'
file = ARGV[0]+"_"+var+".nc"
H2O = GPhys::IO.open(file, var).cut('z'=>0..zlev).cut("x"=>0..xlev)

var  = 'NH3-g'
file = ARGV[0]+"_"+var+".nc"
NH3 = GPhys::IO.open(file, var).cut('z'=>0..zlev).cut("x"=>0..xlev)

var  = 'H2S-g'
file = ARGV[0]+"_"+var+".nc"
H2S = GPhys::IO.open(file, var).cut('z'=>0..zlev).cut("x"=>0..xlev)

var  = 'H2O-gBasicZ'
file = ARGV[0]+"_BasicZ.nc"
H2OBasicZ = GPhys::IO.open(file, var).cut('z'=>0..zlev).cut("x"=>0..xlev)

var  = 'NH3-gBasicZ'
file = ARGV[0]+"_BasicZ.nc"
NH3BasicZ = GPhys::IO.open(file, var).cut('z'=>0..zlev).cut("x"=>0..xlev)

var  = 'H2S-gBasicZ'
file = ARGV[0]+"_BasicZ.nc"
H2SBasicZ = GPhys::IO.open(file, var).cut('z'=>0..zlev).cut("x"=>0..xlev)

H2OAll = H2O + H2OBasicZ

H2SAll = H2S + H2SBasicZ

NH3All = NH3 + NH3BasicZ


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

if ( $OPT_max )
  maximum = $OPT_max.to_f
else
  maximum = (H2OBasicZ.max * 1.1).to_f
end

if ( $OPT_min )
  minimum = $OPT_min.to_f
else
  minimum = 0.0
end



###
### $B?^$N:n@.(B
###
WindowOpen( aspect, $OPT_dump, true )

#DCL.sldiv('y',2,1)   
GGraph.set_axes('ytitle'=>'Height','xtitle'=>'')
GGraph.set_axes('xunits'=>'kg/kg','yunits'=>'m')


maximum = (H2OBasicZ.max * 1.1).to_f
GGraph.line( H2OBasicZ.mean('x'), 
             true, 
             'exchange'=>true,
             'title' => 'H2O-g Mixing Ratio',
             'annotate'=>infosw,
             'index'=> 15,
             'type' => 1, 
             'max'=> maximum, 
             'min'=> 0
               )
for i in 0..num
  time = time0 + i * deltime
  
  GGraph.line( H2OAll.cut('t'=>time).mean('x'),
               false, 
               'annotate'=>infosw,
               'exchange'=>true,
               'index'=> (i+1)*10+2, 
               'type' => 1
               )	
end		

maximum = (NH3BasicZ.max * 1.1).to_f
GGraph.line( NH3BasicZ.mean('x'), 
             true, 
             'exchange'=>true,
             'title' => 'NH3-g Mixing Ratio',
             'annotate'=>infosw,
             'index'=> 15,
             'type' => 1, 
             'max'=> maximum, 
             'min'=> 0
             )
for i in 0..num
  time = time0 + i * deltime
  
  GGraph.line( NH3All.cut('t'=>time).mean('x'), 
               false, 
               'annotate'=>infosw,
               'exchange'=>true,
               'index'=> (i+1)*10+2, 
               'type' => 1
               )	
end	

maximum = (H2SBasicZ.max * 1.1).to_f
GGraph.line( H2SBasicZ.mean('x'), 
             true, 
             'exchange'=>true,
             'title' => 'H2S-g Mixing Ratio',
             'annotate'=>infosw,
             'index'=> 15,
             'type' => 1, 
             'max'=> maximum, 
             'min'=> 0
             )
for i in 0..num
  time = time0 + i * deltime
  
  GGraph.line( H2SAll.cut('t'=>time).mean('x'), 
               false, 
               'annotate'=>infosw,
               'exchange'=>true,
               'index'=> (i+1)*10+2, 
               'type' => 1
               )	
end		


for i in 0..num
  time = time0 + i * deltime
  DCL::slsttl( "TIME = "+time.to_s,  "B", 0.0, 1.0, 0.02, 1)

  maximum = (H2OBasicZ.max * 1.1).to_f
  GGraph.set_fig( 'itr'=> 1)
  GGraph.line( H2OAll.cut('t'=>time).mean('x'), 
               true, 
               'exchange'=>true,
               'title' => 'All Gas Mixing Ratio',
               'annotate'=>infosw,
               'max'=> maximum,
               'min'=> minimum,
               'index'=> 25,
               'type' => 1 
               )
  GGraph.line( H2OBasicZ.mean('x'), 
               false, 
               'exchange'=>true,
               'annotate'=>infosw,
               'index'=> 21,
               'type' => 3 
               )
  GGraph.line( NH3All.cut('t'=>time).mean('x'), 
               false, 
               'exchange'=>true,
               'annotate'=>infosw,
               'index'=> 45,
               'type' => 1 
               )
  GGraph.line( NH3BasicZ.mean('x'), 
               false, 
               'exchange'=>true,
               'annotate'=>infosw,
               'index'=> 41,
               'type' => 3 
               )
  GGraph.line( H2SAll.cut('t'=>time).mean('x'), 
               false, 
               'exchange'=>true,
               'annotate'=>infosw,
               'index'=> 35,
               'type' => 1 
               )
  GGraph.line( H2SBasicZ.mean('x'), 
               false, 
               'exchange'=>true,
               'annotate'=>infosw,
               'index'=> 31,
               'type' => 3 
               )
end


DCL.grcls


if ( $OPT_dump )

  info = ARGV[0]+"_gas-mean"
  convert = '~sugiyama/work/deepconv/arare4/tools/dcmodel-dclps2png.rb'	  
  order = Math::log10(time0+deltime*num).to_i + 1 
  fmt = "%0"+(order.to_i).to_s+"d"  
  
  j = sprintf("%03d", 1)
  pngfile  = info+"_H2O-g.png"
  output1 = 'psselect 1 dcl.ps > dcl2.ps'
  output2 = convert+" dcl2.ps "+pngfile
  system( output1 )
  system( output2 )

  j = sprintf("%03d", 2)
  pngfile  = info+"_NH3-g.png"
  output1 = 'psselect 2 dcl.ps > dcl2.ps'
  output2 = convert+" dcl2.ps "+pngfile
  system( output1 )
  system( output2 )

  j = sprintf("%03d", 3)
  pngfile  = info+"_H2S-g.png"
  output1 = 'psselect 3 dcl.ps > dcl2.ps'
  output2 = convert+" dcl2.ps "+pngfile
  system( output1 )
  system( output2 )

  for i in 0..num
    time = sprintf(fmt, time0 + i * deltime)
    k = i + 4
    
    pngfile  = info+"_t"+time.to_s+".png"
    output1 = 'psselect '+k.to_s+' dcl.ps > dcl2.ps'
    output2 = convert+' dcl2.ps '+pngfile
    system( output1 )
    system( output2 )
  end	

end

