source: trunk/imports/G2pwd_rigaku.py @ 2516

Last change on this file since 2516 was 2516, checked in by toby, 5 years ago

revise import to not assume Bank 1 with multibank instparm files; deal with unicode problem in CIF files; improve atoms use of selection from menu; add Pawley menu variable selection (plenty more to do)

  • 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 ###################
9import sys
10import os
11import numpy as np
12import GSASIIIO as G2IO
13import GSASIIpath
14GSASIIpath.SetVersionNumber("$Revision: $")
15class Rigaku_ReaderClass(G2IO.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.vals = None
32        self.stepsize = None
33        self.skip = 0
34
35    # Validate the contents -- make sure we only have valid lines and set
36    # values we will need for later read.
37    def ContentsValidator(self, filepointer):
38        self.vals = None
39        self.stepsize = None
40        j = 0
41        prevAngle = None
42        header = True
43        self.skip = -1
44        for i,line in enumerate(filepointer):
45            sline = line.split()
46            vals = len(sline)
47            if header:
48                self.skip += 1
49                if not line.strip(): continue # ignore blank lines
50                err = False
51                for item in sline:
52                    try:
53                        float(item)
54                    except:
55                        err = True
56                        break
57                if err: continue
58                if vals < 1: continue
59                header = False # found first non-header line
60            if vals < 2:
61                print('Too few values for Rigaku .txt file')
62                return False
63            if self.vals is None:
64                self.vals = vals
65            elif self.vals != vals:
66                print('Inconsistent numbers values for Rigaku .txt file on line '+
67                      str(i+1))
68                return False
69            else:
70                j += 1
71            try: 
72                angle = float(sline[0])
73            except:
74                print('Unable to read angle on line '+str(i+1))
75                return False
76            if prevAngle is None:
77                prevAngle = angle
78                continue
79            stepsize = (angle-prevAngle)/(vals-1)
80            prevAngle = angle
81            if self.stepsize is None:
82                self.stepsize = stepsize
83            elif abs(self.stepsize - stepsize) > max(abs(stepsize),abs(self.stepsize))/10000. :
84                print('Inconsistent step size for Rigaku .txt file on line '+
85                        str(i+1) + ' here '+ repr(stepsize) + ' prev '+ repr(self.stepsize))
86                return False
87            if j > 30: return True
88        return False
89           
90    def Reader(self,filename,filepointer, ParentFrame=None, **kwarg):
91        'Read a Rigaku .txt file'
92        x = []
93        y = []
94        w = []
95        for i,line in enumerate(filepointer):
96            if i < self.skip: continue
97            sline = line.split()
98            try: 
99                angle = float(sline[0])
100            except:
101                print('Unable to read angle on line '+str(i+1))
102                self.errors = 'Error reading line: '+str(i+1)
103                return False
104            for j in sline[1:]:
105                x.append(angle)
106                angle += self.stepsize
107                try: 
108                    y.append(float(j))
109                except:
110                    print('Unable to read intensity on line '+str(i+1))
111                    self.errors = 'Error reading line: '+str(i+1)
112                    return False
113                w.append(1.0/max(1.,float(j)))
114        N = len(x)
115        self.powderdata = [
116            np.array(x), # x-axis values
117            np.array(y), # powder pattern intensities
118            np.array(w), # 1/sig(intensity)^2 values (weights)
119            np.zeros(N), # calc. intensities (zero)
120            np.zeros(N), # calc. background (zero)
121            np.zeros(N), # obs-calc profiles
122            ]
123        self.powderentry[0] = filename
124        #self.powderentry[2] = 1 # xye file only has one bank
125        self.idstring = os.path.basename(filename)
126        return True
Note: See TracBrowser for help on using the repository browser.