source: trunk/imports/G2pwd_GPX.py @ 887

Last change on this file since 887 was 887, checked in by toby, 10 years ago

xfer lambda & T from GPX histograms; remove Cancel from selection of default parameter set

  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2012-02-13 11:33:35 -0600 (Mon, 13 Feb 2012) $
4# $Author: vondreele & toby $
5# $Revision: 482 $
6# $URL: https://subversion.xor.aps.anl.gov/pyGSAS/trunk/G2importphase_GPX.py $
7# $Id: G2importphase_GPX.py 482 2012-02-13 17:33:35Z vondreele $
8########### SVN repository information ###################
9# Routines to import powder data from GSAS-II .gpx files
10import cPickle
11import numpy as np
12import GSASIIIO as G2IO
13import GSASIIstruct as G2str
14
15class GSAS2_ReaderClass(G2IO.ImportPowderData):
16    """Routines to import powder data from a GSAS-II file
17    This should work to pull data out from a out of date .GPX file
18    as long as the details of the histogram data itself don't change
19    """
20    def __init__(self):
21        super(self.__class__,self).__init__( # fancy way to say ImportPhase.__init__
22            extensionlist=('.gpx',),
23            strictExtension=True,
24            formatName = 'GSAS-II gpx',
25            longFormatName = 'GSAS-II project (.gpx file) import'
26            )
27    def ContentsValidator(self, filepointer):
28        # if the 1st section can't be read as a cPickle file, it can't be a GPX!
29        try: 
30            cPickle.load(filepointer)
31        except:
32            return False
33        return True
34    def Reader(self,filename,filepointer, ParentFrame=None, **kwarg):
35        histnames = []
36        poslist = []
37        rdbuffer = kwarg.get('buffer')
38        # reload previously saved values
39        if self.repeat and rdbuffer is not None:
40            selections = rdbuffer.get('selections')
41            poslist = rdbuffer.get('poslist')
42            histnames = rdbuffer.get('histnames')
43        else:
44            try:
45                fl = open(filename,'rb')
46                while True:
47                    pos = fl.tell()
48                    try:
49                        data = cPickle.load(fl)
50                    except EOFError:
51                        break
52                    if data[0][0][:4] == 'PWDR':
53                        histnames.append(data[0][0])
54                        poslist.append(pos)
55            except:
56                print 'error scanning GPX file',filename
57                return False
58            finally:
59                fl.close()
60        if not histnames:
61            return False            # no blocks with coordinates
62        elif len(histnames) == 1: # no choices
63            selblk = 0
64        elif self.repeat and selections is not None:
65            # we were called to repeat the read
66            #print 'debug: repeat #',self.repeatcount,'selection',selections[self.repeatcount]
67            selblk = selections[self.repeatcount]
68            self.repeatcount += 1
69            if self.repeatcount >= len(selections): self.repeat = False
70        else:                       # choose from options               
71#            selblk = self.BlockSelector(
72#                histnames,
73#                ParentFrame=ParentFrame,
74#                title= 'Select a block from the list below',
75#                )
76#            if selblk is None: return False # User pressed cancel
77            selections = self.MultipleBlockSelector(
78                histnames,
79                ParentFrame=ParentFrame,
80                title='Select histogram(s) to read from the list below',
81                size=(600,100),
82                header='Dataset Selector')
83            if len(selections) == 0: return False
84            selblk = selections[0] # select first in list
85            if len(selections) > 1: # prepare to loop through again
86                self.repeat = True
87                self.repeatcount = 1
88                if rdbuffer is not None:
89                    rdbuffer['poslist'] = poslist
90                    rdbuffer['histnames'] = histnames
91                    rdbuffer['selections'] = selections
92
93        try:
94            fl = open(filename,'rb')
95            fl.seek(poslist[selblk])
96            data = cPickle.load(fl)
97            N = len(data[0][1][1][0])
98            #self.powderdata = data[0][1][1]
99            self.powderdata = [
100                data[0][1][1][0], # x-axis values
101                data[0][1][1][1], # powder pattern intensities
102                data[0][1][1][2], # 1/sig(intensity)^2 values (weights)
103                np.zeros(N), # calc. intensities (zero)
104                np.zeros(N), # calc. background (zero)
105                np.zeros(N), # obs-calc profiles
106            ]
107            self.powderentry[0] = filename
108            self.powderentry[2] = selblk+1
109            self.idstring = data[0][0][5:]
110            # pull out wavelength
111            try:
112                if len(data[4][1]) == 2: # current GPX file
113                    if data[4][1][0].get('Lam'):
114                        self.instdict['wave'] = [data[4][1][0].get('Lam')[1]]
115                    elif data[4][1][0].get('Lam1') and data[4][1][0].get('Lam2'):
116                        self.instdict['wave'] = [
117                            data[4][1][0].get('Lam1')[1],
118                            data[4][1][0].get('Lam2')[1]
119                            ]
120                elif len(data[4][1]) == 4: # original GPX file
121                    pos = data[4][1][3].index('Lam')
122                    self.instdict['wave'] = [data[4][1][1][pos],]
123            except:
124                pass
125            # pull out temperature
126            try:
127                if data[5][1].get('Temperature'):
128                    self.Sample['Temperature'] = data[5][1]['Temperature']
129            except:
130                pass
131            self.repeat_instparm = False # prevent reuse of iparm when several hists are read
132            return True
133        except Exception as detail:
134            import sys
135            print self.formatName+' error:',detail # for testing
136            print sys.exc_info()[0] # for testing
137            import traceback
138            print traceback.format_exc()
139            return False
140        finally:
141            fl.close()
Note: See TracBrowser for help on using the repository browser.