[Numpy-discussion] example reading binary Fortran file

Pradeep Jha pradeep.kumar.jha@gmail....
Wed Feb 13 02:49:36 CST 2013


Pradeep Jha <pradeep.kumar.jha <at> gmail.com> writes:

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


Also, when I am trying to write an unformatted binary file with 
FortranFile I am getting the following error:

---------------------------------------------------
[pradeep@laptop ~]$python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from FortranFile import FortranFile
>>> f = FortranFile("filename", endian="<")
>>> array = [1,2,3,4,5,6,6,7]
>>> f.writeReals(array)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "FortranFile.py", line 214, in writeReals
    self._write_check(length_bytes)
  File "FortranFile.py", line 135, in _write_check
    number_of_bytes))
IOError: File not open for writing
------------------------------------------------

I dont know how to make the file available for writing. I 
checked the file permissions and they are completely fine.

Please help,
Pradeep






More information about the NumPy-Discussion mailing list