Fortran Question SOS

1. Apr 8, 2014

MelinaM

Hi, i am a meteorologist student and I have some data I have to prepare using Fortan. I don't know how to make it read the file from the 4th row till the end and from the 60th line till the 100th. Can you please help me? I'm desperate!!

2. Apr 8, 2014

Staff: Mentor

Welcome to PF!

Read all rows and keep only the rows you want.

3. Apr 8, 2014

MelinaM

I'm afraid I can't do that. There has to be a way I can do that. I know I can use "do" but I don't know how exactly. Does anyone know?

4. Apr 8, 2014

Staff: Mentor

First let's focus on something simpler. Do you know how to read the entire file, one line at a time?

When you can do that, you can modify it to read only the lines that you want.

5. Apr 8, 2014

Staff: Mentor

Here's a reference to reading files in fortran. there are other online tutorials as well that you can search for:

http://en.wikibooks.org/wiki/Fortran/io

and one for do loops (bottom half of page)

http://en.wikibooks.org/wiki/Fortran/Fortran_control

look at how you can place a read statement inside a do loop and iterate over the number of lines you want to keep or skip.

post your code if you want more help on fortran specifics or errors you're getting. Use the (code) and (/code) tags around your example where the parens are really square brackets.

Last edited: Apr 8, 2014
6. Apr 8, 2014

MelinaM

Yes, I know how to read the whole file. I was using the do loop. I wrote it like this:
do i=60, 100
j=4, 60
read(*,*) the name of the file
end do

but it never worked.

7. Apr 8, 2014

Staff: Mentor

I'm sorry, but this isn't even close to what you need to do.

Your loop is semantically correct, but I don't know why it's running 40 times.
The line j = 4, 60 -- what is it supposed to be doing?

Have you written any Fortran code before?

8. Apr 9, 2014

MelinaM

The read should be read(#,$) where # is the number of the file I have to state and$ the format.
I have an excel file where there are 10000 lines and 10000 columns. I want to read from the 4 row till the 60th and from the 60th line till the 100th. That's why I mentioned those numbers

9. Apr 9, 2014

SteamKing

Staff Emeritus
It's not clear if you are trying to read an Excel file directly, or data which has been created in Excel and saved or exported into another format.

In any event, Fortran can access two types of files: formatted files are things like text files, which you could also read in a text editor. The information is presented as a series of lines of data, and each line can have a varying length. Unformatted files are typically stored in some binary format, and there is a unique structure to the data which your Fortran application must be programmed to recognize, so that it can correctly access the proper information from the file.

Perhaps if you could provide more detail about this data file, more pertinent help would be forthcoming.

Can you read your data with a text editor for instance, without having to start Excel?

10. Apr 9, 2014

MelinaM

I have this file in .dat but I couldn't upload it so I made it .txt.
What should I do if I want to read from 1960-1962?

Attached Files:

• examplee.txt
File size:
430.1 KB
Views:
63
11. Apr 9, 2014

MelinaM

I have another question. These numbers are temperature in Kelvin. How can I do that in Celsius using Fortran?

12. Apr 9, 2014

SteamKing

Staff Emeritus
What's the conversion from K to C? Surely they teach this in meteorology school.

13. Apr 9, 2014

MelinaM

Celsius= Kelvin-273,15

14. Apr 9, 2014

SteamKing

Staff Emeritus
Then that's how you convert the data from the file in K to C. After all, Fortran is dsigned to do calculations like this.

15. Apr 9, 2014

MelinaM

Fortran is but I'm not. I am too bad at Fortran :(

16. Apr 9, 2014

SteamKing

Staff Emeritus
As a text file, you cannot randomly access a particular line from your data file by itself with the capabilities Fortran gives you to handle sequential files. As was suggested in an earlier post, if you want to access the data in line N, you've got to read in lines 1 to N-1 first. Now, if you are not interested in using the data in lines 1 thru N-1, that's OK, you just discard it after it is read.

17. Apr 9, 2014

MelinaM

Yes, but we are talking about too many data because the file I have are data from 1950-2100 and not just temperature. So, I have to read just the data I want, somehow. The txt was only for upload use.

18. Apr 9, 2014

SteamKing

Staff Emeritus
We appear to be talking past one another on this point. The program you write to access the data file must be compatible with the format and layout of the file, unless you want to re-format all this data. Believe me, you want to try the former approach first.

PF is not a good place to teach basic programming. Since you are a student, I recommend that you ask someone at your school for help or tutoring in programming.

If your data is already in an Excel file, have you tried using Excel to extract and manipulate the data? Excel should certainly be capable of doing temperature conversions, for example.

19. Apr 10, 2014

MelinaM

Yes, I have tried but I have almost 55000 data so it stuck

20. Apr 10, 2014

SteamKing

Staff Emeritus
Your post is not clear. What 'stuck'? Was it Excel? Did your computer crash?

If 55000 data points are too much, perhaps you need to split the data into smaller files with
fewer points.