1 | # -*- coding: utf-8 -*- |
---|
2 | ########### SVN repository information ################### |
---|
3 | # $Date: 2016-09-08 16:17:54 +0000 (Thu, 08 Sep 2016) $ |
---|
4 | # $Author: toby $ |
---|
5 | # $Revision: 2463 $ |
---|
6 | # $URL: trunk/imports/G2pwd_xye.py $ |
---|
7 | # $Id: G2pwd_xye.py 2463 2016-09-08 16:17:54Z toby $ |
---|
8 | ########### SVN repository information ################### |
---|
9 | ''' |
---|
10 | *Module G2pwd_xye: Topas .xye data* |
---|
11 | ------------------------------------ |
---|
12 | |
---|
13 | Routine to read in powder data from a Topas-compatible .xye file |
---|
14 | |
---|
15 | ''' |
---|
16 | |
---|
17 | import sys |
---|
18 | import os.path as ospath |
---|
19 | import numpy as np |
---|
20 | import GSASIIIO as G2IO |
---|
21 | import GSASIIpath |
---|
22 | GSASIIpath.SetVersionNumber("$Revision: 2463 $") |
---|
23 | class xye_ReaderClass(G2IO.ImportPowderData): |
---|
24 | 'Routines to import powder data from a .xye file' |
---|
25 | def __init__(self): |
---|
26 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
27 | extensionlist=('.xye',), |
---|
28 | strictExtension=False, |
---|
29 | formatName = 'Topas xye', |
---|
30 | longFormatName = 'Topas .xye powder data file' |
---|
31 | ) |
---|
32 | |
---|
33 | # Validate the contents -- make sure we only have valid lines |
---|
34 | def ContentsValidator(self, filepointer): |
---|
35 | 'Look through the file for expected types of lines in a valid Topas file' |
---|
36 | gotCcomment = False |
---|
37 | begin = True |
---|
38 | self.GSAS = False |
---|
39 | for i,S in enumerate(filepointer): |
---|
40 | if i > 1000: break |
---|
41 | if begin: |
---|
42 | if gotCcomment and S.find('*/') > -1: |
---|
43 | begin = False |
---|
44 | continue |
---|
45 | if S.strip().startswith('/*'): |
---|
46 | gotCcomment = True |
---|
47 | continue |
---|
48 | if S[0] in ["'",'#']: |
---|
49 | continue #ignore comments, if any |
---|
50 | else: |
---|
51 | begin = False |
---|
52 | # valid line to read? |
---|
53 | #vals = S.split() |
---|
54 | vals = S.replace(',',' ').replace(';',' ').split() |
---|
55 | if len(vals) == 2 or len(vals) == 3: |
---|
56 | continue |
---|
57 | else: |
---|
58 | self.errors = 'Unexpected information in line: '+str(i+1) |
---|
59 | if all([ord(c) < 128 and ord(c) != 0 for c in str(S)]): # show only if ASCII |
---|
60 | self.errors += ' '+str(S) |
---|
61 | else: |
---|
62 | self.errors += ' (binary)' |
---|
63 | return False |
---|
64 | return True # no errors encountered |
---|
65 | |
---|
66 | def Reader(self,filename,filepointer, ParentFrame=None, **unused): |
---|
67 | 'Read a Topas file' |
---|
68 | x = [] |
---|
69 | y = [] |
---|
70 | w = [] |
---|
71 | try: |
---|
72 | gotCcomment = False |
---|
73 | begin = True |
---|
74 | for i,S in enumerate(filepointer): |
---|
75 | self.errors = 'Error reading line: '+str(i+1) |
---|
76 | # or a block of comments delimited by /* and */ |
---|
77 | # or (GSAS style) each line can begin with '#' |
---|
78 | if begin: |
---|
79 | if gotCcomment and S.find('*/') > -1: |
---|
80 | self.comments.append(S[:-1]) |
---|
81 | begin = False |
---|
82 | continue |
---|
83 | if S.strip().startswith('/*'): |
---|
84 | self.comments.append(S[:-1]) |
---|
85 | gotCcomment = True |
---|
86 | continue |
---|
87 | if S[0] in ["'",'#']: |
---|
88 | self.comments.append(S[:-1]) |
---|
89 | continue #ignore comments, if any |
---|
90 | else: |
---|
91 | begin = False |
---|
92 | # valid line to read |
---|
93 | #vals = S.split() |
---|
94 | vals = S.replace(',',' ').replace(';',' ').split() |
---|
95 | if len(vals) < 2: |
---|
96 | print 'Line '+str(i+1)+' cannot be read:\n\t'+S |
---|
97 | continue |
---|
98 | try: |
---|
99 | x.append(float(vals[0])) |
---|
100 | f = float(vals[1]) |
---|
101 | if f <= 0.0: |
---|
102 | y.append(0.0) |
---|
103 | w.append(0.0) |
---|
104 | elif len(vals) == 3: |
---|
105 | y.append(float(vals[1])) |
---|
106 | w.append(1.0/float(vals[2])**2) |
---|
107 | else: |
---|
108 | y.append(float(vals[1])) |
---|
109 | w.append(1.0/float(vals[1])) |
---|
110 | except ValueError: |
---|
111 | msg = 'Error parsing number in line '+str(i+1) |
---|
112 | print msg |
---|
113 | print S |
---|
114 | break |
---|
115 | except: |
---|
116 | msg = 'Error in line '+str(i+1) |
---|
117 | print msg |
---|
118 | print S |
---|
119 | break |
---|
120 | N = len(x) |
---|
121 | self.powderdata = [ |
---|
122 | np.array(x), # x-axis values |
---|
123 | np.array(y), # powder pattern intensities |
---|
124 | np.array(w), # 1/sig(intensity)^2 values (weights) |
---|
125 | np.zeros(N), # calc. intensities (zero) |
---|
126 | np.zeros(N), # calc. background (zero) |
---|
127 | np.zeros(N), # obs-calc profiles |
---|
128 | ] |
---|
129 | self.powderentry[0] = filename |
---|
130 | #self.powderentry[1] = pos # bank offset (N/A here) |
---|
131 | self.powderentry[2] = 1 # xye file only has one bank |
---|
132 | self.idstring = ospath.basename(filename) |
---|
133 | # scan comments for temperature |
---|
134 | Temperature = 300 |
---|
135 | for S in self.comments: |
---|
136 | if 'Temp' in S.split('=')[0]: |
---|
137 | try: |
---|
138 | Temperature = float(S.split('=')[1]) |
---|
139 | except: |
---|
140 | pass |
---|
141 | self.Sample['Temperature'] = Temperature |
---|
142 | |
---|
143 | return True |
---|
144 | except Exception as detail: |
---|
145 | self.errors += '\n '+str(detail) |
---|
146 | print self.formatName+' read error:'+str(detail) # for testing |
---|
147 | import traceback |
---|
148 | traceback.print_exc(file=sys.stdout) |
---|
149 | return False |
---|
150 | |
---|