Fortran77 data compiler problem

Click For Summary

Discussion Overview

The discussion revolves around a problem related to processing multiple data files in Fortran77 for a chemical simulation. Participants explore methods for extracting specific lines from these files, considering various programming languages and tools for the task.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares an unfinished Fortran77 code for reading and extracting data from multiple files, indicating the complexity of the data structure.
  • Another participant questions whether the files are ASCII readable and suggests using scripting languages like awk, ruby, or python for data extraction.
  • A different participant recommends Perl over awk and Python, citing its capabilities and availability on various systems.
  • A participant experimenting with Perl reports difficulties in filtering the correct lines from the data files and shares their current code approach.
  • Another participant provides a regex suggestion for correctly identifying and printing the desired line based on its number, indicating a potential solution to the previous participant's issue.
  • A participant expresses satisfaction with the results of their Perl implementation after testing it on their research data.

Areas of Agreement / Disagreement

Participants express varying preferences for programming languages and tools, with no consensus on a single best approach. The discussion includes multiple competing views on the most effective method for data extraction.

Contextual Notes

Some participants mention the need for clarification on the format of the data lines, indicating that assumptions about the data structure may affect the proposed solutions.

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')
 
Technology news 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

Similar threads

  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K