Fortran How to Change Directory in Fortran for Gnuplot Execution?

  • Thread starter Thread starter sketos
  • Start date Start date
  • Tags Tags
    Change Fortran
Click For Summary
The discussion revolves around a Fortran subroutine designed to generate and execute gnuplot commands for plotting data. The user seeks guidance on how to change the working directory to save the gnuplot code in a specific folder while ensuring that the commands execute correctly. It is clarified that each call to the SYSTEM function in Fortran runs in a separate process, making directory changes ineffective between calls. Suggestions include combining commands into a single SYSTEM call using semicolons or directly specifying the path to the gnuplot file. Additionally, it is recommended to utilize gnuplot's built-in 'cd' command within the gnuplot script itself, rather than attempting to change directories in Fortran. The conversation highlights the limitations of directory management in Fortran and suggests alternative scripting languages like Python for more complex tasks involving file and directory manipulation.
sketos
Messages
55
Reaction score
0
I am using the following subroutine:

Code:
 subroutine plot_2pCF_2D(A_0,index_0,Point_color,Title)
 implicit none
 integer ret
 real A_0,index_0
 character(len=*) :: Point_color,Title

OPEN(10,ACCESS='SEQUENTIAL',FILE='gp.txt')
write(10,*)'set terminal postscript eps color enhanced size 5,7'
write(10,*)'set output "LCDM_Na_est.eps";'
write(10,*)'set logscale x'
write(10,*)'set logscale y'
write(10,*)'set zeroaxis'
write(10,*)'set yrange [0.001:]'

write(10,*)'set xlabel "{/Symbol q} (degrees)" font "Times-Roman, 20;'
write(10,*)'set ylabel "w({/Symbol q})" font "Times-Roman, 20;'
write(10,*)'set title "'//TRIM(Title)//'" font "Times-Roman, 30;'


write(10,*)'A1=',A_0
write(10,*)'g1=',index_0
write(10,*)'f(x)=10**(log10(A1)+(1-g1)*log10(x))'

write(10,*)'plot  "Na_est_LCDM.txt" with errorbars lc rgb"'//TRIM(Point_color)//'" notitle,f(x) lc rgb"black" notitle'

 CLOSE(10,STATUS='KEEP')	
	
	ret=SYSTEM('gnuplot gp.txt')
	ret=SYSTEM('rm gp.txt')

end subroutine

But if i want to save the gnuplot code in a different file(e.g the file Graphs) i can

Code:
OPEN(10,ACCESS='SEQUENTIAL',FILE='Graphs/gp.txt')

but the how can i change directory from fortran to this file complile the gnuplot code, then remove it and go back to my old directory?

This apparently doesn't work:

Code:
        ret=SYSTEM('cd Graphs')
        ret=SYSTEM('gnuplot gp.txt')
	ret=SYSTEM('rm gp.txt')
        ret=SYSTEM('cd ..')
 
Technology news on Phys.org
It doesn't work because each call to SYSTEM runs a different process. You could probably combine them by doing something like
Code:
ret=SYSTEM('cd Graphs; gnuplot gp.txt; rm gp.txt')
Adding a final "cd .." wouldn't make any difference.

Note, the exact syntax will depend on what OS you are running (Linux, Windows, OSX, etc).

Or you could do
Code:
ret=SYSTEM('gnuplot Graphs/gp.txt')
ret=SYSTEM('rm Graphs/gp.txt')
unless the commands in gp.txt only work if you run gnuplot when you are in the Graphs directory.
 
That won't work in any language I've seen save shell scripts

Have you tried something like this for commands?

Code:
gnuplot graphs/gp.txt

Then use the fortran file API to remove the file or use system with rm graphs/gp.txt

The problem you're seeing is that directory changes are not remembered between system calls

Another thing you could try is:

Cd graphs; gnuplot gp.txt
Rm graphs/gp.txt
 
These doesn't work. I guess beacuse the txt. file

Code:
write(10,*)'plot  "Na_est_LCDM.txt" ...

does not exist in the file Graphs. I did not think of that ...

So is there any way to force the program to create/tranfer my graphs in another file??
 
Sorry, I only read the question at the end of your OP, I didn't read all your Fortran code.

Since your program is generating all the gnuplot commands, why not use gnuplot's built in cd command instead of trying to change the directory before you run gnuplot?
http://www.gnuplot.info/docs_4.2/gnuplot.html#x1-7900023
 
hhhmmm...do you need to use Fortran? If you are going to be executing other scripts and changing directory and stuff like that, I would use some kind of shell: bash, tcl, python...I would choose python, of course.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 54 ·
2
Replies
54
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 25 ·
Replies
25
Views
10K
  • · Replies 9 ·
Replies
9
Views
9K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K