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

Fortran 90 help reading from file

  1. Jul 6, 2009 #1
    Hi All

    I'm completely new to Fortran 90 and I have to create a program that opens a formatted data file and reads in certain values into my program's variables. The variables are represented in the first six rows with ncols, nrows, xllcorner, yllcorner, cellsize and NODATA being the variable's labels. The format of the data file looks like this but the content of the variables will change depending on data file:

    ncols 5
    nrows 4
    xllcorner 2000
    yllcorner 3000
    cellsize 10
    NODATA -9999
    6 7 8 9 10
    7 8 9 10 11
    8 9 10 11 12
    9 10 11 12 13

    Ncols, nrows will always be integers but the other variables are most likely going to be double precision numbers.

    So far I have programmed the code that opens the file (the file is supposed to be called DEM.in):

    integer :: st

    if (st>0) then
    print*,'Error opening file. File load status:', st
    print*, 'File openend correctly'
    end if

    Can anyone help me or indicate me into the right direction on how I can program the code that will read in the values in my data file (5, 4, 2000, 3000, 10, -9999) and write them into my program's variables (ncols, nrows, xllcorner, yllcorner, cellsize, NODATA)? Any help is greatly appreciated; remember I'm a novice with Fortran so labelling any code will be extremely useful.

    Thanks very much guys

  2. jcsd
  3. Jul 7, 2009 #2


    User Avatar
    Science Advisor

    As long as your variables are defined correctly, then you can just do an unformatted read which will read in the variables correctly.
    Code (Text):

    INTEGER :: ncols,nrows
    REAL(kind=8) :: xllcorner,yllcorner
    READ(11,*) ncols
    READ(11,*) nrows
    !--read the rest
    DO i=1,nrows
     READ(11,*) (data(i,j),j=1,ncols) !--   <- implicit in-line do loop
    END DO

  4. Jul 8, 2009 #3
    Thank you! Really helpful!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook