Please, Help with Data Reading in Fortran

In summary: The values of Nm, nw, and nwuThe code that calls the routine where the error occursThe input dataWithout that, we can't help you.In summary, the conversation is about a problem with reading data using Fortran. The array data(i,j,m,n) is declared as data(1:Nm,1:Nm,-nw:nw,0:nw-u) but when trying to read the data, an error message appears for the subscript #4 of the array being out of bound. The code and sample data are provided for reference. The problem is identified as a typo in the declaration of the array, where nw-u should be nwu.
  • #1
komp
5
0
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

The sample data is attached.

Thank you very much for your help.
 

Attachments

  • data_read.txt
    5.1 KB · Views: 446
Last edited:
Technology news on Phys.org
  • #2
komp said:
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".
You don't show the actual array declaration, so I'll have to take your word that it is declared as you say above. The fourth dimension of array has indexes that range betwee 0 and nw - u.

What is u? You don't mention it above. Was this a typo and it should be nu?
komp said:
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

The sample data is attached.

Thank you very much for your help.
 
  • #3
Thank you very much for your assistant.
Sorry as I wasn't explicit enough. The array is declared as:
complex*16:: data(1:Nm,1:Nm,-nw:nw,0:nwu)
And for clarity, nw-u is actually nwu and not a subtraction as it seem to show.
 
  • #4
komp said:
Thank you very much for your assistant.
Sorry as I wasn't explicit enough. The array is declared as:
complex*16:: data(1:Nm,1:Nm,-nw:nw,0:nwu)
That's not what you wrote in post #1, which I have copied below.
The array data(i,j,m,n) is declared as data(1:Nm,1:Nm,-nw:nw,0:nw-u)
komp said:
And for clarity, nw-u is actually nwu and not a subtraction as it seem to show.
So if it's actually nwu, what is the value of nwu?

Your error message shows that there is a problem with subscript 4 of your array.
 
  • #5
It is the same. I just explained that nw-u is actually nwu and not subtraction as it seem to show. Yes, it shows that the error is in the 4th argument but I don't know why it should be out of bound when I clearly defined it.

Thanks.
 
  • #6
Compilers are totally literal beasts.

You may have written "nwu" in your declaration, but somehow, the compiler is reading and trying to figure out what to do with "nw-u", since that is what you copied from your source file.

Without any other reference for us to look at, how are we supposed to help you solve this problem?
 
  • #7
komp said:
It is the same.
NO! nw-u and nwu are completely different!

You wrote nw-u in your post (which I quoted verbatim), but you didn't include the part of your code where you actually declared your array (named data).
komp said:
I just explained that nw-u is actually nwu and not subtraction as it seem to show. Yes, it shows that the error is in the 4th argument but I don't know why it should be out of bound when I clearly defined it.
Show us
  1. The declaration of your array
  2. The declaration of nwu
 
Last edited:

FAQ: Please, Help with Data Reading in Fortran

What is Fortran and why is it used for data reading?

Fortran is a high-level programming language commonly used in scientific and engineering fields. It is known for its efficient handling of mathematical computations and its ability to process large amounts of data. Fortran is often used for data reading because it has built-in features and libraries specifically designed for handling and manipulating data.

How do I read data from a text file in Fortran?

To read data from a text file in Fortran, you can use the "READ" statement. This statement allows you to specify the input file, the format of the data, and the variables to store the data in. You can also use the "OPEN" statement to open the file before reading and the "CLOSE" statement to close the file after reading.

What is the difference between sequential and direct access when reading data in Fortran?

Sequential access means reading the data in the order it appears in the file, while direct access allows you to read specific data points by specifying their location in the file. Sequential access is typically used for reading data from text files, while direct access is used for reading data from binary files.

How do I handle errors when reading data in Fortran?

Fortran has built-in error handling mechanisms that you can use when reading data. These include the "IOSTAT" and "ERR" keywords, which can be used to check for errors during a read operation and take appropriate actions. You can also use the "END" statement to end the program if an error occurs.

Can I read data from external sources, such as a database, in Fortran?

Yes, Fortran has the ability to read data from external sources such as databases. This can be done using libraries or modules that provide database connectivity. These libraries typically have their own set of functions and methods for reading data from a specific type of database. You can also use the "READ" statement to read data from external sources if the appropriate libraries are installed.

Similar threads

Replies
12
Views
2K
Replies
2
Views
1K
Replies
1
Views
3K
Replies
5
Views
2K
Replies
2
Views
1K
Replies
4
Views
1K
Replies
6
Views
1K
Replies
4
Views
8K
Back
Top