source: trunk/imports/G2pwd_rigaku.py @ 2835

Last change on this file since 2835 was 2835, checked in by vondreele, 5 years ago

add self.scriptable=False to ImportBaseClass?
set it True in various PWDR readers
when selecting points for image calibration, shift key forces selection at cursor
Status bar text changes to reflect this

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: $
4# $Author: toby $
5# $Revision: $
6# $URL: $
7# $Id: $
8########### SVN repository information ###################
9
10import os
11import numpy as np
12import GSASIIobj as G2obj
13import GSASIIpath
14GSASIIpath.SetVersionNumber("$Revision: $")
15class Rigaku_ReaderClass(G2obj.ImportPowderData):
16    '''Routines to import powder data from a Rigaku .txt file with an angle and
17    then 1 or 11(!) intensity values on the line. The example file is proceeded
18    with 10 of blank lines, but I have assumed they could be any sort of text.
19    This code should work with an angle and any number of intensity values/line
20    as long as the number is the same on each line. The step size may not change. The
21    number of comment lines can also change, but should not appear to be intensity
22    values (numbers only).
23    '''
24    def __init__(self):
25        super(self.__class__,self).__init__( # fancy way to self-reference
26            extensionlist=('.txt','.TXT'),
27            strictExtension=True,
28            formatName = 'Rigaku .txt exported',
29            longFormatName = 'Rigaku powder data exported as .txt'
30            )
31        self.scriptable = True
32        self.vals = None
33        self.stepsize = None
34        self.skip = 0
35
36    # Validate the contents -- make sure we only have valid lines and set
37    # values we will need for later read.
38    def ContentsValidator(self, filepointer):
39        self.vals = None
40        self.stepsize = None
41        j = 0
42        prevAngle = None
43        header = True
44        self.skip = -1
45        for i,line in enumerate(filepointer):
46            sline = line.split()
47            vals = len(sline)
48            if header:
49                self.skip += 1
50                if not line.strip(): continue # ignore blank lines
51                err = False
52                for item in sline:
53                    try:
54                        float(item)
55                    except:
56                        err = True
57                        break
58                if err: continue
59                if vals < 1: continue
60                header = False # found first non-header line
61            if vals < 2:
62                print('Too few values for Rigaku .txt file')
63                return False
64            if self.vals is None:
65                self.vals = vals
66            elif self.vals != vals:
67                print('Inconsistent numbers values for Rigaku .txt file on line '+
68                      str(i+1))
69                return False
70            else:
71                j += 1
72            try: 
73                angle = float(sline[0])
74            except:
75                print('Unable to read angle on line '+str(i+1))
76                return False
77            if prevAngle is None:
78                prevAngle = angle
79                continue
80            stepsize = (angle-prevAngle)/(vals-1)
81            prevAngle = angle
82            if self.stepsize is None:
83                self.stepsize = stepsize
84            elif abs(self.stepsize - stepsize) > max(abs(stepsize),abs(self.stepsize))/10000. :
85                print('Inconsistent step size for Rigaku .txt file on line '+
86                        str(i+1) + ' here '+ repr(stepsize) + ' prev '+ repr(self.stepsize))
87                return False
88            if j > 30: return True
89        return False
90           
91    def Reader(self,filename,filepointer, ParentFrame=None, **kwarg):
92        'Read a Rigaku .txt file'
93        x = []
94        y = []
95        w = []
96        for i,line in enumerate(filepointer):
97            if i < self.skip: continue
98            sline = line.split()
99            try: 
100                angle = float(sline[0])
101            except:
102                print('Unable to read angle on line '+str(i+1))
103                self.errors = 'Error reading line: '+str(i+1)
104                return False
105            for j in sline[1:]:
106                x.append(angle)
107                angle += self.stepsize
108                try: 
109                    y.append(float(j))
110                except:
111                    print('Unable to read intensity on line '+str(i+1))
112                    self.errors = 'Error reading line: '+str(i+1)
113                    return False
114                w.append(1.0/max(1.,float(j)))
115        N = len(x)
116        self.powderdata = [
117            np.array(x), # x-axis values
118            np.array(y), # powder pattern intensities
119            np.array(w), # 1/sig(intensity)^2 values (weights)
120            np.zeros(N), # calc. intensities (zero)
121            np.zeros(N), # calc. background (zero)
122            np.zeros(N), # obs-calc profiles
123            ]
124        self.powderentry[0] = filename
125        #self.powderentry[2] = 1 # xye file only has one bank
126        self.idstring = os.path.basename(filename)
127        return True
Note: See TracBrowser for help on using the repository browser.