source: trunk/imports/G2pdf_gr.py @ 5064

Last change on this file since 5064 was 5064, checked in by vondreele, 2 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 7.8 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2021-11-03 13:56:03 +0000 (Wed, 03 Nov 2021) $
4# $Author: vondreele $
5# $Revision: 5064 $
6# $URL: trunk/imports/G2pdf_gr.py $
7# $Id: G2pdf_gr.py 5064 2021-11-03 13:56:03Z vondreele $
8########### SVN repository information ###################
9'''
10*Module G2pdf_gr: read PDF G(R) data*
11------------------------------------------------
12
13Routines to read in G(R) data from an .gr type file, with
14Angstrom steps.
15
16'''
17
18from __future__ import division, print_function
19import os.path as ospath
20import numpy as np
21import GSASIIobj as G2obj
22import GSASIIpath
23GSASIIpath.SetVersionNumber("$Revision: 5064 $")
24
25class txt_FSQReaderClass(G2obj.ImportPDFData):
26    'Routines to import S(Q) data from a .fq file'
27    def __init__(self):
28        super(self.__class__,self).__init__( # fancy way to self-reference
29            extensionlist=('.fq','.sq'),
30            strictExtension=False,
31            formatName = 'q (A-1) step S(Q) data',
32            longFormatName = 'q (A-1) stepped S(Q) PDF data from pdfGet or GSAS-II'
33            )
34
35    # Validate the contents -- make sure we only have valid lines
36    def ContentsValidator(self, filename):
37        'Look through the file for expected types of lines in a valid r-step file'
38        filepointer = open(filename,'r')
39        Ndata = 0
40        for i,S in enumerate(filepointer):
41            if '#L' in S[:2]:
42                break
43            if len(S.split()) != 2:
44                break
45        for i,S in enumerate(filepointer):           
46            vals = S.split()
47            if len(vals) >= 2:
48                try:
49                    data = [float(val) for val in vals]
50                    Ndata += 1
51                except ValueError:
52                    pass
53        if not Ndata:     
54            self.errors = 'No 2 or more column numeric data found'
55            filepointer.close()
56            return False
57        filepointer.close()
58        return True # no errors encountered
59
60    def Reader(self,filename,ParentFrame=None, **unused):
61        print ('Read a q-step text file')
62        x = []
63        y = []
64        ifData = False
65        filepointer = open(filename,'r')
66        for i,S in enumerate(filepointer):
67            if not ifData:
68                if '#L' in S[:2]:
69                    ifData = True
70                    continue
71                if len(S) == 1:     #skip blank line
72                    continue
73                if len(S.split()) != 2:
74                    continue
75                self.comments.append(S[:-1])
76            else:
77                vals = S.split()
78                if len(vals) >= 2:
79                    try:
80                        data = [float(val) for val in vals]
81                        x.append(float(data[0]))
82                        y.append(float(data[1]))
83                    except ValueError:
84                        msg = 'Error in line '+str(i+1)
85                        print (msg,S)
86                        continue
87        self.Type = 'X f(q)'             #assume X-ray PDF
88        self.pdfdata = np.array([
89            np.array(x), # x-axis values q
90            np.array(y), # pdf f(q))
91            ])
92        self.pdfentry[0] = filename
93        self.pdfentry[2] = 1 # xy file only has one bank
94        self.idstring = ospath.basename(filename)
95
96        return True
97
98class txt_PDFReaderClass(G2obj.ImportPDFData):
99    'Routines to import PDF G(R) data from a .gr file'
100    def __init__(self):
101        super(self.__class__,self).__init__( # fancy way to self-reference
102            extensionlist=('.gr',),
103            strictExtension=False,
104            formatName = 'r (A) step G(r) data',
105            longFormatName = 'r (A) stepped G(r) PDF data from pdfGet or GSAS-II'
106            )
107
108    # Validate the contents -- make sure we only have valid lines
109    def ContentsValidator(self, filename):
110        'Look through the file for expected types of lines in a valid r-step file'
111        filepointer = open(filename,'r')
112        Ndata = 0
113        for i,S in enumerate(filepointer):
114            if '#L r' in S[:4]:
115                break
116        for i,S in enumerate(filepointer):           
117            vals = S.split()
118            if len(vals) >= 2:
119                try:
120                    data = [float(val) for val in vals]
121                    Ndata += 1
122                except ValueError:
123                    pass
124        if not Ndata:     
125            self.errors = 'No 2 or more column numeric data found'
126            filepointer.close()
127            return False
128        filepointer.close()
129        return True # no errors encountered
130
131    def Reader(self,filename,ParentFrame=None, **unused):
132        print ('Read a r-step text file')
133        x = []
134        y = []
135        ifData = False
136        filepointer = open(filename,'r')
137        self.Type = 'N g(r)'
138        for i,S in enumerate(filepointer):
139            if not ifData:
140                if len(S) == 1:     #skip blank line
141                    continue
142                if '#L r' in S[:4]:
143                    ifData = True
144                if 'X-Ray' in S:
145                    self.Type = 'X g(r)'
146                self.comments.append(S[:-1])
147            else:
148                vals = S.split()
149                if len(vals) >= 2:
150                    try:
151                        data = [float(val) for val in vals]
152                        x.append(float(data[0]))
153                        y.append(float(data[1]))
154                    except ValueError:
155                        msg = 'Error in line '+str(i+1)
156                        print (msg,S[:-1])
157                        continue
158        self.pdfdata = np.array([
159            np.array(x), # x-axis values r
160            np.array(y), # pdf g(r)
161            ])
162        self.pdfentry[0] = filename
163        self.pdfentry[2] = 1 # xy file only has one bank
164        self.idstring = ospath.basename(filename)
165
166        return True
167
168class txt_PDFReaderClassG(G2obj.ImportPDFData):
169    'Routines to import PDF G(R) data from a .dat file'
170    def __init__(self):
171        super(self.__class__,self).__init__( # fancy way to self-reference
172            extensionlist=('.dat',),
173            strictExtension=False,
174            formatName = 'gudrun r (A) step G(r) data',
175            longFormatName = 'r (A) stepped G(r) PDF data from gudrun'
176            )
177
178    # Validate the contents -- make sure we only have valid lines
179    def ContentsValidator(self, filename):
180        'Look through the file for expected types of lines in a valid r-step file'
181        filepointer = open(filename,'r')
182        Ndata = 0
183        for i,S in enumerate(filepointer):
184            if i < 2:
185                continue
186            vals = S.split()
187            if len(vals) >= 2:
188                try:
189                    data = [float(val) for val in vals]
190                    Ndata += 1
191                except ValueError:
192                    pass
193        if not Ndata:     
194            self.errors = 'No 2 or more column numeric data found'
195            filepointer.close()
196            return False
197        filepointer.close()
198        return True # no errors encountered
199
200    def Reader(self,filename,ParentFrame=None, **unused):
201        print ('Read a r-step text file')
202        x = []
203        y = []
204        filepointer = open(filename,'r')
205        for i,S in enumerate(filepointer):
206            if i < 2:
207                continue
208            vals = S.split()
209            if len(vals) >= 2:
210                try:
211                    data = [float(val) for val in vals]
212                    x.append(float(data[0]))
213                    y.append(float(data[1]))
214                except ValueError:
215                    msg = 'Error in line '+str(i+1)
216                    print (msg)
217                    continue
218            else:
219                break
220        self.pdfdata = np.array([
221            np.array(x), # x-axis values r
222            np.array(y), # pdf g(r)
223            ])
224        self.Type = 'X g(r)'         #assume X-ray PDF
225        self.pdfentry[0] = filename
226        self.pdfentry[2] = 1 # xy file only has one bank
227        self.idstring = ospath.basename(filename)
228
229        return True
230
Note: See TracBrowser for help on using the repository browser.