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

Fortran Fortran77 data compiler problem

  1. Jan 19, 2017 #1
    So I am doing a chemical simulation of titans atmosphere and I have potentially 1000 data files to sort through to retrieve concentration values written in Double format. The issue is that each chemical has its own line with well over 80 columns (1993 currently, though it is subject to increase). I was wondering how I could copy a line from each file and paste it into a single output file. This is the current unfinished code I have.

    Code (Fortran):
           CHARACTER(len=12) :: FN
    c      this is the number of runs/files. The character string has
    c      to edited for each chemical it tests
           N=5 !--arbitrary number of files, change for number runs
           CHARACTER*4 :: chem
           chem = C6H6
           INT chemnum
           chemnum = ****  !this is the number associated with the chemical

           OPEN(2,FILE=(chem+".DAT"), STATUS="NEW")
           DO I=1,N

           DO j=1,(chemnum-1) ! cycles to the appropriate line
           END DO


           END DO

        10 FORMAT('plot',I2.2,'.DAT')
  2. jcsd
  3. Jan 19, 2017 #2


    Staff: Mentor

    Are these files ascii readable files?

    If so then you should consider using a scripting language like awk, ruby or python to read, extract and collect the data into a summarized ascii file.

    My guess would be that the grep command could handle this:

    Code (Bash):

    grep "argon" *.DAT >summary.txt
    If you needed something more complicated then switch to awk:

    Code (awk):

    # ----------------------------------------
    #   name of script: mypgm.awk
    # ----------------------------------------

    /argon/ { if (some selection conditions) print $0 }
    and you'd run it from the command line:
    Code (Bash):

    $ chmod 777 mypgm.awk

    $ ./mypgm.awk *.DAT >summary.txt
    In both cases, they are looking for the line that contains "argon" and then printing it out. The >summary.txt instructs the bash shell to redirect standard output to the file summary.txt.

    If you need to count down lines as your code is doing then awk has an internal variable that can be used
    instead of the /argon/ matcher.
  4. Jan 19, 2017 #3
    Thanks for the advice! I'll look into switching.
  5. Jan 19, 2017 #4


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    I recommend Perl instead of the Unix awk, grep, etc. It gave much more capability, was always available on Unix/Linux, and could be easily installed on Windows machines.
    I also recommend Perl over Python for scripting tasks like yours.
  6. Jan 20, 2017 #5
    I tried out perl and have made much quicker progress than with fortran. I am having a hangup though. The program is copying every line I don't need and not the line that I do need.
    Code (Text):
    #declare parameters
    say "what is the chemical formula?";
    my $chem = <STDIN> ;
    chomp $chem;
    say $chem;
    say "what is the number of the chemical?";#this specifies the line needed
    my $num = <STDIN>;
    chomp $num;
    say $num;
    say "what is the amount of runs?";
    my $nrun = <STDIN>;

    #this section will loop through the plot files
    #    keeping the chosen data.
    $a = 0;
    open (my $out, ">>", "$chem.dat"); #opens output with name 'chem'.dat
    until ($a >= $nrun) #loop for opening and collecting from data files
        say "plot$a.dat";
        open (my $fn, "<", "plot$a.dat")
            or die "can't open plot";
        $a = $a +1;
        while (my $line = <$fn>)
            next if $line =~ $num;
            print $out "$line";
        close $fn;
  7. Jan 20, 2017 #6


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    I will assume that the files of data are ASCII files with a line number at the beginning of each line (it would help if you show us what the format of the lines are.)

    Try this. It looks at the beginning of the line ('^' indicates that), followed by any number of spaces or tabs ('\s*' indicates that), then the desired number $num, then a space or tab ('\s' indicates that). If it finds that pattern on the line, it prints the line to $out.
    Code (Text):

    if( $line =~ /^\s*$num\s/ ){
       print $out "$line";
    PS. Man! You are fast at picking up on Perl!
  8. Jan 20, 2017 #7
    Thanks! I just tested it on my research and it had great results!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted