#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

require "getoptlong"        # for option_parse
require 'date'
require "numru/dcl"
require 'csv'
require 'yaml'
require 'pp'
include NumRu

###
### parse options
###

parser = GetoptLong.new
parser.set_options(
                   ###    global option   ###
                   ['--yml',          GetoptLong::REQUIRED_ARGUMENT],
                   ['--file',         GetoptLong::REQUIRED_ARGUMENT]
                   )
begin
  parser.each_option do |name, arg|
    eval "$OPT_#{name.sub(/^--/, '').gsub(/-/, '_')} = '#{arg}'"  # strage option value to $OPT_val
  end
rescue
  help
  raise
end

###
### dcl の全体設定
###

DCL::swpset('IWIDTH',  800)
DCL::swpset('IHEIGHT', 800)

DCL::swlset("lwnd",false)
DCL::gropn(4)                # ウィンドウをオープン   
#DCL.sgpset('lcntl', false)  # 制御文字を解釈しない
DCL.sgpset('lfull',true)     # 全画面表示
DCL.sgpset('lcorner',false)  # コーナーマークを書かない
DCL.uzfact(0.7)              # 座標軸の文字列サイズを定数倍
DCL.sgpset('lfprop',true)    # プロポーショナルフォントを使う
DCL.udpset('lmsg',false)     # コンター間隔非表示
DCL.sgpset('lclip',true)     # 枠からはみ出した分を描画しない.
DCL.gllset("lmiss",true)     # 欠損値処理

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

maxmin = YAML.load_file( $OPT_yml )
pp maxmin

num1 = 0
comms = Array.new

system("mkdir figs")

###
### ディレクトリ内のファイルをオープン => 描画
###

if ( $OPT_file )
  files = $OPT_file 
else
  files = "./[A-Z]*.txt"
end

Dir.glob( files ).sort.each{ |infile|

  title= Array.new
  axis = Array.new
  data = Array.new
  i = -1

  ##
  ## ファイルから数値を抜き出す
  ##

  orig = open( infile )
  while line = orig.gets
    line.chomp!
    
    if (/^\"(.*)\"$/ =~ line)
      i = i + 1
      title.push( $1 )
      data[i] = Array.new
      
    elsif (/^LT/ =~ line)
      axis = line.split(" ")
      
    elsif (/^\d+/ =~ line)
      data0 = line.split(" ")
      data[i].push( data0 )
    end
  end
  orig.close  
  
  ##
  ## 描画する
  ##  

  data.size.times{|i|  

    lt    = Array.new
    data1 = Array.new
    data2 = Array.new
    data3 = Array.new
    data4 = Array.new

    data[i].size.times{|j| 
      
      lt.push(    data[i][j][0].to_f )  
      data1.push( data[i][j][1].to_f )  
      data2.push( data[i][j][2].to_f )  
      if data[i][j].size > 2
        data3.push( data[i][j][3].to_f )  
        data4.push( data[i][j][4].to_f )  
      end 
    }
    
    if data[i][0].size > 3
      all = [ data1, data2, data3, data4 ]
    else
      all = [ data1, data2 ]
    end
    
    all.size.times{|k|
    DCL::grfrm  

      p axis[k+1]
      yaxis = axis[k+1].gsub('_', '-')

      ymin = maxmin["#{yaxis}"][0]
      ymax = maxmin["#{yaxis}"][1]
      
      DCL::grswnd(lt[0], lt[lt.size-1], ymin, ymax)
      DCL::grsvpt(0.3, 0.8, 0.3, 0.8)
      DCL::grstrn(1)
      DCL::uspfit
      DCL::grstrf
      DCL::usdaxs
      
      DCL::uxmttl('T', ".", 1.0)
      DCL::uxmttl('T', title[i], 0.0)
      DCL::uysttl('L', yaxis, 0.0)
      DCL::uxmttl('B', axis[0], 0.0)
      
      DCL::uulinz(lt, all[k], 1, 12)

      ##
      ## ファイル名の指定
      ##  
      num1 = num1 + 1
      num2 = i + 1
      comms.push("mv dcl_#{sprintf('%03d',num1)}.png figs/#{yaxis}_#{sprintf('%03d',num2)}.png")
    }
    
  }

}
DCL::grcls  

comms.each{|comm|
  p comm
  system( comm ) 
}
