source: trunk/G2importpwd_xye.py @ 609

Last change on this file since 609 was 607, checked in by toby, 13 years ago

revise help to include tutorials on tree only; add kw args to all readers; cache cif for multiple passes; start on powder imports

File size: 3.7 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2012-02-13 11:33:35 -0600 (Mon, 13 Feb 2012) $
4# $Author: vondreele & toby $
5# $Revision: 482 $
6# $URL: https://subversion.xor.aps.anl.gov/pyGSAS/trunk/G2importphase.py $
7# $Id: G2importphase.py 482 2012-02-13 17:33:35Z vondreele $
8########### SVN repository information ###################
9# a routine to read in powder data from a Topas-compatible .xye file
10#
11import sys
12import os.path as ospath
13import numpy as np
14import GSASIIIO as G2IO
15
16class xye_ReaderClass(G2IO.ImportPowderData):
17    'Routines to import powder data from a .xye file'
18    def __init__(self):
19        super(self.__class__,self).__init__( # fancy way to self-reference
20            extensionlist=('.xye',),
21            strictExtension=False,
22            formatName = 'Topas xye',
23            longFormatName = 'Topas .xye powder data file'
24            )
25
26    def Reader(self,filename,filepointer, ParentFrame=None, **unused):
27        x = []
28        y = []
29        w = []
30        try:
31            gotCcomment = False
32            begin = True
33            for i,S in enumerate(filepointer):
34                # or a block of comments delimited by /* and */
35                # or (GSAS style) each line can begin with '#'
36                if begin:
37                    if gotCcomment and S.find('*/') > -1:
38                        self.comments.append(S[:-1])
39                        begin = False
40                        continue
41                    if S.strip().startswith('/*'):
42                        self.comments.append(S[:-1])
43                        gotCcomment = True
44                        continue   
45                    if S[0] == '#':
46                        self.comments.append(S[:-1])
47                        continue       #ignore comments, if any
48                    else:
49                        begin = False
50                # valid line to read
51                vals = S.split()
52                try:
53                    x.append(float(vals[0]))
54                    f = float(vals[1])
55                    if f <= 0.0:
56                        y.append(0.0)
57                        w.append(1.0)
58                    elif len(vals) == 3:
59                        y.append(float(vals[1]))
60                        w.append(1.0/float(vals[2])**2)
61                    else:
62                        y.append(float(vals[1]))
63                        w.append(1.0/float(vals[1]))
64                except ValueError:
65                    msg = 'Error in line '+str(i+1)
66                    print msg
67                    break
68            N = len(x)
69            self.powderdata = [
70                np.array(x), # x-axis values
71                np.array(y), # powder pattern intensities
72                np.array(w), # 1/sig(intensity)^2 values (weights)
73                np.zeros(N), # calc. intensities (zero)
74                np.zeros(N), # calc. background (zero)
75                np.zeros(N), # obs-calc profiles
76                ]
77            self.powderentry[0] = filename
78            #self.powderentry[1] = pos # position
79            self.powderentry[2] = 1 # xye file only has one bank
80            self.idstring = ospath.basename(filename)
81            # scan comments for temperature
82            Temperature = 300
83            for S in self.comments:
84                if 'Temp' in S.split('=')[0]:
85                    try:
86                        Temperature = float(S.split('=')[1])
87                    except:
88                        pass
89            self.Sample['Temperature'] = Temperature
90
91            return True
92        except Exception as detail:
93            print self.formatName+' read error:'+str(detail) # for testing
94            import traceback
95            traceback.print_exc(file=sys.stdout)
96            return False
Note: See TracBrowser for help on using the repository browser.