FORTRAN95 dealing with space delimited data files

Click For Summary

Discussion Overview

The discussion revolves around reading space-delimited data files in FORTRAN 95, specifically focusing on how to efficiently read a 20x20 matrix of integers from a file. Participants explore various methods and formats for the READ statement to improve the initial cumbersome approach.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes an initial method for reading a matrix that results in only the first column being read correctly, leading to an inefficient workaround.
  • Another participant suggests using a labeled FORMAT statement and an implied DO construct to streamline the reading process.
  • A third participant, transitioning from FORTRAN 77 to FORTRAN 90, emphasizes avoiding labels and suggests using a simpler format without specifying width, while cautioning against using a fixed width that may not match the data file.
  • Another participant points out the necessity of including a width in the FORTRAN edit descriptor and proposes a different FORMAT that may be more appropriate for the data structure.
  • A final participant expresses appreciation for the advice and indicates intent to experiment with the suggested READ and FORMAT statements.

Areas of Agreement / Disagreement

Participants present multiple competing views on the best approach to reading the data, with no consensus reached on a single method. There is disagreement on the appropriate width for the FORMAT statement and whether to use a labeled FORMAT or an implied DO construct.

Contextual Notes

Some participants note the importance of matching the format to the actual data structure in the file, highlighting potential issues with column orientation and the necessity of specifying widths in edit descriptors.

Who May Find This Useful

Readers interested in FORTRAN programming, particularly those working with data file input and matrix manipulation, may find the discussion beneficial.

JesseC
Messages
247
Reaction score
2
I'm doing project Euler ID11 (http://projecteuler.net/index.php?section=problems&id=11), and I thought I'd try this one in FORTRAN 95. The first problem in the project is finding a suitable way of reading in a 20x20 matrix of integers.

I'm doing it by put the 20x20 matrix into a separate file called data1.txt and thought I could have FORTRAN read in each data point like this:
Code:
integer :: m(20,20), i, j
open(1001,file='data1.txt')
do i = 1, 20
    do j = 1, 20
	    read(1001,*) m(i,j)
    end do
end do
The problem I have is that it READ only reads the first column before reaching end of file and skips the space delimited data on the same row. I got around this by writing:
Code:
integer :: m(20,20), i
open(1001,file='data1.txt')
do i = 1, 20
	read(1001,*) m(i,1), m(i,2), m(i,3), m(i,4), m(i,5), m(i,6), m(i,7), &
	m(i,8), m(i,9), m(i,10), m(i,11), m(i,12), m(i,13), m(i,14), m(i,15), &
	m(i,16), m(i,17), m(i,18), m(i,19), m(i,20)
end do
rewind(1001); close(1001);
But this is a really cumbersome and ugly solution! Surely there must be a better way?
 
Technology news on Phys.org
Instead of using the default format '*' in your READ statement, you can reference a labeled FORMAT statement. In the FORMAT statement, you can account for how the data is organized in the data file. In the corresponding READ statement, try using an implied DO construct. For example:

READ (1001, 2000) M (I, J) (J = 1, 20)

2000 FORMAT (20I5)
 
After many years of F77, I have just started to program in Fortran 90...so, I now do not use GOTOs nor CONTINUEs nor labels...

...along the idea of not using labels, I have also stopped using FORMAT statements...I either put the format string straight into the READ statement or declared a CHARACTER variable if I need to re-use the format and then put the string into the READ...

...having said that, SteamKing has the right idea, you need to use 20I...but do not use 20I5 because this truly will make your reading column oriented and it is not the correct constant width in your file; if anything, it looks like it could be 3, but I cannot tell from here whether the left-most numbers have a space on the left of them or not...so, just use 20I, like this:

READ(1001, '(20I)' ) ( M(I,J), J=1,20 )

I have capitalized all Fortran, but when I write code, I never capitalize anything (other than a few camel case variable names) ...it just make the screen so much more crowded...
 
The Fortran I edit descriptor must include a width, so that I3 and I5 are valid examples, but a simple I will generate an error.

For the table which you wish to read (as referenced by the link in the OP) a better fix would be:

READ (1001, 2000) M (I, J) (J = 1, 20)

2000 FORMAT (20I3)

and the following READ would also be valid (without a FORMAT):

READ (1001, *) M (I, J) (J = 1, 20)
 
Thanks for the advice, I'll play around with the READ and FORMAT statements.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K