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