#!/usr/bin/ruby # coding: cp932 =begin PC773で取得した時系列データをプロットする =end $: << File::join(File::dirname(__FILE__), '..', '..', '..', 'common', 'ruby') require 'gnuplot_support' $stderr.puts "Usage: #{__FILE__} log.csv [--key[=val]]" $options = {} ARGV.reject!{|arg| if arg =~ /--([^=]+)=?/ then k = $1.to_sym v = $' $options[k] = (v.empty? ? true : v) true else false end } # 時系列グラフ class Plotter def Plotter::plot_time_series(file_prefix, t, v, options = {}) Plotter::plot_basic("#{file_prefix}.eps"){|plot| plot.xlabel "'Time [HH:MM]'" plot.set("xdata time") plot.set('timefmt "%H:%M:%S"') plot.set('format x "%H:%M"') plot.set('size', "1,0.4") #set xrange [00:00:00:24:00:00] plot.ylabel "'Voltage [V]'" yield plot if block_given? items = [ Gnuplot::DataSet::new([t, v]){|ds| ds.with = "lines lw 3" ds.using = "1:2" ds.notitle } ] plot.data = items } end end read_proc = proc{|io| data = [] last_hour = 0 base_hour = 0 io.each{|line| line.chomp! break if line =~ /^ *$/ # Before model number, empty line is inserted. new_data = line.split(/,/) next if new_data.size < 2 t, v = new_data[0..1] t =~ /(\d{2}):(\d{2}):(\d{2})/ hour, min, sec = [$1.to_i, $2.to_i, $3.to_i] base_hour += 24 if last_hour > hour last_hour = hour t = sprintf("%02d:%02d:%02d", hour + base_hour, min, sec) data << [t, v] } next if data.empty? data = data.transpose Plotter::plot_time_series(File::basename(__FILE__, '.*'), data[0], data[1]) } src_file = ARGV.shift if (!src_file) || (src_file == '-') then read_proc.call($stdin) else open(src_file){|io| read_proc.call(io) } end