[Numpy-discussion] example reading binary Fortran file

David Froger david.froger.info@gmail....
Fri Jan 30 10:03:44 CST 2009


My question is about reading Fortran binary file (oh no this question

Until now, I was using the unpack module like that :

def lread(f,fourBeginning,fourEnd,*tuple):
    from struct import unpack
    """Reading a Fortran binary file in litte-endian"""
    if fourBeginning: f.seek(4,1)
    for array in tuple:
        for elt in xrange(array.size):
            transpose(array).flat[elt] =
    if fourEnd: f.seek(4,1)

After googling, I read that fopen and npfille was deprecated, and we should
use numpy.fromfile and ndarray.tofile, but despite of the documentaion, the
cookbook, the mailling list and google, I don't succed in making a simple
example. Considering the simple Fortran code below what is the Python script
to read the four arrrays? What about if my pc is little endian and the file
big endian?

I think it will be a good idea to put the Fortran writting-arrays code and
the Python reading-array script in the cookbook and maybe a page to help
people comming from Fortran to start with Python ?


David Froger

program makeArray

implicit none

integer,parameter:: nx=10,ny=20
real(4),dimension(nx,ny):: ux,uy,p
integer :: i,j


do i = 1,nx
do j = 1,ny
   ux(i,j) = real(i*j)
   uy(i,j) = real(i)/real(j)
   p (i,j)  = real(i) + real(j)

write(11) ux,uy
write(11) p


end program makeArray
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20090130/55ac9ebd/attachment.html 

More information about the Numpy-discussion mailing list