source: trunk/sphinxdocs/build/html/_modules/G2pwd_xye.html @ 1183

Last change on this file since 1183 was 1183, checked in by toby, 8 years ago

fixup number formatting; update produced doc files

  • Property svn:mime-type set to text/html
File size: 23.7 KB
Line 
1
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
6
7<html xmlns="http://www.w3.org/1999/xhtml">
8  <head>
9    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
10   
11    <title>G2pwd_xye &mdash; GSAS-II 0.2.0 documentation</title>
12   
13    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
14    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
15   
16    <script type="text/javascript">
17      var DOCUMENTATION_OPTIONS = {
18        URL_ROOT:    '../',
19        VERSION:     '0.2.0',
20        COLLAPSE_INDEX: false,
21        FILE_SUFFIX: '.html',
22        HAS_SOURCE:  true
23      };
24    </script>
25    <script type="text/javascript" src="../_static/jquery.js"></script>
26    <script type="text/javascript" src="../_static/underscore.js"></script>
27    <script type="text/javascript" src="../_static/doctools.js"></script>
28    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
29    <link rel="top" title="GSAS-II 0.2.0 documentation" href="../index.html" />
30    <link rel="up" title="Module code" href="index.html" /> 
31  </head>
32  <body>
33    <div class="related">
34      <h3>Navigation</h3>
35      <ul>
36        <li class="right" style="margin-right: 10px">
37          <a href="../genindex.html" title="General Index"
38             accesskey="I">index</a></li>
39        <li class="right" >
40          <a href="../py-modindex.html" title="Python Module Index"
41             >modules</a> |</li>
42        <li><a href="../index.html">GSAS-II 0.2.0 documentation</a> &raquo;</li>
43          <li><a href="index.html" accesskey="U">Module code</a> &raquo;</li> 
44      </ul>
45    </div> 
46
47    <div class="document">
48      <div class="documentwrapper">
49        <div class="bodywrapper">
50          <div class="body">
51           
52  <h1>Source code for G2pwd_xye</h1><div class="highlight"><pre>
53<span class="c"># -*- coding: utf-8 -*-</span>
54<span class="c">########### SVN repository information ###################</span>
55<span class="c"># $Date: 2013-12-16 10:43:01 -0600 (Mon, 16 Dec 2013) $</span>
56<span class="c"># $Author: toby $</span>
57<span class="c"># $Revision: 1168 $</span>
58<span class="c"># $URL: https://subversion.xor.aps.anl.gov/pyGSAS/trunk/imports/G2pwd_xye.py $</span>
59<span class="c"># $Id: G2pwd_xye.py 1168 2013-12-16 16:43:01Z toby $</span>
60<span class="c">########### SVN repository information ###################</span>
61<span class="sd">&#39;&#39;&#39;</span>
62<span class="sd">*Module G2pwd_xye: Topas .xye data*</span>
63<span class="sd">------------------------------------</span>
64
65<span class="sd">Routine to read in powder data from a Topas-compatible .xye file</span>
66
67<span class="sd">&#39;&#39;&#39;</span>
68
69<span class="kn">import</span> <span class="nn">sys</span>
70<span class="kn">import</span> <span class="nn">os.path</span> <span class="kn">as</span> <span class="nn">ospath</span>
71<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
72<span class="kn">import</span> <span class="nn">GSASIIIO</span> <span class="kn">as</span> <span class="nn">G2IO</span>
73<span class="kn">import</span> <span class="nn">GSASIIpath</span>
74<span class="n">GSASIIpath</span><span class="o">.</span><span class="n">SetVersionNumber</span><span class="p">(</span><span class="s">&quot;$Revision: 1168 $&quot;</span><span class="p">)</span>
75
76<div class="viewcode-block" id="xye_ReaderClass"><a class="viewcode-back" href="../imports.html#G2pwd_xye.xye_ReaderClass">[docs]</a><span class="k">class</span> <span class="nc">xye_ReaderClass</span><span class="p">(</span><span class="n">G2IO</span><span class="o">.</span><span class="n">ImportPowderData</span><span class="p">):</span>
77    <span class="s">&#39;Routines to import powder data from a .xye file&#39;</span>
78    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
79        <span class="nb">super</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span> <span class="c"># fancy way to self-reference</span>
80            <span class="n">extensionlist</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;.xye&#39;</span><span class="p">,),</span>
81            <span class="n">strictExtension</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
82            <span class="n">formatName</span> <span class="o">=</span> <span class="s">&#39;Topas xye&#39;</span><span class="p">,</span>
83            <span class="n">longFormatName</span> <span class="o">=</span> <span class="s">&#39;Topas .xye powder data file&#39;</span>
84            <span class="p">)</span>
85
86    <span class="c"># Validate the contents -- make sure we only have valid lines</span>
87<div class="viewcode-block" id="xye_ReaderClass.ContentsValidator"><a class="viewcode-back" href="../imports.html#G2pwd_xye.xye_ReaderClass.ContentsValidator">[docs]</a>    <span class="k">def</span> <span class="nf">ContentsValidator</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filepointer</span><span class="p">):</span>
88        <span class="s">&#39;Look through the file for expected types of lines in a valid Topas file&#39;</span>
89        <span class="n">gotCcomment</span> <span class="o">=</span> <span class="bp">False</span>
90        <span class="n">begin</span> <span class="o">=</span> <span class="bp">True</span>
91        <span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">S</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">filepointer</span><span class="p">):</span>
92            <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;</span> <span class="mi">1000</span><span class="p">:</span> <span class="k">break</span>
93            <span class="k">if</span> <span class="n">begin</span><span class="p">:</span>
94                <span class="k">if</span> <span class="n">gotCcomment</span> <span class="ow">and</span> <span class="n">S</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">&#39;*/&#39;</span><span class="p">)</span> <span class="o">&gt;</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
95                    <span class="n">begin</span> <span class="o">=</span> <span class="bp">False</span>
96                    <span class="k">continue</span>
97                <span class="k">if</span> <span class="n">S</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;/*&#39;</span><span class="p">):</span>
98                    <span class="n">gotCcomment</span> <span class="o">=</span> <span class="bp">True</span>
99                    <span class="k">continue</span>   
100                <span class="k">if</span> <span class="n">S</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;#&#39;</span><span class="p">:</span>
101                    <span class="k">continue</span>       <span class="c">#ignore comments, if any</span>
102                <span class="k">else</span><span class="p">:</span>
103                    <span class="n">begin</span> <span class="o">=</span> <span class="bp">False</span>
104                <span class="c"># valid line to read? </span>
105            <span class="n">vals</span> <span class="o">=</span> <span class="n">S</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
106            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">vals</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="n">vals</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
107                <span class="k">continue</span>
108            <span class="k">else</span><span class="p">:</span>
109                <span class="bp">self</span><span class="o">.</span><span class="n">errors</span> <span class="o">=</span> <span class="s">&#39;Unexpected information in line: &#39;</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>
110                <span class="bp">self</span><span class="o">.</span><span class="n">errors</span> <span class="o">+=</span> <span class="s">&#39;  &#39;</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">S</span><span class="p">)</span>
111                <span class="k">return</span> <span class="bp">False</span>
112        <span class="k">return</span> <span class="bp">True</span> <span class="c"># no errors encountered</span>
113</div>
114<div class="viewcode-block" id="xye_ReaderClass.Reader"><a class="viewcode-back" href="../imports.html#G2pwd_xye.xye_ReaderClass.Reader">[docs]</a>    <span class="k">def</span> <span class="nf">Reader</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">filename</span><span class="p">,</span><span class="n">filepointer</span><span class="p">,</span> <span class="n">ParentFrame</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">unused</span><span class="p">):</span>
115        <span class="s">&#39;Read a Topas file&#39;</span>
116        <span class="n">x</span> <span class="o">=</span> <span class="p">[]</span>
117        <span class="n">y</span> <span class="o">=</span> <span class="p">[]</span>
118        <span class="n">w</span> <span class="o">=</span> <span class="p">[]</span>
119        <span class="k">try</span><span class="p">:</span>
120            <span class="n">gotCcomment</span> <span class="o">=</span> <span class="bp">False</span>
121            <span class="n">begin</span> <span class="o">=</span> <span class="bp">True</span>
122            <span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">S</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">filepointer</span><span class="p">):</span>
123                <span class="bp">self</span><span class="o">.</span><span class="n">errors</span> <span class="o">=</span> <span class="s">&#39;Error reading line: &#39;</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>
124                <span class="c"># or a block of comments delimited by /* and */</span>
125                <span class="c"># or (GSAS style) each line can begin with &#39;#&#39;</span>
126                <span class="k">if</span> <span class="n">begin</span><span class="p">:</span>
127                    <span class="k">if</span> <span class="n">gotCcomment</span> <span class="ow">and</span> <span class="n">S</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">&#39;*/&#39;</span><span class="p">)</span> <span class="o">&gt;</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
128                        <span class="bp">self</span><span class="o">.</span><span class="n">comments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">S</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
129                        <span class="n">begin</span> <span class="o">=</span> <span class="bp">False</span>
130                        <span class="k">continue</span>
131                    <span class="k">if</span> <span class="n">S</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;/*&#39;</span><span class="p">):</span>
132                        <span class="bp">self</span><span class="o">.</span><span class="n">comments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">S</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
133                        <span class="n">gotCcomment</span> <span class="o">=</span> <span class="bp">True</span>
134                        <span class="k">continue</span>   
135                    <span class="k">if</span> <span class="n">S</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;#&#39;</span><span class="p">:</span>
136                        <span class="bp">self</span><span class="o">.</span><span class="n">comments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">S</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
137                        <span class="k">continue</span>       <span class="c">#ignore comments, if any</span>
138                    <span class="k">else</span><span class="p">:</span>
139                        <span class="n">begin</span> <span class="o">=</span> <span class="bp">False</span>
140                <span class="c"># valid line to read</span>
141                <span class="n">vals</span> <span class="o">=</span> <span class="n">S</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
142                <span class="k">try</span><span class="p">:</span>
143                    <span class="n">x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">vals</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span>
144                    <span class="n">f</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">vals</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
145                    <span class="k">if</span> <span class="n">f</span> <span class="o">&lt;=</span> <span class="mf">0.0</span><span class="p">:</span>
146                        <span class="n">y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mf">0.0</span><span class="p">)</span>
147                        <span class="n">w</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mf">1.0</span><span class="p">)</span>
148                    <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">vals</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
149                        <span class="n">y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">vals</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
150                        <span class="n">w</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mf">1.0</span><span class="o">/</span><span class="nb">float</span><span class="p">(</span><span class="n">vals</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
151                    <span class="k">else</span><span class="p">:</span>
152                        <span class="n">y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">vals</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
153                        <span class="n">w</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mf">1.0</span><span class="o">/</span><span class="nb">float</span><span class="p">(</span><span class="n">vals</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
154                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
155                    <span class="n">msg</span> <span class="o">=</span> <span class="s">&#39;Error in line &#39;</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>
156                    <span class="k">print</span> <span class="n">msg</span>
157                    <span class="k">break</span>
158            <span class="n">N</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
159            <span class="bp">self</span><span class="o">.</span><span class="n">powderdata</span> <span class="o">=</span> <span class="p">[</span>
160                <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="c"># x-axis values</span>
161                <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">y</span><span class="p">),</span> <span class="c"># powder pattern intensities</span>
162                <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">w</span><span class="p">),</span> <span class="c"># 1/sig(intensity)^2 values (weights)</span>
163                <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">N</span><span class="p">),</span> <span class="c"># calc. intensities (zero)</span>
164                <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">N</span><span class="p">),</span> <span class="c"># calc. background (zero)</span>
165                <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">N</span><span class="p">),</span> <span class="c"># obs-calc profiles</span>
166                <span class="p">]</span>
167            <span class="bp">self</span><span class="o">.</span><span class="n">powderentry</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">filename</span>
168            <span class="c">#self.powderentry[1] = pos # bank offset (N/A here)</span>
169            <span class="bp">self</span><span class="o">.</span><span class="n">powderentry</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span> <span class="c"># xye file only has one bank</span>
170            <span class="bp">self</span><span class="o">.</span><span class="n">idstring</span> <span class="o">=</span> <span class="n">ospath</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
171            <span class="c"># scan comments for temperature</span>
172            <span class="n">Temperature</span> <span class="o">=</span> <span class="mi">300</span>
173            <span class="k">for</span> <span class="n">S</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">comments</span><span class="p">:</span>
174                <span class="k">if</span> <span class="s">&#39;Temp&#39;</span> <span class="ow">in</span> <span class="n">S</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;=&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]:</span>
175                    <span class="k">try</span><span class="p">:</span>
176                        <span class="n">Temperature</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">S</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;=&#39;</span><span class="p">)[</span><span class="mi">1</span><span class="p">])</span>
177                    <span class="k">except</span><span class="p">:</span>
178                        <span class="k">pass</span>
179            <span class="bp">self</span><span class="o">.</span><span class="n">Sample</span><span class="p">[</span><span class="s">&#39;Temperature&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">Temperature</span>
180
181            <span class="k">return</span> <span class="bp">True</span>
182        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">detail</span><span class="p">:</span>
183            <span class="bp">self</span><span class="o">.</span><span class="n">errors</span> <span class="o">+=</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">  &#39;</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">detail</span><span class="p">)</span>
184            <span class="k">print</span> <span class="bp">self</span><span class="o">.</span><span class="n">formatName</span><span class="o">+</span><span class="s">&#39; read error:&#39;</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">detail</span><span class="p">)</span> <span class="c"># for testing</span>
185            <span class="kn">import</span> <span class="nn">traceback</span>
186            <span class="n">traceback</span><span class="o">.</span><span class="n">print_exc</span><span class="p">(</span><span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>
187            <span class="k">return</span> <span class="bp">False</span></div></div>
188</pre></div>
189
190          </div>
191        </div>
192      </div>
193      <div class="sphinxsidebar">
194        <div class="sphinxsidebarwrapper">
195            <p class="logo"><a href="../index.html">
196              <img class="logo" src="../_static/G2_html_logo.png" alt="Logo"/>
197            </a></p>
198<div id="searchbox" style="display: none">
199  <h3>Quick search</h3>
200    <form class="search" action="../search.html" method="get">
201      <input type="text" name="q" />
202      <input type="submit" value="Go" />
203      <input type="hidden" name="check_keywords" value="yes" />
204      <input type="hidden" name="area" value="default" />
205    </form>
206    <p class="searchtip" style="font-size: 90%">
207    Enter search terms or a module, class or function name.
208    </p>
209</div>
210<script type="text/javascript">$('#searchbox').show(0);</script>
211        </div>
212      </div>
213      <div class="clearer"></div>
214    </div>
215    <div class="related">
216      <h3>Navigation</h3>
217      <ul>
218        <li class="right" style="margin-right: 10px">
219          <a href="../genindex.html" title="General Index"
220             >index</a></li>
221        <li class="right" >
222          <a href="../py-modindex.html" title="Python Module Index"
223             >modules</a> |</li>
224        <li><a href="../index.html">GSAS-II 0.2.0 documentation</a> &raquo;</li>
225          <li><a href="index.html" >Module code</a> &raquo;</li> 
226      </ul>
227    </div>
228    <div class="footer">
229        &copy; Copyright 2013, Von Dreele and Toby for Argonne National Laboratory.
230      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
231    </div>
232  </body>
233</html>
Note: See TracBrowser for help on using the repository browser.