<spanid="the-fortranfile-module"></span><h1>the fortranfile module<aclass="headerlink"href="#module-pNbody.fortranfile"title="Permalink to this headline">¶</a></h1>
<p>Defines a file-derived class to read/write Fortran unformatted files.</p>
<p>The assumption is that a Fortran unformatted file is being written by
the Fortran runtime as a sequence of records. Each record consists of
an integer (of the default size [usually 32 or 64 bits]) giving the
length of the following data in bytes, then the data itself, then the
same integer as before.</p>
<divclass="section"id="examples">
<h2>Examples<aclass="headerlink"href="#examples"title="Permalink to this headline">¶</a></h2>
<dlclass="docutils">
<dt>To use the default endian and precision settings, one can just do::</dt>
<dd><divclass="first last highlight-python"><divclass="highlight"><pre><spanclass="gp">>>></span><spanclass="n">f</span><spanclass="o">=</span><spanclass="n">FortranFile</span><spanclass="p">(</span><spanclass="s">'filename'</span><spanclass="p">)</span>
<dt>One can read arrays with varying precisions::</dt>
<dd><divclass="first last highlight-python"><divclass="highlight"><pre><spanclass="gp">>>></span><spanclass="n">f</span><spanclass="o">=</span><spanclass="n">FortranFile</span><spanclass="p">(</span><spanclass="s">'filename'</span><spanclass="p">)</span>
<p>Where the format codes are those used by Python’s struct module.</p>
<dlclass="docutils">
<dt>One can change the default endian-ness and header precision::</dt>
<dd><divclass="first last highlight-python"><divclass="highlight"><pre><spanclass="gp">>>></span><spanclass="n">f</span><spanclass="o">=</span><spanclass="n">FortranFile</span><spanclass="p">(</span><spanclass="s">'filename'</span><spanclass="p">,</span><spanclass="n">endian</span><spanclass="o">=</span><spanclass="s">'>'</span><spanclass="p">,</span><spanclass="n">header_prec</span><spanclass="o">=</span><spanclass="s">'l'</span><spanclass="p">)</span>
</pre></div>
</div>
</dd>
</dl>
<p>for a file with little-endian data whose record headers are long
integers.</p>
<dlclass="class">
<dtid="pNbody.fortranfile.FortranFile">
<emclass="property">class </em><ttclass="descclassname">pNbody.fortranfile.</tt><ttclass="descname">FortranFile</tt><big>(</big><em>fname</em>, <em>endian='@'</em>, <em>header_prec='i'</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><aclass="headerlink"href="#pNbody.fortranfile.FortranFile"title="Permalink to this definition">¶</a></dt>
<dd><p>File with methods for dealing with fortran unformatted data files</p>
<pclass="rubric">Methods</p>
<dlclass="attribute">
<dtid="pNbody.fortranfile.FortranFile.ENDIAN">
<ttclass="descname">ENDIAN</tt><aclass="headerlink"href="#pNbody.fortranfile.FortranFile.ENDIAN"title="Permalink to this definition">¶</a></dt>
<dd><p>Possible endian values are ‘<’, ‘>’, ‘@’, ‘=’</p>
<ttclass="descname">HEADER_PREC</tt><aclass="headerlink"href="#pNbody.fortranfile.FortranFile.HEADER_PREC"title="Permalink to this definition">¶</a></dt>
<dd><p>Possible header precisions are ‘h’, ‘i’, ‘l’, ‘q’</p>
</dd></dl>
<dlclass="method">
<dtid="pNbody.fortranfile.FortranFile.readInts">
<ttclass="descname">readInts</tt><big>(</big><em>prec='i'</em><big>)</big><aclass="headerlink"href="#pNbody.fortranfile.FortranFile.readInts"title="Permalink to this definition">¶</a></dt>
<p>Specify the precision of the data to be read using
character codes from Python’s struct module. Possible
values are ‘h’, ‘i’, ‘l’ and ‘q’</p>
</blockquote>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dlclass="method">
<dtid="pNbody.fortranfile.FortranFile.readReals">
<ttclass="descname">readReals</tt><big>(</big><em>prec='f'</em><big>)</big><aclass="headerlink"href="#pNbody.fortranfile.FortranFile.readReals"title="Permalink to this definition">¶</a></dt>
<ttclass="descname">readRecord</tt><big>(</big><big>)</big><aclass="headerlink"href="#pNbody.fortranfile.FortranFile.readRecord"title="Permalink to this definition">¶</a></dt>
<ttclass="descname">readString</tt><big>(</big><big>)</big><aclass="headerlink"href="#pNbody.fortranfile.FortranFile.readString"title="Permalink to this definition">¶</a></dt>
<dd><p>Read a string.</p>
</dd></dl>
<dlclass="method">
<dtid="pNbody.fortranfile.FortranFile.writeInts">
<ttclass="descname">writeInts</tt><big>(</big><em>ints</em>, <em>prec='i'</em><big>)</big><aclass="headerlink"href="#pNbody.fortranfile.FortranFile.writeInts"title="Permalink to this definition">¶</a></dt>
<dd><p>Write an array of integers in given precision</p>
<ttclass="descname">writeReals</tt><big>(</big><em>reals</em>, <em>prec='f'</em><big>)</big><aclass="headerlink"href="#pNbody.fortranfile.FortranFile.writeReals"title="Permalink to this definition">¶</a></dt>
<dd><p>Write an array of floats in given precision</p>
<ttclass="descname">writeRecord</tt><big>(</big><em>s</em><big>)</big><aclass="headerlink"href="#pNbody.fortranfile.FortranFile.writeRecord"title="Permalink to this definition">¶</a></dt>
<ttclass="descname">writeString</tt><big>(</big><em>s</em><big>)</big><aclass="headerlink"href="#pNbody.fortranfile.FortranFile.writeString"title="Permalink to this definition">¶</a></dt>