A bash/gnuplot script to produce movie-like effects - bash

I would like to plot a sequence of images one after the other on the same window using gnuplot. Images are all the same exact size and I have many of them sequentially called test_0X.gnu and are in gnuplot format.
I wrote this in bash
#! /bin/bash
for i in $(seq -f "%06g" 0 1000)
do
echo "set term pdf enhance color" > script.plg
echo "set output 'demo.pdf'" >> script.plg
echo "plot 'test_$i.gnu' with lines" >> script.plg
echo "set output"
gnuplot < script.plg
evince demo.pdf &
sleep 0.3
done;
The seq -f ensures the correct number of padded zeros is present before the number.
This works but once the script starts I am unable to stop it, as ctrl+c or ctrl+z doesn't work and I cannot access other terminal due to constant coming up of a pdf windows.
How do I stop this?
How do I make the sequence skip (e.g. by 10) and show me only test_000010.gnu, test_000020.gnu, .. etc.
Bonus: Any other, more sensible way to do this is welcome.

Just write a short gnuplot script
set terminal wxt
files = system('ls test_*.gnu')
do for [file in files] {
plot file with lines
pause 0.5
}
and run this

gnuplots "gif" terminal can produce a readymade animation.
skip=5 # change as needed
set term gif animate 10 # 10/100 s delay between frames
set out "test.gif"
# assuming your data files are named "test_0001.dat" etc.
do for [i=1:100:skip] {plot sprintf("test_%04.f.dat",i)}
set out

Related

How can I display the run count as part of the `watch` output in a terminal?

I'd like to see a run counter at the top of my watch output.
Ex: this command should print a count value which increments every 2 seconds, in addition to the output of my main command, which, in this case is just echo "hello" for the purposes of this demonstration:
export COUNT=0 && watch -n 2 'export COUNT=$((COUNT+1)); echo "Count = $COUNT" \
&& echo "hello"'
But, all it outputs is this, with the count always being 1 and never changing:
Count = 1
hello
How can I get this Count variable to increment every 2 seconds when watch runs the command?
Thanks #Inian for pointing this out in the comments. I've consulted the cross-site duplicate, slightly modified it, and come up with this:
count=0; while sleep 2 ; do clear; echo "$((count++))"; echo "hello" ; done
Replace echo "hello" with the real command you want to run once every sleep n second now, and it works perfectly.
There's no need to use watch at all, and, as a matter of fact, no way to use watch in this way either unless you write the variable to a file rather than to RAM, which I'd like to avoid since that's unnecessary wear and tear write/erase cycles on a solid state drive (SSD).
Now I can run this command to repeatedly build my asciidoctor document so I can view it in a webpage, hitting only F5 each time I make and save a change to view the updated HTML page.
count=0; while sleep 2 ; do clear; echo "$((count++))"; \
asciidoctor -D temp README.adoc ; done
Sample output:
96
asciidoctor: ERROR: README.adoc: line 6: level 0 sections can only be used when doctype is book
Final answer:
And, here's a slightly better version which prints count = 2 instead of just 2, and which also runs first and then sleeps, rather than sleeping first and then running:
count=1; while true ; do clear; echo "count = $((count++))"; \
asciidoctor -D temp README.adoc; sleep 2 ; done
...but, it looks like it's not just updating the file if it's changed, it's rewriting constantly, wearing down my disk anyway. So, I'd need to write a script and only run this if the source file has changed. Oh well...for the purposes of my original question, this answer is done.

Bash script for gnuplot

I have a c++ code that generates every time a variable number of ".txt" file.
I am using a bash script that loads the txt files and send them to a gnuplot script. Then I have to plot in the same figure always the same 2 columns of each of them (representing a altitude vs time plot).
My problem is that the x coordinate (1st column of txt files) does not finish always at the same value and, when the last value of x coordinate is not zero,I need to insert a cross in the gnuplot plot. Do you have an idea about how to do it?
This is how I am plotting the multiple files
plot for [object in objectnames] object.".txt" using 1:2 w l title object
where "objectnames" is a string with all the filenames sent by the BASH
Now, for plotting the CROSSES, I thought about two ways:
Send a array of coordinates to the gnuplot script and then set some label but GNUPLOT does not like arrays and I cannot create a variable number of label in this way.
The alternative is to read somehow the txt file inside the gnuplot script but I have no idea how to do it.
Thanks for the help
EDIT
BASH SCRIPT
export DATA_DIR=${1:-/home/../output}
export objectname
space=" "
OUT_FILES="$(find -L $DATA_DIR -name '*_Trajectory.txt')"
for file in $OUT_FILES; do
fig=$(basename "$file")
objectID=$(echo $fig| cut -d'_' -f 1)
# filenames concatenation
objectname=$objectname$objectID$space
done
export figNameAltitude="${file/${fig}/altitudePlot.png}"
gnuplot -e "folder='${DATA_DIR}';objectnames='${objectname}';figName='${figNameAltitude}';lineWidth='${linewidth}'" altitudePlot.gp
GNUPLOT SCRIPT ( I avoid to insert all the lines regarding the title, axis and so on)
plot for [object in objectnames] folder.'/'.object."_Trajectory.txt" using 1:2 w l lw lineWidth title object
This might help
set title "Energy vs. Time for Sample Data"
set xlabel "Time"
set ylabel "Energy"
plot "d1.dat" with lines
pause -1 "Hit Enter to continue"
execute the script file as **$**gnuplot filename
click here for more details

Parametric gnuplot Scripts

When I try to write a shell script such as
gnuplot
cd '/home/cagirici/test'
plot "case1.test" with linespoints
and run it in sudo, the script stops after running gnuplot.
In each .test file, I have 12 columns. I won't plot them all at once but choose what to plot. For instance, if I choose to plot the success percentage, it should plot 1:2 and 1:8. But if I choose error percentage, it should plot 1:4 and 1:10 (it is basically i:(i+6).)
I need to write a scripts that takes two inputs: i) file name ii) what to plot.
Notice that I also want to choose the line type, point type and output shell. (if there is a configuration to set specific line and point types as default values, please tell me this as well)
Can I write this script in Ubuntu? Or should I use a different method.
My script would look something like
case "$2" in
("success percentage") plot "$1" using 1:2, plot "$1" using 1:8
("error percentage") plot "$1" using 1:4, plot "$1" using 1:10
You can make this work by creating a Gnuplot script with variables and then passing values to these variables at run time from command line or your bash script.
Lets call the Gnuplot script Myscript.gp:
set terminal pngcairo
set output 'graph.png'
plot file u x:y w linespoints
Here file and x:y can be varried when you invoke Gnuplot from your bash script. A possible call to Myscript.gp is as follows:
gnuplot -e "x=1;y=2;file = \"mydata.dat\"" Myscript.gp
In the snippet of your bash script that you provide this may look like:
case "$2" in
("success percentage") gnuplot -e "x=1; y=2; file = \"$1\"" Myscript.gp
Below I'm also providing a dummy mydata.dat file which you can use to try things.
1.54 23.66 43.66
1.75 26.25 46.25
1.92 30.20 40.20
2.08 34.46 44.46
2.44 42.08 42.08
2.78 46.81 46.81
3.03 51.10 41.10
3.70 52.99 42.99
4.17 56.15 46.15
4.76 59.34 49.34
If you are calculating the column numbers (x and y) in your bash script then you can also use variables inside the plot command.
gnuplot -e "x=$x;y=$y;file = \"mydata.dat\"" Myscript.gp
Below I show the output once using:
gnuplot -e "x=1;y=2;file = \"mydata.dat\"" Myscript.gp
and once
gnuplot -e "x=1;y=3;file = \"mydata.dat\"" Myscript.gp
I had faced similar problem 2 years back, then my senior team members decided to write a tool for plotting graphs using a simple csv and plotting specifications and tool will plot graphs for you,
Initially it was designed for sar data on linux so named RSAR but in general it is a csv plotter.)
if required you can contact me nachiket.kate90#gmail.com

Gnuplot: One plot per file

I'm trying to plot the 1st and 3rd columns of multiple files, where each file is supposed to be plotted to an own output.png.
My files have the following names:
VIB2--135--398.6241
VIB2--136--408.3192
VIB2--137--411.3725
...
The first number in the file name is an integer, which ranges from 135-162. The second number is just a decimal number and there is no regular spacing between the values.
Basically I want to do something like this
plot for [a=135:162] 'VIB2--'.a.'--*' u 1:3 w l
although this doesn't work, of course, since the ' * ' is just the placeholder I know from bash and I don't know, if there is something similar in gnuplot.
Furthermore, each of the files should be, as already said above, plotted to its own output.png, where the two numbers should be in the output name, e.g. VIB2--135--398.6241.png.
I tried to come up with a bash script for this, like (edited):
#!/bin/bash
for file in *
do
gnuplot < $file
set xtics 1
set xtics rotate
set terminal png size 1920,1080 enhanced
set output $file.png
plot "$file" u 1:3 w l
done
but I still get
gnuplot> 1 14 -0.05
^
line 0: invalid command
gnuplot> 2 14 0.01
^
line 0: invalid command
...
which are actually the numbers from my input file. So gnuplot thinks, that the numbers I want to plot are commands... ?? Also, when the end of the file is reached, I get the following error message
#PLOT 1
plot: an unrecognized command `0x20' was encountered in the input
plot: the input file `VIB2--162--496.0271' could not be parsed
I've seen a few questions similar to mine, but the solutions didn't really work for me and I cannot add a comment, since I do not have the reputation.
Please help me with this.
gnuplot < $file starts gnuplot and feeds it the content of $file as input. That means gnuplot will now try to execute the commands in the data file which doesn't work.
What you want is a "here document":
gnuplot <<EOF
set xtics 1
set xtics rotate
set terminal png size 1920,1080 enhanced
set output $file.png
plot "$file" u 1:3 w l
EOF
What this does is: The shell reads the text up to the line with solemn EOF, replaces all variables, puts that into a temporary file and then starts gnuplot feeding it the temporary file as input.
Be careful that the file names don't contain spaces, or set output $file.png will not work. To be safe, you should probably use set output "$file.png" but my gnuplot is a bit rusty.

Shell script that passes parameter to gnuplot does not export the diagram (epslatex)

I have created a shell script (.run) that accepts the prefix for the names of the pictures as a parameter, and then calls gnuplot. However, for some reason, the picture is not saved. The code is:
#!/bin/sh
molecule=$1
echo "Plotting DFT-ADF PY results for $molecule"
echo "Tranmission plot (negatory SO)"
gnuplot -p << EOF
#!/usr/bin/gnuplot
set terminal epslatex size 5,3 color colortext
set output '$molecule_trans.tex'
plot cos(x) w l title 'cos(x)', sin(x) w l title 'sin(x)'
EOF
For my bachelor thesis I have to make several plots that are the same. Additionally, the computational cluster uses a qeueing system. For the purpose of being true to this system, I have created several shell scripts that automatically do stuff. In particular, about 45 simulations are called by the shell scripts, followed by a shell script that enters each simulations' directory and uses python files to evaluate the data into [.dat] files. Next, it should use a gnuplot file to make the graph. I use EPSLaTeX to make my figures, because it is so much nicer. However, in the current implementation this required me to manually edit the various latex files to rename the pictures.
In case you'll need more variables and do not want a $1, $2... mess:
You must use brackets around the variable name
set output '${molecule}_trans.tex'
because the underscore is a valid character for variable names, and bash looks for the variable $molecule_trans, see http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion.

Resources