[SciPy-User] writing data to binary for fortran

Neil Martinsen-Burrell nmb at wartburg.edu
Wed May 12 14:00:36 EDT 2010


On 2010-05-12 12:56, Gideon wrote:
> I've tried the following.
>
> In Python:
> import numpy as np
> from FortranFile import FortranFile
>
> x = np.random.rand(10)
> f = FortranFile('test.bin',mode='w')
> f.writeReals(x)
> f.close()
>
> In Fortran:
>        program bintest
>
>        double precision x(10)
>        integer j
>
>        open(unit=80, file='test.bin', status='old', form='unformatted')
>
>        read(80) x
>        close(80)
>
>        do j=1,10
>           write(*,*) x(j)
>
>        enddo
>
>
>        end
>
> then at the command line,
>
> gfortran bintest.f -o bintest
> ./bintest
> At line 9 of file bintest.f (unit = 80, file = 'test.bin')
> Fortran runtime error: I/O past end of record on unformatted file
>
> Note, I have no difficulty reading the test.bin file back in, while in
> python, using the FortranFile.py routines.

It is likely that the problem is with the endian-ness of the file being 
created by FortranFile not matching what is expected by the fortran 
compiler.  (There is a reason that the format of unformatted I/O is not 
specified in the Fortran standard.)  Try the above with different 
settings of FortranFile(..., endian='<') or '>' or '='.

-Neil



More information about the SciPy-User mailing list