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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fortran error in binary file reading

Status
Not open for further replies.

Thardy

Technical User
Oct 29, 2011
13
Dear members,
I wrote a program to read a binary file I received.
1) The structure of the biary file is as follows:
1st line : P(1),P(2),P(3),....,P(N) ; where,P(1)>P(2)....
2nd line : X,Y,A(P(2)),..,A(P(N)),W(P(2)),..,W(P(N)),T(P(2)), ..,T(P(N)),ST,GA1,GA2,GB1,GB2,GB3
3nd line : X,Y,A(P(2)),..,A(P(N)),W(P(2)),..,W(P(N)),T(P(2)), ..,T(P(N)),ST,GA1,GA2,GB1,GB2,GB3
4nd line : X,Y,A(P(2)),..,A(P(N)),W(P(2)),..,W(P(N)),T(P(2)), ..,T(P(N)),ST,GA1,GA2,GB1,GB2,GB3

2)The program I wrote is as follows:
implicit none
integer, parameter:: k_layer = 6
integer::X,Y,j,j_lay
real :: P(1:k_layer), T(1:k_layer)
real :: A(1:k_layer), W(1:k_layer)
real :: ST
character :: f_i*150, f_o*150
integer,parameter :: i_pix=10000
integer :: itr
real :: GA(2)
real :: GB(3)
integer, parameter :: k_rec = 2*2
& + k_layer*4
& + k_layer*4
& + k_layer*4
& + 4
& + 4*5

integer, parameter :: k_dammy = k_rec - k_layer*4
character(k_dammy) :: cdammy
f_i='binaryfilepixels'
f_o='textfilepixels'
open (11, file = f_i, status = 'old', access = 'direct',
& recl = k_rec,form='unformatted',convert='big_endian')
read (11 , rec=1)(P(j_lay),j_lay=1, k_layer)
write(*,'(6F7.2)')(P(j_lay), j_lay=1, k_layer)
read(11, rec = i_pix + 1)
& X, Y,
& ( A(j_lay), j_lay =1, k_layer),
& ( W(j_lay), j_lay =1, k_layer),
& ( T(j_lay), j_lay =1, k_layer),
& ST,
& (GA(itr),itr=1,2),
& (GB(itr),itr=1,3)

open (12, file = f_o, status = 'new', access = 'direct',
& recl = k_rec,form='formatted')
write(12, 1000)
& (P(j_lay), j_lay=1, k_layer),
& X, Y,
& ( A(j_lay), j_lay =1, k_layer),
& ( W(j_lay), j_lay =1, k_layer),
& ( T(j_lay), j_lay =1, k_layer),
& ST,
& (GA(itr),itr=1, 2),
& (GB(itr),itr=1, 2)
999 format (2I5, 18F7.2, 6F7.2)
1000 format (2I5, 18F7.2, 6F7.2)
close (11)
close (12)
stop
End

3) The gfortran compiling works ok but at the execution part there is an error in the reading of the binary file. Could somebody help to correct what is wrong?

Thanks in advance
Thardy
 
Are you sure that your binary file must be declared with access='direct' ?

What is the origin of that file ? If it comes from a C program for instance, it should be certainly declared with access='stream'. If it was generated by another Fortran program, then access='sequential' is usual (but access='direct' is possible too).

About your text output file which is formatted, it is also very rare to declare such a file access='direct' : access='sequential' (the default access) is much more frequent.

François Jacq
 
Dear FJack,

Thanks a lot for your quick reaction to my posting.
1) the origin of the file is a fortran program generated file.
2)About the reading, yes indeed the file is supposed to be access='direct'.
For the writing, I agree with you that the access declaration could be omitted.
I tried the reading as sequential but this too doesn't work.
3) To have a better idea about the binary file in question, I attach it to this mail.
Thanks in advance while waiting for some more ideas.
Thardy
 
 http://www.mediafire.com/?si6alvb63krj43i
You only mentioned that there is an error, but you don't mention what the error is...

...can you be specific? How do you know there is an error?

Also, like always, I suggest 'baby steps...baby steps'...

...if the program is not reading what you expect, try reading a little bit at a time until you start noticing a problem..

...also, I suggest that you double check the content of the binary file...I know you seem to really know what the file is supposed to look like, but it wouldn't hurt to get a hold of a binary-file editor (hex-editor) and inspect the file that you are trying to read and make sure it is formatted as expected.
 
Hi Salgerman,
Thanks for your reaction.
The error occurs when reading the second record i.e.at the line:
"read(11, rec = i_pix + 1)& X, Y,& ( A(j_lay), j_lay =1, k_layer),& ( W(j_lay), j_lay =1, k_layer),& ( T(j_lay), j_lay =1, k_layer),& ST,& (GA(itr),itr=1,2), & (GB(itr),itr=1,3)"

The execution of the program stops at this level and says there is an error at that point.
I added the binary file to my previous mail. So it be accessed and checked freely.

Thanks in advance for further suggestions
Thardy
 
Could you show us :

- the exact error message (without paraphrasing),

- the instructions which have CREATED the file (OPEN statement and WRITE statements)

You should also check whether the array sizes (k_layer for instance) are the same in both programs.

In addition, could you warrant that the two programs have been created by the same FORTRAN compiler ?



François Jacq
 
Thanks to all those who tried to help.
I did review the program and realized that there was a loop missing at the second read statement.
Now everything is OK.
Thardy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top