1 | # -*- coding: utf-8 -*- |
---|
2 | ########### SVN repository information ################### |
---|
3 | # $Date: 2020-03-03 21:01:43 +0000 (Tue, 03 Mar 2020) $ |
---|
4 | # $Author: toby $ |
---|
5 | # $Revision: 4339 $ |
---|
6 | # $URL: trunk/imports/G2pdf_gr.py $ |
---|
7 | # $Id: G2pdf_gr.py 4339 2020-03-03 21:01:43Z toby $ |
---|
8 | ########### SVN repository information ################### |
---|
9 | ''' |
---|
10 | *Module G2pdf_gr: read PDF G(R) data* |
---|
11 | ------------------------------------------------ |
---|
12 | |
---|
13 | Routines to read in G(R) data from an .gr type file, with |
---|
14 | Angstrom steps. |
---|
15 | |
---|
16 | ''' |
---|
17 | |
---|
18 | from __future__ import division, print_function |
---|
19 | import os.path as ospath |
---|
20 | import numpy as np |
---|
21 | import GSASIIobj as G2obj |
---|
22 | import GSASIIpath |
---|
23 | GSASIIpath.SetVersionNumber("$Revision: 4339 $") |
---|
24 | |
---|
25 | class txt_PDFReaderClass(G2obj.ImportPDFData): |
---|
26 | 'Routines to import PDF G(R) data from a .gr file' |
---|
27 | def __init__(self): |
---|
28 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
29 | extensionlist=('.gr',), |
---|
30 | strictExtension=False, |
---|
31 | formatName = 'r (A) step G(r) data', |
---|
32 | longFormatName = 'r (A) stepped G(r) 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 | for i,S in enumerate(filepointer): |
---|
44 | vals = S.split() |
---|
45 | if len(vals) >= 2: |
---|
46 | try: |
---|
47 | data = [float(val) for val in vals] |
---|
48 | Ndata += 1 |
---|
49 | except ValueError: |
---|
50 | pass |
---|
51 | if not Ndata: |
---|
52 | self.errors = 'No 2 or more column numeric data found' |
---|
53 | filepointer.close() |
---|
54 | return False |
---|
55 | filepointer.close() |
---|
56 | return True # no errors encountered |
---|
57 | |
---|
58 | def Reader(self,filename,ParentFrame=None, **unused): |
---|
59 | print ('Read a r-step text file') |
---|
60 | x = [] |
---|
61 | y = [] |
---|
62 | ifData = False |
---|
63 | filepointer = open(filename,'r') |
---|
64 | for i,S in enumerate(filepointer): |
---|
65 | if not ifData: |
---|
66 | if len(S) == 1: #skip blank line |
---|
67 | continue |
---|
68 | self.comments.append(S[:-1]) |
---|
69 | if '#L' in S[:2]: |
---|
70 | ifData = True |
---|
71 | else: |
---|
72 | vals = S.split() |
---|
73 | if len(vals) >= 2: |
---|
74 | try: |
---|
75 | data = [float(val) for val in vals] |
---|
76 | x.append(float(data[0])) |
---|
77 | y.append(float(data[1])) |
---|
78 | except ValueError: |
---|
79 | msg = 'Error in line '+str(i+1) |
---|
80 | print (msg) |
---|
81 | continue |
---|
82 | self.pdfdata = np.array([ |
---|
83 | np.array(x), # x-axis values r |
---|
84 | np.array(y), # pdf g(r) |
---|
85 | ]) |
---|
86 | self.pdfentry[0] = filename |
---|
87 | self.pdfentry[2] = 1 # xy file only has one bank |
---|
88 | self.idstring = ospath.basename(filename) |
---|
89 | |
---|
90 | return True |
---|
91 | |
---|
92 | class txt_PDFReaderClassG(G2obj.ImportPDFData): |
---|
93 | 'Routines to import PDF G(R) data from a .dat file' |
---|
94 | def __init__(self): |
---|
95 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
96 | extensionlist=('.dat',), |
---|
97 | strictExtension=False, |
---|
98 | formatName = 'gudrun r (A) step G(r) data', |
---|
99 | longFormatName = 'r (A) stepped G(r) PDF data from gudrun' |
---|
100 | ) |
---|
101 | |
---|
102 | # Validate the contents -- make sure we only have valid lines |
---|
103 | def ContentsValidator(self, filename): |
---|
104 | 'Look through the file for expected types of lines in a valid r-step file' |
---|
105 | filepointer = open(filename,'r') |
---|
106 | Ndata = 0 |
---|
107 | for i,S in enumerate(filepointer): |
---|
108 | if i < 2: |
---|
109 | continue |
---|
110 | vals = S.split() |
---|
111 | if len(vals) >= 2: |
---|
112 | try: |
---|
113 | data = [float(val) for val in vals] |
---|
114 | Ndata += 1 |
---|
115 | except ValueError: |
---|
116 | pass |
---|
117 | if not Ndata: |
---|
118 | self.errors = 'No 2 or more column numeric data found' |
---|
119 | filepointer.close() |
---|
120 | return False |
---|
121 | filepointer.close() |
---|
122 | return True # no errors encountered |
---|
123 | |
---|
124 | def Reader(self,filename,ParentFrame=None, **unused): |
---|
125 | print ('Read a r-step text file') |
---|
126 | x = [] |
---|
127 | y = [] |
---|
128 | filepointer = open(filename,'r') |
---|
129 | for i,S in enumerate(filepointer): |
---|
130 | if i < 2: |
---|
131 | continue |
---|
132 | vals = S.split() |
---|
133 | if len(vals) >= 2: |
---|
134 | try: |
---|
135 | data = [float(val) for val in vals] |
---|
136 | x.append(float(data[0])) |
---|
137 | y.append(float(data[1])) |
---|
138 | except ValueError: |
---|
139 | msg = 'Error in line '+str(i+1) |
---|
140 | print (msg) |
---|
141 | continue |
---|
142 | else: |
---|
143 | break |
---|
144 | self.pdfdata = np.array([ |
---|
145 | np.array(x), # x-axis values r |
---|
146 | np.array(y), # pdf g(r) |
---|
147 | ]) |
---|
148 | self.pdfentry[0] = filename |
---|
149 | self.pdfentry[2] = 1 # xy file only has one bank |
---|
150 | self.idstring = ospath.basename(filename) |
---|
151 | |
---|
152 | return True |
---|
153 | |
---|