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 | # |
---|
11 | import sys |
---|
12 | import os.path as ospath |
---|
13 | import numpy as np |
---|
14 | import GSASIIIO as G2IO |
---|
15 | |
---|
16 | class 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 | # Validate the contents -- make sure we only have valid lines |
---|
27 | def ContentsValidator(self, filepointer): |
---|
28 | #print 'ContentsValidator: '+self.formatName |
---|
29 | gotCcomment = False |
---|
30 | begin = True |
---|
31 | for i,S in enumerate(filepointer): |
---|
32 | if i > 1000: break |
---|
33 | if begin: |
---|
34 | if gotCcomment and S.find('*/') > -1: |
---|
35 | begin = False |
---|
36 | continue |
---|
37 | if S.strip().startswith('/*'): |
---|
38 | gotCcomment = True |
---|
39 | continue |
---|
40 | if S[0] == '#': |
---|
41 | continue #ignore comments, if any |
---|
42 | else: |
---|
43 | begin = False |
---|
44 | # valid line to read? |
---|
45 | vals = S.split() |
---|
46 | if len(vals) == 2 or len(vals) == 3: |
---|
47 | continue |
---|
48 | else: |
---|
49 | print 'ContentsValidator: '+self.formatName |
---|
50 | print 'Unexpected information in line:',i+1 # debug info |
---|
51 | print S |
---|
52 | return False |
---|
53 | return True # no errors encountered |
---|
54 | |
---|
55 | def Reader(self,filename,filepointer, ParentFrame=None, **unused): |
---|
56 | x = [] |
---|
57 | y = [] |
---|
58 | w = [] |
---|
59 | try: |
---|
60 | gotCcomment = False |
---|
61 | begin = True |
---|
62 | for i,S in enumerate(filepointer): |
---|
63 | # or a block of comments delimited by /* and */ |
---|
64 | # or (GSAS style) each line can begin with '#' |
---|
65 | if begin: |
---|
66 | if gotCcomment and S.find('*/') > -1: |
---|
67 | self.comments.append(S[:-1]) |
---|
68 | begin = False |
---|
69 | continue |
---|
70 | if S.strip().startswith('/*'): |
---|
71 | self.comments.append(S[:-1]) |
---|
72 | gotCcomment = True |
---|
73 | continue |
---|
74 | if S[0] == '#': |
---|
75 | self.comments.append(S[:-1]) |
---|
76 | continue #ignore comments, if any |
---|
77 | else: |
---|
78 | begin = False |
---|
79 | # valid line to read |
---|
80 | vals = S.split() |
---|
81 | try: |
---|
82 | x.append(float(vals[0])) |
---|
83 | f = float(vals[1]) |
---|
84 | if f <= 0.0: |
---|
85 | y.append(0.0) |
---|
86 | w.append(1.0) |
---|
87 | elif len(vals) == 3: |
---|
88 | y.append(float(vals[1])) |
---|
89 | w.append(1.0/float(vals[2])**2) |
---|
90 | else: |
---|
91 | y.append(float(vals[1])) |
---|
92 | w.append(1.0/float(vals[1])) |
---|
93 | except ValueError: |
---|
94 | msg = 'Error in line '+str(i+1) |
---|
95 | print msg |
---|
96 | break |
---|
97 | N = len(x) |
---|
98 | self.powderdata = [ |
---|
99 | np.array(x), # x-axis values |
---|
100 | np.array(y), # powder pattern intensities |
---|
101 | np.array(w), # 1/sig(intensity)^2 values (weights) |
---|
102 | np.zeros(N), # calc. intensities (zero) |
---|
103 | np.zeros(N), # calc. background (zero) |
---|
104 | np.zeros(N), # obs-calc profiles |
---|
105 | ] |
---|
106 | self.powderentry[0] = filename |
---|
107 | #self.powderentry[1] = pos # bank offset (N/A here) |
---|
108 | self.powderentry[2] = 1 # xye file only has one bank |
---|
109 | self.idstring = ospath.basename(filename) |
---|
110 | # scan comments for temperature |
---|
111 | Temperature = 300 |
---|
112 | for S in self.comments: |
---|
113 | if 'Temp' in S.split('=')[0]: |
---|
114 | try: |
---|
115 | Temperature = float(S.split('=')[1]) |
---|
116 | except: |
---|
117 | pass |
---|
118 | self.Sample['Temperature'] = Temperature |
---|
119 | |
---|
120 | return True |
---|
121 | except Exception as detail: |
---|
122 | print self.formatName+' read error:'+str(detail) # for testing |
---|
123 | import traceback |
---|
124 | traceback.print_exc(file=sys.stdout) |
---|
125 | return False |
---|