1 """
2 A panel to draw the profile
3 """
4
5 import os
6 import wx
7 import wx.aui
8 import numpy
9 import copy
10
11
12 from reflInteractor import ReflectometryInteractor
13 from fit import Fit
14 from listener import Listener
15
16 from reflutils import showErrorMsg, clear_axes, showWarningMsg, \
17 decodeP, filterP, CheckValid
18
19 from addLayerDialog import AddLayerDialog
20 from changeProfileDialog import ChangeProfileDialog
21 from changeLayernameDialog import ChangeLayernameDialog
22 from changeFigureTitle import ChangeFigureTitle
23 from changeMetaDataDialog import ChangeMetaDataDialog
24 from initModelDialog import InitModelDialog,InitModelDialogNM
25 from removeLayerDialog import RemoveLayerDialog
26 from saveModelDialog import SaveModelDialog, SaveModelDialog2
27 from addData2XML import addStrData
28 from getData2XML import getStrData
29 from str2Data import load_inline, Data2Str
30
31
32 import parseReflModel
33 from parseReflModel import ReflModel, ParkFit
34 from plotReflBase import PlotReflBase
35
36
37 from staj2model import Staj2Model, Staj2MModel
38
39 from miscRefl import lookUpModelName, parseProfile, parseProfileValue, \
40 parseStaj, getDataTxt, parseMStaj, _toPyScriptM,\
41 _toPyScript, isStajModelType
42
43 from reflectometry.model1d.adaptor.reflTheory import ReflParameter, \
44 ReflParameterMag
45 from reflectometry.model1d.model.profile import Profile
46 from reflectometry.model1d.model.layer import Layer
47 from reflectometry.model1d.model.bspline import BSpline, spline, slope, \
48 Slope, bspline
49 from reflectometry.model1d.model.tetheredPolymer import tetheredPolymer, \
50 TetheredPolymer, brush, Brush, BRUSH
51
52 from reflectometry.model1d.model.auxs import isvector, tFloat, isfactory, \
53 clipboard,CopyImage
54
55
56 from park.parkAui.common.parkEvent import MODEL_UPDATE, MODEL_ADD, \
57 MODEL_CLEAR, MODEL_UPDATE_META
58
59
60
61 TIMING = False
62
63
64
65 [
66 wxID_Init_Model,
67 wxID_Init_Model_NM,
68 wxID_Load_Model,
69 wxID_Load_Model_From_XML,
70 wxID_Add_Layer,
71 wxID_Remove_Layer,
72 wxID_Save_Model,
73 wxID_Export_Model,
74 wxID_Export_Model_Staj,
75 wxID_Update_Model,
76 wxID_Clear_Model,
77 wxID_Save_Fig,
78 wxID_Copy_Figure,
79 wxID_Copy_Profile,
80 wxID_Copy_Model,
81 wxID_Change_LayerName,
82 wxID_Change_Profile,
83 wxID_Change_Title,
84 wxID_Change_MetaData,
85 wxID_Print_Setup,
86 wxID_Print_Preview,
87 wxID_Print_Print,
88 wxID_Load_Staj,
89 wxID_Show_Legend,
90 wxID_Show_Rho,
91 wxID_Show_Mu,
92 wxID_Show_Phi,
93 wxID_Show_Theta,
94 wxID_Show_Depth,
95 wxID_Show_LayerName
96
97 ] = [ wx.NewId() for item in range(30) ]
98
99
100
101
102
104
105 - def __init__( self,
106 parent,
107 size=wx.DefaultSize
108 ):
109 super(ReflPanel, self).__init__(parent, size=size )
110
111
112 self.listener = Listener()
113
114
115 self._createMenu()
116
117
118 self._data = None
119 self._dataStr = ""
120
121
122 self.wavelength= 4.75
123 self.wavelengthDiv= 0.021
124 self.angularDiv = 7e-4
125 self.background = 1.0e-10
126 self.dtheta1 = 7e-4
127 self.dtheta2 = 7e-4
128 self.dvtheta = 7e-4
129 self.Q1 = 0.0
130 self.Q2 = 10000
131
132
133 self.nTLayers = 2
134 self.nMLayers = 7
135 self.nBLayers = 1
136
137 self.LoadJobFlag=0
138
139
140 - def _initModel( self,
141 names = None,
142 depth = None,
143 rough = None,
144 rho = None,
145 mu = None,
146 phi = None,
147 theta = None
148 ):
149 """
150 Initialize model by profile
151 """
152 if TIMING:
153 import time
154 t0 = time.time()
155
156 if len(names) == 0:
157 raise ValueError("must support the incident and substrate layer")
158
159 self.model = Profile( names = names,
160 depth = depth,
161 rough = rough,
162 rho = rho,
163 mu = mu,
164 phi = phi,
165 theta = theta
166 )
167
168
169 self.profile = ReflectometryInteractor( self.axes,
170 self.model,
171 self.listener,
172 self.parent
173 )
174 fit = Fit( [self.profile] )
175 self.listener.connect("update", self.profile, fit.update)
176
177
178 self.model.refresh()
179 self.profile.interface.refresh()
180 self.profile.update()
181
182 if TIMING:
183 print "Draw profile", time.time()-t0
184
185
195
196
197
199 """ popup menus """
200 menu = wx.Menu(title=u'Edit Model')
201
202 menu.Append(id=wxID_Init_Model_NM,text=u'&Init Model(Non Magnetic)...')
203 menu.Append(id=wxID_Init_Model, text=u'&Init Model(Magnetic)...' )
204
205 menu.AppendSeparator()
206
207 menu.Append( id=wxID_Show_Legend, kind=wx.ITEM_CHECK,
208 text=u'Show L&egend')
209 menu.Check(wxID_Show_Legend, True)
210
211 menu.Append( id=wxID_Show_LayerName,
212 kind=wx.ITEM_CHECK,
213 text=u'Show L&ayer Name')
214 menu.Check(wxID_Show_LayerName, True)
215
216 menu.Append( id=wxID_Show_Rho,
217 kind=wx.ITEM_CHECK,
218 text=u'Show Rh&o')
219 menu.Check(wxID_Show_Rho, True)
220
221 menu.Append( id=wxID_Show_Mu,
222 kind=wx.ITEM_CHECK,
223 text=u'Show M&u')
224 menu.Check(wxID_Show_Mu, True)
225
226 menu.Append( id=wxID_Show_Phi,
227 kind=wx.ITEM_CHECK,
228 text=u'Show Ph&i')
229 menu.Check(wxID_Show_Phi, True)
230
231 menu.Append( id=wxID_Show_Theta,
232 kind=wx.ITEM_CHECK,
233 text=u'Show Thet&a')
234 menu.Check(wxID_Show_Theta, True)
235
236 menu.Append( id=wxID_Show_Depth,
237 kind=wx.ITEM_CHECK,
238 text=u'Show Dept&h')
239 menu.Check(wxID_Show_Depth, True)
240
241 menu.AppendSeparator()
242 menu.Append( id=wxID_Add_Layer, text=u'&Add Layer ...' )
243 menu.Append( id=wxID_Remove_Layer, text = u'&Remove Layer ...')
244
245 menu.AppendSeparator()
246
247 menu.Append( id=wxID_Change_Profile,
248 text=u'&Change Profile Within Layer...')
249 menu.Append( id=wxID_Change_LayerName,
250 text=u'&Change Layer Name ...')
251
252 menu.Append( id=wxID_Change_Title, text=u'&Change Figure Title ...')
253 menu.Append( id=wxID_Change_MetaData, text=u'&Change MetaData...')
254
255 menu.AppendSeparator()
256
257 menu.Append( id=wxID_Load_Model, text=u'&Load Model ...')
258 menu.Append( id=wxID_Load_Staj, text=u'&Load Staj ...')
259
260 menu.AppendSeparator()
261
262 menu.Append( id=wxID_Save_Model, text=u'&Save Model(python)...')
263 menu.Append( id=wxID_Clear_Model, text=u'&Clear Model')
264
265 menu.AppendSeparator()
266
267 menu.Append( id=wxID_Save_Fig, text=u'&Save Figure ...')
268 menu.Append( id=wxID_Copy_Figure, text=u'&Copy Figure')
269 menu.Append( id=wxID_Copy_Profile, text=u'&Copy Profile')
270 menu.Append( id=wxID_Copy_Model, text=u'&Copy Model')
271
272 menu.AppendSeparator()
273 menu.Append(wxID_Print_Setup, text=u'&Page Setup...')
274 menu.Append(wxID_Print_Preview, text=u'&Print Preview...')
275 menu.Append(wxID_Print_Print, text=u'&Print...')
276
277 menu.AppendSeparator()
278
279
280 self.Bind(wx.EVT_MENU, self.OnInitModelMenu, id=wxID_Init_Model)
281 self.Bind(wx.EVT_MENU, self.OnInitModelNMMenu, id=wxID_Init_Model_NM)
282 self.Bind(wx.EVT_MENU, self.OnAddLayerMenu, id=wxID_Add_Layer)
283 self.Bind(wx.EVT_MENU, self.OnRemoveLayerMenu, id=wxID_Remove_Layer)
284 self.Bind(wx.EVT_MENU, self.OnSaveFigureMenu, id=wxID_Save_Fig)
285 self.Bind(wx.EVT_MENU, self.OnCopyFigureMenu, id=wxID_Copy_Figure)
286 self.Bind(wx.EVT_MENU, self.OnCopyProfileMenu, id=wxID_Copy_Profile)
287 self.Bind(wx.EVT_MENU, self.OnCopyModelMenu, id=wxID_Copy_Model)
288
289 self.Bind(wx.EVT_MENU, self.OnLoadModelMenu, id=wxID_Load_Model)
290 self.Bind(wx.EVT_MENU, self.OnLoadStajMenu, id=wxID_Load_Staj)
291 self.Bind(wx.EVT_MENU, self.OnSaveModelMenu, id=wxID_Save_Model)
292 self.Bind(wx.EVT_MENU, self.OnClearModelMenu, id=wxID_Clear_Model)
293
294 self.Bind(wx.EVT_MENU, self.OnShowLegendMenu, id=wxID_Show_Legend)
295 self.Bind(wx.EVT_MENU, self.OnShowLayerNameMenu,id=wxID_Show_LayerName)
296 self.Bind(wx.EVT_MENU, self.OnShowRhoMenu, id=wxID_Show_Rho)
297 self.Bind(wx.EVT_MENU, self.OnShowMuMenu, id=wxID_Show_Mu)
298 self.Bind(wx.EVT_MENU, self.OnShowPhiMenu, id=wxID_Show_Phi)
299 self.Bind(wx.EVT_MENU, self.OnShowThetaMenu, id=wxID_Show_Theta)
300 self.Bind(wx.EVT_MENU, self.OnShowDepthMenu, id=wxID_Show_Depth)
301
302 self.Bind(wx.EVT_MENU, self.onPrint, id=wxID_Print_Print)
303 self.Bind(wx.EVT_MENU, self.onPrinterSetup, id=wxID_Print_Setup)
304 self.Bind(wx.EVT_MENU, self.onPrinterPreview, id=wxID_Print_Preview)
305
306 self.Bind(wx.EVT_MENU, self.OnChangeLayerNameMenu,
307 id=wxID_Change_LayerName)
308
309 self.Bind(wx.EVT_MENU, self.OnChangeProfileLayerMenu,
310 id=wxID_Change_Profile)
311
312 self.Bind(wx.EVT_MENU, self.OnChangeMetaDataMenu,
313 id=wxID_Change_MetaData)
314
315 self.Bind(wx.EVT_MENU, self.OnChangeTitleMenu,
316 id=wxID_Change_Title)
317
318
319 self.canvas.Bind(wx.EVT_KEY_DOWN, self.onKeyEvent)
320 self.editMenu = menu
321
322 self._Disable()
323
324
326 """ Return the context menu"""
327 return self.editMenu
328
330 """
331 True, if show legend, otherwise False
332 """
333 return self.GetContextMenu().IsChecked( wxID_Show_Legend )
334
336 """
337 True, if show layer name, otherwise False
338 """
339 return self.GetContextMenu().IsChecked( wxID_Show_LayerName )
340
341
350
359
360
369
370
379
380
389
390
399
400
401
409
410
412
413 if Pname == "depth":
414 if pm.depth != val: pm.depth = val
415 elif Pname == "rough":
416 if pm.rough != val: pm.rough = val
417 elif Pname == "rho":
418 if pm.rho != val: pm.rho = val
419 elif Pname == "mu":
420 if pm.mu != val: pm.mu = val
421 elif Pname == "phi":
422 if pm.phi != val: pm.phi = val
423 elif Pname == "theta":
424 if pm.theta != val: pm.theta = val
425 else:
426 pass
427
428
429
431
432 if Pname == "rho":
433 _pm = decodeP( pm.rho )
434 _pm._valList[idx] = val
435 pm.rho = _pm.build()
436
437 elif Pname == "mu":
438 _pm = decodeP( pm.mu )
439 _pm._valList[idx] = val
440 pm.mu = _pm.build()
441
442 elif Pname == "phi":
443 _pm = decodeP( pm.phi )
444 _pm._valList[idx] = val
445 pm.phi = _pm.build()
446
447 elif Pname == "theta":
448 _pm = decodeP( pm.theta )
449 _pm._valList[idx] = val
450 pm.theta = _pm.build()
451
452 else: pass
453
454
456
457 n = nLayer
458 if Pname == "depth": self.model.depth[ n] = val
459 elif Pname == "rough":
460
461 try:
462 self.model.rough[ n] = val
463 except:
464 pass
465 elif Pname == "rho": self.model.Lrho[ n]._val = val
466 elif Pname == "mu": self.model.Lmu[ n]._val = val
467 elif Pname == "phi": self.model.Lphi[ n]._val = val
468 elif Pname == "theta": self.model.Ltheta[n]._val = val
469 else: pass
470
471
473
474 n = nLayer
475 if Pname == "rho": self.model.Lrho[ n]._val[idx] = val
476 elif Pname == "mu": self.model.Lmu[ n]._val[idx] = val
477 elif Pname == "phi": self.model.Lphi[ n]._val[idx] = val
478 elif Pname == "theta": self.model.Ltheta[n]._val[idx] = val
479 else: pass
480
481
483 """ TODO: """
484 try:
485 _pms = self.parent.model.getXmlParameters()
486 except:
487 return
488
489 if _pms == None or len(_pms) == 0: return
490
491 for i in xrange( len(_pms) ):
492 pm = _pms[i]
493
494
495 names = [ pm.name, 'depth' ]
496 self.UpdateViewer(names, pm.depth)
497
498
499 p = decodeP(pm.rho)
500 if isfactory(p):
501 if p.build()[:8]=="Tethered":
502 names = [ pm.name, 'rho_polymerSLD' ]
503 self.UpdateViewer( names, pm.rho_polymerSLD )
504 names = [ pm.name, 'rho_solventSLD' ]
505 self.UpdateViewer( names, pm.rho_solventSLD )
506 names = [ pm.name, 'rho_L0' ]
507 self.UpdateViewer( names, pm.rho_L0 )
508 names = [ pm.name, 'rho_phi' ]
509 self.UpdateViewer( names, pm.rho_phi )
510 names = [ pm.name, 'rho_power' ]
511 self.UpdateViewer( names, pm.rho_power )
512 else:
513 for i in xrange( len(p._valList) ):
514 names = [ pm.name, 'rho_%d'%(i) ]
515
516 self.UpdateViewer( names,
517 self.getVal4Pms( names[1], pm ) )
518
519 else:
520 names = [ pm.name, 'rho' ]
521 self.UpdateViewer(names, pm.rho)
522
523
524 if i != len(_pms)-1:
525 names = [ pm.name, 'rough' ]
526 self.UpdateViewer(names, pm.rough)
527
528
529
531 ( LName, PName ) = names
532 for i in xrange(len(self.model.names)):
533 if LName == self.model.names[i]:
534 nLayer = i
535 break
536
537 pm = self.parent.model.getChild( LName )
538
539 _pars = PName.split('_')
540 if len(_pars) == 1:
541 self._updateScalarValue( pm, PName, value)
542 self._updateScalarViewer( _pars[0].strip(), nLayer, value)
543
544 else:
545 _tname=['polymerSLD', 'solventSLD', 'L0', 'phi0', 'power']
546 if _pars[-1].strip() in _tname:
547 for i in xrange(5):
548 if _pars[-1].strip() == _tname[i]:
549 idx = i
550 else:
551 idx = int( _pars[-1].strip() )
552
553 self._updateFactoryValue(pm,
554 _pars[0].strip(),
555 idx,
556 value
557 )
558 self._updateFactoryViewer(_pars[0].strip(),
559 nLayer,
560 idx,
561 value
562 )
563
564 self.model.refresh()
565 self.profile.update()
566
567
569 self.modelName = model.name
570
571 if self.LoadJobFlag == 1 : return
572
573 try:
574 _pms = model.getXmlParameters()
575 except:
576 return
577
578 if _pms == None or len(_pms) == 0: return
579
580 self.LoadJobFlag = 1
581
582 _names=[]; _depth=[]; _rough=[]; _rho=[]; _mu=[]; _phi=[]; _theta=[]
583 for i in xrange( len(_pms) ):
584 pm = _pms[i]
585 _names.append(pm.name)
586 if pm.depth <=0 :
587 pm.depth = 100
588
589 _depth.append(pm.depth)
590 _rough.append(pm.rough)
591 _rho.append( decodeP( pm.rho) )
592 _mu.append( decodeP( pm.mu ) )
593 if hasattr(pm, 'phi'):
594 _phi.append( decodeP( pm.phi ) )
595 _theta.append( decodeP( pm.theta ) )
596
597 if _phi==[]:
598 _phi = None
599 _theta = None
600 self.magnetic = False
601 else:
602 self.magnetic = True
603
604
605
606
607 self._initModel(names = _names,
608 depth = _depth,
609 rough = _rough,
610 rho = _rho,
611 mu = _mu,
612 phi = _phi,
613 theta = _theta
614 )
615
616
617
618 self.GetContextMenu().Enable(wxID_Init_Model, False)
619 self.GetContextMenu().Enable(wxID_Load_Model, False)
620 self.GetContextMenu().Enable(wxID_Init_Model_NM, False)
621 self.GetContextMenu().Enable(wxID_Load_Staj, False)
622
623 self.GetContextMenu().Enable(wxID_Add_Layer, True)
624 self.GetContextMenu().Enable(wxID_Save_Model, True)
625 self.GetContextMenu().Enable(wxID_Remove_Layer, True)
626 self.GetContextMenu().Enable(wxID_Clear_Model, True)
627 self.GetContextMenu().Enable(wxID_Change_LayerName, True)
628 self.GetContextMenu().Enable(wxID_Change_MetaData, True)
629 self.GetContextMenu().Enable(wxID_Change_Profile, True)
630
631
632
634 """ On touch the right mouse """
635 self._current_rightdown_layer = self.current_layer
636 self.PopupMenu(self.GetContextMenu())
637
638
642
643
646
647
687
688
689
691 """ Init the magnetic model Menu """
692 dlg = InitModelDialog(self.parent,
693 -1,
694 "Init incident and substrate",
695 size = (300,300),
696 style = wx.DEFAULT_DIALOG_STYLE,
697 useMetal= False,
698 )
699
700 val = dlg.ShowModal()
701 if val == wx.ID_CANCEL: return
702 if val == wx.ID_OK:
703 ret1 = dlg.getIncidentInfo()
704 ret2 = dlg.getSubstrateInfo()
705 else:
706 ret1 = None
707 ret2 = None
708
709 names = [ret1[0],ret2[0]]
710 depth = [100.0, 100.0]
711 rough = [5.0]
712 rho = [ ret1[1],ret2[1] ]
713 mu = [ ret1[2],ret2[2] ]
714 phi = [ ret1[3],ret2[3] ]
715 theta = [ ret1[4],ret2[4] ]
716
717 self.magnetic = True
718
719 import time
720 t0 = time.time()
721 self._initModel(names = names,
722 depth = depth,
723 rough = rough,
724 rho = rho,
725 mu = mu,
726 phi = phi,
727 theta = theta
728 )
729
730 print "Draw profile", time.time()-t0
731
732 for x in xrange( len(depth) ):
733 _refl = ReflParameterMag()
734 _refl.name = names[x]
735 _refl.depth = tFloat( depth[x] )
736 _refl.rho = rho[x]
737 _refl.mu = mu[x]
738 _refl.phi = phi[x]
739 _refl.theta = theta[x]
740 if x < len(depth)-1:
741 _refl.rough = rough[x]
742 self.parent.model.addChild( _refl )
743
744 self.parent._fireModelEvent(MODEL_ADD)
745
746 self.GetContextMenu().Enable(wxID_Init_Model, False)
747 self.GetContextMenu().Enable(wxID_Load_Model, False)
748 self.GetContextMenu().Enable(wxID_Init_Model_NM, False)
749 self.GetContextMenu().Enable(wxID_Load_Staj, False)
750
751 self.GetContextMenu().Enable(wxID_Add_Layer, True)
752 self.GetContextMenu().Enable(wxID_Save_Model, True)
753 self.GetContextMenu().Enable(wxID_Remove_Layer, True)
754 self.GetContextMenu().Enable(wxID_Clear_Model, True)
755 self.GetContextMenu().Enable(wxID_Change_LayerName, True)
756 self.GetContextMenu().Enable(wxID_Change_MetaData, True)
757 self.GetContextMenu().Enable(wxID_Change_Profile, True)
758
759 return True
760
761
763 """ Init the non magnetic model Menu """
764 dlg = InitModelDialogNM(self.parent,
765 -1,
766 "Init incident and substrate",
767 size = (300,240),
768 style = wx.DEFAULT_DIALOG_STYLE,
769 useMetal= False,
770 )
771
772 val = dlg.ShowModal()
773 if val == wx.ID_CANCEL: return
774 if val == wx.ID_OK:
775 ret1 = dlg.getIncidentInfo()
776 ret2 = dlg.getSubstrateInfo()
777 else:
778 ret1 = None
779 ret2 = None
780
781 names = [ ret1[0], ret2[0] ]
782 depth = [ 300,300 ]
783 rough = [ 5 ]
784 rho = [ ret1[1], ret2[1] ]
785 mu = [ ret1[2], ret2[2] ]
786
787 self.magnetic = False
788 self._initModel( names = names,
789 depth = depth,
790 rough = rough,
791 rho = rho,
792 mu = mu
793 )
794
795 for x in xrange( len(depth) ):
796 _refl = ReflParameter()
797 _refl.name = names[x]
798 _refl.depth = tFloat( depth[x] )
799 _refl.rho = rho[x]
800 _refl.mu = mu[x]
801 if x < len(depth)-1:
802 _refl.rough = rough[x]
803 self.parent.model.addChild( _refl )
804
805 self.parent._fireModelEvent(MODEL_ADD)
806
807 self.GetContextMenu().Enable(wxID_Init_Model, False)
808 self.GetContextMenu().Enable(wxID_Init_Model_NM, False)
809 self.GetContextMenu().Enable(wxID_Load_Model, False)
810 self.GetContextMenu().Enable(wxID_Load_Staj, False)
811
812 self.GetContextMenu().Enable(wxID_Add_Layer, True)
813 self.GetContextMenu().Enable(wxID_Save_Model, True)
814 self.GetContextMenu().Enable(wxID_Add_Layer, True)
815 self.GetContextMenu().Enable(wxID_Remove_Layer, True)
816 self.GetContextMenu().Enable(wxID_Clear_Model, True)
817 self.GetContextMenu().Enable(wxID_Change_LayerName, True)
818 self.GetContextMenu().Enable(wxID_Change_MetaData, True)
819 self.GetContextMenu().Enable(wxID_Change_Profile, True)
820
821 return True
822
823
825 """ parse the depth of model """
826 ret = []
827 for i in xrange( len(self.model.depth) ):
828 ret.append( self.model.depth[i] )
829
830 return ret
831
832
834 """ parse the rough of model """
835 ret = []
836 for i in xrange( len(self.model.rough) ):
837 ret.append( self.model.rough[i] )
838
839 return ret
840
841
845
849
853
857
858
859
861 """ parse the depth value of model ( reflPARK) """
862 ret = []
863 n = len(self.model.depth)
864 for i in xrange(n):
865 ret.append( self.model.depth[i] )
866
867 return ret
868
869
873
877
881
885
886
888 """ Save the model as python script """
889 rough = self.model.rough
890 name = self.model.names
891 depth = self._parseDepth()
892 rho = self._parseRho()
893 mu = self._parseMu()
894 if self.model.magnetic:
895 phi = self._parsePhi()
896 theta = self._parseTheta()
897
898 dlg = SaveModelDialog(self.parent,
899 -1,
900 "Save Model Info",
901 size = (300,200),
902 style = wx.DEFAULT_DIALOG_STYLE,
903 useMetal= False,
904 )
905
906 val = dlg.ShowModal()
907 if val == wx.ID_CANCEL: return
908 if val == wx.ID_OK:
909 [modelname, filename, outfile] = dlg.getOutPutInfo()
910
911 if self.model.magnetic:
912 txts = _toPyScriptM("parseReflModel", 0, modelname,filename,name,
913 depth, rough, rho, mu, phi, theta,
914 self.wavelength, self.background,
915 self.wavelengthDiv, self.angularDiv)
916 else:
917 txts = _toPyScript("parseReflModel", 0, modelname,filename,name,
918 depth, rough, rho, mu,
919 self.wavelength, self.background,
920 self.wavelengthDiv, self.angularDiv)
921
922 try:
923 fd = open( outfile, 'w' )
924 fd.writelines( txts )
925 fd.close()
926 except:
927 showErrorMsg(self, 'Error in saving model XML file:',
928 'model File Error')
929
930
931
932
934
935 n = len(self.dictLayers)
936 names=[]; roughs=[]; depths=[]; rhos=[]; mus=[]; phis=[]; thetas=[]
937 for i in xrange(n):
938 names.append( self.dictLayers[i]["name"] )
939 depths.append( self.dictLayers[i]["depth"] )
940 rhos.append( self.dictLayers[i]["rho"] )
941 mus.append( self.dictLayers[i]["mu"] )
942 phis.append( self.dictLayers[i]["phi"] )
943 thetas.append( self.dictLayers[i]["theta"] )
944
945 n = len(self.dictInterfaces)
946 for i in xrange(n):
947 roughs.append( self.dictInterfaces[i]["interface"] )
948
949 return [names,depths, roughs, rhos, mus,phis,thetas ]
950
951
953
954 _name = []
955 for x in xrange( len(pLists) ):
956 if x <= nT:
957 _name.append( 'T%d'%(x) )
958 elif x >nT and x <= nT + nM:
959 _name.append( 'M%d'%(x-nT) )
960 else:
961 _name.append( 'B%d'%(x-nT-nM) )
962
963 return _name
964
965
966
968 """ Load the model from staj ( magnetic case ) """
969 dlg=wx.FileDialog(self,
970 message="Load a build model from Staj file",
971 wildcard="Staj file(*.staj)|*.staj|Staj file (*.sta)|*.sta|Any file(*.*)|*.*",
972 style=wx.OPEN
973 )
974 fname = ''
975 _val = dlg.ShowModal()
976 if _val == wx.ID_CANCEL: return
977 if _val == wx.ID_OK:
978 dir0 = dlg.GetDirectory()
979 fname = dlg.GetFilename()
980 dlg.Destroy()
981
982 if fname == '':
983 showErrorMsg(self, 'Error in load file:'+fname,'py file Error')
984
985 _fullFileName = os.path.join(dir0,fname)
986 fin = open(_fullFileName,'r')
987 _FirstLine = fin.readlines()[0].strip().split()
988 fin.close()
989
990
991 if( len(_FirstLine) == 4 ):
992
993 self.magnetic = True
994 self._LoadMStajFile( _fullFileName, dir0 )
995
996 elif( len(_FirstLine) == 6 ):
997
998 self.magnetic = False
999 self._LoadNMStajFile( _fullFileName, dir0 )
1000
1001 else:
1002 pass
1003
1004
1005 self.GetContextMenu().Enable(wxID_Load_Model, False)
1006 self.GetContextMenu().Enable(wxID_Load_Staj, False)
1007 self.GetContextMenu().Enable(wxID_Save_Model, True)
1008 self.GetContextMenu().Enable(wxID_Init_Model, False)
1009 self.GetContextMenu().Enable(wxID_Init_Model_NM, False)
1010
1011 self.GetContextMenu().Enable(wxID_Add_Layer, True)
1012 self.GetContextMenu().Enable(wxID_Remove_Layer, True)
1013 self.GetContextMenu().Enable(wxID_Clear_Model, True)
1014 self.GetContextMenu().Enable(wxID_Change_LayerName, True)
1015 self.GetContextMenu().Enable(wxID_Change_Profile, True)
1016 self.GetContextMenu().Enable(wxID_Change_MetaData, True)
1017
1018
1019
1021 """ Load the model from staj """
1022 s2m = Staj2Model( fname, scale=True )._toModel()
1023 ( self.wavelength, self.wavelengthDiv, self.angularDiv,
1024 self.intensity, self.background ) = s2m[1]
1025
1026 ( _nTLayer, _nMLayer, _nBLayer, _nRepeat, _nFit, self._nRough )=s2m[2]
1027 (_d, _rough, _rho, _mu, _rhom) = s2m[0]
1028
1029 _names = self.parseStajLayerName( _d, _nTLayer, _nMLayer, _nBLayer)
1030 ( qmin, qmax, npnts) = s2m[3]
1031 ( self._proftyp, self._stajdatafile, outfile) = s2m[4]
1032
1033 self.Q1 = 0.0
1034 self.Q2 = 0.0
1035
1036
1037
1038 _d[0] = 100.0
1039
1040 _rough_ = []
1041 for x in xrange( len(_rough)-1 ):
1042 _rough_.append( _rough[x+1] )
1043 _rough_.append( _rough[ len(_rough) -1 ] )
1044
1045 self._initModel(names = _names,
1046 depth = _d,
1047 rough = _rough_,
1048 rho = _rho,
1049 mu = _mu
1050 )
1051 for x in xrange( len(_d) ):
1052
1053 _refl = ReflParameter()
1054 _refl.name = _names[x]
1055 _refl.depth = tFloat( _d[x] )
1056 _refl.rho = _rho[x]
1057 _refl.mu = _mu[x]
1058
1059 if x < len(_d):
1060 _refl.rough = _rough_[x]
1061
1062 self.parent.model.addChild( _refl )
1063
1064 try:
1065 _meta = self.parent.parent.datasetPanel.GetDataset(
1066 ).getXmlData()[0].getXmlMetaData()
1067 except:
1068 showErrorMsg(self, 'Error in change metadata:','meta data Error')
1069 return
1070
1071 _meta.angularDiv = self.angularDiv
1072 _meta.background = self.background
1073 _meta.wavelength = self.wavelength
1074 _meta.wavelengthDiv = self.wavelengthDiv
1075
1076 self.parent._fireModelEvent(MODEL_ADD)
1077
1078
1079
1081 """ Load the model from staj ( magnetic case ) """
1082
1083
1084 s2m = Staj2MModel( fname, scale=True )._toModel()
1085
1086 (self.wavelength, self.wavelengthDiv, self.angularDiv,
1087 self.intensity, self.background, self.Aguide ) = s2m[0]
1088
1089 self.Q1 = 0.0
1090 self.Q2 = 0.0
1091 self.dtheta1 = self.angularDiv
1092 self.dtheta2 = self.angularDiv
1093
1094 ( _rho,_phi, _depth,_depthm, _rough,_roughm, _mu,_the)= s2m[1]
1095 _Qranges = s2m[2]
1096 (_nLayer, _nRough , _nFit ) = s2m[3]
1097 (_type, _suffix, _outfile) = s2m[4]
1098
1099 self._stajdatafile = _outfile + _suffix[0]
1100
1101 _names = []
1102 for x in xrange( len(_depth) ):
1103 if x ==0: _names.append( 'MV' )
1104 else: _names.append( 'M%d'%(x) )
1105
1106
1107 if _depth[0] < 0.00001:
1108 _depth[0] = 100.0
1109
1110 _rough_ = []
1111 for x in xrange( len(_rough)-1 ):
1112 _rough_.append( _rough[x+1] )
1113 _rough_.append( _rough[ len(_rough) -1 ] )
1114
1115 self._initModel(names = _names,
1116 depth = _depth,
1117 rough = _rough_,
1118 rho = _rho,
1119 mu = _mu,
1120 phi = _phi,
1121 theta = _the
1122 )
1123
1124 for x in xrange( len(_depth) ):
1125
1126 _refl = ReflParameterMag()
1127 _refl.name = _names[x]
1128 _refl.depth = tFloat( _depth[x] )
1129 _refl.rho = _rho[x]
1130 _refl.mu = _mu[x]
1131 _refl.phi = _phi[x]
1132 _refl.theta = _the[x]
1133
1134 if x < len(_depth):
1135 _refl.rough = _rough[x]
1136
1137 self.parent.model.addChild( _refl )
1138
1139 try:
1140 _meta = self.parent.parent.datasetPanel.GetDataset().getXmlData(
1141 )[0].getXmlMetaData()
1142 except:
1143 showErrorMsg(self, 'Error in change metadata:','meta data Error')
1144 return
1145
1146 _meta.angularDiv = self.angularDiv
1147 _meta.background = self.background
1148 _meta.wavelength = self.wavelength
1149 _meta.wavelengthDiv = self.wavelengthDiv
1150
1151 self.parent._fireModelEvent(MODEL_ADD)
1152
1153
1154
1156 """ Load the model """
1157 dlg=wx.FileDialog(self,
1158 message ="Load a build model python script",
1159 wildcard="python file (*.py)|*.py|Any file(*.*)|*.*",
1160 style =wx.OPEN
1161 )
1162 fname=''
1163 _val = dlg.ShowModal()
1164 if _val == wx.ID_CANCEL: return
1165 if _val == wx.ID_OK:
1166 dir0 = dlg.GetDirectory()
1167 fname = dlg.GetFilename()
1168 _inFullName = os.path.join(dir0, fname)
1169 dlg.Destroy()
1170
1171 txts = ''
1172 if fname != '':
1173 fd = open( _inFullName )
1174 for txt in fd:
1175 if not txt.isspace(): txts += txt
1176 fd.close()
1177
1178
1179 if txts == '': return
1180
1181 M_n = lookUpModelName( txts )
1182 cmd = txts + 'self.dictLayers=' + M_n + '.DictLayers\n'+ \
1183 'self.dictInterfaces='+ M_n + '.DictInterfaces\n' + \
1184 'self.magnetic = ' + M_n + '.magnetic\n' + \
1185 'self.wavelength=' + M_n + '.wavelength\n' + \
1186 'self.wavelengthDiv=' + M_n + '.wavelengthdivergence\n'+\
1187 'self.angularDiv=' + M_n + '.angulardivergence\n' + \
1188 'self.background=' + M_n + '.background'
1189 exec cmd
1190
1191 (_names,_depth,_rough,_rho, _mu, _phi, _theta) = self.parseDictLayers()
1192 if not self.magnetic:
1193 _phi = None
1194 _theta = None
1195
1196 _depth[0] = 100
1197 _depth[-1] = 100
1198
1199 self._initModel(names = _names,
1200 depth = _depth,
1201 rough = _rough,
1202 rho = _rho,
1203 mu = _mu,
1204 phi = _phi,
1205 theta = _theta
1206 )
1207
1208
1209 _rough_ = [ _rough[0] ]
1210 for x in xrange( len(_rough) ):
1211 _rough_.append( _rough[x] )
1212
1213 for x in xrange( len(_depth) ):
1214
1215 if not self.magnetic: _refl = ReflParameter()
1216 else: _refl = ReflParameterMag()
1217
1218 _refl.name = _names[x]
1219 _refl.depth = tFloat( _depth[x] )
1220 _refl.rho = filterP( _rho[x] )
1221 _refl.mu = filterP( _mu[x] )
1222 if self.magnetic:
1223 _refl.phi = filterP( _phi[x] )
1224 _refl.theta = filterP( _theta[x] )
1225
1226
1227 _refl.rough = _rough_[x]
1228
1229 self.parent.model.addChild( _refl )
1230
1231 self.parent._fireModelEvent(MODEL_ADD)
1232
1233 self.GetContextMenu().Enable(wxID_Load_Model, False)
1234 self.GetContextMenu().Enable(wxID_Load_Staj, False)
1235 self.GetContextMenu().Enable(wxID_Init_Model, False)
1236 self.GetContextMenu().Enable(wxID_Init_Model_NM, False)
1237
1238 self.GetContextMenu().Enable(wxID_Add_Layer, True)
1239 self.GetContextMenu().Enable(wxID_Remove_Layer, True)
1240 self.GetContextMenu().Enable(wxID_Clear_Model, True)
1241 self.GetContextMenu().Enable(wxID_Change_LayerName, True)
1242 self.GetContextMenu().Enable(wxID_Change_Profile, True)
1243 self.GetContextMenu().Enable(wxID_Change_MetaData, True)
1244 self.GetContextMenu().Enable(wxID_Save_Model, True)
1245
1246
1247
1281
1282
1283
1285 """ Change a profile in a layer """
1286 if isfactory( b ):
1287 mL[idx] = Layer( b )
1288 mP[idx] = b
1289 else:
1290 val = CheckValid(b)
1291 mL[idx] = Layer( val )
1292 mP[idx] = val
1293
1294
1295
1297 """ Change a profile from a layer """
1298 idx = self.current_layer
1299 dlg = ChangeProfileDialog(self.parent,
1300 -1,
1301 "Change a profile within layer(%d)"%(idx),
1302 size = (500,200),
1303 style = wx.DEFAULT_DIALOG_STYLE,
1304 useMetal= False,
1305 model = self.model,
1306 layer_num = idx
1307 )
1308
1309
1310
1311 val = dlg.ShowModal()
1312 if val == wx.ID_CANCEL: return
1313 if val == wx.ID_OK:
1314 ret = dlg.getLayerInfo()
1315 else:
1316 ret = [1]
1317
1318
1319 if ret[-1] != 0:
1320 showWarningMsg(self,
1321 "Wrong input format(invalid layer), try again",
1322 "Add Layer Error message"
1323 )
1324 return False
1325
1326
1327 _type = ret[0]
1328 _nProfile = ret[2]
1329 _nfProfile = filterP( ret[2] )
1330
1331 nameID = self.model.names[idx]
1332 _pm = self.parent.model.getChild( nameID )
1333
1334 if _type == "rho":
1335 self.change_a_profile( idx, self.model.Lrho, self.model.rho,
1336 _nProfile )
1337 _pm.rho = _nfProfile
1338
1339 if _type == "mu":
1340 self.change_a_profile( idx, self.model.Lmu, self.model.mu,
1341 _nProfile )
1342 _pm.mu = _nfProfile
1343
1344 if _type == "phi":
1345 self.change_a_profile( idx, self.model.Lphi, self.model.phi,
1346 _nProfile )
1347 _pm.phi = _nfProfile
1348
1349 if _type == "theta":
1350 self.change_a_profile(idx,self.model.Ltheta, self.model.theta,
1351 _nProfile )
1352 _pm.theta = _nfProfile
1353
1354 self.parent._fireModelEvent(MODEL_UPDATE)
1355
1356
1357 self.model.refresh()
1358 self.profile.interface.refresh()
1359 self.profile.set_layer(idx)
1360 self.profile.update()
1361
1362 self.GetContextMenu().Enable(wxID_Remove_Layer, True)
1363
1364 return True
1365
1366
1367
1369 """ Add a profile in a layer """
1370 if isfactory( b ):
1371 mL.insert(idx, Layer( b ) )
1372 mP.insert(idx, b )
1373
1374 else:
1375 val = CheckValid(b)
1376 mL.insert(idx, Layer( val) )
1377 mP.insert(idx, val )
1378
1379
1380
1382 """ Remove the model """
1383 for i in xrange( len(self.model.names) ):
1384 nameID = self.model.names[i]
1385 self.parent.model.removeChild( nameID )
1386
1387
1407
1408
1410 """ On add a layer """
1411 idx = self._current_rightdown_layer
1412
1413 if self.model.magnetic: sizes = (500,300)
1414 else: sizes = (500,240)
1415
1416
1417 if idx <=0:
1418 showErrorMsg(self,
1419 "Can't add a layer before incident layer(%s)"%(self.model.names[0]), "Add Layer Error message" )
1420 return
1421
1422 if idx >= len(self.model.names):
1423 showErrorMsg(self,
1424 "Can't add a layer before substrate layer(%s)"%(self.model.names[-1]), "Add Layer Error message" )
1425 return
1426
1427
1428
1429 dlg = AddLayerDialog(self.parent,
1430 -1,
1431 "Add a layer between layer(%s) and layer(%s)"%(self.model.names[idx-1], self.model.names[idx]),
1432 size = sizes,
1433 style = wx.DEFAULT_DIALOG_STYLE,
1434 useMetal= False,
1435 model = self.model
1436 )
1437 val = dlg.ShowModal()
1438 if val == wx.ID_CANCEL: return
1439 if val == wx.ID_OK:
1440 ret = dlg.getLayerInfo()
1441 else:
1442 ret = [1]
1443
1444
1445 if ret[-1] != 0:
1446 showErrorMsg(self, "Wrong input format(invalid layer), try again",
1447 "Add Layer Error message" )
1448 return
1449
1450
1451 if self.model.names.count( ret[0] ) >=1:
1452 showErrorMsg(self,
1453 "The layer name %s exists( not unique )"%( ret[0] ),
1454 "Add Layer Error message" )
1455 return
1456
1457
1458 self._removeModelChild()
1459
1460 self.model.names.insert(idx, ret[0])
1461 self.model.depth = numpy.insert( self.model.depth, idx, ret[1])
1462 self.model.rough = numpy.insert( self.model.rough, idx-1, ret[2])
1463
1464 self.add_a_layer(idx, self.model.Lrho, self.model.rho, ret[3] )
1465 self.add_a_layer(idx, self.model.Lmu, self.model.mu, ret[4] )
1466 if self.magnetic:
1467 self.add_a_layer(idx, self.model.Lphi, self.model.phi, ret[5] )
1468 self.add_a_layer(idx, self.model.Ltheta,self.model.theta, ret[6] )
1469
1470
1471 self._addModelChild()
1472
1473
1474
1475 self.model.refresh()
1476 self.profile.interface.refresh()
1477
1478 self.profile.update()
1479
1480
1481 self.parent._fireModelEvent(MODEL_ADD)
1482
1483 self.GetContextMenu().Enable(wxID_Remove_Layer, True)
1484
1485 return True
1486
1487
1488
1490 """ On Remove a layer """
1491 dlg = RemoveLayerDialog(self.parent,
1492 -1,
1493 title = "Remove a layer",
1494 size = (360,160),
1495 style = wx.DEFAULT_DIALOG_STYLE,
1496 useMetal = False,
1497 model = self.model
1498 )
1499
1500 val = dlg.ShowModal()
1501 if val == wx.ID_CANCEL: return
1502 if val == wx.ID_OK:
1503 ret = dlg.getLayerInfo()
1504 else:
1505 ret = [1]
1506
1507 if ret[-1] != 0 :
1508 showErrorMsg(self, "We can't remove a invalid Layer",
1509 "Remove Layer Error message")
1510 return False
1511
1512
1513
1514 idx = ret[0]
1515
1516
1517
1518 self._removeModelChild()
1519
1520 self.model.names.pop( idx )
1521 self.model.depth = numpy.delete( self.model.depth, idx )
1522 self.model.rough = numpy.delete( self.model.rough, idx-1 )
1523
1524 self.model.Lrho.pop( idx )
1525 self.model.rho.pop( idx )
1526 self.model.Lmu.pop( idx )
1527 self.model.mu.pop( idx )
1528 if self.magnetic:
1529 self.model.Lphi.pop( idx )
1530 self.model.phi.pop( idx )
1531 self.model.Ltheta.pop( idx )
1532 self.model.theta.pop( idx )
1533
1534
1535
1536 self._addModelChild()
1537
1538
1539 self.model.refresh()
1540 self.profile.interface.refresh()
1541 self.profile.update()
1542
1543
1544 self.parent._fireModelEvent(MODEL_ADD)
1545
1546 return True
1547
1548
1551
1552
1554 """ Change Figure Title """
1555
1556 oldTitle = self.profile.get_title()
1557
1558 dlg = ChangeFigureTitle(self.parent,
1559 -1,
1560 "Change or Set Figure Title",
1561 size = (500,200),
1562 style = wx.DEFAULT_DIALOG_STYLE,
1563 useMetal= False,
1564 OldFig_Title = oldTitle
1565 )
1566
1567 val = dlg.ShowModal()
1568 if val == wx.ID_CANCEL: return
1569 if val == wx.ID_OK:
1570 ret = dlg.getTitleInfo()
1571
1572
1573 _newTitle = ret[0]
1574
1575
1576
1577 self.profile.set_title( _newTitle )
1578 self.profile.interface.refresh()
1579 self.profile.update()
1580
1581
1583 """ Change layer name """
1584 idx = self.current_layer
1585 nameID = self.model.names[idx]
1586
1587 dlg = ChangeLayernameDialog(self.parent,
1588 -1,
1589 "Change a profile within layer(%d)"%(idx),
1590 size = (500,200),
1591 style = wx.DEFAULT_DIALOG_STYLE,
1592 useMetal= False,
1593 layer_name = nameID
1594 )
1595
1596 val = dlg.ShowModal()
1597 if val == wx.ID_CANCEL: return
1598 if val == wx.ID_OK:
1599 ret = dlg.getLayerInfo()
1600
1601
1602 _newName = ret[0]
1603
1604
1605 if self.model.names.count( _newName ) >=1:
1606 showErrorMsg(self,
1607 "The layer name %s exists( not unique )"%(_newName),
1608 "Change layer name Error message" )
1609 return
1610
1611 self._removeModelChild()
1612
1613
1614
1615
1616 self.model.names[ idx ] = _newName
1617
1618 self._addModelChild()
1619 self.parent._fireModelEvent(MODEL_UPDATE)
1620
1621 self.model.refresh()
1622 self.profile.interface.refresh()
1623 self.profile.update()
1624
1625 return True
1626
1627
1629 """
1630 Copy the current model
1631 """
1632 try:
1633 rough = copy.deepcopy( self.model.rough )
1634 rough.append('')
1635 try:
1636 rho = []
1637 for i in xrange(len(self.model.rho)):
1638 rho.append( filterP( self.model.rho[i] ) )
1639 except:
1640 rho = self.model.rho
1641 if not self.model.magnetic:
1642 clipboard( ( self.model.names,
1643 self.model.depth,
1644 rough,
1645 rho,
1646 self.model.mu),
1647 ('names', 'depth', 'rough', 'rho', 'mu' ) )
1648 else:
1649 clipboard( ( self.model.names,
1650 self.model.depth,
1651 rough,
1652 self.model.rho,
1653 self.model.mu,
1654 self.model.phi,
1655 self.model.theta,),
1656 ('names', 'depth', 'rough',
1657 'rho', 'mu','phi','theta' ) )
1658 except:
1659 print "Error in copy Model"
1660
1661
1663 """
1664 Copy the current profile
1665 """
1666 try:
1667 z,p=self.model.calc()
1668 except:
1669 z=[0.0]
1670 p=[ [0.0], [0.0] ]
1671
1672 if len(p)==2:
1673 clipboard( ( z, p[0], p[1]), ('z', 'rho', 'mu') )
1674 else:
1675 clipboard( ( z, p[0], p[1], p[2], p[3]),
1676 ('z', 'rho', 'mu', 'phi', 'theta') )
1677