Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Change directory using fortran

  1. Aug 23, 2014 #1
    I am using the following subroutine:

    Code (Text):
     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

    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,*)'plot  "Na_est_LCDM.txt" with errorbars lc rgb"'//TRIM(Point_color)//'" notitle,f(x) lc rgb"black" notitle'

        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 (Text):
    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 (Text):
            ret=SYSTEM('cd Graphs')
            ret=SYSTEM('gnuplot gp.txt')
        ret=SYSTEM('rm gp.txt')
            ret=SYSTEM('cd ..')
  2. jcsd
  3. Aug 23, 2014 #2


    User Avatar
    Science Advisor
    Homework Helper

    It doesn't work because each call to SYSTEM runs a different process. You could probably combine them by doing something like
    Code (Text):

    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 (Text):

    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.
  4. Aug 23, 2014 #3


    Staff: Mentor

    That won't work in any language I've seen save shell scripts

    Have you tried something like this for commands?

    Code (Text):

    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
  5. Aug 23, 2014 #4
    These doesn't work. I guess beacuse the txt. file

    Code (Text):
    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??
  6. Aug 23, 2014 #5


    User Avatar
    Science Advisor
    Homework Helper

    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?
  7. Aug 24, 2014 #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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook