source: trunk/imports/G2pdf_gr.py @ 3742

Last change on this file since 3742 was 3136, checked in by vondreele, 8 years ago

make GSAS-II python 3.6 compliant & preserve python 2.7 use;changes:
do from future import division, print_function for all GSAS-II py sources
all menu items revised to be py 2.7/3.6 compliant
all wx.OPEN --> wx.FD_OPEN in file dialogs
all integer divides (typically for image pixel math) made explicit with ; ambiguous ones made floats as appropriate
all print "stuff" --> print (stuff)
all print >> pFile,'stuff' --> pFile.writeCIFtemplate('stuff')
all read file opens made explicit 'r' or 'rb'
all cPickle imports made for py2.7 or 3.6 as cPickle or _pickle; test for '2' platform.version_tuple[0] for py 2.7
define cPickleload to select load(fp) or load(fp,encoding='latin-1') for loading gpx files; provides cross compatibility between py 2.7/3.6 gpx files
make dict.keys() as explicit list(dict.keys()) as needed (NB: possible source of remaining py3.6 bugs)
make zip(a,b) as explicit list(zip(a,b)) as needed (NB: possible source of remaining py3.6 bugs)
select unichr/chr according test for '2' platform.version_tuple[0] for py 2.7 (G2pwdGUI * G2plot) for special characters
select wg.EVT_GRID_CELL_CHANGE (classic) or wg.EVT_GRID_CELL_CHANGED (phoenix) in grid Bind
maxint --> maxsize; used in random number stuff
raise Exception,"stuff" --> raise Exception("stuff")
wx 'classic' sizer.DeleteWindows?() or 'phoenix' sizer.Clear(True)
wx 'classic' SetToolTipString?(text) or 'phoenix' SetToolTip?(wx.ToolTip?(text)); define SetToolTipString?(self,text) to handle the choice in plots
status.SetFields? --> status.SetStatusText?
'classic' AddSimpleTool? or 'phoenix' self.AddTool? for plot toolbar; Bind different as well
define GetItemPydata? as it doesn't exist in wx 'phoenix'
allow python versions 2.7 & 3.6 to run GSAS-II
Bind override commented out - no logging capability (NB: remove all logging code?)
all import ContentsValidator? open filename & test if valid then close; filepointer removed from Reader
binary importers (mostly images) test for 'byte' type & convert as needed to satisfy py 3.6 str/byte rules

File size: 3.1 KB
Line 
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
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: 2738 $")
24
25class 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, filepointer):
37        'Look through the file for expected types of lines in a valid r-step file'
38        Ndata = 0
39        for i,S in enumerate(filepointer):
40            if '#L' in S[:2]:
41                break
42        filepointer.seek(i)
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            return False
54        return True # no errors encountered
55
56    def Reader(self,filename,filepointer, ParentFrame=None, **unused):
57        print ('Read a q-step text file')
58        x = []
59        y = []
60        ifData = False
61        for i,S in enumerate(filepointer):
62            if not ifData:
63                if len(S) == 1:     #skip blank line
64                    continue
65                self.comments.append(S[:-1])
66                if '#L' in S[:2]:
67                    ifData = True
68            else:
69                vals = S.split()
70                if len(vals) >= 2:
71                    try:
72                        data = [float(val) for val in vals]
73                        x.append(float(data[0]))
74                        y.append(float(data[1]))
75                    except ValueError:
76                        msg = 'Error in line '+str(i+1)
77                        print (msg)
78                        continue
79        self.pdfdata = np.array([
80            np.array(x), # x-axis values r
81            np.array(y), # pdf g(r)
82            ])
83        self.pdfentry[0] = filename
84        self.pdfentry[2] = 1 # xy file only has one bank
85        self.idstring = ospath.basename(filename)
86
87        return True
88
Note: See TracBrowser for help on using the repository browser.