source: trunk/imports/G2pwd_FP.py @ 2465

Last change on this file since 2465 was 2465, checked in by toby, 7 years ago

implement 3 dimensional HDF images

File size: 6.5 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 GSASIIIO as G2IO
21import GSASIIpath
22GSASIIpath.SetVersionNumber("$Revision: 1620 $")
23class xye_ReaderClass(G2IO.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
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 FullProf file'
36        gotCcomment = False
37        begin = True
38        steps = False
39        self.GSAS = False
40        for i,S in enumerate(filepointer):
41            if i > 1000: break
42            if begin:
43                if gotCcomment and S.find('*/') > -1:
44                    begin = False
45                    continue
46                if S.strip().startswith('/*'):
47                    gotCcomment = True
48                    continue   
49                if S.lstrip()[0] in ["'",'#','!',]:
50                    continue       #ignore comments, if any
51                else:
52                    begin = False
53                # valid line to read?
54            vals = S.split()
55            try:    #look for start,step,stop card
56                for j,val in enumerate(vals):
57                    num = float(val)
58                    if j == 2:
59                        break
60            except ValueError:
61                self.errors = 'Unexpected information in line: '+str(i+1)
62                if all([ord(c) < 128 and ord(c) != 0 for c in str(S)]): # show only if ASCII
63                    self.errors += '  '+str(S)
64                else: 
65                    self.errors += '  (binary)'
66                return False
67        return True # no errors encountered
68
69    def Reader(self,filename,filepointer, ParentFrame=None, **unused):
70        'Read a FullProf file'
71        x = []
72        y = []
73        w = []
74        try:
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                    print msg
129                    print S
130                    break
131                except:
132                    msg = 'Error in line '+str(i+1)
133                    print msg
134                    print S
135                    break
136            N = len(x)
137            if N <= 1: return False
138            self.powderdata = [
139                np.array(x), # x-axis values
140                np.array(y), # powder pattern intensities
141                np.array(w), # 1/sig(intensity)^2 values (weights)
142                np.zeros(N), # calc. intensities (zero)
143                np.zeros(N), # calc. background (zero)
144                np.zeros(N), # obs-calc profiles
145                ]
146            self.powderentry[0] = filename
147            #self.powderentry[1] = pos # bank offset (N/A here)
148            self.powderentry[2] = 1 # xye file only has one bank
149            self.idstring = ospath.basename(filename)
150            # scan comments for temperature
151            Temperature = 300
152            for S in self.comments:
153                if 'Temp' in S.split('=')[0]:
154                    try:
155                        Temperature = float(S.split('=')[1])
156                    except:
157                        pass
158            self.Sample['Temperature'] = Temperature
159
160            return True
161        except Exception as detail:
162            self.errors += '\n  '+str(detail)
163            print self.formatName+' read error:'+str(detail) # for testing
164            import traceback
165            traceback.print_exc(file=sys.stdout)
166            return False
167
Note: See TracBrowser for help on using the repository browser.