Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Reading Data in Fortran

Reading Data in Fortran

Reading Data in Fortran

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
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
goto 2015
2018 continue

An example of the data is as as attached.

Thank you very much for your help.

RE: Reading Data in Fortran

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:


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 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close