source: trunk/imports/G2pwd_GPX.py @ 886

Last change on this file since 886 was 886, checked in by toby, 9 years ago

make GSAS iparm files the default option; suppress iparm reuse for GPX data import

  • Property svn:eol-style set to native
File size: 5.1 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            # could pull out wavelength from data[4][1] here
111            # and save it as self.instdict['wave'] = wl
112            # likewise for dataset type as self.instdict['type'] = 'SNC' etc
113            # and self.Sample['Temperature'] for T
114            self.repeat_instparm = False # prevent reuse of iparm when several hists are read
115            return True
116        except Exception as detail:
117            import sys
118            print self.formatName+' error:',detail # for testing
119            print sys.exc_info()[0] # for testing
120            import traceback
121            print traceback.format_exc()
122            return False
123        finally:
124            fl.close()
Note: See TracBrowser for help on using the repository browser.