source: trunk/imports/G2phase_GPX.py @ 1168

Last change on this file since 1168 was 1168, checked in by toby, 8 years ago

update imports to provide error messages

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 2.8 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2013-12-16 16:43:01 +0000 (Mon, 16 Dec 2013) $
4# $Author: toby $
5# $Revision: 1168 $
6# $URL: trunk/imports/G2phase_GPX.py $
7# $Id: G2phase_GPX.py 1168 2013-12-16 16:43:01Z toby $
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'''
17import sys
18import cPickle
19import GSASIIIO as G2IO
20import GSASIIstrIO as G2stIO
21import GSASIIpath
22GSASIIpath.SetVersionNumber("$Revision: 1168 $")
23
24class PhaseReaderClass(G2IO.ImportPhase):
25    'Opens a .GPX file and pulls out a selected phase'
26    def __init__(self):
27        super(self.__class__,self).__init__( # fancy way to say ImportPhase.__init__
28            extensionlist=('.gpx',),
29            strictExtension=True,
30            formatName = 'GSAS-II gpx',
31            longFormatName = 'GSAS-II project (.gpx file) import'
32            )
33       
34    def ContentsValidator(self, filepointer):
35        "Test if the 1st section can be read as a cPickle block, if not it can't be .GPX!"
36        try: 
37            cPickle.load(filepointer)
38        except:
39            self.errors = 'This is not a valid .GPX file. Not recognized by cPickle'
40            return False
41        return True
42
43    def Reader(self,filename,filepointer, ParentFrame=None, **unused):
44        '''Read a phase from a .GPX file. Does not (yet?) support selecting and reading
45        more than one phase at a time.'''
46        try:
47            phasenames = G2stIO.GetPhaseNames(filename)
48        except:
49            self.errors = 'Reading of phase names failed'
50            return False
51        if not phasenames:
52            self.errors = 'No phases found in '+str(filename)
53            return False            # no blocks with coordinates
54        elif len(phasenames) == 1: # one block, no choices
55            selblk = 0
56        else:                       # choose from options               
57            selblk = self.PhaseSelector(
58                phasenames,
59                ParentFrame=ParentFrame,
60                title= 'Select a phase from the list below',
61                )
62            if selblk is None:
63                self.errors = 'No phase selected'
64                return False # User pressed cancel
65        try:
66            self.Phase = G2stIO.GetAllPhaseData(filename,phasenames[selblk])
67            self.Phase['Histograms'] = {}       #remove any histograms
68            self.Phase['Pawley ref'] = []       # & any Pawley refl.
69            return True
70        except Exception as detail:
71            self.errors = 'Error reading selected phase'
72            self.errors += '\n  '+str(detail)
73            print self.formatName+' error:',detail # for testing
74            print sys.exc_info()[0] # for testing
75            import traceback
76            print traceback.format_exc()
Note: See TracBrowser for help on using the repository browser.