1 | import math |
---|
2 | import time |
---|
3 | import copy |
---|
4 | import numpy as np |
---|
5 | import wx |
---|
6 | import wx.aui |
---|
7 | import matplotlib as mpl |
---|
8 | import GSASIIgrid as G2gd |
---|
9 | import GSASIIcomp as G2cmp |
---|
10 | from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas |
---|
11 | from matplotlib.backends.backend_wxagg import NavigationToolbar2Wx as Toolbar |
---|
12 | |
---|
13 | # useful degree trig functions |
---|
14 | sind = lambda x: math.sin(x*math.pi/180.) |
---|
15 | cosd = lambda x: math.cos(x*math.pi/180.) |
---|
16 | tand = lambda x: math.tan(x*math.pi/180.) |
---|
17 | asind = lambda x: 180.*math.asin(x)/math.pi |
---|
18 | acosd = lambda x: 180.*math.acos(x)/math.pi |
---|
19 | atan2d = lambda x,y: 180.*math.atan2(y,x)/math.pi |
---|
20 | atand = lambda x: 180.*math.atan(x)/math.pi |
---|
21 | |
---|
22 | class G2Plot(wx.Panel): |
---|
23 | |
---|
24 | def __init__(self,parent,id=-1,dpi=None,**kwargs): |
---|
25 | wx.Panel.__init__(self,parent,id=id,**kwargs) |
---|
26 | self.figure = mpl.figure.Figure(dpi=dpi,figsize=(5,7)) |
---|
27 | self.canvas = Canvas(self,-1,self.figure) |
---|
28 | self.toolbar = Toolbar(self.canvas) |
---|
29 | |
---|
30 | self.toolbar.Realize() |
---|
31 | |
---|
32 | sizer=wx.BoxSizer(wx.VERTICAL) |
---|
33 | sizer.Add(self.canvas,1,wx.EXPAND) |
---|
34 | sizer.Add(self.toolbar,0,wx.LEFT|wx.EXPAND) |
---|
35 | self.SetSizer(sizer) |
---|
36 | |
---|
37 | class G2PlotNoteBook(wx.Panel): |
---|
38 | def __init__(self,parent,id=-1): |
---|
39 | wx.Panel.__init__(self,parent,id=id) |
---|
40 | self.nb = wx.aui.AuiNotebook(self) #style=wx.AUI_NB_DEFAULT_STYLE ^ wx.AUI_NB_CLOSE_ON_ACTIVE_TAB) |
---|
41 | sizer = wx.BoxSizer() |
---|
42 | sizer.Add(self.nb,1,wx.EXPAND) |
---|
43 | self.SetSizer(sizer) |
---|
44 | self.status = parent.CreateStatusBar() |
---|
45 | |
---|
46 | self.plotList = [] |
---|
47 | |
---|
48 | def add(self,name=""): |
---|
49 | page = G2Plot(self.nb) |
---|
50 | self.nb.AddPage(page,name) |
---|
51 | |
---|
52 | self.plotList.append(name) |
---|
53 | |
---|
54 | return page.figure |
---|
55 | |
---|
56 | def PlotSngl(self): |
---|
57 | from matplotlib.patches import Circle |
---|
58 | |
---|
59 | def OnSCMotion(event): |
---|
60 | xpos = event.xdata |
---|
61 | if xpos: |
---|
62 | xpos = round(xpos) #avoid out of frame mouse position |
---|
63 | ypos = round(event.ydata) |
---|
64 | zpos = Data['Layer'] |
---|
65 | if '100' in Data['Zone']: |
---|
66 | HKLtxt = '(%3d,%3d,%3d)'%(zpos,xpos,ypos) |
---|
67 | elif '010' in Data['Zone']: |
---|
68 | HKLtxt = '(%3d,%3d,%3d)'%(xpos,zpos,ypos) |
---|
69 | elif '001' in Data['Zone']: |
---|
70 | HKLtxt = '(%3d,%3d,%3d)'%(xpos,ypos,zpos) |
---|
71 | snglPage.canvas.SetToolTipString(HKLtxt) |
---|
72 | self.G2plotNB.status.SetFields(['HKL = '+HKLtxt,]) |
---|
73 | |
---|
74 | def OnSCPick(event): |
---|
75 | zpos = Data['Layer'] |
---|
76 | pos = event.artist.center |
---|
77 | if '100' in Data['Zone']: |
---|
78 | snglPage.canvas.SetToolTipString('(picked:(%3d,%3d,%3d))'%(zpos,pos[0],pos[1])) |
---|
79 | hkl = [zpos,pos[0],pos[1]] |
---|
80 | elif '010' in Data['Zone']: |
---|
81 | snglPage.canvas.SetToolTipString('(picked:(%3d,%3d,%3d))'%(pos[0],zpos,pos[1])) |
---|
82 | hkl = [pos[0],zpos,pos[1]] |
---|
83 | elif '001' in Data['Zone']: |
---|
84 | snglPage.canvas.SetToolTipString('(picked:(%3d,%3d,%3d))'%(pos[0],pos[1],zpos)) |
---|
85 | hkl = [pos[0],pos[1],zpos] |
---|
86 | h,k,l = hkl |
---|
87 | i = HKL.all(hkl) |
---|
88 | print i |
---|
89 | HKLtxt = '(%3d,%3d,%3d %10.2f %6.3f %10.2f)'%(h,k,l,Fosq,sig,Fcsq) |
---|
90 | self.G2plotNB.status.SetFields(['HKL, Fosq, sig, Fcsq = '+HKLtxt,]) |
---|
91 | |
---|
92 | |
---|
93 | def OnSCKeyPress(event): |
---|
94 | print event.key |
---|
95 | |
---|
96 | try: |
---|
97 | plotNum = self.G2plotNB.plotList.index('Structure Factors') |
---|
98 | snglPage = self.G2plotNB.nb.GetPage(plotNum) |
---|
99 | snglPlot = snglPage.figure.gca() |
---|
100 | snglPlot.cla() |
---|
101 | except ValueError,error: |
---|
102 | snglPlot = self.G2plotNB.add('Structure Factors').gca() |
---|
103 | plotNum = self.G2plotNB.plotList.index('Structure Factors') |
---|
104 | snglPage = self.G2plotNB.nb.GetPage(plotNum) |
---|
105 | snglPage.canvas.mpl_connect('key_press_event', OnSCKeyPress) |
---|
106 | snglPage.canvas.mpl_connect('pick_event', OnSCPick) |
---|
107 | snglPage.canvas.mpl_connect('motion_notify_event', OnSCMotion) |
---|
108 | snglPage.SetFocus() |
---|
109 | |
---|
110 | snglPlot.set_aspect(aspect='equal') |
---|
111 | HKLref = self.PatternTree.GetItemPyData(self.Sngl) |
---|
112 | Data = self.PatternTree.GetItemPyData( \ |
---|
113 | G2gd.GetPatternTreeItemId(self,self.Sngl, 'HKL Plot Controls')) |
---|
114 | Type = Data['Type'] |
---|
115 | scale = Data['Scale'] |
---|
116 | HKLmax = Data['HKLmax'] |
---|
117 | HKLmin = Data['HKLmin'] |
---|
118 | FosqMax = Data['FoMax'] |
---|
119 | FoMax = math.sqrt(FosqMax) |
---|
120 | ifFc = Data['ifFc'] |
---|
121 | xlabel = ['k, h=','h, k=','h, l='] |
---|
122 | ylabel = ['l','l','k'] |
---|
123 | zones = ['100','010','001'] |
---|
124 | pzone = [[1,2],[0,2],[0,1]] |
---|
125 | izone = zones.index(Data['Zone']) |
---|
126 | snglPlot.set_title(self.PatternTree.GetItemText(self.Sngl)[5:]) |
---|
127 | HKL = [] |
---|
128 | for H,Fosq,sig,Fcsq,x,x,x in HKLref: |
---|
129 | HKL.append(H) |
---|
130 | if H[izone] == Data['Layer']: |
---|
131 | B = 0 |
---|
132 | if Type == 'Fosq': |
---|
133 | A = scale*Fosq/FosqMax |
---|
134 | B = scale*Fcsq/FosqMax |
---|
135 | C = abs(A-B) |
---|
136 | elif Type == 'Fo': |
---|
137 | A = scale*math.sqrt(max(0,Fosq))/FoMax |
---|
138 | B = scale*math.sqrt(max(0,Fcsq))/FoMax |
---|
139 | C = abs(A-B) |
---|
140 | elif Type == '|DFsq|/sig': |
---|
141 | A = abs(Fosq-Fcsq)/(scale*sig) |
---|
142 | elif Type == '|DFsq|>sig': |
---|
143 | A = abs(Fosq-Fcsq)/(scale*sig) |
---|
144 | if A < 1.0: A = 0 |
---|
145 | elif Type == '|DFsq|>3sig': |
---|
146 | A = abs(Fosq-Fcsq)/(scale*sig) |
---|
147 | if A < 3.0: A = 0 |
---|
148 | xy = (H[pzone[izone][0]],H[pzone[izone][1]]) |
---|
149 | if A > 0.0: |
---|
150 | snglPlot.add_artist(Circle(xy,radius=A,ec='g',fc='w',picker=3)) |
---|
151 | if B: |
---|
152 | snglPlot.add_artist(Circle(xy,radius=B,ec='b',fc='w')) |
---|
153 | radius = C |
---|
154 | if radius > 0: |
---|
155 | if A > B: |
---|
156 | snglPlot.add_artist(Circle(xy,radius=radius,ec='r',fc='r')) |
---|
157 | else: |
---|
158 | snglPlot.add_artist(Circle(xy,radius=radius,ec='g',fc='g')) |
---|
159 | HKL = np.array(HKL) |
---|
160 | snglPlot.set_xlabel(xlabel[izone]+str(Data['Layer']),fontsize=12) |
---|
161 | snglPlot.set_ylabel(ylabel[izone],fontsize=12) |
---|
162 | snglPlot.set_xlim((HKLmin[pzone[izone][0]],HKLmax[pzone[izone][0]])) |
---|
163 | snglPlot.set_ylim((HKLmin[pzone[izone][1]],HKLmax[pzone[izone][1]])) |
---|
164 | snglPage.canvas.draw() |
---|
165 | |
---|
166 | def PlotImage(self): |
---|
167 | from matplotlib.patches import Ellipse,Arc |
---|
168 | |
---|
169 | def OnImMotion(event): |
---|
170 | imgPage.canvas.SetToolTipString('') |
---|
171 | size = len(self.ImageZ) |
---|
172 | if (xlim[0] < event.xdata < xlim[1]) & (ylim[0] > event.ydata > ylim[1]): |
---|
173 | Data = self.PatternTree.GetItemPyData( \ |
---|
174 | G2gd.GetPatternTreeItemId(self,self.Image, 'Image Controls')) |
---|
175 | imgPage.canvas.SetCursor(wx.CROSS_CURSOR) |
---|
176 | item = self.itemPicked |
---|
177 | pixelSize = Data['pixelSize'] |
---|
178 | scalex = 1000./pixelSize[0] |
---|
179 | scaley = 1000./pixelSize[1] |
---|
180 | if item and self.PatternTree.GetItemText(self.PickId) == 'Image Controls': |
---|
181 | if 'Text' in str(item): |
---|
182 | imgPage.canvas.SetToolTipString('%8.3f %8.3fmm'%(event.xdata,event.ydata)) |
---|
183 | else: |
---|
184 | xcent,ycent = Data['center'] |
---|
185 | xpos = event.xdata-xcent |
---|
186 | ypos = event.ydata-ycent |
---|
187 | if 'line3' in str(item) or 'line4' in str(item) and not Data['fullIntegrate']: |
---|
188 | ang = int(atan2d(-ypos,xpos)) |
---|
189 | imgPage.canvas.SetToolTipString('%6d deg'%(ang)) |
---|
190 | elif 'inner' in str(item.get_gid()) or 'outer' in str(item.get_gid()): |
---|
191 | tth = G2cmp.GetTth(event.xdata,event.ydata,Data) |
---|
192 | imgPage.canvas.SetToolTipString('%8.3fdeg'%(tth)) |
---|
193 | else: |
---|
194 | xpos = event.xdata |
---|
195 | ypos = event.ydata |
---|
196 | xpix = xpos*scalex |
---|
197 | ypix = ypos*scaley |
---|
198 | if (0 <= xpix <= size) and (0 <= ypix <= size): |
---|
199 | imgPage.canvas.SetToolTipString('%6d'%(self.ImageZ[ypix][xpix])) |
---|
200 | tth,dsp,azm = G2cmp.GetTthDspAzm(xpos,ypos,Data) |
---|
201 | Q = 2.*math.pi/dsp |
---|
202 | self.G2plotNB.status.SetFields(\ |
---|
203 | ['Detector 2-th =%9.2fdeg, dsp =%9.3fA, Q = %6.3fA-1, azm = %7.2fdeg'%(tth,dsp,Q,azm),]) |
---|
204 | |
---|
205 | def OnImPlotKeyPress(event): |
---|
206 | if self.PatternTree.GetItemText(self.PickId) == 'Image Controls': |
---|
207 | Data = self.PatternTree.GetItemPyData(self.PickId) |
---|
208 | pixelSize = Data['pixelSize'] |
---|
209 | size = len(self.ImageZ) |
---|
210 | Xpos = event.xdata |
---|
211 | if not Xpos: #got point out of frame |
---|
212 | return |
---|
213 | Ypos = event.ydata |
---|
214 | if event.key == 'm': |
---|
215 | print 'mask = ',Xpos,Ypos |
---|
216 | |
---|
217 | def OnImPick(event): |
---|
218 | if self.PatternTree.GetItemText(self.PickId) != 'Image Controls': |
---|
219 | return |
---|
220 | if self.itemPicked is not None: return |
---|
221 | pick = event.artist |
---|
222 | self.itemPicked = pick |
---|
223 | |
---|
224 | def OnImRelease(event): |
---|
225 | if self.PatternTree.GetItemText(self.PickId) != 'Image Controls': |
---|
226 | return |
---|
227 | Data = self.PatternTree.GetItemPyData(self.PickId) |
---|
228 | pixelSize = Data['pixelSize'] |
---|
229 | scalex = 1000./pixelSize[0] |
---|
230 | scaley = 1000./pixelSize[1] |
---|
231 | if self.itemPicked is None: |
---|
232 | size = len(self.ImageZ) |
---|
233 | Xpos = event.xdata |
---|
234 | if not (Xpos and self.ifGetRing): #got point out of frame |
---|
235 | return |
---|
236 | Ypos = event.ydata |
---|
237 | if Ypos and not imgPage.toolbar._active: #make sure zoom/pan not selected |
---|
238 | if event.button == 1: |
---|
239 | Xpix = Xpos*scalex |
---|
240 | Ypix = Ypos*scaley |
---|
241 | xpos,ypos,I,J = G2cmp.ImageLocalMax(self.ImageZ,20,Xpix,Ypix) |
---|
242 | if I and J: |
---|
243 | xpos /= scalex |
---|
244 | ypos /= scaley |
---|
245 | Data['ring'].append([xpos,ypos]) |
---|
246 | PlotImage(self) |
---|
247 | return |
---|
248 | else: |
---|
249 | xpos = event.xdata |
---|
250 | if xpos: #avoid out of frame mouse position |
---|
251 | ypos = event.ydata |
---|
252 | if self.ifGetRing: |
---|
253 | xypos = [xpos,ypos] |
---|
254 | rings = Data['ring'] |
---|
255 | for ring in rings: |
---|
256 | if np.allclose(ring,xypos,.01,0): |
---|
257 | rings.remove(ring) |
---|
258 | else: |
---|
259 | tth,dsp,azm = G2cmp.GetTthDspAzm(xpos,ypos,Data) |
---|
260 | if 'Line2D' in str(self.itemPicked): |
---|
261 | if 'line1' in str(self.itemPicked): |
---|
262 | Data['IOtth'][0] = tth |
---|
263 | elif 'line2' in str(self.itemPicked): |
---|
264 | Data['IOtth'][1] = tth |
---|
265 | elif 'line3' in str(self.itemPicked) and not Data['fullIntegrate']: |
---|
266 | Data['LRazimuth'][0] = int(azm) |
---|
267 | elif 'line4' in str(self.itemPicked) and not Data['fullIntegrate']: |
---|
268 | Data['LRazimuth'][1] = int(azm) |
---|
269 | |
---|
270 | if Data['LRazimuth'][1] < Data['LRazimuth'][0]: |
---|
271 | Data['LRazimuth'][1] += 360 |
---|
272 | if Data['IOtth'][0] > Data['IOtth'][1]: |
---|
273 | Data['IOtth'] = G2cmp.SwapXY(Data['IOtth'][0],Data['IOtth'][1]) |
---|
274 | |
---|
275 | self.InnerTth.SetValue("%8.2f" % (Data['IOtth'][0])) |
---|
276 | self.OuterTth.SetValue("%8.2f" % (Data['IOtth'][1])) |
---|
277 | self.Lazim.SetValue("%6d" % (Data['LRazimuth'][0])) |
---|
278 | self.Razim.SetValue("%6d" % (Data['LRazimuth'][1])) |
---|
279 | else: |
---|
280 | print event.xdata,event.ydata,event.button |
---|
281 | PlotImage(self) |
---|
282 | self.itemPicked = None |
---|
283 | |
---|
284 | try: |
---|
285 | plotNum = self.G2plotNB.plotList.index('2D Powder Image') |
---|
286 | imgPage = self.G2plotNB.nb.GetPage(plotNum) |
---|
287 | imgPage.figure.clf() |
---|
288 | imgPlot = imgPage.figure.gca() |
---|
289 | if imgPage.views: |
---|
290 | imgPage.toolbar._views = copy.deepcopy(imgPage.views) |
---|
291 | view = imgPage.toolbar._views.forward() |
---|
292 | |
---|
293 | except ValueError,error: |
---|
294 | imgPlot = self.G2plotNB.add('2D Powder Image').gca() |
---|
295 | plotNum = self.G2plotNB.plotList.index('2D Powder Image') |
---|
296 | imgPage = self.G2plotNB.nb.GetPage(plotNum) |
---|
297 | imgPage.canvas.mpl_connect('key_press_event', OnImPlotKeyPress) |
---|
298 | imgPage.canvas.mpl_connect('motion_notify_event', OnImMotion) |
---|
299 | imgPage.canvas.mpl_connect('pick_event', OnImPick) |
---|
300 | imgPage.canvas.mpl_connect('button_release_event', OnImRelease) |
---|
301 | imgPage.views = False |
---|
302 | view = False |
---|
303 | imgPage.SetFocus() |
---|
304 | |
---|
305 | imgPlot.set_title(self.PatternTree.GetItemText(self.Image)[4:]) |
---|
306 | size,self.ImageZ = self.PatternTree.GetItemPyData(self.Image) |
---|
307 | Data = self.PatternTree.GetItemPyData( \ |
---|
308 | G2gd.GetPatternTreeItemId(self,self.Image, 'Image Controls')) |
---|
309 | imScale = 1 |
---|
310 | if len(self.ImageZ) > 1024: |
---|
311 | imScale = len(self.ImageZ)/1024 |
---|
312 | pixelSize = Data['pixelSize'] |
---|
313 | scalex = 1000./pixelSize[0] |
---|
314 | scaley = 1000./pixelSize[1] |
---|
315 | xmax = len(self.ImageZ) |
---|
316 | Xmax = len(self.ImageZ)*pixelSize[0]/1000. |
---|
317 | xlim = (-0.5,Xmax-.5) |
---|
318 | ylim = (Xmax-.5,-0.5,) |
---|
319 | if self.Img: |
---|
320 | xlim = self.Img.axes.get_xlim() |
---|
321 | ylim = self.Img.axes.get_ylim() |
---|
322 | Imin,Imax = Data['range'][1] |
---|
323 | acolor = mpl.cm.get_cmap(Data['color']) |
---|
324 | xcent,ycent = Data['center'] |
---|
325 | imgPlot.set_xlabel('Image x-axis, mm',fontsize=12) |
---|
326 | imgPlot.set_ylabel('Image y-axis, mm',fontsize=12) |
---|
327 | A = G2cmp.ImageCompress(self.ImageZ,imScale) |
---|
328 | self.Img = imgPlot.imshow(self.ImageZ[::imScale,::imScale], \ |
---|
329 | aspect='equal',cmap=acolor, \ |
---|
330 | interpolation='nearest',vmin=Imin,vmax=Imax, \ |
---|
331 | extent=[0,Xmax,Xmax,0]) |
---|
332 | imgPlot.plot(xcent,ycent,'x') |
---|
333 | if Data['showLines']: |
---|
334 | LRAzim = Data['LRazimuth'] #NB: integers |
---|
335 | IOtth = Data['IOtth'] |
---|
336 | wave = Data['wavelength'] |
---|
337 | dsp = wave/(2.0*sind(IOtth[0]/2.0)) |
---|
338 | ell0 = G2cmp.GetEllipse(dsp,Data) #=False if dsp didn't yield an ellipse (ugh! a parabola or a hyperbola) |
---|
339 | dsp = wave/(2.0*sind(IOtth[1]/2.0)) |
---|
340 | ell1 = G2cmp.GetEllipse(dsp,Data) #Ditto & more likely for outer ellipse |
---|
341 | if Data['fullIntegrate']: |
---|
342 | arcxI = arcyI = np.array(range(0,361)) |
---|
343 | arcxO = arcyO = np.array(range(0,361)) |
---|
344 | else: |
---|
345 | arcxI = arcyI = np.array(range(LRAzim[0],LRAzim[1]+1)) |
---|
346 | arcxO = arcyO = np.array(range(LRAzim[0],LRAzim[1]+1)) |
---|
347 | if ell0: |
---|
348 | cent0,phi0,radii0 = ell0 |
---|
349 | radii0 = np.sum(G2cmp.makeMat(phi0,2).T*np.array(radii0+[0.,]),axis=1) |
---|
350 | arcxI = np.cos(arcxI*math.pi/180.)*radii0[0]+cent0[0] |
---|
351 | arcyI = np.sin(arcyI*math.pi/180.)*radii0[1]+cent0[1] |
---|
352 | imgPlot.plot(arcxI,arcyI,picker=3) |
---|
353 | if ell1: |
---|
354 | cent1,phi1,radii1 = ell1 |
---|
355 | radii1 = np.sum(G2cmp.makeMat(phi1,2).T*np.array(radii1+[0.,]),axis=1) |
---|
356 | arcxO = np.cos(arcxO*math.pi/180.)*radii1[0]+cent1[0] |
---|
357 | arcyO = np.sin(arcyO*math.pi/180.)*radii1[1]+cent1[1] |
---|
358 | imgPlot.plot(arcxO,arcyO,picker=3) |
---|
359 | if ell0 and ell1 and not Data['fullIntegrate']: |
---|
360 | imgPlot.plot([arcxI[0],arcxO[0]],[arcyI[0],arcyO[0]],picker=3) |
---|
361 | imgPlot.plot([arcxI[-1],arcxO[-1]],[arcyI[-1],arcyO[-1]],picker=3) |
---|
362 | for xring,yring in Data['ring']: |
---|
363 | imgPlot.text(xring,yring,'+',color='b',ha='center',va='center',picker=3) |
---|
364 | if Data['setRings']: |
---|
365 | rings = Data['rings'] |
---|
366 | for xring,yring,tth in rings: |
---|
367 | imgPlot.text(xring,yring,'+',ha='center',va='center') |
---|
368 | for ellipse in Data['ellipses']: |
---|
369 | cent,phi,[width,height],col = ellipse |
---|
370 | imgPlot.add_artist(Ellipse([cent[0],cent[1]],2*width,2*height,phi,ec=col,fc='none')) |
---|
371 | imgPlot.text(cent[0],cent[1],'+',color=col,ha='center',va='center') |
---|
372 | colorBar = imgPage.figure.colorbar(self.Img) |
---|
373 | if view: |
---|
374 | self.Img.axes.set_xlim(view[0][:2]) |
---|
375 | self.Img.axes.set_ylim(view[0][2:]) |
---|
376 | else: |
---|
377 | self.Img.axes.set_xlim(xlim) |
---|
378 | self.Img.axes.set_ylim(ylim) |
---|
379 | imgPage.canvas.draw() |
---|
380 | |
---|
381 | def PlotPeakWidths(self): |
---|
382 | PatternId = self.PatternId |
---|
383 | limitID = G2gd.GetPatternTreeItemId(self,PatternId, 'Limits') |
---|
384 | if limitID: |
---|
385 | limits = self.PatternTree.GetItemPyData(limitID) |
---|
386 | else: |
---|
387 | return |
---|
388 | instParms = self.PatternTree.GetItemPyData( \ |
---|
389 | G2gd.GetPatternTreeItemId(self,PatternId, 'Instrument Parameters')) |
---|
390 | if instParms[0][0] == 'PXC': |
---|
391 | lam = instParms[1][1] |
---|
392 | if len(instParms[1]) == 12: |
---|
393 | GU,GV,GW,LX,LY = instParms[0][6:11] |
---|
394 | else: |
---|
395 | GU,GV,GW,LX,LY = instParms[0][4:9] |
---|
396 | peakID = G2gd.GetPatternTreeItemId(self,PatternId, 'Peak List') |
---|
397 | if peakID: |
---|
398 | peaks = self.PatternTree.GetItemPyData(peakID) |
---|
399 | else: |
---|
400 | peaks = [] |
---|
401 | |
---|
402 | try: |
---|
403 | plotNum = self.G2plotNB.plotList.index('Peak Widths') |
---|
404 | pkwPage = self.G2plotNB.nb.GetPage(plotNum) |
---|
405 | pkwPage.figure.clf() |
---|
406 | pkwPlot = pkwPage.figure.gca() |
---|
407 | except ValueError,error: |
---|
408 | pkwPlot = self.G2plotNB.add('Peak Widths').gca() |
---|
409 | plotNum = self.G2plotNB.plotList.index('Peak Widths') |
---|
410 | pkwPage = self.G2plotNB.nb.GetPage(plotNum) |
---|
411 | pkwPage.SetFocus() |
---|
412 | |
---|
413 | pkwPage.canvas.SetToolTipString('') |
---|
414 | colors=['b','g','r','c','m','k'] |
---|
415 | Xmin,Xmax = limits[1] |
---|
416 | Xmin = min(0.5,max(Xmin,1)) |
---|
417 | Xmin /= 2 |
---|
418 | Xmax /= 2 |
---|
419 | nPts = 100 |
---|
420 | delt = (Xmax-Xmin)/nPts |
---|
421 | thetas = [] |
---|
422 | for i in range(nPts): |
---|
423 | thetas.append(Xmin+i*delt) |
---|
424 | X = [] |
---|
425 | Y = [] |
---|
426 | Z = [] |
---|
427 | W = [] |
---|
428 | sig = lambda Th,U,V,W: 1.17741*math.sqrt(U*tand(Th)**2+V*tand(Th)+W)*math.pi/18000. |
---|
429 | gam = lambda Th,X,Y: (X/cosd(Th)+Y*tand(Th))*math.pi/18000. |
---|
430 | gamFW = lambda s,g: math.exp(math.log(g**5+2.69269*g**4*s+2.42843*g**3*s**2+4.47163*g**2*s**3+0.07842*g*s**4+s**5)/5.) |
---|
431 | for theta in thetas: |
---|
432 | X.append(4.0*math.pi*sind(theta)/lam) #q |
---|
433 | s = sig(theta,GU,GV,GW) |
---|
434 | g = gam(theta,LX,LY) |
---|
435 | G = gamFW(g,s) |
---|
436 | Y.append(s/tand(theta)) |
---|
437 | Z.append(g/tand(theta)) |
---|
438 | W.append(G/tand(theta)) |
---|
439 | pkwPlot.set_title('Instrument and sample peak widths') |
---|
440 | pkwPlot.set_ylabel(r'$\Delta q/q, \Delta d/d$',fontsize=14) |
---|
441 | pkwPlot.set_xlabel(r'$q, \AA^{-1}$',fontsize=14) |
---|
442 | pkwPlot.plot(X,Y,color='r',label='Gaussian') |
---|
443 | pkwPlot.plot(X,Z,color='g',label='Lorentzian') |
---|
444 | pkwPlot.plot(X,W,color='b',label='G+L') |
---|
445 | X = [] |
---|
446 | Y = [] |
---|
447 | Z = [] |
---|
448 | W = [] |
---|
449 | for peak in peaks: |
---|
450 | X.append(4.0*math.pi*sind(peak[0]/2.0)/lam) |
---|
451 | s = 1.17741*math.sqrt(peak[4])*math.pi/18000. |
---|
452 | g = peak[6]*math.pi/18000. |
---|
453 | G = gamFW(g,s) |
---|
454 | Y.append(s/tand(peak[0]/2.)) |
---|
455 | Z.append(g/tand(peak[0]/2.)) |
---|
456 | W.append(G/tand(peak[0]/2.)) |
---|
457 | pkwPlot.plot(X,Y,'+',color='r',label='G peak') |
---|
458 | pkwPlot.plot(X,Z,'+',color='g',label='L peak') |
---|
459 | pkwPlot.plot(X,W,'+',color='b',label='G+L peak') |
---|
460 | pkwPlot.legend(loc='best') |
---|
461 | pkwPage.canvas.draw() |
---|
462 | |
---|
463 | def PlotPatterns(self): |
---|
464 | |
---|
465 | def OnPick(event): |
---|
466 | if self.itemPicked is not None: return |
---|
467 | PatternId = self.PatternId |
---|
468 | PickId = self.PickId |
---|
469 | pick = event.artist |
---|
470 | mouse = event.mouseevent |
---|
471 | xpos = pick.get_xdata() |
---|
472 | ypos = pick.get_ydata() |
---|
473 | view = pdrPage.toolbar._views.forward() |
---|
474 | ind0 = np.searchsorted(xye[0],view[0][0]) |
---|
475 | ind = event.ind+ind0 |
---|
476 | xy = zip(xpos[ind],ypos[ind])[0] |
---|
477 | xm = mouse.xdata |
---|
478 | if self.PatternTree.GetItemText(PickId) == 'Peak List': |
---|
479 | if ind.all() != [0]: #picked a data point |
---|
480 | inst = self.PatternTree.GetItemPyData(G2gd.GetPatternTreeItemId(self,PatternId, 'Instrument Parameters')) |
---|
481 | if len(inst[1]) == 10: |
---|
482 | ins = inst[1][4:10] |
---|
483 | else: |
---|
484 | ins = inst[1][6:12] |
---|
485 | sig = ins[0]*tand(xy[0]/2.0)**2+ins[1]*tand(xy[0]/2.0)+ins[2] |
---|
486 | gam = ins[3]/cosd(xy[0]/2.0)+ins[4]*tand(xy[0]/2.0) |
---|
487 | data = self.PatternTree.GetItemPyData(self.PickId) |
---|
488 | XY = [xy[0],0, xy[1],1, sig,0, gam,0, ins[5],0] #default refine intensity 1st |
---|
489 | data.append(XY) |
---|
490 | G2gd.UpdatePeakGrid(self,data) |
---|
491 | else: #picked a peak list line |
---|
492 | self.itemPicked = pick |
---|
493 | elif self.PatternTree.GetItemText(PickId) == 'Limits': |
---|
494 | if ind.all() != [0]: #picked a data point |
---|
495 | LimitId = G2gd.GetPatternTreeItemId(self,PatternId, 'Limits') |
---|
496 | data = self.PatternTree.GetItemPyData(LimitId) |
---|
497 | if mouse.button==1: |
---|
498 | data[1][0] = min(xy[0],data[1][1]) |
---|
499 | if mouse.button==3: |
---|
500 | data[1][1] = max(xy[0],data[1][0]) |
---|
501 | self.PatternTree.SetItemPyData(LimitId,data) |
---|
502 | G2gd.UpdateLimitsGrid(self,data) |
---|
503 | else: #picked a limit line |
---|
504 | self.itemPicked = pick |
---|
505 | PlotPatterns(self) |
---|
506 | |
---|
507 | def OnPlotKeyPress(event): |
---|
508 | if event.key == 'w': |
---|
509 | if self.Weight: |
---|
510 | self.Weight = False |
---|
511 | else: |
---|
512 | self.Weight = True |
---|
513 | self.PlotPatterns() |
---|
514 | print 'plot weighting:',self.Weight |
---|
515 | if self.PatternTree.GetChildrenCount(self.root,False) > 1: |
---|
516 | if event.key == 'u' and self.Offset < 100.: |
---|
517 | self.Offset += 1. |
---|
518 | self.PlotPatterns() |
---|
519 | elif event.key == 'd' and self.Offset > 0.: |
---|
520 | self.Offset -= 1. |
---|
521 | self.PlotPatterns() |
---|
522 | elif event.key == 'c': |
---|
523 | print 'contouring' |
---|
524 | if self.Contour: |
---|
525 | self.Contour = False |
---|
526 | else: |
---|
527 | self.Contour = True |
---|
528 | PlotPatterns(self) |
---|
529 | else: |
---|
530 | event.Skip() |
---|
531 | |
---|
532 | def OnMotion(event): |
---|
533 | xpos = event.xdata |
---|
534 | if xpos: #avoid out of frame mouse position |
---|
535 | ypos = event.ydata |
---|
536 | pdrPage.canvas.SetCursor(wx.CROSS_CURSOR) |
---|
537 | self.G2plotNB.status.SetFields(['2-theta =%9.3f Intensity =%9.1f'%(xpos,ypos),]) |
---|
538 | if self.itemPicked: |
---|
539 | pdrPage.canvas.SetToolTipString('%9.3f'%(xpos)) |
---|
540 | |
---|
541 | def OnRelease(event): |
---|
542 | if self.itemPicked is None: return |
---|
543 | xpos = event.xdata |
---|
544 | if xpos: #avoid out of frame mouse position |
---|
545 | lines = [] |
---|
546 | for line in self.Lines: lines.append(line.get_xdata()[0]) |
---|
547 | lineNo = lines.index(self.itemPicked.get_xdata()[0]) |
---|
548 | if lineNo in [0,1]: |
---|
549 | LimitId = G2gd.GetPatternTreeItemId(self,self.PatternId, 'Limits') |
---|
550 | data = self.PatternTree.GetItemPyData(LimitId) |
---|
551 | data[1][lineNo] = xpos |
---|
552 | self.PatternTree.SetItemPyData(LimitId,data) |
---|
553 | if self.PatternTree.GetItemText(self.PickId) == 'Limits': |
---|
554 | G2gd.UpdateLimitsGrid(self,data) |
---|
555 | else: |
---|
556 | PeakId = G2gd.GetPatternTreeItemId(self,self.PatternId, 'Peak List') |
---|
557 | data = self.PatternTree.GetItemPyData(PeakId) |
---|
558 | data[lineNo-2][0] = xpos |
---|
559 | self.PatternTree.SetItemPyData(PeakId,data) |
---|
560 | G2gd.UpdatePeakGrid(self,data) |
---|
561 | PlotPatterns(self) |
---|
562 | self.itemPicked = None |
---|
563 | |
---|
564 | try: |
---|
565 | plotNum = self.G2plotNB.plotList.index('Powder Patterns') |
---|
566 | pdrPage = self.G2plotNB.nb.GetPage(plotNum) |
---|
567 | pdrPage.figure.clf() |
---|
568 | pdrPlot = pdrPage.figure.gca() |
---|
569 | pdrPage.toolbar._views = copy.deepcopy(pdrPage.views) |
---|
570 | except ValueError,error: |
---|
571 | pdrPlot = self.G2plotNB.add('Powder Patterns').gca() |
---|
572 | plotNum = self.G2plotNB.plotList.index('Powder Patterns') |
---|
573 | pdrPage = self.G2plotNB.nb.GetPage(plotNum) |
---|
574 | pdrPage.canvas.mpl_connect('key_press_event', OnPlotKeyPress) |
---|
575 | pdrPage.canvas.mpl_connect('motion_notify_event', OnMotion) |
---|
576 | pdrPage.canvas.mpl_connect('pick_event', OnPick) |
---|
577 | pdrPage.canvas.mpl_connect('button_release_event', OnRelease) |
---|
578 | pdrPage.xylim = 0 |
---|
579 | |
---|
580 | pdrPage.SetFocus() |
---|
581 | |
---|
582 | PickId = self.PickId |
---|
583 | PatternId = self.PatternId |
---|
584 | colors=['b','g','r','c','m','k'] |
---|
585 | Ymax = 1.0 |
---|
586 | PlotList = [] |
---|
587 | Lines = [] |
---|
588 | item, cookie = self.PatternTree.GetFirstChild(self.root) |
---|
589 | while item: |
---|
590 | if 'PWDR' in self.PatternTree.GetItemText(item): |
---|
591 | Pattern = self.PatternTree.GetItemPyData(item) |
---|
592 | Pattern.append(self.PatternTree.GetItemText(item)) |
---|
593 | PlotList.append(Pattern) |
---|
594 | item, cookie = self.PatternTree.GetNextChild(self.root, cookie) |
---|
595 | xlim = [0.,180.] |
---|
596 | xmin = 0. |
---|
597 | xmax = 180. |
---|
598 | ylim = [0.,1.0e12] |
---|
599 | for Pattern in PlotList: |
---|
600 | xye = Pattern[1] |
---|
601 | if pdrPage.xylim: |
---|
602 | xlim,ylim = pdrPage.xylim |
---|
603 | else: |
---|
604 | xlim = [xye[0][np.argmin(xye[0])],xye[0][np.argmax(xye[0])]] |
---|
605 | ylim = [xye[1][np.argmin(xye[1])],xye[1][np.argmax(xye[1])]] |
---|
606 | Ymax = max(Ymax,max(xye[1])) |
---|
607 | offset = self.Offset*Ymax/100.0 |
---|
608 | pdrPlot.set_title('Powder Patterns') |
---|
609 | pdrPlot.set_xlabel(r'$\mathsf{2\theta}$',fontsize=14) |
---|
610 | pdrPlot.set_ylabel('Intensity',fontsize=12) |
---|
611 | if self.Contour: |
---|
612 | ContourZ = [] |
---|
613 | ContourY = [] |
---|
614 | Nseq = 0 |
---|
615 | for Pattern in PlotList: |
---|
616 | ifpicked = False |
---|
617 | LimitId = 0 |
---|
618 | xye = Pattern[1] |
---|
619 | if PickId: |
---|
620 | ifpicked = Pattern[2] == self.PatternTree.GetItemText(PatternId) |
---|
621 | LimitId = G2gd.GetPatternTreeItemId(self,PatternId, 'Limits') |
---|
622 | N = PlotList.index(Pattern) |
---|
623 | X,Y = xye[0:2] |
---|
624 | Y += offset*N |
---|
625 | if LimitId: |
---|
626 | limits = self.PatternTree.GetItemPyData(LimitId) |
---|
627 | Lines.append(pdrPlot.axvline(limits[1][0],color='g',dashes=(5,5),picker=3)) |
---|
628 | Lines.append(pdrPlot.axvline(limits[1][1],color='r',dashes=(5,5),picker=3)) |
---|
629 | if self.Contour: |
---|
630 | ContourY.append(N) |
---|
631 | ContourZ.append(Y) |
---|
632 | ContourX = X |
---|
633 | Nseq += 1 |
---|
634 | pdrPlot.set_ylabel('Data sequence',fontsize=12) |
---|
635 | else: |
---|
636 | if ifpicked: |
---|
637 | Z = xye[3]+offset*N |
---|
638 | W = xye[4]+offset*N |
---|
639 | D = xye[5]+offset*N |
---|
640 | if self.Weight: |
---|
641 | W2 = np.sqrt(xye[2]) |
---|
642 | D *= W2 |
---|
643 | pdrPlot.plot(X,Y,colors[N%6]+'+',picker=3) |
---|
644 | pdrPlot.plot(X,Z,colors[(N+1)%6],picker=False) |
---|
645 | pdrPlot.plot(X,W,colors[(N+2)%6],picker=False) |
---|
646 | pdrPlot.plot(X,D,colors[(N+3)%6],picker=False) |
---|
647 | pdrPlot.axhline(0.,color=wx.BLACK) |
---|
648 | pdrPage.canvas.SetToolTipString('') |
---|
649 | if self.PatternTree.GetItemText(PickId) == 'Peak List': |
---|
650 | tip = 'On data point: Pick peak - L or R MB.On line: MB down to move' |
---|
651 | pdrPage.canvas.SetToolTipString(tip) |
---|
652 | data = self.PatternTree.GetItemPyData(G2gd.GetPatternTreeItemId(self,PatternId, 'Peak List')) |
---|
653 | for item in data: |
---|
654 | Lines.append(pdrPlot.axvline(item[0],color=colors[N%6],picker=2)) |
---|
655 | if self.PatternTree.GetItemText(PickId) == 'Limits': |
---|
656 | tip = 'On data point: Lower limit - L MB; Upper limit - R MB. On limit: MB down to move' |
---|
657 | pdrPage.canvas.SetToolTipString(tip) |
---|
658 | data = self.LimitsTable.GetData() |
---|
659 | else: |
---|
660 | pdrPlot.plot(xye[0],Y,colors[N%6],picker=False) |
---|
661 | if PickId and self.PatternTree.GetItemText(PickId) in ['Index Peak List','Unit Cells List']: |
---|
662 | peaks = self.PatternTree.GetItemPyData(G2gd.GetPatternTreeItemId(self,PatternId, 'Index Peak List')) |
---|
663 | for peak in peaks: |
---|
664 | pdrPlot.axvline(peak[0],color='b') |
---|
665 | for hkl in self.HKL: |
---|
666 | pdrPlot.axvline(hkl[5],color='r',dashes=(5,5)) |
---|
667 | if self.Contour: |
---|
668 | pdrPlot.contourf(ContourX,ContourY,ContourZ) |
---|
669 | pdrPlot.set_ylim(0,Nseq-1) |
---|
670 | self.Lines = Lines |
---|
671 | view = pdrPage.toolbar._views.forward() |
---|
672 | if view: |
---|
673 | pdrPlot.set_xlim(view[0][:2]) |
---|
674 | pdrPlot.set_ylim(view[0][2:]) |
---|
675 | self.Pwdr = True |
---|
676 | pdrPage.canvas.draw() |
---|
677 | |
---|
678 | def PlotPowderLines(self): |
---|
679 | |
---|
680 | def OnMotion(event): |
---|
681 | xpos = event.xdata |
---|
682 | if xpos: #avoid out of frame mouse position |
---|
683 | pksPage.canvas.SetCursor(wx.CROSS_CURSOR) |
---|
684 | self.G2plotNB.status.SetFields(['2-theta =%9.3f '%(xpos,),]) |
---|
685 | |
---|
686 | try: |
---|
687 | plotNum = self.G2plotNB.plotList.index('Powder Lines') |
---|
688 | pksPage = self.G2plotNB.nb.GetPage(plotNum) |
---|
689 | pksPage.figure.clf() |
---|
690 | pksPlot = pksPage.figure.gca() |
---|
691 | except ValueError,error: |
---|
692 | newPlot = True |
---|
693 | pksPlot = self.G2plotNB.add('Powder Lines').gca() |
---|
694 | plotNum = self.G2plotNB.plotList.index('Powder Lines') |
---|
695 | pksPage = self.G2plotNB.nb.GetPage(plotNum) |
---|
696 | pksPage.canvas.mpl_connect('motion_notify_event', OnMotion) |
---|
697 | |
---|
698 | pksPage.SetFocus() |
---|
699 | pksPlot.set_title('Powder Pattern Lines') |
---|
700 | pksPlot.set_xlabel(r'$\mathsf{2\theta}$',fontsize=14) |
---|
701 | PickId = self.PickId |
---|
702 | PatternId = self.PatternId |
---|
703 | peaks = self.PatternTree.GetItemPyData(G2gd.GetPatternTreeItemId(self,PatternId, 'Index Peak List')) |
---|
704 | for peak in peaks: |
---|
705 | pksPlot.axvline(peak[0],color='b') |
---|
706 | for hkl in self.HKL: |
---|
707 | pksPlot.axvline(hkl[5],color='r',dashes=(5,5)) |
---|
708 | xmin = peaks[0][0] |
---|
709 | xmax = peaks[-1][0] |
---|
710 | delt = xmax-xmin |
---|
711 | xlim = [max(0,xmin-delt/20.),min(180.,xmax+delt/20.)] |
---|
712 | pksPlot.set_xlim(xlim) |
---|
713 | pksPage.canvas.draw() |
---|
714 | |
---|