How to Extract Gridded Data in Fortran?

In summary, the speaker is not a programmer but needs help modifying a code in Fortran. The code is used to extract data from a file and the speaker wants to be able to automatically call the file without typing it in every time. They also want to modify the code to extract data for all months in a given year and to only include certain latitude values. There may be an issue with using backslashes in the file names. The code also includes some errors, such as using a real number to compare with an integer.
  • #1
jainax
2
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
  • #2
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.
 
  • #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.
 
  • #4
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.
 
  • #5
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...
 
  • #6
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
 
  • #7
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.
 

1. What is Fortran programming language?

Fortran (short for Formula Translation) is a high-level programming language used for scientific and engineering applications. It was developed in the 1950s and is known for its efficient data handling and mathematical capabilities.

2. How do I write a Fortran program?

To write a Fortran program, you will need a text editor and a Fortran compiler. The program must begin with the keyword "program" followed by the program name. Then, you can write your code using Fortran syntax and end the program with the "end program" statement. Finally, use the compiler to translate your code into machine language.

3. What are the main features of Fortran?

Fortran is a powerful language with many useful features, including support for array operations, built-in functions for mathematical operations, and the ability to handle large and complex data sets. It also has a simple and easy-to-learn syntax, making it popular among scientists and engineers.

4. How can I debug my Fortran program?

Debugging in Fortran is similar to other programming languages. You can use tools such as print statements, debugging libraries, or a debugger program to identify and fix errors in your code. It is also a good practice to test your code frequently and use proper coding conventions to avoid errors.

5. Can I use Fortran for modern applications?

Yes, Fortran is still widely used for modern applications, especially in the scientific and engineering fields. It has evolved over the years and now has features that support object-oriented programming and parallel computing. Many popular software packages, such as MATLAB and Mathematica, use Fortran for their mathematical and computational capabilities.

Similar threads

  • Programming and Computer Science
Replies
4
Views
588
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
22
Views
3K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
11
Views
987
Back
Top