Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code successfully compiled but won't execute

Status
Not open for further replies.

Aclems

Technical User
Mar 19, 2007
6
CA
Hi guys,
Could anyone help with a problem I am having with a Fortran code? I have written a code to open a file(with binary extension) and want to change this file into an integer format. Using a gfortran compiler on MAC OS, the program compiled well but on trying to execute it, I get this error message:

At line 11 of file convert2integer.f90
Fortran runtime error: End of file

Please, could anyone let me know what is wrong with this program? Here is the code:

PROGRAM convertToInteger

IMPLICIT NONE

REAL(KIND=4), DIMENSION(15,9,1) :: precipitation_real
INTEGER(KIND=4), DIMENSION(15,9) :: precipitation_int

OPEN(UNIT=10, FILE='upas_test.bin', FORM="unformatted", ACCESS="sequential")
OPEN(UNIT=11, FILE='upas_test_int.bin', FORM="unformatted", ACCESS="direct", RECL=15*9*4))

READ(UNIT=10) precipitation_real

precipitation_int = NINT((precipitation_real:),:,1)-273.16)*10.)

WRITE(*,'(15I5)') precipitation_int
WRITE(UNIT=11, REC=1) precipitation_int

CLOSE(UNIT=10)
CLOSE(UNIT=11)

END PROGRAM convertToInteger
 
You have 2 ) on the second open statement.
 
Hello Aclems

You are simply trying to read beyond the end of the file. I suggest that you make a DO loop and read only ONE real value at a time (not the whole file!). You should put an END directive into the read statement [READ(UNIT=10,END=99) p_real] and go out of the loop when the end of the file is encountered. This also gives you more control over the program.

Best wishes
GulliPe

 
This was my response, when this was with the other thread.:

Actems: I would say, start a new thread so that more users might benefit from it. I would reenter my answer to it.

Xwb: if this is a free format fortran file, length of line would not matter (at least not at this length).

Actems again:
check the actual size of your file 'upas_test.bin'. There might be a pitfall that is very confusing. Opening a file in your program without the status='old' clause would cause a file with this name to be entered in your directory if it was not listed allready. If you check the content of your directory after the faulty execution of your prog you will see the entry in your directory and you might overlook its actual size indicating it has no content.

So


(1) Check on the size of the file in your directory and delete it if it is 0

(2) open your files you want to read from with status='old' or action='read' only to prevent the directory entry

(3) see what happens.



I would add:

gullipe might be right:

precipitation_real is declared dimension(15,9,1) that is 135 elements. If the file contains less than that the same error will occur.

one comment:

precipitation_int = NINT((precipitation_real:),:,1)-273.16)*10.)
I really do not believe that fortran can handle this statement. Work with do-loops like

do j1=1,15
do j2=1,9
precipitation_int(j1,j2) = NINT((precipitation_real(j1,j2,1)-273.16)*10.)
enddo
enddo

Norbert.
 
Thanks guys for your great suggestions, they are kindly appreciated. I figured out where the problem is. I had used GRADS(Grid Analysis and Display System) model to write out "upas_test.bin" using ACCESS="stream" and when I got into the FORTRAN code, I said use ACCESS="sequential". There is bound to be problem since there will be 8 extra byte of data added to the top and bottom when using sequential access from GRADS.

Once again, thanks to everyone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top