Fortran77 data compiler problem

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
6 replies · 2K views
Carl Loomis-Anderson
Messages
6
Reaction score
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.

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

       WRITE(FN,10)I
       WRITE(6,*)FN
       OPEN(1,FILE=FN)
     
       DO j=1,(chemnum-1) ! cycles to the appropriate line
       READ(1,*)
       END DO

       Read(1,*)
                 CLOSE(1)
            
       END DO
     
       CLOSE(2)    10 FORMAT('plot',I2.2,'.DAT')
 
on Phys.org
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:

Bash:
grep "argon" *.DAT >summary.txt

If you needed something more complicated then switch to awk:

Code:
#!/bin/awk
# ----------------------------------------
#   name of script: mypgm.awk
# ----------------------------------------

/argon/ { if (some selection conditions) print $0 }

and you'd run it from the command line:
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.
 
Thanks for the advice! I'll look into switching.
 
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.
 
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:
#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>;
chomp$nrun;

#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;
}
 
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:
if( $line =~ /^\s*$num\s/ ){
   print $out "$line";
}

PS. Man! You are fast at picking up on Perl!
 
Thanks! I just tested it on my research and it had great results!
 
  • Like
Likes   Reactions: FactChecker