gnuplot with a variable in the path to a file - linux

I need to run gnuplot scripts on different devices. The file structure is the same on the devices, once I get to a particular directory, but the paths to those directories are different. I think I need to have a variable in the path name that I can change according to which device I am on.
I thought the following would work, because I found something similar here, but it doesn't:
path_to_directory="/desktop/path/to/directory"
# path_to_directory="laptop/path/to/directory"
# the line above is commented out but it can be included
# if I want to switch from my desktop to my laptop
path_to_file="$path_to_directory/path/to/file"
plot path_to_file ...
A warning message says: Skipping unreadable file "$path_to_directory/path/to/file"
Do you know how I can include a variable in the path of a gnuplot script, so that I can switch easily between paths?
Thank you.

In gnuplot you don't use $ to indicate variables as you do in bash. You would do a concatenation operation with a dot:
path_to_directory="/desktop/path/to/directory"
path_to_file=path_to_directory."/path/to/file"

You simply need to concatenate two string variables with the . operator:
path_to_directory = "/desktop/path/to/directory"
path_to_file = path_to_directory . "/path/to/file"
plot path_to_file ...
You can also define a default path which can be overridden:
default_path_to_directory = "/desktop/path/to/directory"
if (!exists("path_to_directory")) path_to_directory = default_path_to_directory
path_to_file = path_to_directory . "/path/to/file"
plot path_to_file ...
Now you can override the path_to_directory but must not: e.g. when calling gnuplot like gnuplot -e "path_to_directory='...'" script.gp or with a configuration file.

Related

Strange tex files management in Gnuplot with tikz term

I fount a strange phenomenon. If we run the following gnuplot script (in a folder with gnuplot-lua-tikz-common.tex, gnuplot-lua-tikz.sty, gnuplot-lua-tikz.tex, t-gnuplot-lua-tikz.tex)
tikzfile="test.tex"
plot x**2
set term tikz standalone monochrome
set output tikzfile #
replot #
cmd="pdflatex -interaction=nonstopmode ". tikzfile
system(cmd)
we found the following fatal error
! Emergency stop.
<*> test.tex
! ==> Fatal error occurred, no output PDF file produced!
Anyway we have the test.tex file. Hence if we re-run the same script with the #-marked lines commented we obtain no error and the perfect test.pdf file.
During the first exec, with set term we have an empty file, with replot we fill it, but until the end of the exec we can't use it as an input of pdflatex. Why?
During the second exec we already have the test.tex file, so if we comment set term and replot we can use it as an input of pdflatex. Why?
Thank you.
Gnuplot doesn't automatically flush and finish an output file after a plot.
So, if you want to further process an output file from within the gnuplot script you must explicitly close the file with set output beforehand:
tikzfile="test.tex"
set term tikz standalone
set output tikzfile
plot x**2
set output
cmd="pdflatex -interaction=nonstopmode ". tikzfile
system(cmd)

make an output directory in Gnuplot

I'm trying to save an .eps file in a folder that doesn't exist yet. I wrote the following gnuplot script:
plot [0:0.13][0:55] 'example/x_-4_U.xy'
set output 'output/x=-4.eps'
replot
The script only works if the "output" folder is already there . Can I create this folder with gnuplot?
Thanks.
You can use the system command:
system "mkdir output"
on Linux, I seem to remember that it is md output on Windows.
Combined with string operators, this is rather flexible:
dir = "output1"
command = "mkdir " . dir
system command
works nicely.
You can concatenate the name of the output file with the folder name:
folderout = 'output/'
plot [0:0.13][0:55] 'example/x_-4_U.xy'
set output folderout.'x=-4.eps'
replot
Sincerely.

gnuplot script name OR bash $0 alternative for gnuplot

How to programmatically get the name of current gnuplot script? I know that I can call gnuplot script from bash and get it file name but I am wondering if it is possible from inside gnuplot. My goal is to make something like:
date=system("date +%F_%T | sed 's/:/-/g'")
my_name=$0 # THIS IS HOW TO DO IT IN BASH
set term png
set output my_name.date.".png"
I've tried:
my_name=system("cat /proc/$$/cmdline")
but it returned sh instead of script name
Not quite an answer to your question, but this might help with what you want to do:
You can leave my_name unset in the script, and set it either inside gnuplot, just before you load the script (where you need to know the script name anyway):
my_name=...
load(my_name)
or set it when you invoke gnuplot from the shell:
$ gnuplot -e "my_name=${FILE}" ${FILE}
A few more things:
date=system("date +%F_%T | sed 's/:/-/g'")
can be replaced with
date=system("date +%F_%H-%M-%S")
(which is shorter and doesn't need to be parsed through sed) or without any forking at all:
date=strftime("%F_%H-%M-%S",time(0.0))
Using gnuplot version 5 you have access to the file called with load via the variable ARG0
Consider the script test.gp which contains only
print ARG0
Now, calling this with
gnuplot -e "load 'test.gp'"
prints you test.gp on the screen. With earlier versions you don't have access to a similar variable (also not when using call). For earlier versions you must stick to one of the solutions given by #chw21

Plot all files in a directory simultanously with gnuplot?

I want to do something similar to this question: gnuplot : plotting data from multiple input files in a single graph.
I want to plot simultaneously all the files in a directory, without having to explicitly write their names. The column numbers are the same for all the files. What can I do?
Doing plot for [file in *] file u 3:2 doesn't work.
Also, I don't want each file to have a different legend. All points from all files should be treated the same, as if they all came from a single file.
As an alternative to Jonatan's answer, I would go with
FILES = system("ls -1 *.dat")
plot for [data in FILES] data u 1:2 w p pt 1 lt rgb 'black' notitle
or
plot '<(cat *.dat)' u 3:2 title 'your data'
The first option gives you more flexibility if you want to label each curve. For example, if you have several files with names data_1.dat, data_2.dat, etc., which will be labeled as 1, 2, etc., then:
FILES = system("ls -1 data_*.dat")
LABEL = system("ls -1 data_*.dat | sed -e 's/data_//' -e 's/.dat//'")
plot for [i=1:words(FILES)] word(FILES,i) u 3:2 title word(LABEL,i) noenhanced
You could try something like:
a=system('a=`tempfile`;cat *.dat > $a;echo "$a"')
plot a u 3:2
This uses the command line tempfile command to create a safe, unique, and disposable temporary file. It mashes all of the data files into this file. It then echoes the file's name so gnuplot can retrieve it. Gnuplot then plots things.
Worried about header lines? Try this:
a=system('a=`tempfile`;cat *.dat | grep "^\s*[0-9]" > $a;echo "$a"')
The regular expression ^\s*[0-9] will match all lines which begin with any amount of whitespace followed by a number.
I like to be able too choose the files to plot with wildcards, so if you like that you can do as follows, though there are many ways. Create the following script.
script.sh:
gnuplot -p << eof
set term wxt size 1200,900 title 'plots'
set logs
set xlabel 'energy'
plot for [ file in "$#" ] file w l
eof
do chmod u+x script.sh
Run like ./script.sh dir/* *.dat
If you need it often make an alias for it and put it in some reasonable place:)
Cheers /J
Try the following command:
gnuplot -e 'plot for [file in system("find . -depth 1 -type f -print")] file u 3:2'
Note: Add -p to keep the plot window.

Gnuplot - Command Substitution and Script Name

I'm using Gnuplot as my back-end plotter and I often use the following setup :
#Filename : my_plot.gnuplot
set terminal pdfcairo [my_options]
set output 'my_plot.pdf'
....
coupled with a Makefile :
%.pdf : %.gnuplot
gnuplot $<
My question is simple : is there a command / way to refer to the name of the script inside the script (the equivalent of bash's $0) and set the output with a clever sprintf or equivalent ?
You cannot access the script name from gnuplot, but you can give a parameter when calling the script, which should work fine, especially when using Makefiles.
%.pdf: %.gnuplot
gnuplot -e "scriptname='$<'" $<

Resources