Fortran Having trouble when reading file using Fortran 90

AI Thread Summary
The discussion revolves around troubleshooting a Fortran 90 script designed to read rainfall data from a CSV file. The user encountered issues with the read statement, specifically using '()' instead of '*', which was corrected to successfully skip header rows. Further complications arose with an out-of-range error related to the flag_11 array, indicating that its values were not properly initialized, leading to attempts to access invalid indices. The conversation also highlighted the need for better variable naming and code readability, as well as concerns about memory usage due to large array declarations. Ultimately, the user was advised to review their code structure and ensure proper initialization of variables to avoid runtime errors.
Novia Lusiana
Messages
6
Reaction score
0
TL;DR Summary
I am a new user in fortran language programming, I would calculate the rainfall data (.csv) using the script in Fortran. The script is attached on below.
However, after i executed the script there is an error especially in line this one --> read (51, '()').
I want the software read all data in 51 files. But i do not have any idea what the mistake is and how to fix it.
Please give me suggestion. Thank you in advance.
Fortran:
implicit none!do not use an undetermined constant
character(len=18)::fname
character(len=22)::fname_2
character(len=19)::fname_3
character(len=7)::yyyymmd
character(len=1)::dash
character(len=1)::d
character(len=1)::f1
character(len=1)::f2
character(len=1)::f3
character(len=1)::f4
character(len=2)::f5
character(len=1)::zero
character(len=4)::last
      character(len=4)::last_2
      integer  i,j,count_num,k
real x(1495,1002,300),lat(1495), longt(1002),x_sum(1495, 1002)
integer flag_d(300),flag_1(300),flag_2(300),flag_3(300)
integer flag_11(0:1495)!only one day data can be inputed!
      real x_sumh(1495,1002,0:23)
101   format(5(f7.2,","),f6.2)
      !*********reading file***********
      open(51,file="input.csv",status="old")
count_num=0
read (51,'()')!skip the header rows
do
read (51,*,end =100)
count_num=count_num+1
end do
100 continue
rewind(51)!return to previous files
 
Last edited by a moderator:
Technology news on Phys.org
The '()' is not a valid format. Replace it with *.
 
Novia Lusiana said:
Summary:: I am a new user in fortran language programming, I would calculate the rainfall data (.csv) using the script in Fortran. The script is attached on below.
However, after i executed the script there is an error especially in line this one --> read (51, '()').
I want the software read all data in 51 files. But i do not have any idea what the mistake is and how to fix it.
Please give me suggestion. Thank you in advance.

Fortran:
implicit none!do not use an undetermined constant
character(len=18)::fname
character(len=22)::fname_2
character(len=19)::fname_3
character(len=7)::yyyymmd
character(len=1)::dash
character(len=1)::d
character(len=1)::f1
character(len=1)::f2
character(len=1)::f3
character(len=1)::f4
character(len=2)::f5
character(len=1)::zero
character(len=4)::last
      character(len=4)::last_2
      integer  i,j,count_num,k
real x(1495,1002,300),lat(1495), longt(1002),x_sum(1495, 1002)
integer flag_d(300),flag_1(300),flag_2(300),flag_3(300)
integer flag_11(0:1495)!only one day data can be inputed!
      real x_sumh(1495,1002,0:23)
101   format(5(f7.2,","),f6.2)
      !*********reading file***********
      open(51,file="input.csv",status="old")
count_num=0
read (51,'()')!skip the header rows
do
read (51,*,end =100)
count_num=count_num+1
end do
100 continue
rewind(51)!return to previous files
Two things come to mind:
  1. If you intend to read from 51 files, you will need to open each of them individually and read from them.
  2. When you read from a file, you need to have a variable or an array into which the values read from the file get stored. In your two read statements, there is no variable.
Here's a link to a brief tutorial on how to do file I/O - Fortran - File Input Output - Tutorialspoint
 
Mark44 said:
Two things come to mind:
  1. If you intend to read from 51 files, you will need to open each of them individually and read from them.
  2. When you read from a file, you need to have a variable or an array into which the values read from the file get stored. In your two read statements, there is no variable.
Here's a link to a brief tutorial on how to do file I/O - Fortran - File Input Output - Tutorialspoint
what do you mean with "no variable" overthere? should I give some variables after the statement?
 
FactChecker said:
The '()' is not a valid format. Replace it with *.
Thank you for your answer, but I already replace ```````"()" with (*), but the error still exist at the same line.
 
Novia Lusiana said:
what do you mean with "no variable" overthere? should I give some variables after the statement?
Did you look at the link I posted? There are examples of reading from and writing to files.
Novia Lusiana said:
Thank you for your answer, but I already replace ```````"()" with (*), but the error still exist at the same line.
You need a variable in your read statement.
 
I did not say (*). Do not include parenthesis, only *.
Fortran:
read (51,*) !skip the header rows
 
Last edited:
Mark44 said:
Did you look at the link I posted? There are examples of reading from and writing to files.
You need a variable in your read statement.
yess...i already saw it and the problem has been solved. But anyway I have a trouble again here. This is the script, and after i executed I got a report : `"Run-time error: ../rainfall_2.f90 (99): X_SUMH's third subscript (value -1) is out of range (0:23)". Would you please tell me what is the mistake? for your information that i is number of rows, j is number of coloumn on my .csv data.

open(11,file="20180706-1300.CSV",status='old')!input the data
do i=1,3200,1
read(11,*) x(i,1:3200,k)
end do
do i=1,3200,1
do j=1,3200,1
if(x(i,j,k)<0 )x(i,j,k)=0
x_sum(i,j)=x_sum(i,j)+x(i,j,k)
end do
end do
do i=1,3200,1
do j=1,3200,1
x_sumh(i,j,flag_11(i))=x_sumh(i,j,flag_11(i))+x(i,j,k)!total data
end do
end do
 
FactChecker said:
I did not say (*). Do not include parenthesis, only *.
Fortran:
read (51,*) !skip the header rows
Thank you, it works!
 
  • #10
Novia Lusiana said:
Fortran:
x_sumh(i,j,flag_11(i))=x_sumh(i,j,flag_11(i))+x(i,j,k)!total data
The error message is pretty clear. A value for an element of the flag_11 array is -1, but should be in the range 0 through 23, being that it's an index in an array.

Some comments:
  • What you have is not a script -- it's Fortran code. Typically the source code for compiled languages such as Fortran, C, C++, and so on, is not called a script. That's usually reserved for the source code for interpreted languages, such as Javascript.
  • Your variable names are terrible. i and j aren't too bad -- they are historically used for array indexes. x_sumh and flag_11 should be given names so that others reading your code can grasp what it is supposed to be doing.
  • Use white space to make your code more readable. The line I quoted above has zero extra spaces in it, and there is almost no white space in the rest of your code.
 
  • Like
Likes Vanadium 50
  • #11
Mark44 said:
The error message is pretty clear. A value for an element of the flag_11 array is -1, but should be in the range 0 through 23, being that it's an index in an array.

Some comments:
  • What you have is not a script -- it's Fortran code. Typically the source code for compiled languages such as Fortran, C, C++, and so on, is not called a script. That's usually reserved for the source code for interpreted languages, such as Javascript.
  • Your variable names are terrible. i and j aren't too bad -- they are historically used for array indexes. x_sumh and flag_11 should be given names so that others reading your code can grasp what it is supposed to be doing.
  • Use white space to make your code more readable. The line I quoted above has zero extra spaces in it, and there is almost no white space in the rest of your code.
Thank you for your reply and your correction. I tried using the white spaces and it works. Actually I confused about how to fix that code. I only got this code and try to calculate my data, but i don't know why there is an error when i combine these code with my data. I`m not sure that I have to change the codes, I only can changes the number according to my data and I already adjusted the number with my data.

Now my problem is about the final code, the report showed as follows:
Variable X is too large to specify the SAVE attribute (12288000000 bytes).
What should I do for this report?
 
  • #12
I don't see anything in your code where flag_11 is getting set to a value. Have you given us the code that is actually being used? If you do, put it in using the code option ( see the <\> option at the top of the edit window.) That will keep the indentation correct. Try to indent your code loops to make the code easy to read.
 
  • #13
Novia Lusiana said:
I tried using the white spaces and it works.
Yes, of course it works. The compiler doesn't care whether you use extra spaces or not, but doing this makes it easier for humans to read.
Novia Lusiana said:
Now my problem is about the final code, the report showed as follows:
Variable X is too large to specify the SAVE attribute (12288000000 bytes).
What should I do for this report?
Why are you using the SAVE attribute? You don't show it in your original post.
In post #1 you have this declaration:
Fortran:
real x(1495,1002,300)
(There are other declarations on the same line, but I wanted to focus on just this one.)
By my count, this array will take up 1495 * 1002 * 300 * 4 = 1,797,588,000 bytes of memory, nearly 2 gigabytes. The error or warning you show suggests to me that you have changed the declaration of x.

By the way, why did you name the variable x? This name doesn't tell anyone reading your code how you will be using it.

Please show us the code that produced this error, using code tags, as in the following example.

Fortran:
open(11,file="20180706-1300.CSV",status='old')!input the data
do i = 1, 3200, 1                                    ! You don't need the final 1
    read(11,*) x(i, 1:3200, k)                 ! Does k have a known value?
end do
do i =1,3200, 1
    do j = 1,3200, 1
        if(x(i, j, k) < 0 )x(i, j, k) = 0                       ! Does k have a known value in this line and the next?
        x_sum(i, j) = x_sum(i, j) + x(i, j, k)
    end do
end do
! etc.
 
  • Like
Likes FactChecker and Vanadium 50

Similar threads

Replies
6
Views
10K
Replies
5
Views
2K
Replies
33
Views
5K
Replies
4
Views
8K
Replies
5
Views
2K
Replies
2
Views
25K
Replies
4
Views
11K
Replies
4
Views
4K
Replies
3
Views
4K
Back
Top