1 | # -*- coding: utf-8 -*- |
---|
2 | ########### SVN repository information ################### |
---|
3 | # $Date: 2014-11-20 18:57:51 +0000 (Thu, 20 Nov 2014) $ |
---|
4 | # $Author: toby $ |
---|
5 | # $Revision: 1574 $ |
---|
6 | # $URL: trunk/imports/G2pwd_GPX.py $ |
---|
7 | # $Id: G2pwd_GPX.py 1574 2014-11-20 18:57:51Z toby $ |
---|
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 sys |
---|
16 | import cPickle |
---|
17 | import numpy as np |
---|
18 | import GSASIIIO as G2IO |
---|
19 | import GSASIIpath |
---|
20 | GSASIIpath.SetVersionNumber("$Revision: 1574 $") |
---|
21 | |
---|
22 | class GSAS2_ReaderClass(G2IO.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 = self.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 | try: |
---|
103 | fl = open(filename,'rb') |
---|
104 | fl.seek(poslist[selblk]) |
---|
105 | data = cPickle.load(fl) |
---|
106 | N = len(data[0][1][1][0]) |
---|
107 | #self.powderdata = data[0][1][1] |
---|
108 | self.powderdata = [ |
---|
109 | data[0][1][1][0], # x-axis values |
---|
110 | data[0][1][1][1], # powder pattern intensities |
---|
111 | data[0][1][1][2], # 1/sig(intensity)^2 values (weights) |
---|
112 | np.zeros(N), # calc. intensities (zero) |
---|
113 | np.zeros(N), # calc. background (zero) |
---|
114 | np.zeros(N), # obs-calc profiles |
---|
115 | ] |
---|
116 | self.powderentry[0] = filename |
---|
117 | self.powderentry[2] = selblk+1 |
---|
118 | # pull some sections from the PWDR children |
---|
119 | for i in range(1,len(data)): |
---|
120 | if data[i][0] == 'Comments': |
---|
121 | self.comments = data[i][1] |
---|
122 | continue |
---|
123 | elif data[i][0] == 'Sample Parameters': |
---|
124 | self.Sample = data[i][1] |
---|
125 | continue |
---|
126 | for keepitem in ('Limits','Background','Instrument Parameters'): |
---|
127 | if data[i][0] == keepitem: |
---|
128 | self.pwdparms[data[i][0]] = data[i][1] |
---|
129 | break |
---|
130 | self.idstring = data[0][0][5:] |
---|
131 | self.repeat_instparm = False # prevent reuse of iparm when several hists are read |
---|
132 | return True |
---|
133 | except Exception as detail: |
---|
134 | self.errors = 'Error reading selected histogram\n '+str(detail) |
---|
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() |
---|