[Numpy-discussion] example reading binary Fortran file

Sturla Molden sturla@molden...
Fri Jan 30 10:23:45 CST 2009

On 1/30/2009 5:03 PM, David Froger wrote:

> 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 ?

If you want to be completely safe, read the file in Fortran, then send 
it as an array to Python (use f2py). Aside from that, assuming your 
compiler only writes the raw bytes in Fortran order to the file:

If you have

real(4),dimension(nx,ny):: ux

in Fortran, and writes ux to disk. It could be retrieved like this in NumPy:

ux = np.fromfile(nx*ny, dtype=np.float32).view((nx,ny), order='F')

assuming real(kind=4) is equivalent to np.float32.

Sturla Molden

More information about the Numpy-discussion mailing list