Gnuplot: skip last/incomplete line of data file - c++

I have a C++ program using an embedded lua script to write data points from the program to file, and I want to simultaneously be able to run a gnuplot instance to plot the data points.
io.output(pfile);
io.write(t, "\t", p_x, "\t", p_y, "\t", p_z, "\n");
The gnuplot file looks like:
set termopt enhanced
set title "Linear Momentum Vector"
set xlabel "t (s)"
set ylabel "p (N-s)"
plot "data/plot_p.dat" using 1:2 title "p_x(t)" with lines, \
"data/plot_p.dat" using 1:3 title "p_y(t)" with lines, \
"data/plot_p.dat" using 1:4 title "p_z(t)" with lines
set style line 81 lt 0 lc rgb "#808080" lw 0.5
set grid xtics ytics mxtics mytics
set grid back ls 81
pause 0.25
reread
The above gnuplot script works for a complete data file, but I want it to plot in real time while the program is running. While the lua script writes to file, sometimes the gnuplot script catches the file with an incomplete last line. And this generates an error:
"liveplot_p.gnu", line 9: x range is invalid
How would I get gnuplot to script the last line or invalid data sets?
Thank you!

Some messing around with lua actually solved it for me. Calling the io.flush() function after the io.write(...) call seems to write the complete line into the file every time.
This ofcourse, doesn't tell gnuplot to SKIP or IGNORE the last line but it makes sure the last line is complete.

Related

Commands in GWBASIC

I am using GWBASIC and cannot figure out a few things. Like, when I'm saving a program after running it with F4, it says: File not found.
Secondly, when I'm using auto command it shows * with line numbers.
Finally, if I want to take program and its output's print on paper, what should I do?
I am using GWBASIC and cannot figure out a few things. Like, when I'm
saving a program after running it with F4, it says: File not found.
Try saving like this:
SAVE"myprog.bas",a
Secondly, when I'm using auto command it shows * with line numbers.
The star (*) on a line means that line already exists and you are overwriting it.
If you use the command NEW to wipe-out the program from memory before running 'auto', you won't see those stars on lines.
Finally, if I want to take program and its output's print on paper,
what should I do?
1) Save the program as a text file:
SAVE"myprog.bas",a
2) Open the file 'myprog.bas' with a text editor (like Notepad++).
3) Print it.
To print a GW-BASIC program, use the LIST command.
The optional , filename parameter specifies the output for the listing. It could be to a file (e.g. to dump the text so another program can load it), or it could be to a printer device (e.g. LPT1:).
So this should work:
LIST ,LPT1:
See also https://robhagemans.github.io/pcbasic/doc/1.2/#LIST

Unable to read a number from a text file

I am using Fortran to make a subroutine to use in a CFD shallow water software.
I have written this code to read and use the values stored.
PROGRAM hieto
! Calcula la precipitacion efectiva en funcion del tiempo
!IMPLICIT NONE
real::a
!Abrir CSV
!OPEN(UNIT=10,FILE="datos.txt",FORM="formatted",STATUS="replace",ACTION="readwrite",ACCESS='sequential')
open(unit=10, file='datos.txt')
!Leer el archivo
read(10, *, iostat=ios)a
print*,ios
print*, a
close (UNIT=10)
END PROGRAM hieto
My text file datos, looks like this
1
2
3
When I run the code as is, I get the following output
-1
0.0000000000
Process return 0 (0x0) execution time: 0.002 s
the first number in the row one is one not zero, so I don't know why this happens.
And if I remove the iostat=ios from the read statement, I get the following error:
At ine 13 (the line od the read stament) of file /home/Dropbox/scripts_tesis/fortran/hieto_telemac.f90 (unit=10, file=datos.txt')
Fortran runtime error: end of file.
Proceess returned 2 (0x2)
I have read some answers here so I tried adding end=3 in the read statement, and also to end my text file with a blank line at the end.
The end=3 gives an error saying 3 is not a defined label and putting a blank row in the text file does nothing.
I am using ubuntu 16.04 LTS and Gfortran compiler.
What happens is that your file is empty.
Make sure that there is indeed a file called datos.txt in that directory. Pay attention to the exact name. datos.txt and just datos is not the same thing.
If you tried to open it before with the commented command that includes STATUS="replace" your old file would have been replaced.
And because the file is empty, you didn't real anything useful. If iostat is non-zero, and your is -1, then the value of the variable being read is undefined. So your a is undefined. Again, because your file is empty.
Additionally, you cannot just blindly put end=3 in your code because you saw it somewhere on Stack Overflow. You must first understand what it is supposed to do. There is no reason to combine iostat= and end=. The iostat is perfectly sufficient.

How to read in irregularly numbered data files in script to make gnuplot images?

I have a script (called: anim.sh) to read in my simulation data, plot each data file in gnuplot, and save each as a .gif file. The data is named: g000000, g000007, g000008, g000010....etc. My script reads in these 4 data files and makes a plot in gnuplot, then saves the output as a .gif file. My question is how do I read in this irregularly numbered data? I have:
do for [i=0:3] {
str_num=sprintf('%03d',i) #Write integer to string
set term gif
set output 'my_output.'.str_num.'.gif'
set xr [-0.2:0.2]
set yr [-0.2:0.2]
plot 'g000'.str_num w d
}
This would work fine if my files were called g000000, g000001, g000002, g000003 but they are not! so I get an error:
line 8: warning: Skipping unreadable file "g000001"
line 8: No data in plot
How do I add to the code to skip missing files (e.g. g000001 - g000006) and continue the loop?
p.s. In reality i have hundreds of files and not just 4 so I don't want to rename them by hand.
You can generate a list of all files and iterate over them:
set terminal gif
set xrange[-0.2:0.2]
set yrange[-0.2:0.2]
filelist = system('ls g00*')
do for [file in filelist] {
set output 'my_output.'.(file[2:*]).'.gif'
plot file
}

creating a pipe and writing to gnuplot terminal from c++

I am trying to call the gnuplot from c++. I am using wgnuplot for Windows and VS2005 c++.
The following statement works because it opens the gnuplot terminal
FILE *p = _popen("wgnuplot -persist","w");
But I cannot write anything there. My terminal is still blank even after running the following code.
fprintf(p, "set terminal x11 enhanced\n"); //set appropriate output terminal for the plot
fprintf(p, "set xlabel 'N'\n");//set xlabel
fprintf(p, "set ylabel 'error'\n");//set ylabel
Could you please tell me what might be the problem, i.e. why the terminal is blank and fprintf() doesn't seem to work?
Thanks,
Boris
Check that FILE pointer is not NULL:
if(!p)
// _popen() has failed...
I do not know if that helps you, but this is my approach of executing gnuplot from my C programs:
I create a template file (usually I do not delete it, so that it is easier to troubleshoot), where all the gnuplot commands are are scripted in.
I run gnuplot with
system("gnuplot <TemplateFile>")
If you are only interested in creating a plot, that his would do the job. If you are explicitly interested in the approach you described above then just disregard this posting ;)
Cherio Woltan

C++ and gnuplot

This is my first post and I'm quite a novice on C++ and compiling in general.
I'm compiling a program which requires some graphs to be drawn. The program create a .dat file and then i should open gnuplot and write plot '.dat'. That's fine.
Is there a way to make gnuplot automatically open and show me the plot I need? I should use some system() function in the code to call gnuplot but how can I make him plot what I need?
Sorry for my non-perfect English :s
Thanks for the attention anyway!
Depending on your OS, you might be able to use popen(). This would let you spawn a gnuplot process and just just write to it like any other FILE*.
If you have datapoints to plot, you can pass them inline with the plot "-" ... option. Similarly, you may want to explore set data style points/lines/linespoints/etc options.
Without pause or persist, gnuplot will terminate upon end-of-input-stream. In your example case, that would be when the end of the file is reached.
To produce (write) an output file (graph), use:
set terminal png small
set output "filename.png"
There's lots of options to set terminal. Png is usually there. If not, perhaps gif, tiff, or jpeg?
Watch out for overwriting the file!
You may want to use set size 2,2 to make a larger graph. Some set terminal variants also allow you to specify the size.
I'm learning this today too. Here is a small example I cooked up.
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv) {
ofstream file("data.dat");
file << "#x y" << endl;
for(int i=0; i<10; i++){
file << i << ' ' << i*i << endl;
}
file.close();
return 0;
}
Save that as plot.cpp and compile that with g++:
g++ plot.cpp -o plot
Run the program to create the .dat file:
./plot
Save the following gnuplot script as plot.plt:
set terminal svg enhanced size 1000 1000 fname "Times" fsize 36
set output "plot.svg"
set title "A simple plot of x^2 vs. x"
set xlabel "x"
set ylabel "y"
plot "./data.dat" using 1:2 title ""
Run the script with gnuplot to generate your .svg file:
gnuplot plot.plt
The resulting plot will be in plot.svg. If you leave out the first couple lines that specify the output, it will render in a window. Have fun!
Sometimes it is as easy as one may think
gnuplot file
where file is neither your data nor your result file, but a file with the command you would type in the commandline. Just enter there your commands you need (either constant file you have or generate it).
After executing all commands in that file gnuplot exits.
Yes you can. You can make a file that has the commands you would otherwise type in to set up the plot and open gnuplot running from that file. This link has an article that explains how to do it. You can also output to an EPS or other graphical output formats and display the plot using another widget that reads in the file.
You may need to use the '-persist' flag to the command. I know that on *nix systems, this flag is required if you want the plot window to remain after the gnuplot process has completed and exited.
gnuplot -persist commands.gp
Also, you can put as many gnuplot commands as you like in the file. The file acts sort of like a batch script in this regard.
You might need to add a line
pause -1
This will show the plot until return has been pressed.
What you are probably seeing is that gnuplot runs and exits before the plot has time to be displayed.
You might need to set a terminal type. Read the gnuplot documentation about that.