[Numpy-discussion] reading binary Fortran

Catherine Moroney Catherine.M.Moroney@jpl.nasa....
Tue Feb 3 18:27:12 CST 2009

I've noticed a lot of discussion on how to read binary files
written out from Fortran, and nobody seems to have mentioned
how to modify your Fortran code so it writes out a file that
can be read with numpy.fromfile() in a single line.

For example, to write out a NLINE x NSMP array of floats in Fortran
in a line by line fashion, the code below works just fine.

open(iunit,file=flname,form='unformatted',access='direct', &
do iline=1,nline
    write(iunit,rec=iline) (data(iline,ismp),ismp=1,nsmp)
end do

It's more code than simply "write(iunit) data", but it has the advantage
of being easily imported into python, matlab and other software packages
that can read flat binary data.  To import a file written out in
this fashion into numpy, a single call to numpy.fromfile(flname)
works with no need to define datatypes or the like.

Apologies if I'm missing some reason why the above doesn't work or
is not preferable to the "write(iunit) data".


More information about the Numpy-discussion mailing list