Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran Question SOS

  1. Apr 8, 2014 #1
    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. jcsd
  3. Apr 8, 2014 #2

    jedishrfu

    Staff: Mentor

    Welcome to PF!

    Read all rows and keep only the rows you want.
     
  4. Apr 8, 2014 #3
    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?
     
  5. Apr 8, 2014 #4

    jtbell

    User Avatar

    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.
     
  6. Apr 8, 2014 #5

    jedishrfu

    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
  7. Apr 8, 2014 #6
    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.
     
  8. Apr 8, 2014 #7

    Mark44

    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?
    The read statement won't work.

    Have you written any Fortran code before?
     
  9. Apr 9, 2014 #8
    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
     
  10. Apr 9, 2014 #9

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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?
     
  11. Apr 9, 2014 #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?
     

    Attached Files:

  12. Apr 9, 2014 #11
    I have another question. These numbers are temperature in Kelvin. How can I do that in Celsius using Fortran?
     
  13. Apr 9, 2014 #12

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    What's the conversion from K to C? Surely they teach this in meteorology school.
     
  14. Apr 9, 2014 #13
    Celsius= Kelvin-273,15
     
  15. Apr 9, 2014 #14

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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.
     
  16. Apr 9, 2014 #15
    Fortran is but I'm not. I am too bad at Fortran :(
     
  17. Apr 9, 2014 #16

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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.
     
  18. Apr 9, 2014 #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.
     
  19. Apr 9, 2014 #18

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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.
     
  20. Apr 10, 2014 #19
    Yes, I have tried but I have almost 55000 data so it stuck
     
  21. Apr 10, 2014 #20

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Fortran Question SOS
  1. Fortran Question (Replies: 19)

  2. Question - Fortran (Replies: 2)

  3. FORTRAN Question (Replies: 4)

  4. Fortran question (Replies: 2)

  5. Fortran Question (Replies: 32)

Loading...