source: trunk/imports/G2pwd_rigaku.py @ 2452

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

read Rigaku 2 column files

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