0

I'm new to FORTRAN and need to work with a code to read and write Data. The Code is a bit older and I need to understand how things happen inside ;)

The code reads a Line of a .dat file with the

FORMAT(36A2): 
READ(11,FORMAT(36A2)) ITEXT

The variable ITEXT is declared as an Integer array: INTEGER(KIND=2), DIMENSION(36) :: ITEXT

So if I read in the following line:

          SREF   = 0.031416,
ITEXT hast the following value:
2313   8224  21075  17989   8224  15648  12320  12334  12595  12596  11318   8224   8224   8224   8224   8224   8224   8224   8224   8224   8224   8224   8224   8224   8224   8224   8224   8224   8224   8224   8224   8224   8224   8224   8224   8224

If I do WRITE(11,FORMAT(36A2)) it gives me back my Full Text. SREF = 0.031416,

So what is this Kind of "Integer Code"? How can I work with it and why should I use it?

It is completely new to me.

In the following part is an example Code. The integer "21075" represents "SR" of "SREF = 0.031416"

program example

    integer :: ITEXT = 21075

    WRITE (*,100), ITEXT
100 FORMAT(36A2)

end program example
PM Weis
  • 1
  • 4

2 Answers2

0

There was no character datatype in the original Fortran. Chracter data had to be stored in integers. Character strings could be stored in there using the obsolete Hollerith constants

  ITEXT = 2HSR

This stores two characters "SR" in ITEXT. Now a better way is to use read to store it there

  WRITE(ITEXT,'(A2)') "SR"

or

  WRITE(ITEXT,'("SR")')

The format itself

100 FORMAT(36A2)

is an ordinary string input/output format. It outputs 36 strings of length two. Typically it is applied to string data, but it can be applied to other data types as well if character data is stored in them. If the numeric data types actually contain numeric data, the output from the character format will be garbage.

Be aware that the literal constant 2 in (KIND=2) is not portable and is not guaranteed to be the right one to store two characters (unlike the non-standard INTEGER*2). See Fortran: integer*4 vs integer(4) vs integer(kind=4)

  • Thank you very much. Now I even know the name of this format. I should better change my whole skript to a state of the art format. – PM Weis Jun 24 '19 at 05:29
0

The code represents a really old fortran style. The characters of the string are encoded as the decimal values of the ASCII table into the integer elements of the array itext. As the code read the character string with the format A2 two characters of the input from the file are stored into each array element. The numerical value that results in any element will be:

ASCII_1*2**8 + ASCII_2

where ASCII_1 and ASCII_2 are the two decimal entries of the characters in the ASCII table.

As the first two characters are ' ' (blanks) with ASCII value 32, you get 32*2**8 + 32 = 8224

Try out your program with this

program bla
integer(KIND=1), DIMENSION(72) :: itext
open(11,file='bla.dat',status='old')
read(11,'(72a1)') itext
write(*,'(72a1)') itext
write(*,'(72(i4,2x))') itext
close(11)
end program bla

you will then get a string of numbers like 32 for a ' ', 83 for a 'S' etc.

Reinhard
  • 123
  • 7
  • Wow, thank you very much for the great explanation. I guessed, that it is an old format, no one uses anymore. – PM Weis Jun 24 '19 at 05:28