Having trouble when reading file using Fortran 90

In summary: What are those supposed to mean? They are not descriptive of what you are trying to do. They are basically useless unless you have some additional documentation or comments to explain them.What is the purpose of reading from the same file twice? You read from the file 3200 times in the first loop, and then 3200 times in the second loop. That's a lot of reading from the same file. Typically you would read from a file once and then process the data you read in.Why are you looping through j in the first loop when you only read from column 1?Why are you looping through j in the second loop when you only read from column 1?Why are you looping through j in the third loop
  • #1
Novia Lusiana
6
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
  • #2
The '()' is not a valid format. Replace it with *.
 
  • #3
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
 
  • #4
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?
 
  • #5
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.
 
  • #6
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.
 
  • #7
I did not say (*). Do not include parenthesis, only *.
Fortran:
read (51,*) !skip the header rows
 
Last edited:
  • #8
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
 
  • #9
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.

[code=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.
[/code]
 
  • Like
Likes FactChecker and Vanadium 50

FAQ: Having trouble when reading file using Fortran 90

1. Why am I getting a "File not found" error when trying to read a file in Fortran 90?

There could be several reasons for this error. First, make sure that the file you are trying to read actually exists in the specified location. Also, check for any typos in the file name or path. Additionally, check the file permissions to make sure you have the necessary access to read the file.

2. How do I read a specific line from a file in Fortran 90?

To read a specific line from a file in Fortran 90, you can use the READ statement with the UNIT and REC keywords, where UNIT specifies the file unit number and REC specifies the record number (line number) to be read.

3. Can I read a file with non-numeric data in Fortran 90?

Yes, you can read non-numeric data from a file in Fortran 90 using the READ statement with the FORMAT specifier. You can specify the format of the data to be read, including non-numeric characters like letters or symbols.

4. How do I handle errors when reading a file in Fortran 90?

You can use the IOSTAT keyword in the READ statement to check for errors while reading a file. The IOSTAT variable will have a value of 0 if the read operation was successful, or a non-zero value if there was an error. You can then use IF-ELSE statements to handle the error accordingly.

5. Can I read a file in Fortran 90 without specifying the file format?

No, you must specify the format of the data to be read when using the READ statement in Fortran 90. This ensures that the data is read correctly and avoids any potential errors or data corruption.

Similar threads

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