Fortran How to Extract Gridded Data in Fortran?

  • Thread starter Thread starter jainax
  • Start date Start date
  • Tags Tags
    Fortran Program
AI Thread Summary
The discussion focuses on modifying a Fortran program designed to extract data from a large dataset spanning 108 years. Key modifications requested include automating the file input process to eliminate the need for manual entry, changing the program to extract data for all months of a specified year instead of just one month, and filtering latitude values to exclude those greater than 8.5. Participants discuss issues related to file path formatting, specifically the use of backslashes and forward slashes in file names, which can cause errors in reading the file. Suggestions include testing with a simplified program to isolate the file opening issue. Additionally, there are concerns about comparing integer and floating-point values in the code, with recommendations for correcting the syntax of conditional statements to ensure proper execution. Overall, the conversation highlights common programming challenges in Fortran, particularly around file handling and data filtering.
jainax
Messages
2
Reaction score
0
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
 
Technology news on Phys.org
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.
 
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.
 
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.
 
Code:
 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...
 
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 statement, which is not allowed. The following are three different ways you could do this:

Code:
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
 
540 write(*,*) 'Cannot close ',infile
560 write(*,*) 'Cannot close output file'
900 write(*,*) 'Requested Data Not Available!'
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.
 

Similar threads

Replies
12
Views
3K
Replies
22
Views
5K
Replies
16
Views
2K
Replies
6
Views
2K
Replies
54
Views
5K
Replies
6
Views
2K
Replies
5
Views
5K
Back
Top