1 | # -*- coding: utf-8 -*- |
---|
2 | ########### SVN repository information ################### |
---|
3 | # $Date: 2017-05-04 21:06:25 +0000 (Thu, 04 May 2017) $ |
---|
4 | # $Author: vondreele $ |
---|
5 | # $Revision: 2820 $ |
---|
6 | # $URL: trunk/imports/G2pwd_GPX.py $ |
---|
7 | # $Id: G2pwd_GPX.py 2820 2017-05-04 21:06:25Z vondreele $ |
---|
8 | ########### SVN repository information ################### |
---|
9 | ''' |
---|
10 | *Module G2pwd_GPX: GSAS-II projects* |
---|
11 | ------------------------------------ |
---|
12 | Routine to import powder data from GSAS-II .gpx files |
---|
13 | |
---|
14 | ''' |
---|
15 | import cPickle |
---|
16 | import numpy as np |
---|
17 | import GSASIIobj as G2obj |
---|
18 | import GSASIIpath |
---|
19 | GSASIIpath.SetVersionNumber("$Revision: 2820 $") |
---|
20 | |
---|
21 | class GSAS2_ReaderClass(G2obj.ImportPowderData): |
---|
22 | """Routines to import powder data from a GSAS-II file |
---|
23 | This should work to pull data out from a out of date .GPX file |
---|
24 | as long as the details of the histogram data itself don't change |
---|
25 | """ |
---|
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 | filepointer.seek(0) |
---|
42 | nhist = 0 |
---|
43 | while True: |
---|
44 | try: |
---|
45 | data = cPickle.load(filepointer) |
---|
46 | except EOFError: |
---|
47 | break |
---|
48 | if data[0][0][:4] == 'PWDR': |
---|
49 | nhist += 1 |
---|
50 | self.dnames.append(data[0][0]) |
---|
51 | if nhist: |
---|
52 | if not len(self.selections): #no PWDR entries |
---|
53 | self.selections = range(nhist) |
---|
54 | return True |
---|
55 | self.errors = 'No powder entries found' |
---|
56 | return False |
---|
57 | |
---|
58 | def Reader(self,filename,filepointer, ParentFrame=None, **kwarg): |
---|
59 | '''Read a dataset from a .GPX file. |
---|
60 | If multiple datasets are requested, use self.repeat and buffer caching. |
---|
61 | ''' |
---|
62 | histnames = [] |
---|
63 | poslist = [] |
---|
64 | rdbuffer = kwarg.get('buffer') |
---|
65 | # reload previously saved values |
---|
66 | if self.repeat and rdbuffer is not None: |
---|
67 | self.selections = rdbuffer.get('selections') |
---|
68 | poslist = rdbuffer.get('poslist') |
---|
69 | histnames = rdbuffer.get('histnames') |
---|
70 | else: |
---|
71 | try: |
---|
72 | fl = open(filename,'rb') |
---|
73 | while True: |
---|
74 | pos = fl.tell() |
---|
75 | try: |
---|
76 | data = cPickle.load(fl) |
---|
77 | except EOFError: |
---|
78 | break |
---|
79 | if data[0][0][:4] == 'PWDR': |
---|
80 | histnames.append(data[0][0]) |
---|
81 | poslist.append(pos) |
---|
82 | except: |
---|
83 | self.errors = 'Reading of histogram names failed' |
---|
84 | return False |
---|
85 | finally: |
---|
86 | fl.close() |
---|
87 | if not histnames: |
---|
88 | return False # no blocks with powder data |
---|
89 | elif len(histnames) == 1: # one block, no choices |
---|
90 | selblk = 0 |
---|
91 | elif self.repeat and self.selections is not None: |
---|
92 | # we were called to repeat the read |
---|
93 | #print 'debug: repeat #',self.repeatcount,'selection',selections[self.repeatcount] |
---|
94 | selblk = self.selections[self.repeatcount] |
---|
95 | self.repeatcount += 1 |
---|
96 | if self.repeatcount >= len(self.selections): self.repeat = False |
---|
97 | else: # choose from options |
---|
98 | selblk = self.selections[0] # select first in list |
---|
99 | if len(self.selections) > 1: # prepare to loop through again |
---|
100 | self.repeat = True |
---|
101 | self.repeatcount = 1 |
---|
102 | if rdbuffer is not None: |
---|
103 | rdbuffer['poslist'] = poslist |
---|
104 | rdbuffer['histnames'] = histnames |
---|
105 | rdbuffer['selections'] = self.selections |
---|
106 | |
---|
107 | fl = open(filename,'rb') |
---|
108 | fl.seek(poslist[selblk]) |
---|
109 | data = cPickle.load(fl) |
---|
110 | N = len(data[0][1][1][0]) |
---|
111 | #self.powderdata = data[0][1][1] |
---|
112 | self.powderdata = [ |
---|
113 | data[0][1][1][0], # x-axis values |
---|
114 | data[0][1][1][1], # powder pattern intensities |
---|
115 | data[0][1][1][2], # 1/sig(intensity)^2 values (weights) |
---|
116 | np.zeros(N), # calc. intensities (zero) |
---|
117 | np.zeros(N), # calc. background (zero) |
---|
118 | np.zeros(N), # obs-calc profiles |
---|
119 | ] |
---|
120 | self.powderentry[0] = filename |
---|
121 | self.powderentry[2] = selblk+1 |
---|
122 | # pull some sections from the PWDR children |
---|
123 | for i in range(1,len(data)): |
---|
124 | if data[i][0] == 'Comments': |
---|
125 | self.comments = data[i][1] |
---|
126 | continue |
---|
127 | elif data[i][0] == 'Sample Parameters': |
---|
128 | self.Sample = data[i][1] |
---|
129 | continue |
---|
130 | for keepitem in ('Limits','Background','Instrument Parameters'): |
---|
131 | if data[i][0] == keepitem: |
---|
132 | self.pwdparms[data[i][0]] = data[i][1] |
---|
133 | break |
---|
134 | self.idstring = data[0][0][5:] |
---|
135 | self.repeat_instparm = False # prevent reuse of iparm when several hists are read |
---|
136 | fl.close() |
---|
137 | return True |
---|