0

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?

francescalus
  • 30,576
  • 16
  • 61
  • 96
Xoca
  • 23
  • 4

1 Answers1

0

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.

Jerry Hu
  • 357
  • 5
  • 17
  • What is the purpose of the `status='UNKNOWN'` why not `replace` or nothing at all? – Vladimir F Героям слава Jun 29 '16 at 10:07
  • 1
    If you use `REPLACE`, and it doesn't exist, there might be an error. If you use `NEW` and it does, there certainly is an error. `UNKNOWN` is uglier, but more flexible for a short snippet. – chw21 Jun 29 '16 at 13:58
  • From context, I think you're taking `data1` etc as arrays, making `denom` an array. This means one should be careful with `if(denom.eq.0)` - perhaps `if(any(denom.eq.0))`? Or do you mean that there is reference to only the first column? [I find the question difficult to understand.] – francescalus Jun 29 '16 at 15:12
  • @chw21 With `replace` and the file not existing, there should not be an error (the file is just created). I think the point about "nothing at all" from Vladimir F, is that `unknown` is the default if the specifier is omitted. – francescalus Jun 29 '16 at 15:13
  • Thanks, maybe this should work, but I have another problem, I have to know the number of files "NMAX" in the data. There is a simple way to do that? Or is easiest to check in the data file the number of files. Thanks again! – Xoca Jun 29 '16 at 15:35
  • @francescalus there is no array, as data1, data2, and data3 are updated each time through iterations. – Jerry Hu Jun 30 '16 at 00:37
  • @Xoca there are two ways: The first and less preferable is to quickly skim through the files and record file length, assign NMAX, and start the "real" reading. Second is to use a DO WHILE logic so the program will keep reading until end of file, where it will return an error and break the DO loop. – Jerry Hu Jun 30 '16 at 00:46