I have a binary file that was created using a Python code. This code mainly scripts a bunch of tasks to pre-process a set of data files. I would now like to read this binary file in Fortran. The content of the binary file is coordinates of points in a simple format e.g.: number of points, x0, y0, z0, x1, y1, z1, ....
These binary files were created using the 'tofile' function in numpy. I have the following code in Fortran so far:
integer:: intValue
double precision:: dblValue
integer:: counter
integer:: check
open(unit=10, file='file.bin', form='unformatted', status='old', access='stream')
counter = 1
do
if ( counter == 1 ) then
read(unit=10, iostat=check) intValue
if ( check < 0 ) then
print*,"End Of File"
stop
else if ( check > 0 ) then
print*, "Error Detected"
stop
else if ( check == 0 ) then
counter = counter + 1
print*, intValue
end if
else if ( counter > 1 ) then
read(unit=10, iostat=check) dblValue
if ( check < 0 ) then
print*,"End Of File"
stop
else if ( check > 0 ) then
print*, "Error Detected"
stop
else if ( check == 0 ) then
counter = counter + 1
print*,dblValue
end if
end if
end do
close(unit=10)
This unfortunately does not work, and I get garbage numbers (e.g 6.4731191026611484E+212, 2.2844499004808491E-279 etc.). Could someone give some pointers on how to do this correctly? Also what would be a good way of writing and reading binary files interchangeably between Python and Fortran - as it seems like that is going to be one of the requirements of my application.
Thanks
access=stream:) I just realized that you're already doing that, so I deleted my answer for now. So, question: are you sure that the byte size of your pythonintand your fortranintegerare the same? You should check both. If there is a single byte of discrepancy, the misalignment of the data will lead to garbage afterreading. What fortran compiler are you using? How are you declaring yourintegers? What is the scpecific type of your pythonints?integersizes obviously also applies to thedoublesinvolved. And even if the types check out, there can still be an endianness-problem if you're using the two codes on two very different machines.kind=32is invalid for all compilers I know. Usekind=real32andkind=real64(the constants come from the moduleiso_fortran_envgcc.gnu.org/onlinedocs/gfortran/ISO_005fFORTRAN_005fENV.html). They should be equivalent to NumPy float32 and float64.