Coverage for /Users/Newville/Codes/xraylarch/larch/wxxrd/XRDCalibrationFrame.py: 12%

382 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-09 10:08 -0600

1#!/usr/bin/env pythonw 

2''' 

3popup for 2D XRD calibration 

4 

5''' 

6import os 

7import numpy as np 

8 

9import wx 

10 

11from wxmplot.imagepanel import ImagePanel 

12from larch.io import tifffile 

13from larch.xrd import lambda_from_E, E_from_lambda 

14from larch.utils import get_cwd 

15from .ImageControlsFrame import ImageToolboxFrame 

16 

17 

18HAS_pyFAI = False 

19try: 

20 import pyFAI 

21 import pyFAI.calibrant 

22 #from pyFAI.calibration import Calibration 

23 HAS_pyFAI = True 

24except ImportError: 

25 pass 

26 

27 

28################################### 

29 

30class CalibrationPopup(wx.Frame): 

31 

32 def __init__(self,parent): 

33 

34 self.frame = wx.Frame.__init__(self, parent, title='Calibration',size=(900,700)) 

35 

36 self.parent = parent 

37 

38 self.statusbar = self.CreateStatusBar(2,wx.CAPTION ) 

39 self.default_cal = 0 

40 self.default_det = 0 

41 self.img_fname = '' 

42 

43 try: 

44 self.raw_img = parent.plt_img ## raw_img or flp_img or plt_img mkak 2016.10.28 

45 self.img_fname = 'Image from diFFit2D viewer.' 

46 except: 

47 self.raw_img = np.zeros((1024,1024)) 

48 

49 self.Init() 

50 self.Show() 

51 

52# wx.Window.GetEffectiveMinSize 

53# wx.GetBestSize(self) 

54 

55 self.setDefaults() 

56 

57 

58 

59 def Init(self): 

60 

61 self.panel = wx.Panel(self) 

62 

63 self.DirectionsSizer() 

64 self.MainSizer() 

65# self.OKsizer() 

66 

67 self.framebox = wx.BoxSizer(wx.VERTICAL) 

68 self.framebox.Add(self.dirbox, flag=wx.ALL|wx.EXPAND, border=10) 

69 self.framebox.Add(self.mainbox, flag=wx.ALL|wx.EXPAND, border=10) 

70# self.framebox.Add(self.okbox, flag=wx.ALL|wx.ALIGN_RIGHT, border=10) 

71 

72 ########################### 

73 ## Pack all together in self.panel 

74 self.panel.SetSizer(self.framebox) 

75 

76 ########################### 

77 ## Set default information 

78 self.stepno = 0 

79 self.checkRANGE() 

80 self.showDirection() 

81 

82 def setDefaults(self): 

83 

84 ## Sets some typical defaults specific to GSE 13-ID procedure 

85 self.entr_pix.SetValue('400') ## binned pixels (2x200um) 

86 self.entr_EorL.SetValue('19.0') ## 19.0 keV 

87 self.entr_dist.SetValue('0.5') ## 0.5 m 

88 self.ch_det.SetSelection(self.default_det) ## Perkin detector 

89 self.ch_cal.SetSelection(self.default_cal) ## CeO2 

90 self.entr_calimg.SetValue(self.img_fname) 

91 

92 self.entr_cntrx.SetValue(str(int(self.raw_img.shape[0]/2))) ## x-position of beam 

93 self.entr_cntry.SetValue(str(int(self.raw_img.shape[1]/2))) ## y-position of beam 

94 

95 self.onDorPSel() 

96 

97 def DirectionsSizer(self): 

98 

99 ########################### 

100 ## Directions 

101 dirbx = wx.StaticBox(self.panel,label='DIRECTIONS', size=(100, 50)) 

102 self.dirbox = wx.StaticBoxSizer(dirbx,wx.VERTICAL) 

103 

104 hbox_direct = wx.BoxSizer(wx.HORIZONTAL) 

105 self.followdir = wx.StaticText(self.panel,label='') 

106 

107 #hbox_direct.Add(self.txt_shp, flag=wx.RIGHT, border=8) 

108 hbox_direct.Add(self.followdir, flag=wx.ALL|wx.EXPAND, border=8) 

109 

110 self.dirbox.Add(hbox_direct, flag=wx.ALL|wx.EXPAND, border=10) 

111 

112 hbox_next = wx.BoxSizer(wx.HORIZONTAL) 

113 self.btn_prev = wx.Button(self.panel,label='PREVIOUS') 

114 self.btn_next = wx.Button(self.panel,label='NEXT') 

115 

116 self.btn_prev.Bind(wx.EVT_BUTTON,self.onPREVIOUS) 

117 self.btn_next.Bind(wx.EVT_BUTTON,self.onNEXT) 

118 

119 hbox_next.Add(self.btn_prev, flag=wx.ALL, border=8) 

120 hbox_next.Add((-1, 100)) 

121 hbox_next.Add(self.btn_next, flag=wx.ALIGN_RIGHT|wx.ALL, border=8) 

122 

123 self.dirbox.Add(hbox_next, flag=wx.ALL|wx.EXPAND, border=10) 

124 

125 def MainSizer(self): 

126 

127 self.mainbox = wx.BoxSizer(wx.VERTICAL) 

128 

129 ########################### 

130 ## -----> Main Panel 

131 self.hmain = wx.BoxSizer(wx.HORIZONTAL) 

132 

133 self.ImageSizer() 

134 self.ParameterSizer() 

135 

136 self.hmain.Add(self.imagebox,proportion=1,flag=wx.ALL|wx.EXPAND, border=10) 

137 self.hmain.Add(self.parbox, flag=wx.ALL, border=10) 

138 

139 self.mainbox.Add(self.hmain, flag=wx.ALL|wx.EXPAND, border=10) 

140 

141 def ParameterSizer(self): 

142 ''' 

143 This is where the parameters will be. 

144 ''' 

145 

146 #self.parbox = wx.BoxSizer(wx.VERTICAL) 

147 prbx = wx.StaticBox(self.panel,label='PARAMETERS', size=(50, 100)) 

148 self.parbox = wx.StaticBoxSizer(prbx,wx.VERTICAL) 

149 

150 ########################### 

151 ## Establish lists from pyFAI 

152 clbrnts = [] #['None'] 

153 self.dets = [] #['None'] 

154 for key,value in pyFAI.detectors.ALL_DETECTORS.items(): 

155 self.dets.append(key) 

156 if key == 'perkin': 

157 self.default_det = len(self.dets)-1 

158 for key,value in pyFAI.calibrant.ALL_CALIBRANTS.items(): 

159 clbrnts.append(key) 

160 if key == 'CeO2': 

161 self.default_cal = len(clbrnts)-1 

162 

163 

164 ##### 

165 ## Calibration Image selection 

166 hbox_cal1 = wx.BoxSizer(wx.HORIZONTAL) 

167 ttl_calimg = wx.StaticText(self.panel, label='Calibration Image:' ) 

168 self.entr_calimg = wx.TextCtrl(self.panel, size=(210, -1)) 

169# btn_calimg = wx.Button(self.panel, label='Browse...') 

170 

171# btn_calimg.Bind(wx.EVT_BUTTON, self.loadIMAGE) 

172 

173 hbox_cal1.Add(ttl_calimg, flag=wx.RIGHT, border=8) 

174 hbox_cal1.Add(self.entr_calimg, flag=wx.RIGHT|wx.EXPAND, border=8) 

175# hbox_cal1.Add(btn_calimg, flag=wx.RIGHT, border=8) 

176 self.parbox.Add(hbox_cal1, flag=wx.BOTTOM|wx.TOP, border=8) 

177 

178 btn_calimg = wx.Button(self.panel, label='Browse...') 

179 btn_calimg.Bind(wx.EVT_BUTTON, self.loadIMAGE) 

180 self.parbox.Add(btn_calimg, flag=wx.BOTTOM|wx.ALIGN_RIGHT, border=8) 

181 

182 ##### 

183 ## Calibrant selection 

184 hbox_cal2 = wx.BoxSizer(wx.HORIZONTAL) 

185 ttl_cal = wx.StaticText(self.panel, label='Calibrant:') 

186 self.ch_cal = wx.Choice(self.panel,choices=clbrnts) 

187 

188 self.ch_cal.Bind(wx.EVT_CHOICE, self.onCalSel) 

189 

190 hbox_cal2.Add(ttl_cal, flag=wx.RIGHT, border=8) 

191 hbox_cal2.Add(self.ch_cal, flag=wx.RIGHT, border=8) 

192 self.parbox.Add(hbox_cal2, flag=wx.BOTTOM, border=30) 

193 

194 ##### 

195 ## Set-up specific parameters 

196 hbox_cal3 = wx.BoxSizer(wx.HORIZONTAL) 

197 txt_exp = wx.StaticText(self.panel, label='SET-UP PARAMETERS') 

198 btn_pni = wx.Button(self.panel, label='Load file') 

199 

200 btn_pni.Bind(wx.EVT_BUTTON, self.openPONI) 

201 

202 hbox_cal3.Add(txt_exp, flag=wx.RIGHT, border=8) 

203 hbox_cal3.Add(btn_pni, flag=wx.LEFT, border=60) 

204 self.parbox.Add(hbox_cal3, flag=wx.BOTTOM, border=8) 

205 

206 ##### 

207 ## Detector selection 

208 hbox_cal4 = wx.BoxSizer(wx.HORIZONTAL) 

209 self.ch_DorP = wx.Choice(self.panel,choices=['Detector name','Pixel size (um)']) 

210 self.ch_det = wx.Choice(self.panel, choices=self.dets) 

211 self.entr_pix = wx.TextCtrl(self.panel, size=(110, -1)) 

212 

213 self.ch_det.Bind(wx.EVT_CHOICE, self.onDetSel) 

214 self.ch_DorP.Bind(wx.EVT_CHOICE, self.onDorPSel) 

215 

216 hbox_cal4.Add(self.ch_DorP, flag=wx.RIGHT, border=8) 

217 hbox_cal4.Add(self.ch_det, flag=wx.RIGHT, border=8) 

218 hbox_cal4.Add(self.entr_pix, flag=wx.RIGHT, border=8) 

219 self.parbox.Add(hbox_cal4, flag=wx.BOTTOM, border=8) 

220 

221 ##### 

222 ## Energy or Wavelength 

223 hbox_cal5 = wx.BoxSizer(wx.HORIZONTAL) 

224 self.ch_EorL = wx.Choice(self.panel,choices=['Energy (keV)','Wavelength (A)']) 

225 self.entr_EorL = wx.TextCtrl(self.panel, size=(110, -1)) 

226 

227 self.ch_EorL.Bind(wx.EVT_CHOICE, self.onEorLSel) 

228 

229 hbox_cal5.Add(self.ch_EorL, flag=wx.RIGHT, border=8) 

230 hbox_cal5.Add(self.entr_EorL, flag=wx.RIGHT, border=8) 

231 self.parbox.Add(hbox_cal5, flag=wx.BOTTOM, border=8) 

232 

233 ## Distance 

234 hbox_cal6 = wx.BoxSizer(wx.HORIZONTAL) 

235 ttl_dist = wx.StaticText(self.panel, label='Detector distance (m):') 

236 self.entr_dist = wx.TextCtrl(self.panel, size=(110, -1)) 

237 

238 hbox_cal6.Add(ttl_dist, flag=wx.RIGHT, border=8) 

239 hbox_cal6.Add(self.entr_dist, flag=wx.RIGHT, border=8) 

240 

241 self.parbox.Add(hbox_cal6, flag=wx.BOTTOM, border=8) 

242 

243 ## Beam center x 

244 hbox_cal7 = wx.BoxSizer(wx.HORIZONTAL) 

245 ttl_cntrx = wx.StaticText(self.panel, label='Beam center, x (pixels):') 

246 self.entr_cntrx = wx.TextCtrl(self.panel, size=(110, -1)) 

247 

248 hbox_cal7.Add(ttl_cntrx, flag=wx.RIGHT, border=8) 

249 hbox_cal7.Add(self.entr_cntrx, flag=wx.RIGHT, border=8) 

250 

251 self.parbox.Add(hbox_cal7, flag=wx.BOTTOM, border=8) 

252 

253 ## Beam center y 

254 hbox_cal8 = wx.BoxSizer(wx.HORIZONTAL) 

255 ttl_cntry = wx.StaticText(self.panel, label='Beam center, y (pixels):') 

256 self.entr_cntry = wx.TextCtrl(self.panel, size=(110, -1)) 

257 

258 hbox_cal8.Add(ttl_cntry, flag=wx.RIGHT, border=8) 

259 hbox_cal8.Add(self.entr_cntry, flag=wx.RIGHT, border=8) 

260 

261 self.parbox.Add(hbox_cal8, flag=wx.BOTTOM, border=8) 

262 

263 

264 def onCalSel(self,event=None): 

265 print('Selected calibrant: %s' % self.ch_cal.GetString(self.ch_cal.GetSelection())) 

266 

267 def onDetSel(self,event=None): 

268 print('Selected detector: %s' % self.ch_det.GetString(self.ch_det.GetSelection())) 

269 

270 

271 def onEorLSel(self,event=None): 

272 

273 if self.ch_EorL.GetSelection() == 1: 

274 energy = float(self.entr_EorL.GetValue()) ## units keV 

275 wavelength = lambda_from_E(energy) ## units: A 

276 self.entr_EorL.SetValue(str(wavelength)) 

277 else: 

278 wavelength = float(self.entr_EorL.GetValue())*1e-10 ## units: m 

279 energy = E_from_lambda(wavelength) ## units: keV 

280 self.entr_EorL.SetValue(str(energy)) 

281 

282 def onDorPSel(self,event=None): 

283 if self.ch_DorP.GetSelection() == 0: 

284 self.entr_pix.Hide() 

285 self.ch_det.Show() 

286 else: 

287 self.ch_det.Hide() 

288 self.entr_pix.Show() 

289 

290 self.panel.GetSizer().Layout() 

291 self.panel.GetParent().Layout() 

292 

293 def loadIMAGE(self,event=None): 

294 wildcards = 'XRD image (*.edf,*.tif,*.tiff)|*.tif;*.tiff;*.edf|All files (*.*)|*.*' 

295 if os.path.exists(self.entr_calimg.GetValue()): 

296 dfltDIR = self.entr_calimg.GetValue() 

297 else: 

298 dfltDIR = get_cwd() 

299 

300 dlg = wx.FileDialog(self, message='Choose XRD calibration file', 

301 defaultDir=dfltDIR, 

302 wildcard=wildcards, style=wx.FD_OPEN) 

303 

304 path, read = None, False 

305 if dlg.ShowModal() == wx.ID_OK: 

306 read = True 

307 path = dlg.GetPath().replace('\\', '/') 

308 dlg.Destroy() 

309 

310 if read: 

311 try: 

312# self.raw_img = plt.imread(path) 

313 self.raw_img = tifffile.imread(path) 

314 #self.raw_img = fabio.open(path).data 

315 except: 

316 print('Image not properly opened.') 

317 pass 

318 self.plot2Dimg.display(self.raw_img) 

319 self.plot2Dimg.redraw() 

320 self.AutoContrast() 

321 

322 self.entr_calimg.Clear() 

323 self.entr_calimg.SetValue(path) #os.path.split(path)[-1] 

324 

325 

326 def ImageSizer(self): 

327 ''' 

328 Image Panel 

329 ''' 

330 self.imagebox = wx.BoxSizer(wx.VERTICAL) 

331 

332 self.plot2Dimage() 

333 

334 self.btn_image = wx.Button(self.panel,label='IMAGE TOOLS') 

335 

336 self.btn_image.Bind(wx.EVT_BUTTON,self.onImageTools) 

337 

338 self.imagebox.Add(self.plot2Dimg,proportion=1,flag=wx.ALL|wx.EXPAND, border=10) 

339 self.imagebox.Add(self.btn_image, flag=wx.ALL, border=10) 

340 

341# def OKsizer(self): 

342# ########################### 

343# ## OK - CANCEL 

344# self.okbox = wx.BoxSizer(wx.HORIZONTAL) 

345# 

346# okBtn = wx.Button(self.panel, wx.ID_OK ) 

347# canBtn = wx.Button(self.panel, wx.ID_CANCEL ) 

348# 

349# self.okbox.Add(canBtn, flag=wx.RIGHT, border=5) 

350# self.okbox.Add(okBtn, flag=wx.RIGHT, border=5) 

351 

352 def write_message(self, s, panel=0): 

353 """write a message to the Status Bar""" 

354 self.SetStatusText(s, panel) 

355 

356 def onImageTools(self,event=None): 

357 

358 self.toolbox = ImageToolboxFrame(self.plot2Dimg,self.raw_img) 

359 

360 def plot2Dimage(self): 

361 

362 self.plot2Dimg = ImagePanel(self.panel,size=(300, 300)) 

363 self.plot2Dimg.messenger = self.write_message 

364 

365 self.plot2Dimg.display(self.raw_img) 

366 self.AutoContrast() 

367 

368 self.plot2Dimg.redraw() 

369 

370 def AutoContrast(self): 

371 

372 self.minINT = int(np.min(self.raw_img)) 

373 self.maxINT = int(np.max(self.raw_img)/15) # /15 scales image to viewable 

374 if self.maxINT == self.minINT: 

375 self.minINT = self.minINT-50 

376 self.maxINT = self.minINT+100 

377 

378 self.minCURRENT = self.minINT 

379 self.maxCURRENT = self.maxINT 

380 if self.maxCURRENT > self.maxINT: 

381 self.maxCURRENT = self.maxINT 

382 

383 self.plot2Dimg.conf.auto_intensity = False 

384 self.plot2Dimg.conf.int_lo[0] = self.minCURRENT 

385 self.plot2Dimg.conf.int_hi[0] = self.maxCURRENT 

386# self.plot2Dimg.conf.int_lo['int'] = self.minCURRENT 

387# self.plot2Dimg.conf.int_hi['int'] = self.maxCURRENT 

388 

389 ## vertical flip default 

390 self.plot2Dimg.conf.flip_ud = True 

391 self.plot2Dimg.conf.flip_lr = False 

392 

393 self.plot2Dimg.redraw() 

394 

395 def checkRANGE(self): 

396 

397 if self.stepno <= 0: 

398 self.stepno = 0 

399 self.btn_prev.Disable() 

400 else: 

401 self.btn_prev.Enable() 

402 

403 if self.stepno >= 8: 

404 self.stepno = 8 

405 self.btn_next.Disable() 

406 else: 

407 self.btn_next.Enable() 

408 

409 def onNEXT(self,event=None): 

410 self.stepno = self.stepno + 1 

411 self.checkRANGE() 

412 self.showDirection() 

413 

414 def onPREVIOUS(self,event=None): 

415 self.stepno = self.stepno - 1 

416 self.checkRANGE() 

417 self.showDirection() 

418 

419 def showDirection(self): 

420 

421 dirsteps = ['Enter parameters into the fields below.', 

422 'Select point(s) on the first ring.', 

423 'Select point(s) on the second ring.', 

424 'Select point(s) on the third ring.', 

425 'Select point(s) on the fourth ring.', 

426 'Select point(s) on the fifth ring.', 

427 'Select point(s) on the sixth ring.', 

428 'Check preliminary calibration. Continue for final refinement.', 

429 'Refinement complete.' ] 

430 

431 self.followdir.SetLabel(dirsteps[self.stepno]) 

432 

433 def openPONI(self,event=None): 

434 

435 wildcards = 'pyFAI calibration file (*.poni)|*.poni|All files (*.*)|*.*' 

436 dlg = wx.FileDialog(self, message='Choose pyFAI calibration file', 

437 defaultDir=get_cwd(), 

438 wildcard=wildcards, style=wx.FD_OPEN) 

439 

440 path, read = None, False 

441 if dlg.ShowModal() == wx.ID_OK: 

442 read = True 

443 path = dlg.GetPath().replace('\\', '/') 

444 dlg.Destroy() 

445 

446 if read: 

447 

448 try: 

449 print 

450 self.ai = pyFAI.load(path) 

451 print('Loading calibration file: %s' % path) 

452 except: 

453 print('Not recognized as a pyFAI calibration file: %s' % path) 

454 return 

455 

456 ## Sets viewer to values in .poni file 

457 self.entr_dist.SetValue('%0.4f' % self.ai._dist) 

458 self.entr_pix.SetValue('%0.1f' % float(self.ai.detector.pixel1*1000000.)) 

459 self.ch_DorP.SetSelection(1) 

460 self.entr_EorL.SetValue('%0.4f' % float(self.ai._wavelength*1.e10)) 

461 self.ch_EorL.SetSelection(1) 

462 self.onDorPSel() 

463 

464 cenx = float(self.ai._poni1)/float(self.ai.detector.pixel1) 

465 ceny = float(self.ai._poni2)/float(self.ai.detector.pixel2) 

466 self.entr_cntrx.SetValue('%0.3f' % cenx) 

467 self.entr_cntry.SetValue('%0.3f' % ceny) 

468 

469class CalXRD(wx.Dialog): 

470 """""" 

471 

472 #---------------------------------------------------------------------- 

473 def __init__(self): 

474 

475 if HAS_pyFAI: 

476 ## Constructor 

477 dialog = wx.Dialog.__init__(self, None, title='XRD Calibration',size=(460, 440)) 

478 ## remember: size=(width,height) 

479 self.panel = wx.Panel(self) 

480 

481 self.InitUI() 

482 self.Centre() 

483 self.Show() 

484 

485 ## Sets some typical defaults specific to GSE 13-ID procedure 

486 self.pixel.SetValue('400') ## binned pixels (2x200um) 

487 self.EorL.SetValue('19.0') ## 19.0 keV 

488 self.Distance.SetValue('0.5') ## 0.5 m 

489 self.detslct.SetSelection(22) ## Perkin detector 

490 self.calslct.SetSelection(20) ## CeO2 

491 

492 if self.slctDorP.GetSelection() == 0: 

493 self.sizer.Hide(self.pixel) 

494 

495 ## Do not need flags if defaults are set 

496 #self.FlagCalibrant = False 

497 #self.FlagDetector = False 

498 self.FlagCalibrant = True 

499 self.FlagDetector = True 

500 

501 else: 

502 print('pyFAI must be available for calibration.') 

503 return 

504 

505 def InitUI(self): 

506 

507 

508 ## Establish lists from pyFAI 

509 clbrnts = [] #['None'] 

510 self.dets = [] #['None'] 

511 for key,value in pyFAI.detectors.ALL_DETECTORS.items(): 

512 self.dets.append(key) 

513 for key,value in pyFAI.calibrant.ALL_CALIBRANTS.items(): 

514 clbrnts.append(key) 

515 self.CaliPath = None 

516 

517 

518 ## Calibration Image selection 

519 caliImg = wx.StaticText(self.panel, label='Calibration Image:' ) 

520 self.calFil = wx.TextCtrl(self.panel, size=(190, -1)) 

521 fileBtn1 = wx.Button(self.panel, label='Browse...' ) 

522 

523 ## Calibrant selection 

524 self.calslct = wx.Choice(self.panel,choices=clbrnts) 

525 CalLbl = wx.StaticText(self.panel, label='Calibrant:' ,style=LEFT) 

526 

527 ## Detector selection 

528 self.slctDorP = wx.Choice(self.panel,choices=['Detector','Pixel size (um)']) 

529 self.detslct = wx.Choice(self.panel, choices=self.dets) 

530 self.pixel = wx.TextCtrl(self.panel, size=(140, -1)) 

531 

532 ## Energy or Wavelength 

533 self.slctEorL = wx.Choice(self.panel,choices=['Energy (keV)','Wavelength (A)']) 

534 self.EorL = wx.TextCtrl(self.panel, size=(140, -1)) 

535 

536 ## Refine label 

537 RefLbl = wx.StaticText(self.panel, label='To be refined...' ,style=LEFT) 

538 

539 ## Distance 

540 self.Distance = wx.TextCtrl(self.panel, size=(140, -1)) 

541 DstLbl = wx.StaticText(self.panel, label='Distance (m):' ,style=LEFT) 

542 

543 hlpBtn = wx.Button(self.panel, wx.ID_HELP ) 

544 okBtn = wx.Button(self.panel, wx.ID_OK ) 

545 canBtn = wx.Button(self.panel, wx.ID_CANCEL ) 

546 

547 self.Bind(wx.EVT_BUTTON, self.onBROWSE1, fileBtn1 ) 

548 self.calslct.Bind(wx.EVT_CHOICE, self.onCalSel) 

549 self.detslct.Bind(wx.EVT_CHOICE, self.onDetSel) 

550 self.slctDorP.Bind(wx.EVT_CHOICE, self.onDorPSel) 

551 self.slctEorL.Bind(wx.EVT_CHOICE, self.onEorLSel) 

552 

553 self.sizer = wx.GridBagSizer(3, 3) 

554 

555 self.sizer.Add(caliImg, pos = ( 1,1) ) 

556 self.sizer.Add(self.calFil, pos = ( 1,2), span = (1,2) ) 

557 self.sizer.Add(fileBtn1, pos = ( 1,4) ) 

558 self.sizer.Add(CalLbl, pos = ( 3,1) ) 

559 self.sizer.Add(self.calslct, pos = ( 3,2), span = (1,2) ) 

560 

561 self.sizer.Add(self.slctDorP, pos = ( 4,1) ) 

562 self.sizer.Add(self.detslct, pos = ( 4,2), span = (1,4) ) 

563 self.sizer.Add(self.pixel, pos = ( 5,2), span = (1,2) ) 

564 

565 self.sizer.Add(self.slctEorL, pos = ( 6,1) ) 

566 self.sizer.Add(self.EorL, pos = ( 6,2), span = (1,2) ) 

567 

568 self.sizer.Add(RefLbl, pos = ( 8,1) ) 

569 self.sizer.Add(DstLbl, pos = ( 9,1) ) 

570 self.sizer.Add(self.Distance, pos = ( 9,2), span = (1,2) ) 

571 

572 self.sizer.Add(hlpBtn, pos = (11,1) ) 

573 self.sizer.Add(canBtn, pos = (11,2) ) 

574 self.sizer.Add(okBtn, pos = (11,3) ) 

575 

576 self.FindWindowById(wx.ID_OK).Disable() 

577 

578 self.panel.SetSizer(self.sizer) 

579 

580 

581 def onCalSel(self,event=None): 

582 #if self.calslct.GetSelection() == 0: 

583 # self.FlagCalibrant = False 

584 #else: 

585 # self.FlagCalibrant = True 

586 self.checkOK() 

587 

588 def onDetSel(self,event=None): 

589 #if self.detslct.GetSelection() == 0: 

590 # self.FlagDetector = False 

591 #else: 

592 # self.FlagDetector = True 

593 self.checkOK() 

594 

595 def onCheckOK(self,event=None): 

596 self.checkOK() 

597 

598 def checkOK(self): 

599 if self.FlagCalibrant and self.CaliPath is not None: 

600 if self.slctDorP.GetSelection() == 1: 

601 self.FindWindowById(wx.ID_OK).Enable() 

602 else: 

603 if self.FlagDetector: 

604 self.FindWindowById(wx.ID_OK).Enable() 

605 else: 

606 self.FindWindowById(wx.ID_OK).Disable() 

607 else: 

608 self.FindWindowById(wx.ID_OK).Disable() 

609 

610 def onEorLSel(self,event=None): 

611 

612 if self.slctEorL.GetSelection() == 1: 

613 energy = float(self.EorL.GetValue()) ## units keV 

614 wavelength = lambda_from_E(energy) ## units: A 

615 self.EorL.SetValue(str(wavelength)) 

616 else: 

617 wavelength = float(self.EorL.GetValue()) ## units: A 

618 energy = E_from_lambda(wavelength) ## units: keV 

619 self.EorL.SetValue(str(energy)) 

620 

621 self.checkOK() 

622 

623 def onDorPSel(self,event=None): 

624 if self.slctDorP.GetSelection() == 0: 

625 self.sizer.Hide(self.pixel) 

626 self.sizer.Show(self.detslct) 

627 else: 

628 self.sizer.Hide(self.detslct) 

629 self.sizer.Show(self.pixel) 

630 

631 self.checkOK() 

632 

633 def onBROWSE1(self,event=None): 

634 wildcards = 'XRD image (*.edf,*.tif,*.tiff)|*.tif;*.tiff;*.edf|All files (*.*)|*.*' 

635 if os.path.exists(self.calFil.GetValue()): 

636 dfltDIR = self.calFil.GetValue() 

637 else: 

638 dfltDIR = get_cwd() 

639 

640 dlg = wx.FileDialog(self, message='Choose XRD calibration file', 

641 defaultDir=dfltDIR, 

642 wildcard=wildcards, style=wx.FD_OPEN) 

643 

644 path, read = None, False 

645 if dlg.ShowModal() == wx.ID_OK: 

646 read = True 

647 path = dlg.GetPath().replace('\\', '/') 

648 dlg.Destroy() 

649 

650 if read: 

651 self.calFil.Clear() 

652 self.calFil.SetValue(os.path.split(path)[-1]) 

653 self.CaliPath = path 

654 self.checkOK() 

655 

656# # # # # # # WAS IN mapviewer.py ; needs to be corrected or removed 

657# 

658# # # # def onCalXRD(self, evt=None): 

659# # # # """ 

660# # # # Perform calibration with pyFAI 

661# # # # mkak 2016.09.16 

662# # # # """ 

663# # # # 

664# # # # ### can this pop up pyFAI or Dioptas GUI instead of creating own? 

665# # # # 

666# # # # myDlg = CalXRD() 

667# # # # 

668# # # # path, read = None, False 

669# # # # if myDlg.ShowModal() == wx.ID_OK: 

670# # # # read = True 

671# # # # 

672# # # # myDlg.Destroy() 

673# # # # 

674# # # # if read: 

675# # # # 

676# # # # usr_calimg = myDlg.CaliPath 

677# # # # 

678# # # # if myDlg.slctEorL.GetSelection() == 1: 

679# # # # usr_lambda = float(myDlg.EorL.GetValue())*1e-10 ## units: m 

680# # # # usr_E = E_from_lambda(usr_lambda,lambda_units='m') ## units: keV 

681# # # # else: 

682# # # # usr_E = float(myDlg.EorL.GetValue()) ## units keV 

683# # # # usr_lambda = lambda_from_E(usr_E,lambda_units='m') ## units: m 

684# # # # 

685# # # # if myDlg.slctDorP.GetSelection() == 1: 

686# # # # usr_pixel = float(myDlg.pixel.GetValue())*1e-6 

687# # # # else: 

688# # # # usr_det = myDlg.detslct.GetString(myDlg.detslct.GetSelection()) 

689# # # # usr_clbrnt = myDlg.calslct.GetString(myDlg.calslct.GetSelection()) 

690# # # # usr_dist = float(myDlg.Distance.GetValue()) 

691# # # # 

692# # # # verbose = True #False 

693# # # # if verbose: 

694# # # # print('\n=== Calibration input ===') 

695# # # # print('XRD image: %s' % usr_calimg) 

696# # # # print('Calibrant: %s' % usr_clbrnt) 

697# # # # if myDlg.slctDorP.GetSelection() == 1: 

698# # # # print('Pixel size: %0.1f um' % (usr_pixel*1e6)) 

699# # # # else: 

700# # # # print('Detector: %s' % usr_det) 

701# # # # print('Incident energy: %0.2f keV (%0.4f A)' % (usr_E,usr_lambda*1e10)) 

702# # # # print('Starting distance: %0.3f m' % usr_dist) 

703# # # # print('=========================\n') 

704# # # # 

705# # # # ## Adapted from pyFAI-calib 

706# # # # ## note: -l:units mm; -dist:units m 

707# # # # ## mkak 2016.09.19 

708# # # # 

709# # # # if myDlg.slctDorP.GetSelection() == 1: 

710# # # # pform1 = 'pyFAI-calib -c %s -p %s -e %0.1f -dist %0.3f %s' 

711# # # # command1 = pform1 % (usr_clbrnt,usr_pixel,usr_E,usr_dist,usr_calimg) 

712# # # # else: 

713# # # # pform1 = 'pyFAI-calib -c %s -D %s -e %0.1f -dist %0.3f %s' 

714# # # # command1 = pform1 % (usr_clbrnt,usr_det,usr_E,usr_dist,usr_calimg) 

715# # # # pform2 = 'pyFAI-recalib -i %s -c %s %s' 

716# # # # command2 = pform2 % (usr_calimg.split('.')[0]+'.poni',usr_clbrnt,usr_calimg) 

717# # # # 

718# # # # if verbose: 

719# # # # print('\nNot functioning within code yet... but you could execute:') 

720# # # # print('\t $ %s' % command1) 

721# # # # print('\t $ %s\n\n' % command2) 

722 

723 

724 

725 

726# class diFFit_XRDcal(wx.App): 

727# def __init__(self): 

728# wx.App.__init__(self) 

729# 

730# def run(self): 

731# self.MainLoop() 

732# 

733# def createApp(self): 

734# frame = CalibrationPopup() 

735# frame.Show() 

736# self.SetTopWindow(frame) 

737# 

738# def OnInit(self): 

739# self.createApp() 

740# return True 

741# 

742 

743# class DebugViewer(diFFit_XRDcal): 

744# def __init__(self, **kws): 

745# diFFit_XRDcal.__init__(self, **kws) 

746# 

747# def OnInit(self): 

748# #self.Init() 

749# self.createApp() 

750# #self.ShowInspectionTool() 

751# return True 

752# 

753# if __name__ == '__main__': 

754# diFFit_XRDcal().run()