I\'m trying to read a binary file output from Fortran code below, but the results are not the same from output file.
Fortran 77 code:
program tes
Use nupy.fromfile (http://docs.scipy.org/doc/numpy/reference/generated/numpy.fromfile.html)
I guess you have missed something in fortran code, to write to binary file apply this code:
program test
implicit none
integer i,j,k,l, reclen
real*4 pcp(2,3,4)
inquire(iolength=reclen)pcp(:,:,1)
open(10, file='pcp.bin', form='unformatted', access = 'direct', recl = reclen)
pcp = 0
l = 0
do i=1,2
do j=1,2
do k=1,2
print*,i,j,k,k+l*2
pcp(i,j,k)=k+l*2
l = l + 1
enddo
enddo
enddo
do k=1,4
write(10, rec=k)pcp(:,:,k)
enddo
close(10)
end
To read file by python:
import numpy as np
with open('pcp.bin','rb') as f:
for k in xrange(4):
data = np.fromfile(f, dtype=np.float32, count = 2*3)
print np.reshape(data,(2,3))
Output:
[[ 1. 9. 5.]
[ 13. 0. 0.]]
[[ 4. 12. 8.]
[ 16. 0. 0.]]
[[ 0. 0. 0.]
[ 0. 0. 0.]]
[[ 0. 0. 0.]
[ 0. 0. 0.]]