the fortranfile module¶
Based on a script written by Neil Martinsen-Burrell.
Copyright (C) 2001, 2002 Enthought, Inc. All rights reserved.
Copyright (C) 2003-2013 SciPy Developers. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of Enthought nor the names of the SciPy Developers may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS âAS ISâ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Module to read / write Fortran unformatted sequential files.
This is in the spirit of code written by Neil Martinsen-Burrell and Joe Zuntz.
- class pNbody.fortranfile.FortranFile(filename, mode='r', header_dtype=<class 'numpy.uint32'>)¶
A file object for unformatted sequential files from Fortran code.
- Parameters
filename (file or str) – Open file object or filename.
mode ({'r', 'w'}, optional) – Read-write mode, default is ‘r’.
header_dtype (dtype, optional) – Data type of the header. Size and endiness must match the input/output file.
Notes
These files are broken up into records of unspecified types. The size of each record is given at the start (although the size of this header is not standard) and the data is written onto disk without any formatting. Fortran compilers supporting the BACKSPACE statement will write a second copy of the size to facilitate backwards seeking.
This class only supports files written with both sizes for the record. It also does not support the subrecords used in Intel and gfortran compilers for records which are greater than 2GB with a 4-byte header.
An example of an unformatted sequential file in Fortran would be written as:
OPEN(1, FILE=myfilename, FORM='unformatted') WRITE(1) myvariable
Since this is a non-standard file format, whose contents depend on the compiler and the endianness of the machine, caution is advised. Files from gfortran 4.8.0 and gfortran 4.1.2 on x86_64 are known to work.
Consider using Fortran direct-access files or files from the newer Stream I/O, which can be easily read by numpy.fromfile.
Examples
To create an unformatted sequential Fortran file:
>>> from scipy.io import FortranFile >>> f = FortranFile('test.unf', 'w') >>> f.write_record(np.array([1,2,3,4,5],dtype=np.int32)) >>> f.write_record(np.linspace(0,1,20).reshape((5,-1))) >>> f.close()
To read this file:
>>> from scipy.io import FortranFile >>> f = FortranFile('test.unf', 'r') >>> print(f.read_ints(dtype=np.int32)) [1 2 3 4 5] >>> print(f.read_reals(dtype=float).reshape((5,-1))) [[ 0. 0.05263158 0.10526316 0.15789474] [ 0.21052632 0.26315789 0.31578947 0.36842105] [ 0.42105263 0.47368421 0.52631579 0.57894737] [ 0.63157895 0.68421053 0.73684211 0.78947368] [ 0.84210526 0.89473684 0.94736842 1. ]] >>> f.close()
- close()¶
Closes the file. It is unsupported to call any other methods off this object after closing it. Note that this class supports the ‘with’ statement in modern versions of Python, to call this automatically
- read_ints(dtype='i4')¶
Reads a record of a given type from the file, defaulting to an integer type (INTEGER*4 in Fortran)
- Parameters
dtype (dtype, optional) – Data type specifying the size and endiness of the data.
- Returns
data – A one-dimensional array object.
- Return type
ndarray
See also
- read_reals(dtype='f8')¶
Reads a record of a given type from the file, defaulting to a floating point number (real*8 in Fortran)
- Parameters
dtype (dtype, optional) – Data type specifying the size and endiness of the data.
- Returns
data – A one-dimensional array object.
- Return type
ndarray
See also
- read_record(dtype=None)¶
Reads a record of a given type from the file.
- Parameters
dtype (dtype, optional) – Data type specifying the size and endiness of the data.
- Returns
data – A one-dimensional array object.
- Return type
ndarray
Notes
If the record contains a multi-dimensional array, calling reshape or resize will restructure the array to the correct size. Since Fortran multidimensional arrays are stored in column-major format, this may have some non-intuitive consequences. If the variable was declared as ‘INTEGER var(5,4)’, for example, var could be read with ‘read_record(dtype=np.integer).reshape( (4,5) )’ since Python uses row-major ordering of indices.
One can transpose to obtain the indices in the same order as in Fortran.
For records that contain several variables or mixed types (as opposed to single scalar or array types), it is possible to specify a dtype with mixed types:
>>> record = f.read_record([('a', '<f4'), ('b', '<i4')]) >>> record['a'] # access the variable 'a' 5.6
and if any of the variables are arrays, the shape can be specified as the third item in the relevant tuple:
>>> record = f.read_record([('a', '<f4'), ('b', '<i4', (3,3))])
Numpy also supports a short syntax for this kind of type:
>>> record = f.read_record('<f4,(3,3)<i4') >>> record['f0'] # variables are called f0, f1, ... 5.6
See also
- write_record(s)¶
Write a record (including sizes) to the file.
- Parameters
s (array_like) – The data to write.