0

I have a file which has values of pressure at every element. I require the element number (ELNO) and pressure (PLOAD) and pass through a subroutine. I am not able to read them separately from the file. The data would read like given below starting with S175..

I want to be able to read this file, say A0001.txt, and read the lines one-by-one. When reading the lines I want to store the number after the first dot, for eg 1007, 1008 etc into a variable ELNO and the number after the last comma in a variable PLOAD in a loop. This is because I will require each value of ELNO and check a condition with the IF loop.

My problem is reading the file and storing in a variable like ELNO(i) something like that. The name S175 is constant.

I think I understand the logic. I have to store each like as a string and start iterating from the 6th position in the string till it finds the first "," and store that in ELNO(i). But I am new to FORTRAN and not able to get it. I have been trying for the past week learning FORTRAN to do this. But, not able to do this problem.

I tried a code like this below but its not reading line by line since I did not put it under a loop I guess.

     S175.1007,P,0.221948
     S175.1008,P,0.221943
     S175.1009,P,0.221929
     S175.1010,P,0.222287
     S175.1018,P,0.222438
     S175.1019,P,0.222425
     .....
     .....
     .....
     .....
     .....
     S175.13000,P,-1990
     S175.13001,P,-1980
     S175.13002,P,-2009

  PROGRAM BARGE 
     implicit none    
     CHARACTER X*80
     OPEN(UNIT=60, FILE="A0001.txt", ACCESS='SEQUENTIAL', ACTION='READ')
     READ(UNIT=60, FMT='(A)', END=10)X
  10 OPEN(UNIT=61, FILE="2.txt", ACTION="write")
     WRITE (UNIT=61,FMT='(A)')X 


  END PROGRAM BARGE
Arun Krishnan
  • 85
  • 1
  • 9
  • Have you read https://stackoverflow.com/questions/1262695/convert-integers-to-strings-to-create-output-filenames-at-run-time ? – Vladimir F Героям слава Apr 24 '19 at 11:49
  • Thank you. I did not see this. I tried googling but did not find this example. I have done it in the meantime. May be there is better method. I am a novice so this might not be the best way to do it. – Arun Krishnan Apr 24 '19 at 12:44

2 Answers2

1

Thank you everyone. I have completed it myself. There might be an easier and faster method but this works fine. Let me know if there is a more efficient method. I could learn. :)

   PROGRAM BARGE    
     implicit none    
     CHARACTER PRES*80       
     INTEGER  :: SUCCESS
     INTEGER  :: K, L, M, ELNO    ! K is for the element number,L is word P and M is for pressure value
     REAL     :: PLOAD
     OPEN(UNIT=60, FILE="1.txt", ACCESS='SEQUENTIAL', ACTION='READ')
     DO
        READ(UNIT=60, FMT='(A)', IOSTAT=SUCCESS)PRES
        IF (SUCCESS.NE.0) EXIT
        K=6
        DO WHILE (PRES(K:K) .NE. ',')
           K=K+1
        END DO          

        READ(PRES(6:K-1), *) ELNO
        PRINT *, ELNO     ! ELEMENT NUMBER          
        L=K+1

        DO WHILE (PRES(L:L) .NE. ',')
           L=L+1
        END DO


        M=L+1

        DO WHILE (PRES(M:M) .NE. ' ')
           M=M+1
        END DO

        READ(PRES(L+1:M-1), *) PLOAD     ! PRESSURE ON THE ELEMENT      
        PRINT *, PLOAD      

        OPEN(UNIT=61, FILE="2.txt", ACTION="write")
        WRITE (UNIT=61,FMT='(A)')PRES   
     END DO 
  READ (*,*) PRES
  END PROGRAM BARGE
Arun Krishnan
  • 85
  • 1
  • 9
0

It would be easy to use Fortran's list-directed input to read the data items from a line such as S175.1019,P,0.222425. The following snippet can replace the do loop in OP's own answer:

DO
  READ(UNIT=60, FMT='(A)', IOSTAT=SUCCESS) PRES
  IF (SUCCESS.NE.0) EXIT
  READ(PRES(6:),*) ELNO, P, PLOAD
  WRITE(*,*) ELNO, PLOAD
END DO

For this to work you have to include a declaration such as

CHARACTER(LEN=1) :: P

to catch the letter P in each line of the input file. The important line is this one

READ(PRES(6:),*) ELNO, P, PLOAD

which uses the edit descriptor * which tells the compiler / run-time to figure out how to read values for the three variables (one integer, one character, one real) from the 6th and following characters in the line PRES.

Fortunately, with a nice clean input file such as the one shown the compiler has no trouble reading the values, all the scanning for occurrences of , is unnecessary. If, for another application, you ever need to search a line for occurrences of a character use the intrinsic function index.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161