Fortran Fortran Help for Meteorologist Student: 4th-End, 60-100

  • Thread starter Thread starter MelinaM
  • Start date Start date
  • Tags Tags
    Fortran
AI Thread Summary
A meteorology student seeks assistance with Fortran to read specific rows from a large data file. The student aims to read from the 4th row to the 60th and from the 60th line to the 100th. Initial attempts using a "do" loop were unsuccessful, prompting discussions on reading the entire file first and then filtering the desired rows. It was clarified that Fortran can handle formatted and unformatted files, and the importance of understanding file structure was emphasized. Suggestions included using a text editor to verify file accessibility and considering splitting large files into smaller segments to avoid performance issues in Excel. The conversation also touched on converting temperature data from Kelvin to Celsius, with a formula provided. Ultimately, participants offered pseudo code to help the student structure their Fortran program to read and output the required data efficiently, highlighting the need for clarity on the file format and structure for successful data manipulation.
MelinaM
Messages
13
Reaction score
0
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!
 
Technology news on Phys.org
Welcome to PF!

Read all rows and keep only the rows you want.
 
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?
 
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.
 
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:
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.
 
MelinaM said:
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.
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?
The read statement won't work.

Have you written any Fortran code before?
 
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
 
MelinaM said:
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

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
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?
 

Attachments

  • #11
I have another question. These numbers are temperature in Kelvin. How can I do that in Celsius using Fortran?
 
  • #12
MelinaM said:
I have another question. These numbers are temperature in Kelvin. How can I do that in Celsius using Fortran?

What's the conversion from K to C? Surely they teach this in meteorology school.
 
  • #13
Celsius= Kelvin-273,15
 
  • #14
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
Fortran is but I'm not. I am too bad at Fortran :(
 
  • #16
MelinaM said:
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?

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
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
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
Yes, I have tried but I have almost 55000 data so it stuck
 
  • #20
MelinaM said:
Yes, I have tried but I have almost 55000 data so it stuck

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.
 
  • #21
Not the entire computer. Just the excel wasn't responding. I have trouble opening it as well, let alone work on it
 
  • #22
MelinaM said:
Not the entire computer. Just the excel wasn't responding. I have trouble opening it as well, let alone work on it

Then this strongly suggests that you should split the large data file into several smaller files, and work on these files individually. I'm not sure what you want to do with the data, but it might be possible once the individual small data files have been processed, you can collect summaries of results for the smaller files into one summary file.

Sometimes, when working with large files bogs down Excel, the automatic calculation option can be turned off so that making one change to a data cell doesn't set off a cascade of calculations in other parts of the spreadsheet as Excel updates the file.

It could be that the computer you are using has insufficient memory to accommodate these large data files. It might be easier to add memory to your existing computer, or use a different computer which has more memory.

In any event, it seems that most of your problems originate in trying to manipulate scads of data in one massive file.
 
  • #23
It would help if you gave a better description of what you're trying to do and why.

All you've told us is that you want to process a large amount of data using Fortran, converting temperature values to Kelvin and then importing the data to Excel? or is it the other way around? whether this is a project for some course on Fortran or not?

For example, if you're doing this as part of some academic research then perhaps other programming tools like AWK, Perl or Python would be more helpful than using Excel and Fortran.
 
  • #24
Yes, i know that excel can do that but not if you have 460 columns and 55000 lines. I just did the program (from K to C) and it worked. But i still don't know how I should do the other program. The one I have to make it read from the 4th row till the end and from the 60th line till the 100th. Do you have any suggestions please? If you had a file with many data how would you make a program so it would read the whole thing but make an output file with the data you want. Let's say you have 500 columns and 700 lines. How would you make it make an output file with the numbers from the 50th line till the 300tha and from the 12th column till the 360th?
 
  • #25
Here's some pseudo code to do what you wanted, you'll have to rewrite in correct Fortran fr it to work.

Code:
do i=1 to 700

    read (---) cols

    if (i <= 50) continue
    if(i >= 300) continue
   
    // write out selected columns
    do j=12 to 360
        newcols[j-12]=cols[j]
    enddo

    write (---) newcols

enddo
 
  • #26
Thanks! Thank kinda helped. I will try it and I hope it will work.
 
  • #27
For another approach:

Code:
// read lines 1 through 49 but don't do anything with them

do i = 1 to 49
    // read only the first column into a "dummy" variable
    read (---) dummy
enddo

// read lines 50 through 300 and process them

do i = 50 to 300
    read (---) cols
    // ... and write out the selected columns as per jedishrfu
enddo

// no need to read the remaining lines since you
// aren't going to do anything with them!
 

Similar threads

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