How to Change Directory in Fortran for Gnuplot Execution?

  • Context: Fortran 
  • Thread starter Thread starter sketos
  • Start date Start date
  • Tags Tags
    Change Fortran
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 7K views
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 ..')
 
Physics 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??
 
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.