Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fortran - Reading a 2D Array Column-by-Column 1

Status
Not open for further replies.

mwatson87

Technical User
May 28, 2013
5
GB
Hi Guys,

Im relatively new to fortran but been learning FAST! :D I am using fortran to carry out thousands of iterative calculations from a data set which is in the form of a csv file. My code works excellently considering how new I am to fortran, however, I have encountered a big problem (that seems simple) and i can't get past it.

The data set is 2601 rows by 20 column. I need the program to - read one column - store as an array - analyse it - read the next column - etc etc etc... Till all columns are analysed. I have shown an excerpt of the code below (the code ONLY shows the part I cannot solve). Basically, I need to store the data as an array, column by column, so if my method is wrong then please comment.


Code:
Program ReadingTest
	implicit none

!----------------------------------------------------------------------------------------------------------------------
    
	Integer :: row
    Integer :: col
    Real, Dimension(2601,20) :: Fo

!----------------------------------------------------------------------------------------------------------------------

	Open (unit=1, file='G:\Programs\SilverFrost\FTN95\FULLACC.csv', status='old', action='read')
		
Do 15 col = 1,20
  		
	Do 10 row = 1,2601
		
			Read (1,*) Fo(row,col)
            print *, Fo(row,col)
	
	10 continue

15 Continue
            
    Close(1)
  
!----------------------------------------------------------------------------------------------------------------------
!
!IN THIS SECTION I HAVE INPUT A COMPLEX GEOTECHNICAL FUNCTION WHICH IS IRRELEVANT TO MY PROBLEM AND HENCE REMOVED
!
!----------------------------------------------------------------------------------------------------------------------

End Program ReadingTest


Fortran seems to have issues when reading the array and is very particular. For some reason it gives the error 'Attempt to read past end of file'. However I have specified at the end of reading one column, it moves to the next, starting at 1 again so I am not sure why this error is ocuring. I have done my research on this however it seems this one i cannot solve alone.

Thanks in advance guys for any help you can give,

Martin
 
I have had a bit of a brainwave.... would making it an 'allocatable array' be better? If so I have never done this so pointers would be excellent :D
 
You seem to be taking a human approach to this reading process as if you could simply run your finger down a column and then the next...

...that's not so easily done with a computer program, as the program would need to skip stuff, etc.

What you need to do is simply read the entire table in a single loop and THEN, afterwards, in another loop, process one-column at a time.

I am improvising, here, and have not compiled this but reading the table should go something like this:

Code:
do row = 1, 2601
    read(1,*) Fo(row,:)
end do

or like this:
Code:
do row = 1, 2601
    read(1,*) ( Fo(row,col), col=1,20)
end do

 
The reading on this one isn't very efficient but if you are processing the data as a 1D array, it is a lot simpler.

Code:
real, dimension(2601):: Fo
integer:: row, col

open (1, ...)
do n = 1, 20
   rewind(1)
   do row = 1, 2601
      ! Read the data in the nth column
      read(1, *) (Fo(row), col = 1, n)
   end do
   ! Process Fo
end do
close(1)
 
Thanks for the help guys, I got it sorted thanks to your help :)

Kind Regards,

Martin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top