Change directory using fortran

In summary, I am using the following subroutine:subroutine plot_2pCF_2D(A_0,index_0,Point_color,Title)implicit noneinteger 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
  • #1
sketos
56
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
  • #2
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.
 
  • #3
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
 
  • #4
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??
 
  • #5
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
 
  • #6
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.
 

1. How do I change directory using fortran?

To change directory in fortran, use the chdir function and specify the new directory path as an argument. For example, chdir("/Users/username/new_directory/"); will change the current directory to the specified one.

2. What is the syntax for changing directory in fortran?

The syntax for changing directory in fortran is chdir(path), where path is the path to the new directory you want to switch to. This function is part of the system module in fortran.

3. Can I use relative paths when changing directory in fortran?

Yes, you can use relative paths when changing directory in fortran. Relative paths are specified as the path relative to the current working directory. For example, chdir("../new_directory/"); will change the current directory to the new_directory that is one level above the current working directory.

4. How do I check if the directory change was successful in fortran?

After using the chdir function, you can check the value of the errno variable. If the value is 0, then the directory change was successful. If the value is 1, then there was an error and the directory change was not successful.

5. Is there a way to change directory in fortran without using the chdir function?

Yes, you can use the cd command in fortran to change directory without using the chdir function. However, the cd command is not part of the standard fortran language and may vary depending on the compiler and operating system you are using.

Similar threads

  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
2
Replies
54
Views
4K
  • Programming and Computer Science
Replies
25
Views
10K
  • Programming and Computer Science
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Back
Top