How to Change Directory in Fortran for Gnuplot Execution?

  • Context: Fortran 
  • Thread starter Thread starter sketos
  • Start date Start date
  • Tags Tags
    Change Fortran
Click For Summary

Discussion Overview

The discussion revolves around how to change directories in Fortran when executing Gnuplot commands, particularly when the Gnuplot script needs to be saved in a different directory. Participants explore various methods to manage directory changes and file paths within the Fortran subroutine.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant suggests using the command ret=SYSTEM('cd Graphs; gnuplot gp.txt; rm gp.txt') to combine directory change and command execution in one line.
  • Another participant notes that each call to SYSTEM runs in a separate process, meaning directory changes do not persist across calls.
  • It is proposed to directly call Gnuplot with the file path as ret=SYSTEM('gnuplot Graphs/gp.txt') and remove the file afterward.
  • A participant mentions that the Gnuplot commands may require the working directory to be set correctly for the input data file Na_est_LCDM.txt to be found.
  • One suggestion is to use Gnuplot's built-in cd command within the Gnuplot script instead of changing directories in Fortran.
  • Another participant questions the necessity of using Fortran for this task, suggesting that a shell script or a language like Python might be more suitable for handling directory changes and executing commands.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to manage directory changes and file paths in Fortran when executing Gnuplot commands. No consensus is reached on a single solution.

Contextual Notes

Participants note that the effectiveness of certain commands may depend on the operating system being used, and there are unresolved questions about the dependencies of the Gnuplot commands on the current working directory.

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.
 

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