Created
May 31, 2011 12:32
-
-
Save theirix/1000432 to your computer and use it in GitHub Desktop.
Plot unicode graph using gnuplot/svg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'fileutils' | |
def base_dir | |
File.dirname(File.expand_path(__FILE__)) | |
end | |
def read_matrix path, file | |
IO.readlines(File.join(path, file))\ | |
.map { |s| s.split(/[ ;]+/).map { |sf| yield sf } } | |
end | |
def read_matrix_float path, file | |
read_matrix(path, file) { |s| s.to_f } | |
end | |
def read_matrix_int path, file | |
read_matrix(path, file) { |s| s.to_i } | |
end | |
prefix = ARGV[0] | |
raise 'no prefix' if (prefix or '') == '' | |
prefix = prefix.gsub('/','__') | |
path = (ARGV[1] or File.join(base_dir,'bin', 'WorkDirectory')) | |
plot_dir = File.join(base_dir, 'bin', 'plots') | |
FileUtils.mkdir plot_dir unless File.directory? plot_dir | |
#FileUtils.rm(Dir.glob(File.join(plot_dir, 'plot*')), {:verbose => true}) | |
data_exper = read_matrix_float(path, 'Exper.txt') | |
data_observ = read_matrix_int(path, 'ObservSubst.txt').flatten | |
data_comp = read_matrix_float(path, 'output.csv') | |
observ_ind = [] | |
data_observ.each_with_index { |q,i| observ_ind << i if q==1 } | |
puts 'Indices: ' + observ_ind.join(', ') | |
puts 'Temperatures: ' + data_exper.map { |l| l[0] }.join('; ') | |
subst_count = data_exper[0].size - 1 | |
# format: (time exper comp), grouped into subst | |
total_data = [] | |
(0...subst_count).each do |ind| | |
puts "----- substance #{ind}, global #{observ_ind[ind]}" | |
cur_data = [] | |
data_exper.each do |line_exper| | |
t = line_exper[0] | |
line_comp = data_comp.find do |l| | |
l if (l[0] - t).abs < 0.01 | |
end | |
raise 'No temperature found ' + t.to_s unless line_comp | |
exp_value = line_exper[ind+1] | |
comp_value = line_comp[observ_ind[ind]+1] | |
cur_data << [ t, comp_value, exp_value ] | |
end | |
cur_data.each { |s| puts s.inspect } | |
total_data << cur_data | |
end | |
# plot them | |
(0...subst_count).each do |ind| | |
base_name = "plot_#{prefix}_subst#{observ_ind[ind]+1}" | |
data_file = base_name + '.dat' | |
img_file = base_name + '.png' | |
plot_file = base_name + '.gp' | |
File.open(File.join(plot_dir, data_file), 'w') do |f| | |
total_data[ind].each { |l| f.puts l.join(' ') } | |
end | |
File.open(File.join(plot_dir, plot_file), 'w') do |f| | |
f.puts <<-EOF | |
set terminal svg | |
set encoding utf8 | |
set title 'Температура #{prefix}, вещество A#{observ_ind[ind]+1}' | |
#set xrange [0:6] | |
#set yrange [0:1] | |
set xlabel 't' | |
set ylabel 'Концентрации' | |
#set grid | |
plot '#{data_file}' u 1:2 title 'Расчет' w points pt 5, '#{data_file}' u 1:3 title 'Эксперимент' w linespoints | |
EOF | |
end | |
FileUtils.cd(plot_dir) do | |
system "cat #{plot_file} | gnuplot | rsvg-convert --background-color=white -o #{img_file}" | |
raise 'Error plotting' if $? != 0 | |
end | |
puts "Done with #{img_file}..." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment