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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Data in Fortran

Status
Not open for further replies.

okoye

Programmer
Dec 4, 2013
7
0
0
US
Hi All,
I have been trying to read in a data using Fortran but for some reasons, it doesn't read the data correctly. Please, any help will be appreciated. To read the data, I used the following (Note, for the attached data, Nm=1, nw-nu=5, nw=10).

The array data(i,j,m,n) is declared as data(1:Nm,1:Nm,-nw:nw,0:nw-u). When it tries reading, it spits the error message "Subscript #4 of the array data has value 1 which is greater than the upper bound of 0". A snippet of the code is below.

2015 read(40,"(a72)") lineda
do n=0,nu
do m=-nw,nw-nu
do i=1,Nm
do j=1,Nm
if(index(lineda(1:72),'data') /= 0 ) then
read(40,"(1x,i2,1x,i2,2x,i5,2x,i5,1x,2(1x,e15.8))",IOSTAT=istat) i1,i2,i3,i4,r1,r2
data(i,j,m,n)=dcmplx(r1,r2)
write(809,"(1x,i2,1x,i2,2x,i5,2x,i5,1x,2(1x,e15.8))") i,j,m,n,data(i,j,m,n)
if(istat /= 0) goto 2018
else
goto 2015
endif
2018 continue
enddo
enddo
enddo
enddo

An example of the data is as as attached.

Thank you very much for your help.
 
 http://files.engineering.com/getfile.aspx?folder=ac38b0be-90c0-4b40-9e6f-c94d3c07dabc&file=data_read.dat
I highly recommend to stop using column-oriented reading and simply go for list-oriented, i.e., "read(40,*)"; for as long as the type of the value and the type of the variable match, things should read in just fine.

The first line in the data file seems to indicate that the first four numbers are meant to be i, j, n, m; but, variable declaration and do loops seem to indicate that those columns should be i, j, m, n...presently, to you, this is of no consequence as you read those numbers into dummy variables and do not use them directly as indices...clearly, this isn't cool...you could very well be placing input values into a different location.

You should do away with the nested do loops and use the read values as indices, something like:

Code:
nlines = ( nu - 0 + 1 )*( (nw-nu) - (-nw) + 1 )*( Nm - 1 + 1 )*( Nm - 1 + 1 )
do k = 1, nlines
    read(40,*) i, j, m, n, r1, r2
    data(i, j, m, n) = dcmplx(r1, r2)
end do
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top