1 | # -*- coding: utf-8 -*- |
---|
2 | ########### SVN repository information ################### |
---|
3 | # $Date: 2017-03-04 08:34:05 -0600 (Sat, 04 Mar 2017) $ |
---|
4 | # $Author: vondreele $ |
---|
5 | # $Revision: 2738 $ |
---|
6 | # $URL: https://subversion.xray.aps.anl.gov/pyGSAS/trunk/imports/G2sad_xye.py $ |
---|
7 | # $Id: G2sad_xye.py 2738 2017-03-04 14:34:05Z vondreele $ |
---|
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: 2738 $") |
---|
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 q-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 | |
---|