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

Help with fortran program

  1. Jun 19, 2008 #1
    Hi,
    Please bear with me on this because I'm really not a programmer at all but I need some help with modifying a code in fortran. This program is supposed to extract data for a single month of data at a time from the file highlighted in red below which contains 108 years of data. First of all, I would like to be able to call forth that file automatically without having to type it in everytime. I tried to put in a 'write' line to do that but when it came up in the program run, it didn't have the backslashes.
    Second, rather than getting one month at a time, I would like to get all the months from a given year in each output file.
    Lastly, the part at the bottom highlighted in green is dealing with latitudes. I only want to get latitudes within a certain range, which I've already plugged in. however, for the values i.gt.8.5, I don't want them to be included, so what do I have to put on the right side of that part to make that happen. Thanks in advance for any help anyone can offer!

    Program read_gridded_all

    integer valu(36,72)
    integer i
    integer imonth,jmonth,iyear,jyear
    integer datachk
    integer openfile
    integer loopcnt
    real lat,lon
    character*110 infile,outfile
    character*5 FMT100
    character*6 FMT20

    C INITIALIZE VARIABLES
    datachk = 0
    openfile = 0
    loopcnt = 0

    C ASK USER FOR INPUT GRID SET
    write(*,*) 'Enter gridded dataset filename. '
    C write(*,*) 'C:\Program Files\Force 2.0\grid_prcp_1900-2008.dat'
    read(*,10) infile
    C OPEN INPUT FILE
    open(10,file=infile,status='old',err=500)
    openfile = 1

    C GRIDDED FILENAME NOT VALID
    500 do while(openfile .eq. 0)
    write(*,*) 'Cannot open ',infile
    write(*,*) 'Reenter gridded dataset filename.'
    read(*,10) infile
    open(10,file=infile,status='old',err=500)
    openfile = 1
    enddo

    C ASK USER FOR OUTPUT FILENAME
    write(*,*) 'Enter output filename for gridded subset. '
    read(*,10) outfile
    C OPEN OUTPUT FILE
    open(11,file=outfile)

    C ASK USER FOR MONTH AND YEAR TO EXTRACT
    write(*,*) 'Enter month to extract',
    *'- integer format (1=>Jan, 2=>Feb, etc.)'
    read(*,40) imonth
    write(*,*) 'Enter year to extract'
    read(*,40) iyear

    10 format(a110)
    40 format(i5)

    FMT100 = '(2i5)'

    write(*,*) 'Enter type of data, 1==>temp, 2==>prcp'
    read(*,*) itype

    if(itype.eq.1) then
    FMT20 = '(12i5)'
    else
    FMT20 = '(12i7)'
    endif

    C LOOP OVER ALL MONTHS AND YEARS UNTIL FIND REQUESTED DATA
    do while(datachk .ne. 1)
    read(10,FMT100,end=900) jmonth, jyear
    loopcnt = loopcnt + 1
    C IF FOUND, THEN READ IN GRIDDED DATA
    if(imonth .eq. jmonth .and. iyear .eq. jyear) then
    write(*,50) imonth, iyear
    50 format(' Reading gridded data for month ', i2.2,
    *' and year ',i4)
    do i = 1,36
    do j = 1,6
    read(10,FMT20) (valu(i,k),k=j*12-11,j*12)
    enddo
    enddo
    C SET BOOLEAN TO TRUE
    datachk = 1
    C NOT CORRECT MONTH AND YEAR, SO SKIP GRIDDED DATA FIELDS
    else
    if(loopcnt.eq.1) then
    write(*,110) imonth,iyear
    110 format(' Searching for month ', i2.2, ' and year ',i4)
    endif
    do i = 1,216
    read(10,*)
    enddo
    endif
    enddo

    C WRITE DATA TO OUTPUT FILE
    write(*,*)'Writing output data to file'
    do i = 1,36
    if(i.le.8.5) lat = 92.5 - i * 5.0
    elseif(i.gt.8.5) lat = ((i-18) * (-5.0)) + 2.5

    do j = 1,72
    if(valu(i,j) .ne. -9999) then
    if(j.le.36) lon = (182.5 - (j * 5.0)) * (-1)
    if(j.gt.36) lon = ((j-36) * 5.0) - 2.5
    write(11,30) lat,lon,valu(i,j)/100.
    endif
    enddo
    enddo

    C CLOSE INPUT AND OUTPUT FILES
    close(10,err=540)
    close(11,err=560)

    GO TO 999

    30 format(f6.1,2x,f7.1,2x,f8.2)

    540 write(*,*) 'Cannot close ',infile
    560 write(*,*) 'Cannot close output file'
    900 write(*,*) 'Requested Data Not Available!!!!'
    write(*,*) 'Sorry - Try Again. Exiting Program!!!!'

    999 STOP
    END
     
  2. jcsd
  3. Jun 19, 2008 #2

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    It looks like a problem with putting the file names in.
    Possibly due to \char meaning something - in 'C' you would either use / or \\ to escape.
    Sorry it's been a lot of years, back when I did fortran you only had numbered streams and had to associate a file with the stream before running the program.

    I would suggest you write a very small program just attempting to open a file that will let you trace the problem without positing lots of other parts of the code that aren't relevant.
     
  4. Jun 19, 2008 #3
    I doubled the slashes and now it's showing up properly in the program but it's still not reading it. Howevever, if I type in the exact same thing manually, it works.
     
  5. Jun 19, 2008 #4

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    That's my point, in 'C' if you have "c:\test" the \t is interpreted as a tab character by the compiler - its the only way to put code characters inside a string. When you type in the string to a running program it isn't seen by the compiler so control chars aren't expanded.

    I don't know about string literals in Fortran but I suspect they have a similair mechanism. In windows you can use the forward slash / to avoid this.
     
  6. Jun 20, 2008 #5
    Code (Text):
     do i = 1,36
    if(i.le.8.5) lat = 92.5 - i * 5.0
    elseif(i.gt.8.5) lat = ((i-18) * (-5.0)) + 2.5
    do j = 1,72
     
    Isn't "i" an integer?

    You appear to be comparing it with a real number (floating point)...

    Dunno if that'll make any difference, but it used to do strange things in FORTRAN IV...
     
  7. Jun 20, 2008 #6

    alphysicist

    User Avatar
    Homework Helper

    Hi jainax,

    These lines do not seem to have the right form:


    if(i.le.8.5) lat = 92.5 - i * 5.0
    elseif(i.gt.8.5) lat = ((i-18) * (-5.0)) + 2.5

    With them like that, the elseif is a separate statment, which is not allowed. The following are three different ways you could do this:

    Code (Text):

    if(i.le.8.5) lat = 92.5 - i * 5.0
    if(i.gt.8.5) lat = ((i-18) * (-5.0)) + 2.5



    if(i.le.8.5) then
        lat = 92.5 - i * 5.0
    elseif(i.gt.8.5) then
        lat = ((i-18) * (-5.0)) + 2.5
    endif


    if(i.le.8.5) then
        lat = 92.5 - i * 5.0
    else
        lat = ((i-18) * (-5.0)) + 2.5
    endif
     
     
  8. Jun 20, 2008 #7

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Oops sorry, I forgot that you didn't need goto lables in fortran.
    I thought these were the error messages when you ran it, not part of the code!
    That's why I mentioned the problem with the filenames.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Help with fortran program
Loading...