Get format in which text file was written in FORTRAN

0

Hello, I need to make a program that reads from an input file that has lines of text written in specific formats and process the read information in order to create an output file with some data of interest. I know all the formats used to write the input file (they are described in a format file), but the data itself is written by another program, in a "random" way. Some lines are easy to identify (headers and end-of-table markings, etc.) others do not. It would be much easier if FORTRAN was somehow able to identify the format in which each line read from the input file was written (for example: '(1X, I5,3F8.1,2 (5A, 1X))) . So I could just compare the format of the line with one of the formats of the list I own and extract almost directly the necessary data, for example knowing that the format of the line read and stored in the LINE variable is described in the variable FORMAT, could do something of type:

IF (FORMATO='(1X,I5,3F8.1,2(5A,1X))') THEN
  READ(LINHA,'(6X,F8.1)') minha_variavel
END IF

Because there may be another read line format such as:

'(6A, 2F8.1, F8.6,2 (6A))'

If I use the same READ command above, I will have an F8.1 variable written in "my_variable", but this will not be the value I wanted.

Does anyone know if FORTRAN has a function that does something similar (get the format in which a line of text file was written)? If yes, what is the function?

    
asked by anonymous 01.02.2018 / 16:31

1 answer

2

Problem solved using a mixture of some of the suggestions posted on the stack in English. I have read each of the lines of the input file in an internal variable (RLIN) in the format '(A165)'. After that, I read all the contents of the string that I put in this internal variable in several dummy variables, using the format I knew of the lines from where I wanted to get some information (read all the information of the line in the desired format and read with IOSTAT = 0 I guarantee that this is the correct line), so if the result of the reading is ok (IOSTAT = 0), it is because the line I just read was the correct one for the information I wanted, so I store the contents of some of the variables dummy variables that represent the values that interest me. In the code, the solution looked something like this:

OPEN(UNIT=LU1,FILE=RlinName,STATUS='OLD')
ilin = 0
formato = '(14X,A,1X,F7.1,1X,F7.1,5X,A,1X,A,1X,A,5X,A,I5,1X,A,I3,3F8.1,A,A,A,1X,A,2(1X,F8.2),1X,A,1X,A)'
DO WHILE (.TRUE.)
  READ(LU1,'(A165)',END=300) RLINFILE
  READ(RLINFILE,formato,IOSTAT=linhaok) dum2_a1,dum2_f1,dum2_f2,dum2_a2,dum2_a3,dum2_a4,dum2_a5,dum2_i1,dum2_a6,dum2_i2,dum2_f3,dum2_f4,dum2_f5,dum2_a7,dum2_a8,dum2_a9,dum2_a10,dum2_f6,dum2_f7,dum2_a11,dum2_a12
  IF(linhaok.EQ.0) THEN
    ilin = ilin+1
    rlin_lshu(ilin) = dum2_a4
    rlin_nbpa(ilin) = dum2_i1
    rlin_ncir(ilin) = dum2_i2
    rlin_ppij(ilin) = dum2_f3
    rlin_pqij(ilin) = dum2_f4
    rlin_tapn(ilin) = dum2_a7
  END IF
END DO
300 CLOSE(UNIT=LU1)
    
05.02.2018 / 13:21