[SciPy-user] reading binary files written by a gfortran code...

George Nurser gnurser at googlemail.com
Sun Jun 15 14:31:14 EDT 2008


Hi Fred,

Fortran ( and this includes gfortran) seems to add stuff at the
beginning and end of a file.

If the unformatted fortran file just holds one array of real*8 data
the following works for me (and I think it works for real*4 as well).
It may need some modernizing...:

from numpy import *
def readbin(file_in,swap=False,f8=True):
    if f8:
        htot =fromfile(file = file_in ,dtype=float)
        c = htot.view(single)
        hc = c[1:-1].view(double)
    else:
        htot =fromfile(file = file_in ,dtype=float32)
        hc = c[1:-1]

    if swap:
        hc = hc.byteswap()
    return hc


import readbin
a1 = readbin.readbin('unf.dat')

2008/6/15 fred <fredmfp at gmail.com>:
> Hi,
>
> Ok, this is a well known issue that binary files written by a fortran
> program have a "special" format.
>
> So my question is : is it possible to write binary file _using gfortran_
> (no problem with intel fortran compiler) without this formatting ?
>
> Is there a peculiar syntax to the open() function ?
>
> I did not find any relevant information on the web.
>
> The obvious reason is that I have to read these files with
> scipy.io.numpyio fread method.
>
> TIA.
>
> Cheers,
>
> --
> Fred
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>



More information about the SciPy-User mailing list