Page MenuHomec4science

Io.html
No OneTemporary

File Metadata

Created
Tue, Nov 26, 00:52
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>the Io module &mdash; pNbody v4 documentation</title>
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '4',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="top" title="pNbody v4 documentation" href="../index.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="../np-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li><a href="../index.html">pNbody v4 documentation</a> &raquo;</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="the-io-module">
<h1>the Io module<a class="headerlink" href="#the-io-module" title="Permalink to this headline"></a></h1>
<dl class="function">
<dt id="pNbody.io.checkfile">
<tt class="descclassname">pNbody.io.</tt><tt class="descname">checkfile</tt><big>(</big><em>name</em><big>)</big><a class="headerlink" href="#pNbody.io.checkfile" title="Permalink to this definition"></a></dt>
<dd><p>Check if a file exists. An error is generated if the file
does not exists.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters :</th><td class="field-body"><strong>name</strong> : the path to a filename</td>
</tr>
</tbody>
</table>
<p class="rubric">Examples</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">io</span><span class="o">.</span><span class="n">checkfile</span><span class="p">(</span><span class="s">&#39;an_existing_file&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">io</span><span class="o">.</span><span class="n">checkfile</span><span class="p">(</span><span class="s">&#39;a_non_existing_file&#39;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n-Identifier">&lt;module&gt;</span>
File <span class="nb">&quot;/home/epfl/revaz/local/lib64/python2.6/site-packages/pNbody/io.py&quot;</span>, line <span class="m">33</span>, in <span class="n-Identifier">checkfile</span>
<span class="k">raise</span> <span class="ne">IOError</span><span class="p">(</span><span class="mi">915</span><span class="p">,</span><span class="s">&#39;file </span><span class="si">%s</span><span class="s"> not found ! Pease check the file name.&#39;</span><span class="o">%</span><span class="p">(</span><span class="n">name</span><span class="p">))</span>
<span class="nc">IOError</span>: <span class="n-Identifier">[Errno 915] file nofile not found ! Pease check the file name. </span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="pNbody.io.end_of_file">
<tt class="descclassname">pNbody.io.</tt><tt class="descname">end_of_file</tt><big>(</big><em>f</em>, <em>pio='no'</em>, <em>MPI=None</em><big>)</big><a class="headerlink" href="#pNbody.io.end_of_file" title="Permalink to this definition"></a></dt>
<dd><p>Return True if we have reached the end of the file f, False instead</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters :</th><td class="field-body"><p class="first"><strong>f</strong> : ndarray or matrix object</p>
<blockquote>
<p>an open file</p>
</blockquote>
<p><strong>pio</strong> : &#8216;yes&#8217; or &#8216;no&#8217;</p>
<blockquote>
<p>if the file is read in parallel or not</p>
</blockquote>
<p><strong>MPI</strong> : MPI communicator</p>
</td>
</tr>
<tr class="field"><th class="field-name">Returns :</th><td class="field-body"><p class="first"><strong>status</strong> : Bool</p>
<blockquote class="last">
<p>True if the we reached the end of the file
False if not</p>
</blockquote>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="pNbody.io.write_array">
<tt class="descclassname">pNbody.io.</tt><tt class="descname">write_array</tt><big>(</big><em>file</em>, <em>vec</em><big>)</big><a class="headerlink" href="#pNbody.io.write_array" title="Permalink to this definition"></a></dt>
<dd><p>Write an array to a file, in a very simple ascii format.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters :</th><td class="field-body"><p class="first"><strong>file</strong> : the path to a file</p>
<p class="last"><strong>vec</strong> : an ndarray object</p>
</td>
</tr>
</tbody>
</table>
<p class="rubric">Examples</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">io</span><span class="o">.</span><span class="n">write_array</span><span class="p">(</span><span class="s">&#39;/tmp/array.dat&#39;</span><span class="p">,</span><span class="n">x</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="pNbody.io.read_ascii">
<tt class="descclassname">pNbody.io.</tt><tt class="descname">read_ascii</tt><big>(</big><em>file</em>, <em>columns=None</em>, <em>lines=None</em>, <em>dtype=&lt;type 'float'&gt;</em>, <em>skipheader=False</em>, <em>cchar='#'</em><big>)</big><a class="headerlink" href="#pNbody.io.read_ascii" title="Permalink to this definition"></a></dt>
<dd><p>Read an ascii file.
The function allows to set the number of columns or line to read.
If it contains a header, the header is used to label all column. In
this case, a dictionary is returned.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters :</th><td class="field-body"><p class="first"><strong>file</strong> : the path to a file or an open file</p>
<p><strong>columns</strong> : list</p>
<blockquote>
<p>the list of the columns to read
if none, all columns are read</p>
</blockquote>
<p><strong>lines</strong> : list</p>
<blockquote>
<p>the list of the lines to read
if none, all lines are read</p>
</blockquote>
<p><strong>dtype</strong> : dtype</p>
<blockquote>
<p>the ndtype of the objects to read</p>
</blockquote>
<p><strong>skipheader</strong> : bool</p>
<blockquote>
<p>if true, do not read the header
if there is one</p>
</blockquote>
<p><strong>cchar</strong> : char</p>
<blockquote>
<p>lines begining with cchar are skiped
the first line is considered as the header</p>
</blockquote>
</td>
</tr>
<tr class="field"><th class="field-name">Returns :</th><td class="field-body"><p class="first"><strong>data</strong> : Dict or ndarray</p>
<blockquote class="last">
<p>A python dictionary or an ndarray object</p>
</blockquote>
</td>
</tr>
</tbody>
</table>
<p class="rubric">Examples</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;afile.txt&#39;</span><span class="p">,</span><span class="s">&#39;w&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;# x y&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)):</span>
<span class="gp">... </span> <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%g</span><span class="s"> </span><span class="si">%g</span><span class="s">&#39;</span><span class="o">%</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">]))</span>
<span class="gp">... </span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pNbody</span> <span class="kn">import</span> <span class="n">io</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="n">io</span><span class="o">.</span><span class="n">read_ascii</span><span class="p">(</span><span class="s">&quot;afile.txt&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span><span class="p">[</span><span class="s">&#39;x&#39;</span><span class="p">]</span>
<span class="go">array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span><span class="p">[</span><span class="s">&#39;y&#39;</span><span class="p">]</span>
<span class="go">array([ 0., 1., 4., 9., 16., 25., 36., 49., 64., 81.]) </span>
</pre></div>
</div>
</dd></dl>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="../index.html">
<img class="logo" src="../_static/icon-small.jpg" alt="Logo"/>
</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/rst/Io.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<input type="text" name="q" size="18" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="../np-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li><a href="../index.html">pNbody v4 documentation</a> &raquo;</li>
</ul>
</div>
<div class="footer">
&copy; Copyright 2011, Yves Revaz.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.4.
</div>
</body>
</html>

Event Timeline