Skip to content

Instantly share code, notes, and snippets.

@yohm
Last active September 26, 2015 21:48
Show Gist options
  • Save yohm/1164203 to your computer and use it in GitHub Desktop.
Save yohm/1164203 to your computer and use it in GitHub Desktop.
convert GNUPLOT .plt file into eps
#!/usr/bin/env ruby
require 'fileutils'
require 'optparse'
if ARGV.size == 0
raise "Usage epsplot plot1.plt (Specify gnuplot files)"
end
def thicken_lines( filename, bl=5, pl=3,point_size=51.5)
data = File.read(filename)
data.sub!(/BL \{stroke userlinewidth 2 mul/,"BL {stroke userlinewidth #{bl} mul")
data.sub!(/PL \{stroke userlinewidth setlinewidth/,"PL {stroke userlinewidth #{pl} mul setlinewidth")
data.sub!(/vpt_ 31.5/,"vpt_ #{point_size}")
data.sub!(/hpt_ 31.5/,"hpt_ #{point_size}")
data.sub!(/\/LC0 \{[\d\s\.]+\} def/,"/LC0 {0.12 0.47 0.71} def")
data.sub!(/\/LC1 \{[\d\s\.]+\} def/,"/LC1 {1.0 0.5 0.05} def")
data.sub!(/\/LC2 \{[\d\s\.]+\} def/,"/LC2 {0.17 0.63 0.17} def")
data.sub!(/\/LC3 \{[\d\s\.]+\} def/,"/LC3 {0.84 0.15 0.16} def")
data.sub!(/\/LC4 \{[\d\s\.]+\} def/,"/LC4 {0.58 0.4 0.74} def")
data.sub!(/\/LC5 \{[\d\s\.]+\} def/,"/LC5 {0.55 0.34 0.29} def")
data.sub!(/\/LT2 \{PL \[2 dl1 3 dl2\] LC2 DL\} def/,"/LT2 {PL [4 dl1 4 dl2] LC2 DL} def")
data.sub!(/\/LT3 \{PL \[1 dl1 1.5 dl2\] LC3 DL\} def/,"/LT3 {PL [6 dl1 6 dl2] LC3 DL} def")
data.sub!(/\/LT4 \{PL \[6 dl1 2 dl2 1 dl1 2 dl2\] LC4 DL\} def/,"/LT4 {PL [8 dl1 4 dl2] LC4 DL} def")
data.sub!(/\/LT5 \{PL \[3 dl1 3 dl2 1 dl1 3 dl2\] LC5 DL\} def/,"/LT5 {PL [8 dl1 4 dl2 2 dl1 4 dl2] LC5 DL} def")
data.gsub!(/^0.58 0.00 0.83 C/, "LC0 C")
data.gsub!(/^0.00 0.62 0.45 C/, "LC1 C")
data.gsub!(/^0.34 0.71 0.91 C/, "LC2 C")
data.gsub!(/^0.90 0.62 0.00 C/, "LC3 C")
data.gsub!(/^0.94 0.89 0.26 C/, "LC4 C")
data.gsub!(/^0.00 0.45 0.70 C/, "LC5 C")
io = File.open(filename,'w')
io.puts data
io.flush
end
$force = false
$font = 26
$no_gv = false
$point_size=31.5
$bl = 3
opt = OptionParser.new
opt.on('-f', "force overwrite") { |v| $force = v }
opt.on('-s FONT_SIZE', "font size (default: #{$font})") { |v| $font = v.to_i if v.to_i > 0 }
opt.on('-n', "no gv") { |v| $no_gv = v }
opt.on('-p POINT_SIZE', "point size") { |v| $point_size = v }
opt.on('-b ', "line width (default: #{$bl})") { |v| $bl = v.to_f }
opt.parse!(ARGV)
ARGV.each do |plotfile|
raise "plot file must have .plt extension" unless plotfile =~ /\.plt$/
epsfile = File.basename( plotfile.sub(/\.plt$/,'.eps') )
if File.exist?(epsfile) and $force == false
puts "overwrite? (y/n)"
answer = $stdin.gets.chomp
next unless answer == 'y' or answer == 'Y'
end
cmd = <<"CMD"
set term postscript eps enhanced color "Helvetica" #{$font}
set output \"#{epsfile}\"
CMD
tempfile = plotfile + '~'
begin
str = cmd + File.open(plotfile,'r').read
File.open(tempfile,'w') do |io|
io.puts str
io.flush
end
system("gnuplot #{tempfile}")
raise "gnuplot failed" if $? != 0
thicken_lines( epsfile, $bl, $bl*0.6, $point_size)
puts "File #{epsfile} has been written."
ensure
FileUtils.rm(tempfile) if File.exist?(tempfile)
end
system("gv #{epsfile}") unless $no_gv
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment