Search code examples
fortrangfortranread-data

Compare data from 3 dat files


I am trying to write a code in Fortran 90 that reads the data from 3 different files (called "data1.dat", "data2.dat", data3.dat"). All the files are made by 3 columns and N lines (the lines depend on another code), and then do the following operation:

C(i)=(data1(i)-data2(i))/(data2(i)-data3(i))

When i represents the data "i" of the second column of each file.

I know how to read the files but I don't know how to manipulate the data from each file.

Any thoughts?


Solution

  • I will count on you to have all variable declaration done and all files open.

    I don't have a compiler at hand so please verify my syntax before using it. Hope the following serves as something between a working code and a pseudo code:

    OPEN (400, file='XXXX.YYY', status='UNKNOWN')
    
    DO i = 1, NMAX
        READ(100, *) data1
        READ(200, *) data2
        READ(300, *) data3
    
        denom = data2 - data3
    
        IF (denom .EQ. 0.) STOP "Cannot Divide by zero"
    
        CCC = (data1-data2)/denom
        WRITE (400, *) CCC
    ENDDO
    

    Each time through only one line in each file is read to memory; CCC is in turn calculated and written to an output file. In the next iteration, the program will read the line below instead of starting from top. No array needed.

    In the end remember to close your files. You can play with IO formatting wherever you see fit.

    Hope this helps.