#!/usr/bin/env ruby require 'win32ole' #GNUPLOT_DEBUG = true require 'gnuplot' $: << '~/src/eclipse/autopilot/common/ruby/' require 'gnuplot_support' def get_path_abs(file) return WIN32OLE::new('Scripting.FileSystemObject').GetAbsolutePathName(file) end def open_book(file) xl = WIN32OLE::new('Excel.Application') book = xl.Workbooks.Open(file) begin yield(book) ensure book.Close xl.Quit end end class PlotData class DataSet PROPERTIES = [ 'x', 'y', 'y_error', 'title', 'fit_x_range' ] PROPERTIES.each{|property| eval("@#{property}") eval("attr_accessor :#{property}") } end PROPERTIES = [ 'title', 'dataset', 'y_label', 'x_range', 'y_range', 'x_label', 'y_label' ] PROPERTIES.each{|property| eval("@#{property}") eval("attr_accessor :#{property}") } def initialize @dataset = [] end end def plot_data(data, out_file = 'hoge.eps') Gnuplot.open{|gp| #def gp.<< (cmd); p cmd; end items = [] data.dataset.each_with_index{|item, i| #p item temp = [item.x, item.y] temp << item.y_error if item.y_error items << Gnuplot::DataSet::new(temp){|ds| ds.with = "points lw 2 lt #{i+1}" #"yerrorbars lw 2" if item.title then ds.title = item.title else ds.notitle end } # ‹ίŽ—‹Θό‚Μ’θ‹` gp << "a#{i}=1\n" gp << "b#{i}=0\n" gp << "f#{i}(x)=a#{i}*x+b#{i}\n" # ‹ίŽ—‹Θό‚ΜŒvŽZ gp << "fit f#{i}(x) '-' via a#{i},b#{i}\n" if item.fit_x_range then fit_x = [] fit_y = [] fit_y_error = item.y_error ? [] : nil item.x.each_with_index{|v, j| if !item.fit_x_range.include?(v) then next end fit_x << v fit_y << item.y[j] fit_y_error << item.y_error[j] if item.y_error } temp = [fit_x, fit_y] temp << fit_y_error if fit_y_error #p Gnuplot::DataSet::new(temp).to_gplot gp << Gnuplot::DataSet::new(temp).to_gplot else gp << items.last.to_gplot end gp << "e\n" # ‹ίŽ—‹Θό‚Μ’Η‰Α items << Gnuplot::DataSet::new("f#{i}(x)"){|ds| ds.with = "lines lw 2 lt #{i+1}" ds.title = "a_{#{i+1}} x + b_{#{i+1}}" } gp << "set label 'a_{#{i+1}} = %f, ',a#{i},'b_{#{i+1}} = %f',b#{i} at 0,#{3E+6 - 1E+6 * i}\n" } Plotter::plot_eps(out_file, gp){|plot| plot.xrange data.x_range if data.x_range plot.yrange data.y_range if data.y_range plot.xlabel "'#{data.x_label}'" plot.ylabel "'#{data.y_label}'" plot.data = items } } end TARGET_FILE = 'mu_sigma.xls' # {Sheet–Ό => [•\‘θ, XŽ²ƒ^ƒCƒgƒ‹, YŽ²ƒ^ƒCƒgƒ‹, [[XŽ², YŽ², ƒGƒ‰[ƒo[], ...], ‹ίŽ—‹Θό‚ΜŒvŽZ”ΝˆΝ} TARGET = { '0_1' => ['Mode1', 'Angular speed [m/s]', 'A/D Converted Value', [['A', 'I', 'J'], ['A', 'K', 'L'], ['A', 'M', 'N']], -150..150], '0_2' => ['Mode2', 'Angular speed [m/s]', 'A/D Converted Value', [['A', 'I', 'J'], ['A', 'K', 'L'], ['A', 'M', 'N']], -150..150], '0_3' => ['Mode3', 'Angular speed [m/s]', 'A/D Converted Value', [['A', 'I', 'J'], ['A', 'K', 'L'], ['A', 'M', 'N']], -150..150], '1_1' => ['Mode1', 'Angular speed [m/s]', 'A/D Converted Value', [['A', 'C', 'D'], ['A', 'E', 'F'], ['A', 'G', 'H']], -90..90], '1_2' => ['Mode2', 'Angular speed [m/s]', 'A/D Converted Value', [['A', 'C', 'D'], ['A', 'E', 'F'], ['A', 'G', 'H']], -90..90], '1_3' => ['Mode3', 'Angular speed [m/s]', 'A/D Converted Value', [['A', 'C', 'D'], ['A', 'E', 'F'], ['A', 'G', 'H']], -90..90] } open_book(get_path_abs(TARGET_FILE)){|book| book.Worksheets.each{|sheet| if !(target = TARGET[sheet.Name]) then next end data = PlotData::new data.title = target[0] data.x_label = target[1] data.y_label = target[2] data.y_range = "[0:1.6E+7]" target[3].each{|xy| dataset = PlotData::DataSet::new data.dataset << dataset x = dataset.x = [] y = dataset.y = [] y_error = dataset.y_error = [] dataset.fit_x_range = target[4] if target[4] sheet.UsedRange.Columns.each{|column| address = "#{column.Address}" record = nil case address when /#{xy[0]}/ record = x when /#{xy[1]}/ record = y when /#{xy[2]}/ record = y_error else next end column.Rows.each{|cell| record << cell.Value } } x.each_with_index{|v, i| if v then next end x.delete_at(i) y.delete_at(i) y_error.delete_at(i) } #p x #p y #p y_error } plot_data(data, "#{sheet.Name}.eps") } =begin book.Worksheets.each{|sheet| sheet.UsedRange.Rows.each{|row| record = [] row.Columns.each{|cell| record << cell.Value } puts record.join(",") } } =end }