1

I usually deal with data file with multiple data block, repeating n number of lines for m number of times.

This is one of the example:

9276
4900000
      AA      4       4    6260     519    8350    1571       0       0       0       0       0       0       0   0.934   0.933    0.93   0.935       0       0       0       0       0       0   3.867     0.0   1.541
      BB      3       3    3388    8391    6637       0       0       0       0       0       0       0       0   0.939   0.565   0.361       0       0       0       0       0       0       0   1.913     2.0  -0.732
      CC      3       2     241     694       0       0       0       0       0       0       0       0       0   0.933   0.941       0       0       0       0       0       0       0       0   1.888     2.0  -0.834
      ...

Top line is total number of line for data block, second line is other value, those two lines are header. Then following 9276 lines are data. One datablock has 9278 lines, and then the same format of datablock repeats from 9279 line, for m number of data block. Usually m become really huge.

So far, I use something like this using f77 format:

       program test

       parameter (nn = 20000)
       integer,dimension(nn,nn) :: conn
       integer,dimension(nn) :: aaa,bbb,ccc,ddd,eee
       real,dimension(nn) :: fff,ggg,hhh,iii,zzz
       real,dimension(nn,nn) :: bos
       character*2,dimension(nn) :: elem

       open (2, file = 'input.txt', status = 'old')
       iframe=0
  100  continue
       iframe=iframe+1

       read (2,15, err = 50,end= 50) nat2
       read (2,15) framenum2
       do i=1,nat2
       read (2,42) elem(i),aaa(i),ccc(i),(conn(i,j),j=1,10),ddd(i),(bos(i,j),j=1,10),fff(i),ggg(i),zzz(i)
       enddo

       do something using do and if loops

       write (120,15) nat2
       write (120,15) framenum2
       do i=1,nat
        write (120,75) elem(i),test(i),result(i)
       write (121,76) iframe, zzz2, iii2
       enddo
       goto 100

I don't need to write formats, they are all properly formatted. As you could see, my script reads data per each datablock, do something, print them, then go to next datablock.

I learned fortran so long time ago, and I didn't cared that much about my style, and didn't think I should learn new style. But these days I feels like I should be more familiar with newer style of fortran. I feel I'm bit too late to learn new language at this point (mostly due to time constraint) so I think I better stick to the fortran...

Anyway, I want to change this to f90 style. But I'm not sure where should I need to begin. How can I improve this style of declaration, open, read, and write to be more efficient and faster? Any suggestion about those? I prefer f90 style but if there are any newer fortran way, I will be interested (if it is freeware and compilable with f90 or intel fortran)

exsonic01
  • 615
  • 7
  • 27
  • 2
    Firstly this is not Fortan77. It is Fortran90 at least, the :: in variable declarations and variable names longer than 6 characters are 2 things that tell me this, there may be more - people forget how primitive F77 was by today's standards. Secondly please show us a complete (but not over-long) code doing what you want to do, it will make answering much, much easier - and accurate. – Ian Bush Apr 23 '20 at 05:53
  • 4
    I suspect OP has not (yet) separated the concepts of *fixed source form* and FORTRAN77. One step to take would be to learn about *free source form*, it's a great aid to setting out source files which are easier to read and to understand. Even if you continue to write programs which use no features which were added to the language standard more recently than FORTRAN77. – High Performance Mark Apr 23 '20 at 07:20
  • 2
    Oh, and the other gratuitous comment to add is: Always use `implicit none`. Always. – High Performance Mark Apr 23 '20 at 09:32
  • @IanBush I just wish to learn how can I improve declaring, reading, and writing. About the full code, there are so many analysis I perform so I cannot pick one to show. But usually I do statistical analysis per each datablock and over entire datablocks. Get the average of specific columns, distance and angle among points, etc... – exsonic01 Apr 23 '20 at 13:53
  • @HighPerformanceMark Thanks, may I ask where can I get more info about free source form? – exsonic01 Apr 23 '20 at 13:55
  • @HighPerformanceMark Oh, this is about fixed format and free format fortran you are talking, right? – exsonic01 Apr 23 '20 at 14:01
  • *where can I get more info about free source form?* Your favourite search engine would be a good start. And there are some Qs and As hereabouts which may be useful to review. As to your second comment, the terms I use(d) are those that the language standard uses but yes, lots of people think of them as *fixed format* and *free format*. – High Performance Mark Apr 23 '20 at 15:06
  • Springer are offering this recent book on Fortran for free at the moment https://link.springer.com/book/10.1007/978-3-319-75502-1 I haven't read it but I own an earlier edition, and while it's not the first book I would recommend it's not bad and it's free. – Ian Bush Apr 23 '20 at 16:04

0 Answers0