[Numpy-discussion] example reading binary Fortran file
Pradeep Jha
pradeep.kumar.jha@gmail....
Wed Feb 13 01:51:04 CST 2013
Neil Martinsen-Burrell <nmb <at> wartburg.edu> writes:
>
> On 2009-05-29 10:12 , David Froger wrote:
> > I think the FortranFile class is not intended to read arrays written
> > with the syntax 'write(11) array1, array2, array3' (correct me if I'm
> > wrong). This is the use in the laboratory where I'm currently
> > completing a phd.
>
> You're half wrong. FortranFile can read arrays written as above, but it
> sees them as a single real array. So, with the attached Fortran program::
>
> In [1]: from fortranfile import FortranFile
>
> In [2]: f = FortranFile('uxuyp.bin', endian='<') # Original bug was
> incorrect byte order
>
> In [3]: u = f.readReals()
>
> In [4]: u.shape
> Out[4]: (20,)
>
> In [5]: u
> Out[5]:
> array([ 101., 111., 102., 112., 103., 113., 104., 114., 105.,
> 115., 201., 211., 202., 212., 203., 213., 204., 214.,
> 205., 215.], dtype=float32)
>
> In [6]: ux = u[:10].reshape(2,5); uy = u[10:].reshape(2,5)
>
> In [7]: p = f.readReals().reshape(2,5)
>
> In [8]: ux, uy, p
> Out[8]:
> (array([[ 101., 111., 102., 112., 103.],
> [ 113., 104., 114., 105., 115.]], dtype=float32),
> array([[ 201., 211., 202., 212., 203.],
> [ 213., 204., 214., 205., 215.]], dtype=float32),
> array([[ 301., 311., 302., 312., 303.],
> [ 313., 304., 314., 305., 315.]], dtype=float32))
>
> What doesn't currently work is to have arrays of mixed types in the same
> write statement, e.g.
>
> integer :: index(10)
> real :: x(10,10)
> ...
> write(13) x, index
>
> To address the original problem, I've changed the code to default to the
> native byte-ordering (f.ENDIAN='@') and to be more informative about
> what happened in the error. In the latest version (attached):
>
> In [1]: from fortranfile import FortranFile
>
> In [2]: f = FortranFile('uxuyp.bin', endian='>') # incorrect endian-ness
>
> In [3]: u = f.readReals()
>
> IOError: Could not read enough data. Wanted 1342177280 bytes, got 132
>
Hello,
I am trying to read a fortran unformatted binary file with
FortranFile as follows but I get an error.
-----------------------------------------
>>> from FortranFile import FortranFile
>>> f = FortranFile("vor_465.float",endian="<")
>>> u = f.readReals()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "FortranFile.py", line 193, in readReals
data_str = self.readRecord()
File "FortranFile.py", line 140, in readRecord
data_str = self._read_exactly(l)
File "FortranFile.py", line 124, in _read_exactly
' Wanted %d bytes, got %d.' % (num_bytes, l))
IOError: Could not read enough data.
Wanted 1124054321 bytes, got 536870908.
------------------------------------------
My file, "vor_465.float" has the following size:
------------------------------------------
[pradeep@laptop ~]$ls -l vor_465.float
-rwxr-xr-x 1 pradeep staff 536870912 Feb 13 ***
-----------------------------------------
I am sure this file has data in the right format as
when I read it using fortran using the following command:
--------------------------------------------------
open (in_file_id,FILE="vor_465.float",form='unformatted',
access='direct',recl=4*512*512*512)
read (in_file_id,rec=1) buffer
--------------------------------------------------
it works completely fine. This data contains single precision float
values for a scalar over a cube with 512 points in each direction.
Any ideas?
Pradeep
More information about the NumPy-Discussion
mailing list