Search code examples
fortran

How to skip lines beginning with # when reading an external file


I am having trouble with my code. My goal is to calculate the average of 5 different columns of data. Problem is, in the data file every 4000 lines or so there is a comment beginning with #. I need to read each of these lines into a character string, so that my code will not read them as numbers to be added. I am doing this as a research project for my university and was instructed to use Fortran 77 without arrays. Here is my code:

    program mean_analysis
    implicit none
    
    integer i
    integer N
    double precision a, b, c, d, e
    double precision sumb
    double precision sumc
    double precision sumd
    double precision suma
    double precision sume
    character(100) event

    sumb = 0
    sumc = 0
    sumd = 0
    suma = 0
    sume = 0
    
    N=40010
    
    open(unit = 7, file="zpc_initial_momenta.dat")
        
        do i=1, 1
        read(7,'(A)') event
        end do
        do i=4002, 4002
        read(7,'(A)') event
        end do
        do i=8003, 8003
        read(7,'(A)') event
        end do
        do i=12004, 12004
        read(7,'(A)') event
        end do
        do i=16005, 16005
        read(7,'(A)') event
        end do
        do i=20006, 20006
        read(7,'(A)') event
        end do
        do i=24007, 24007
        read(7,'(A)') event
        end do
        do i=28008, 28008
        read(7,'(A)') event
        end do
        do i=32009, 32009
        read(7,'(A)') event
        end do
        do i=36010, 36010
        read(7,'(A)') event
        end do
                    
        do i=1, N 
        
            read(7,*) a, b, c, d, e 
            write(7, *) event
            suma = suma+a
            sumb = sumb+b
            sumc = sumc+c
            sumd = sumd+d
            sume = sume+e
            
                
        end do
        
        
    close(7)
    
    open(unit = 8, file="outputanalysis.dat")
            write(8,*) suma/N, sumb/N, sumc/N, sumd/N, sume/N
    close(8)
    
    end program
            

The issue is when I run this code, there is a runtime error. The code still thinks that each event (at the lines provided) is a number and attempts to add it into the sum for each column (Bad real number in item 1 of list input) [there is a # on the first line in the data file]). I want the program to essentially ignore each row beginning with a #.

Can someone fill me in on what I'm doing wrong, or suggestions on what to include? I am new to Fortran, so anything too advanced might go over my head.

I tried putting each "event" (rows beginning with #) into a character string so that they would not be read as a number. The program still thinks they are numbers to be added.


Solution

  • You're looking for Fortran's internal read capability. First, read a line from the file, then determine if it is a comment or needs to be parsed for values. Here's a quick hack. You should probably include error handling if a read fails.

        program mean_analysis
    
        implicit none
    
        integer i, n
        double precision a(5), s(5)
        character(100) event
    
        s = 0
        n = 0
    
        open(unit = 7, file="zpc_initial_momenta.dat")
        do
           read(7,'(A)',end=10) event
           event = adjustl(event)
    
           if (event(1:1) == '#') cycle  ! Skip comment lines
    
           read(event,*) a
           s = s + a
           n = n + 1
        end do
    
        10 close(7)
    
        open(unit = 8, file="outputanalysis.dat")
        write(8,*) s / n
        close(8)
    
        end program
    

    Edited: Seems you should make an appointment with the department chair or dean or provost. Forcing you to use a 46 year old dialect of a language is not preparing you for the future. If your prof insists on F77, then at least have some fun (although I doubt he'll look at the code).

          INTEGER I, N
          DOUBLE PRECISION A,B,C,D,E,SA,SB,SC,SD,SE
          DATA SA,SB,SC,SD,SE,N/5*0,0/
          COMMON SA, A
          OPEN(UNIT=7,FILE="zpc_initial_momenta.dat")
    1000  READ(7,*,ERR=1000,END=100) A,B,C,D,E
          CALL SA AND A
          SB=S  B + B
          S  C                    =SC                   + C
          SD =D + S      D
          CALLADDE(S     E,                          E)
          N = N + 1 
          GOTO 1000
    1 0 0 CONTINUE
          CLOSE(7)
          OPEN(UNIT=8,FILE="outputanalysis.dat")
          WRITE(8,*) SA/N, S B/N, S  C / N, SD/    N, SE/N
          CLOSE(8)
          END
          SUBROUTINE ADD E(S,                                             X)
          DOUBLEPRECISION S,X
          DOUBLE                                             PRECISION C,T,Y
          DATA C /0/
          Y = X - C
          T = S + Y
          C = (T - S) - Y
          S = T
          END
          SUBROUTINE SA AND A
          DOUBLE PRECISION SS,TT
          COMMON SS,TT
          SS = SS + TT
          END