source: trunk/imports/G2phase_GPX.py @ 3146

Last change on this file since 3146 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

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 3.0 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2017-10-23 16:39:16 +0000 (Mon, 23 Oct 2017) $
4# $Author: vondreele $
5# $Revision: 3136 $
6# $URL: trunk/imports/G2phase_GPX.py $
7# $Id: G2phase_GPX.py 3136 2017-10-23 16:39:16Z vondreele $
8########### SVN repository information ###################
9'''
10*Module G2phase_GPX: Import phase from GSAS-II project*
11--------------------------------------------------------
12
13Copies a phase from another GSAS-II project file into the
14current project.
15
16'''
17from __future__ import division, print_function
18import platform
19import sys
20if '2' in platform.python_version_tuple()[0]:
21    import cPickle
22else:
23    import _pickle as cPickle
24import random as ran
25import GSASIIobj as G2obj
26import GSASIIIO as G2IO
27import GSASIIstrIO as G2stIO
28import GSASIIpath
29GSASIIpath.SetVersionNumber("$Revision: 3136 $")
30
31class PhaseReaderClass(G2obj.ImportPhase):
32    'Opens a .GPX file and pulls out a selected phase'
33    def __init__(self):
34        super(self.__class__,self).__init__( # fancy way to say ImportPhase.__init__
35            extensionlist=('.gpx',),
36            strictExtension=True,
37            formatName = 'GSAS-II gpx',
38            longFormatName = 'GSAS-II project (.gpx file) import'
39            )
40       
41    def ContentsValidator(self, filename):
42        "Test if the 1st section can be read as a cPickle block, if not it can't be .GPX!"
43        if True:
44            fp = open(filename,'rb')
45        try: 
46            if '2' in platform.python_version_tuple()[0]:
47                data = cPickle.load(fp)
48            else:
49                data = cPickle.load(fp,encoding='latin-1')
50        except:
51            self.errors = 'This is not a valid .GPX file. Not recognized by cPickle'
52            fp.close()
53            return False
54        fp.close()
55        return True
56
57    def Reader(self,filename, ParentFrame=None, **unused):
58        '''Read a phase from a .GPX file. Does not (yet?) support selecting and reading
59        more than one phase at a time.'''
60        try:
61            phasenames = G2stIO.GetPhaseNames(filename)
62        except:
63            self.errors = 'Reading of phase names failed'
64            return False
65        if not phasenames:
66            self.errors = 'No phases found in '+str(filename)
67            return False            # no blocks with coordinates
68        elif len(phasenames) == 1: # one block, no choices
69            selblk = 0
70        else:                       # choose from options               
71            selblk = G2IO.PhaseSelector(phasenames,ParentFrame=ParentFrame,
72                title= 'Select a phase from the list below',)
73            if selblk is None:
74                self.errors = 'No phase selected'
75                return False # User pressed cancel
76        self.Phase = G2stIO.GetAllPhaseData(filename,phasenames[selblk])
77        self.Phase['Histograms'] = {}       #remove any histograms
78        self.Phase['Pawley ref'] = []       # & any Pawley refl.
79        self.Phase['RBModels'] = {}
80        del self.Phase['MCSA']
81        if 'Map Peaks' in self.Phase:
82            del self.Phase['Map Peaks']
83        del self.Phase['General']['Map']
84        self.Phase['ranId'] = ran.randint(0,sys.maxsize)
85        return True
Note: See TracBrowser for help on using the repository browser.