[Numpy-discussion] Another question on reading from binary FORTRAN file
Michael Gilbert
michael.s.gilbert@gmail....
Mon Mar 9 22:41:12 CDT 2009
On Mon, 9 Mar 2009 18:21:45 -0400 "Michael S. Gilbert" wrote:
> On Mon, 9 Mar 2009 21:45:42 +0100, Mark Bakker wrote:
>
> > Hello -
> >
> > I tried to figure this out from the list, but haven't succeeded yet.
> >
> > I have a simple FORTRAN binary file.
> > It contains:
> > 1 integer
> > 1 float
> > 1 array with 16 numbers (float)
> >
> > How do I read these into Python?
>
> I figured this out a long time (4 years) ago, but haven't thought about
> it for a while, so I don't really have the answer, but I can provide
> a little guidance. Fortran pads its output, so you just have to
> figure out how the padding works and how to get around it to get the
> information that you actually need.
>
> I suggest writing out some examples via fortran and using hexdump to
> view the results, then you can determine the pattern. For example,
> you would get something like:
>
> $ hexdump -C fort.out
> 0000008 0000000F 00000008
>
> when you use fortran to write 15 as an integer. Then you can use
> python's binary file i/o to read in the file and extract the
> information that you are interested in. Hope this helps.
>
> Regards,
> Mike
I probably should have mentioned fromfile, which you can actually use
to read the binary data:
fid = open( 'fort.out' , 'r' )
junk = numpy.fromfile( fid , numpy.int , 1 )
integer = numpy.fromfile( fid, numpy.int , 1 )
junk = numpy.fromfile( fid , numpy.int , 2 )
floats = numpy.fromfile( fid , numpy.float , 16 )
.
.
.
fid.close()
Regards,
Mike
More information about the Numpy-discussion
mailing list