source: trunk/imports/G2pdf_gr.py @ 4425

Last change on this file since 4425 was 4425, checked in by vondreele, 4 years ago

fix generation of fullrmc pdb file for structures with partial substitutions & vacancies.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 7.6 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2020-05-17 21:31:21 +0000 (Sun, 17 May 2020) $
4# $Author: vondreele $
5# $Revision: 4425 $
6# $URL: trunk/imports/G2pdf_gr.py $
7# $Id: G2pdf_gr.py 4425 2020-05-17 21:31:21Z 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: 4425 $")
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 '#' 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 len(S) == 1:     #skip blank line
69                    continue
70                if len(S.split()) != 2:
71                    continue
72                self.comments.append(S[:-1])
73                if '#' in S[:2]:
74                    ifData = True
75            else:
76                vals = S.split()
77                if len(vals) >= 2:
78                    try:
79                        data = [float(val) for val in vals]
80                        x.append(float(data[0]))
81                        y.append(float(data[1]))
82                    except ValueError:
83                        msg = 'Error in line '+str(i+1)
84                        print (msg)
85                        continue
86        self.pdfdata = np.array([
87            np.array(x), # x-axis values q
88            np.array(y), # pdf f(q))
89            ])
90        self.pdfentry[0] = filename
91        self.pdfentry[2] = 1 # xy file only has one bank
92        self.idstring = ospath.basename(filename)
93
94        return True
95
96class txt_PDFReaderClass(G2obj.ImportPDFData):
97    'Routines to import PDF G(R) data from a .gr file'
98    def __init__(self):
99        super(self.__class__,self).__init__( # fancy way to self-reference
100            extensionlist=('.gr',),
101            strictExtension=False,
102            formatName = 'r (A) step G(r) data',
103            longFormatName = 'r (A) stepped G(r) PDF data from pdfGet or GSAS-II'
104            )
105
106    # Validate the contents -- make sure we only have valid lines
107    def ContentsValidator(self, filename):
108        'Look through the file for expected types of lines in a valid r-step file'
109        filepointer = open(filename,'r')
110        Ndata = 0
111        for i,S in enumerate(filepointer):
112            if '#' in S[:2]:
113                break
114        for i,S in enumerate(filepointer):           
115            vals = S.split()
116            if len(vals) >= 2:
117                try:
118                    data = [float(val) for val in vals]
119                    Ndata += 1
120                except ValueError:
121                    pass
122        if not Ndata:     
123            self.errors = 'No 2 or more column numeric data found'
124            filepointer.close()
125            return False
126        filepointer.close()
127        return True # no errors encountered
128
129    def Reader(self,filename,ParentFrame=None, **unused):
130        print ('Read a r-step text file')
131        x = []
132        y = []
133        ifData = False
134        filepointer = open(filename,'r')
135        for i,S in enumerate(filepointer):
136            if not ifData:
137                if len(S) == 1:     #skip blank line
138                    continue
139                self.comments.append(S[:-1])
140                if '#' in S[:2]:
141                    ifData = True
142            else:
143                vals = S.split()
144                if len(vals) >= 2:
145                    try:
146                        data = [float(val) for val in vals]
147                        x.append(float(data[0]))
148                        y.append(float(data[1]))
149                    except ValueError:
150                        msg = 'Error in line '+str(i+1)
151                        print (msg)
152                        continue
153        self.pdfdata = np.array([
154            np.array(x), # x-axis values r
155            np.array(y), # pdf g(r)
156            ])
157        self.pdfentry[0] = filename
158        self.pdfentry[2] = 1 # xy file only has one bank
159        self.idstring = ospath.basename(filename)
160
161        return True
162
163class txt_PDFReaderClassG(G2obj.ImportPDFData):
164    'Routines to import PDF G(R) data from a .dat file'
165    def __init__(self):
166        super(self.__class__,self).__init__( # fancy way to self-reference
167            extensionlist=('.dat',),
168            strictExtension=False,
169            formatName = 'gudrun r (A) step G(r) data',
170            longFormatName = 'r (A) stepped G(r) PDF data from gudrun'
171            )
172
173    # Validate the contents -- make sure we only have valid lines
174    def ContentsValidator(self, filename):
175        'Look through the file for expected types of lines in a valid r-step file'
176        filepointer = open(filename,'r')
177        Ndata = 0
178        for i,S in enumerate(filepointer):
179            if i < 2:
180                continue
181            vals = S.split()
182            if len(vals) >= 2:
183                try:
184                    data = [float(val) for val in vals]
185                    Ndata += 1
186                except ValueError:
187                    pass
188        if not Ndata:     
189            self.errors = 'No 2 or more column numeric data found'
190            filepointer.close()
191            return False
192        filepointer.close()
193        return True # no errors encountered
194
195    def Reader(self,filename,ParentFrame=None, **unused):
196        print ('Read a r-step text file')
197        x = []
198        y = []
199        filepointer = open(filename,'r')
200        for i,S in enumerate(filepointer):
201            if i < 2:
202                continue
203            vals = S.split()
204            if len(vals) >= 2:
205                try:
206                    data = [float(val) for val in vals]
207                    x.append(float(data[0]))
208                    y.append(float(data[1]))
209                except ValueError:
210                    msg = 'Error in line '+str(i+1)
211                    print (msg)
212                    continue
213            else:
214                break
215        self.pdfdata = np.array([
216            np.array(x), # x-axis values r
217            np.array(y), # pdf g(r)
218            ])
219        self.pdfentry[0] = filename
220        self.pdfentry[2] = 1 # xy file only has one bank
221        self.idstring = ospath.basename(filename)
222
223        return True
224
Note: See TracBrowser for help on using the repository browser.