source: trunk/imports/G2pwd_GPX.py @ 2817

Last change on this file since 2817 was 2817, checked in by vondreele, 6 years ago

major revision - move all importers to GSASIIobj & make them independent of wx so they can be used in a scripting environment.
Still to move are PhaseSelector?, and 3 BlockSelector? dialogs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 5.0 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2017-05-02 15:03:41 +0000 (Tue, 02 May 2017) $
4# $Author: vondreele $
5# $Revision: 2817 $
6# $URL: trunk/imports/G2pwd_GPX.py $
7# $Id: G2pwd_GPX.py 2817 2017-05-02 15:03:41Z vondreele $
8########### SVN repository information ###################
9'''
10*Module G2pwd_GPX: GSAS-II projects*
11------------------------------------
12Routine to import powder data from GSAS-II .gpx files
13
14'''
15import cPickle
16import numpy as np
17import GSASIIobj as G2obj
18import GSASIIIO as G2IO
19import GSASIIpath
20GSASIIpath.SetVersionNumber("$Revision: 2817 $")
21
22class GSAS2_ReaderClass(G2obj.ImportPowderData):
23    """Routines to import powder data from a GSAS-II file
24    This should work to pull data out from a out of date .GPX file
25    as long as the details of the histogram data itself don't change
26    """
27    def __init__(self):
28        super(self.__class__,self).__init__( # fancy way to say ImportPhase.__init__
29            extensionlist=('.gpx',),
30            strictExtension=True,
31            formatName = 'GSAS-II gpx',
32            longFormatName = 'GSAS-II project (.gpx file) import'
33            )
34       
35    def ContentsValidator(self, filepointer):
36        "Test if the 1st section can be read as a cPickle block, if not it can't be .GPX!"
37        try: 
38            cPickle.load(filepointer)
39        except:
40            self.errors = 'This is not a valid .GPX file. Not recognized by cPickle'
41            return False
42        return True
43
44    def Reader(self,filename,filepointer, ParentFrame=None, **kwarg):
45        '''Read a dataset from a .GPX file.
46        If multiple datasets are requested, use self.repeat and buffer caching.
47        '''
48        histnames = []
49        poslist = []
50        rdbuffer = kwarg.get('buffer')
51        # reload previously saved values
52        if self.repeat and rdbuffer is not None:
53            selections = rdbuffer.get('selections')
54            poslist = rdbuffer.get('poslist')
55            histnames = rdbuffer.get('histnames')
56        else:
57            try:
58                fl = open(filename,'rb')
59                while True:
60                    pos = fl.tell()
61                    try:
62                        data = cPickle.load(fl)
63                    except EOFError:
64                        break
65                    if data[0][0][:4] == 'PWDR':
66                        histnames.append(data[0][0])
67                        poslist.append(pos)
68            except:
69                self.errors = 'Reading of histogram names failed'
70                return False
71            finally:
72                fl.close()
73        if not histnames:
74            return False            # no blocks with powder data
75        elif len(histnames) == 1: # one block, no choices
76            selblk = 0
77        elif self.repeat and selections is not None:
78            # we were called to repeat the read
79            #print 'debug: repeat #',self.repeatcount,'selection',selections[self.repeatcount]
80            selblk = selections[self.repeatcount]
81            self.repeatcount += 1
82            if self.repeatcount >= len(selections): self.repeat = False
83        else:                       # choose from options               
84            selections = G2IO.MultipleBlockSelector(
85                histnames,
86                ParentFrame=ParentFrame,
87                title='Select histogram(s) to read from the list below',
88                size=(600,250),
89                header='Dataset Selector')
90            if len(selections) == 0:
91                self.errors = 'No histogram selected'
92                return False
93            selblk = selections[0] # select first in list
94            if len(selections) > 1: # prepare to loop through again
95                self.repeat = True
96                self.repeatcount = 1
97                if rdbuffer is not None:
98                    rdbuffer['poslist'] = poslist
99                    rdbuffer['histnames'] = histnames
100                    rdbuffer['selections'] = selections
101
102        fl = open(filename,'rb')
103        fl.seek(poslist[selblk])
104        data = cPickle.load(fl)
105        N = len(data[0][1][1][0])
106        #self.powderdata = data[0][1][1]
107        self.powderdata = [
108            data[0][1][1][0], # x-axis values
109            data[0][1][1][1], # powder pattern intensities
110            data[0][1][1][2], # 1/sig(intensity)^2 values (weights)
111            np.zeros(N), # calc. intensities (zero)
112            np.zeros(N), # calc. background (zero)
113            np.zeros(N), # obs-calc profiles
114        ]
115        self.powderentry[0] = filename
116        self.powderentry[2] = selblk+1
117        # pull some sections from the PWDR children
118        for i in range(1,len(data)):
119            if data[i][0] == 'Comments':
120                self.comments = data[i][1]
121                continue
122            elif data[i][0] == 'Sample Parameters':
123                self.Sample = data[i][1]
124                continue
125            for keepitem in ('Limits','Background','Instrument Parameters'): 
126                if data[i][0] == keepitem:
127                    self.pwdparms[data[i][0]] = data[i][1]
128                    break
129        self.idstring = data[0][0][5:]
130        self.repeat_instparm = False # prevent reuse of iparm when several hists are read
131        fl.close()
132        return True
Note: See TracBrowser for help on using the repository browser.