source: trunk/imports/G2pwd_FP.py @ 3098

Last change on this file since 3098 was 3098, checked in by toby, 6 years ago

fix powder data import errors with G2scriptable

File size: 6.1 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2014-12-27 11:14:59 -0600 (Sat, 27 Dec 2014) $
4# $Author: vondreele $
5# $Revision: 1620 $
6# $URL: https://subversion.xray.aps.anl.gov/pyGSAS/trunk/imports/G2pwd_xye.py $
7# $Id: G2pwd_xye.py 1620 2014-12-27 17:14:59Z vondreele $
8########### SVN repository information ###################
9'''
10*Module G2pwd_FP: FullProf .dat data*
11-------------------------------------
12
13Routine to read in powder data from a FullProf .dat file
14
15'''
16
17import sys
18import os.path as ospath
19import numpy as np
20import GSASIIobj as G2obj
21import GSASIIpath
22GSASIIpath.SetVersionNumber("$Revision: 1620 $")
23class xye_ReaderClass(G2obj.ImportPowderData):
24    'Routines to import powder data from a FullProf 1-10 column .dat file'
25    def __init__(self):
26        super(self.__class__,self).__init__( # fancy way to self-reference
27            extensionlist=('.dat',),
28            strictExtension=False,
29            formatName = 'FullProf .dat',
30            longFormatName = 'FullProf 1-10 column .dat powder data file'
31            )
32        self.scriptable = True
33
34    # Validate the contents -- make sure we only have valid lines
35    def ContentsValidator(self, filepointer):
36        'Look through the file for expected types of lines in a valid FullProf file'
37        gotCcomment = False
38        begin = True
39        steps = False
40        self.GSAS = False
41        for i,S in enumerate(filepointer):
42            if i > 1000: break
43            if begin:
44                if gotCcomment and S.find('*/') > -1:
45                    begin = False
46                    continue
47                if S.strip().startswith('/*'):
48                    gotCcomment = True
49                    continue   
50                if S.lstrip()[0] in ["'",'#','!',]:
51                    continue       #ignore comments, if any
52                else:
53                    begin = False
54                # valid line to read?
55            vals = S.split()
56            try:    #look for start,step,stop card
57                for j,val in enumerate(vals):
58                    num = float(val)
59                    if j == 2:
60                        break
61            except ValueError:
62                self.errors = 'Unexpected information in line: '+str(i+1)
63                if all([ord(c) < 128 and ord(c) != 0 for c in str(S)]): # show only if ASCII
64                    self.errors += '  '+str(S)
65                else: 
66                    self.errors += '  (binary)'
67                return False
68        return True # no errors encountered
69
70    def Reader(self,filename,filepointer, ParentFrame=None, **unused):
71        'Read a FullProf file'
72        x = []
73        y = []
74        w = []
75        gotCcomment = False
76        begin = True
77        steps = False
78        Stop = False
79        N = 0
80        for i,S in enumerate(filepointer):
81            self.errors = 'Error reading line: '+str(i+1)
82            # or a block of comments delimited by /* and */
83            # or (GSAS style) each line can begin with '#' or '!'
84            if begin:
85                if gotCcomment and S.find('*/') > -1:
86                    self.comments.append(S[:-1])
87                    begin = False
88                    continue
89                if S.strip().startswith('/*'):
90                    self.comments.append(S[:-1])
91                    gotCcomment = True
92                    continue   
93                if S.lstrip()[0] in ["'",'#','!',]:
94                    self.comments.append(S[:-1])
95                    continue       #ignore comments, if any
96                else:
97                    begin = False
98            # valid line to read
99            if not steps:
100                vals = S.split(None,4)
101                if len(vals) >= 3:
102                    steps = True
103                    start = float(vals[0])
104                    step = float(vals[1])
105                    stop = float(vals[2])
106                    if len(vals) > 3:
107                        self.comments.append(vals[3][:-1])
108                    continue
109            vals = S.split()    #data strings
110            try:
111                for j in range(len(vals)):
112                    x.append(start+N*step)
113                    f = float(vals[j])
114                    if f <= 0.0:
115                        y.append(0.0)
116                        w.append(0.0)
117                    else:
118                        y.append(float(vals[j]))
119                        w.append(1.0/float(vals[j]))
120                    if x[-1] >= stop:
121                        Stop = True
122                        break
123                    N += 1
124                if Stop:
125                    break
126            except ValueError:
127                msg = 'Error parsing number in line '+str(i+1)
128                if GSASIIpath.GetConfigValue('debug'):
129                    print msg
130                    print S.strip()
131                break
132            except:
133                msg = 'Error in line '+str(i+1)
134                if GSASIIpath.GetConfigValue('debug'):
135                    print msg
136                    print S.strip()
137                break
138        N = len(x)
139        if N <= 1: return False
140        self.powderdata = [
141            np.array(x), # x-axis values
142            np.array(y), # powder pattern intensities
143            np.array(w), # 1/sig(intensity)^2 values (weights)
144            np.zeros(N), # calc. intensities (zero)
145            np.zeros(N), # calc. background (zero)
146            np.zeros(N), # obs-calc profiles
147            ]
148        self.powderentry[0] = filename
149        #self.powderentry[1] = pos # bank offset (N/A here)
150        #self.powderentry[2] = 1 # xye file only has one bank
151        self.idstring = ospath.basename(filename)
152        # scan comments for temperature
153        Temperature = 300
154        for S in self.comments:
155            if 'Temp' in S.split('=')[0]:
156                try:
157                    Temperature = float(S.split('=')[1])
158                except:
159                    pass
160        self.Sample['Temperature'] = Temperature
161
162        return True
163
Note: See TracBrowser for help on using the repository browser.