Having trouble when reading file using Fortran 90

  • Context: Fortran 
  • Thread starter Thread starter Novia Lusiana
  • Start date Start date
  • Tags Tags
    File Fortran Reading
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a Fortran 90 program that reads data from CSV files, specifically focusing on issues related to file input/output operations, variable declarations, and runtime errors encountered during execution. Participants share code snippets and seek advice on resolving errors related to reading files and managing array indices.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant notes that the format '()' is invalid and suggests replacing it with '*'.
  • Another participant expresses confusion about needing a variable in the read statement, questioning how to implement this.
  • A participant mentions that the error persists even after changing '()' to '*', indicating ongoing issues with the read statement.
  • Concerns are raised about an out-of-range error related to the flag_11 array, with a participant noting that a value of -1 is causing the issue.
  • Participants discuss the importance of variable naming and code readability, suggesting improvements for clarity.
  • One participant reports a new error regarding the size of a variable being too large for the SAVE attribute, seeking advice on how to address this issue.
  • There is a suggestion to provide the complete code for better assistance, emphasizing the need for proper indentation and formatting.

Areas of Agreement / Disagreement

Participants generally agree on the need for valid format specifications in read statements and the importance of variable management. However, there is no consensus on the best approach to resolve the runtime errors, as multiple issues are being discussed without a clear resolution.

Contextual Notes

Participants mention various limitations in their code, including the handling of array indices and the implications of using large variables. Specific assumptions about the data structure and the intended functionality of the code are not fully clarified.

Who May Find This Useful

This discussion may be useful for individuals learning Fortran programming, particularly those dealing with file I/O operations and debugging runtime errors in their code.

Novia Lusiana
Messages
6
Reaction score
0
TL;DR
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   Reactions: 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   Reactions: FactChecker and Vanadium 50

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
11K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
5K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
25K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 3 ·
Replies
3
Views
4K