1 | """ |
---|
2 | This module provide GUI for the neutron scattering length density calculator |
---|
3 | @author: Gervaise B. Alina |
---|
4 | """ |
---|
5 | |
---|
6 | import wx |
---|
7 | import sys |
---|
8 | |
---|
9 | from sans.guiframe.utils import format_number, check_float |
---|
10 | from sans.guicomm.events import StatusEvent |
---|
11 | from sldCalculator import SldCalculator |
---|
12 | |
---|
13 | _BOX_WIDTH = 76 |
---|
14 | _STATICBOX_WIDTH = 350 |
---|
15 | _SCALE = 1e-6 |
---|
16 | |
---|
17 | #SLD panel size |
---|
18 | if sys.platform.count("win32")>0: |
---|
19 | _STATICBOX_WIDTH = 350 |
---|
20 | PANEL_SIZE = 400 |
---|
21 | else: |
---|
22 | _STATICBOX_WIDTH = 380 |
---|
23 | PANEL_SIZE = 430 |
---|
24 | |
---|
25 | class SldPanel(wx.Panel): |
---|
26 | """ |
---|
27 | Provides the SLD calculator GUI. |
---|
28 | """ |
---|
29 | def __init__(self, parent,base=None, id = -1): |
---|
30 | wx.Panel.__init__(self, parent, id = id) |
---|
31 | # Object that receive status event |
---|
32 | self.base= base |
---|
33 | self.calculator = SldCalculator() |
---|
34 | self.wavelength = self.calculator.wavelength |
---|
35 | |
---|
36 | self._do_layout() |
---|
37 | self.SetAutoLayout(True) |
---|
38 | self.Layout() |
---|
39 | |
---|
40 | |
---|
41 | def _do_layout(self): |
---|
42 | """ |
---|
43 | Draw window content |
---|
44 | """ |
---|
45 | unit_a = '[A]' |
---|
46 | unit_density = '[g/cm^(3)]' |
---|
47 | unit_sld= '[1/A^(2)]' |
---|
48 | unit_cm1='[1/cm]' |
---|
49 | unit_cm='[cm]' |
---|
50 | sizer_input = wx.GridBagSizer(5,5) |
---|
51 | sizer_output = wx.GridBagSizer(5,5) |
---|
52 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
53 | sizer1 = wx.BoxSizer(wx.HORIZONTAL) |
---|
54 | sizer2 = wx.BoxSizer(wx.HORIZONTAL) |
---|
55 | sizer3 = wx.BoxSizer(wx.HORIZONTAL) |
---|
56 | #---------inputs---------------- |
---|
57 | inputbox = wx.StaticBox(self, -1, "Input") |
---|
58 | boxsizer1 = wx.StaticBoxSizer(inputbox, wx.VERTICAL) |
---|
59 | boxsizer1.SetMinSize((_STATICBOX_WIDTH,-1)) |
---|
60 | |
---|
61 | compound_txt = wx.StaticText(self, -1, 'Compound ') |
---|
62 | self.compound_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
63 | density_txt = wx.StaticText(self, -1, 'Density ') |
---|
64 | self.density_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
65 | unit_density_txt = wx.StaticText(self, -1, unit_density) |
---|
66 | wavelength_txt = wx.StaticText(self, -1, 'Wavelength ') |
---|
67 | self.wavelength_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
68 | self.wavelength_ctl.SetValue(str(self.wavelength)) |
---|
69 | unit_a_txt = wx.StaticText(self, -1, unit_a) |
---|
70 | iy = 0 |
---|
71 | ix = 0 |
---|
72 | sizer_input.Add(compound_txt,(iy, ix),(1,1),\ |
---|
73 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
74 | ix +=1 |
---|
75 | sizer_input.Add(self.compound_ctl,(iy, ix),(1,1),\ |
---|
76 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
77 | iy += 1 |
---|
78 | ix = 0 |
---|
79 | sizer_input.Add(density_txt,(iy, ix),(1,1),\ |
---|
80 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
81 | ix +=1 |
---|
82 | sizer_input.Add(self.density_ctl,(iy, ix),(1,1),\ |
---|
83 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
84 | ix +=1 |
---|
85 | sizer_input.Add(unit_density_txt,(iy, ix),(1,1),\ |
---|
86 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
87 | iy += 1 |
---|
88 | ix = 0 |
---|
89 | sizer_input.Add(wavelength_txt,(iy, ix),(1,1),\ |
---|
90 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
91 | ix +=1 |
---|
92 | sizer_input.Add(self.wavelength_ctl,(iy, ix),(1,1),\ |
---|
93 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
94 | ix +=1 |
---|
95 | sizer_input.Add(unit_a_txt,(iy, ix),(1,1),\ |
---|
96 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
97 | boxsizer1.Add( sizer_input ) |
---|
98 | sizer1.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
99 | #---------Outputs sizer-------- |
---|
100 | outputbox = wx.StaticBox(self, -1, "Output") |
---|
101 | boxsizer2 = wx.StaticBoxSizer(outputbox, wx.VERTICAL) |
---|
102 | boxsizer2.SetMinSize((_STATICBOX_WIDTH,-1)) |
---|
103 | |
---|
104 | i_complex= '+ i' |
---|
105 | neutron_sld_txt = wx.StaticText(self, -1, 'Neutron SLD') |
---|
106 | self.neutron_sld_reel_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
107 | self.neutron_sld_reel_ctl.SetEditable(False) |
---|
108 | self.neutron_sld_reel_ctl.SetToolTipString("Neutron SLD reel.") |
---|
109 | self.neutron_sld_im_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
110 | self.neutron_sld_im_ctl.SetEditable(False) |
---|
111 | self.neutron_sld_im_ctl.SetToolTipString("Neutron SLD imaginary.") |
---|
112 | neutron_sld_units_txt = wx.StaticText(self, -1, unit_sld) |
---|
113 | |
---|
114 | cu_ka_sld_txt = wx.StaticText(self, -1, 'Cu Ka SLD') |
---|
115 | self.cu_ka_sld_reel_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
116 | self.cu_ka_sld_reel_ctl.SetEditable(False) |
---|
117 | self.cu_ka_sld_reel_ctl.SetToolTipString("Cu Ka SLD reel.") |
---|
118 | self.cu_ka_sld_im_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
119 | self.cu_ka_sld_im_ctl.SetEditable(False) |
---|
120 | self.cu_ka_sld_im_ctl.SetToolTipString("Cu Ka SLD imaginary.") |
---|
121 | cu_ka_sld_units_txt = wx.StaticText(self, -1, unit_sld) |
---|
122 | |
---|
123 | mo_ka_sld_txt = wx.StaticText(self, -1, 'Mo Ka SLD') |
---|
124 | self.mo_ka_sld_reel_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
125 | self.mo_ka_sld_reel_ctl.SetEditable(False) |
---|
126 | self.mo_ka_sld_reel_ctl.SetToolTipString("Mo Ka SLD reel.") |
---|
127 | self.mo_ka_sld_im_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
128 | self.mo_ka_sld_im_ctl.SetEditable(False) |
---|
129 | self.mo_ka_sld_im_ctl.SetToolTipString("Mo Ka SLD reel.") |
---|
130 | mo_ka_sld_units_txt = wx.StaticText(self, -1, unit_sld) |
---|
131 | |
---|
132 | neutron_inc_txt = wx.StaticText(self, -1, 'Neutron Inc. Xs') |
---|
133 | self.neutron_inc_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
134 | self.neutron_inc_ctl.SetEditable(False) |
---|
135 | self.neutron_inc_ctl.SetToolTipString("Neutron Inc. Xs") |
---|
136 | neutron_inc_units_txt = wx.StaticText(self, -1, unit_cm1) |
---|
137 | |
---|
138 | neutron_abs_txt = wx.StaticText(self, -1, 'Neutron Abs. Xs') |
---|
139 | self.neutron_abs_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
140 | self.neutron_abs_ctl.SetEditable(False) |
---|
141 | self.neutron_abs_ctl.SetToolTipString("Neutron Abs. Xs") |
---|
142 | neutron_abs_units_txt = wx.StaticText(self, -1, unit_cm1) |
---|
143 | |
---|
144 | neutron_length_txt = wx.StaticText(self, -1, 'Neutron 1/e length') |
---|
145 | self.neutron_length_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
146 | self.neutron_length_ctl.SetEditable(False) |
---|
147 | self.neutron_length_ctl.SetToolTipString("Neutron 1/e length") |
---|
148 | neutron_length_units_txt = wx.StaticText(self, -1, unit_cm) |
---|
149 | iy = 0 |
---|
150 | ix = 0 |
---|
151 | sizer_output.Add(neutron_sld_txt,(iy, ix),(1,1), |
---|
152 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
153 | ix +=1 |
---|
154 | sizer_output.Add(self.neutron_sld_reel_ctl,(iy, ix),(1,1), |
---|
155 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
156 | ix +=1 |
---|
157 | sizer_output.Add(wx.StaticText(self, -1, i_complex) |
---|
158 | ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
159 | ix +=1 |
---|
160 | sizer_output.Add(self.neutron_sld_im_ctl |
---|
161 | ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
162 | ix +=1 |
---|
163 | sizer_output.Add(neutron_sld_units_txt |
---|
164 | ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
165 | iy += 1 |
---|
166 | ix = 0 |
---|
167 | sizer_output.Add(cu_ka_sld_txt,(iy, ix),(1,1), |
---|
168 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
169 | ix +=1 |
---|
170 | sizer_output.Add(self.cu_ka_sld_reel_ctl,(iy, ix),(1,1), |
---|
171 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
172 | ix +=1 |
---|
173 | sizer_output.Add(wx.StaticText(self, -1, i_complex) |
---|
174 | ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
175 | ix +=1 |
---|
176 | sizer_output.Add(self.cu_ka_sld_im_ctl |
---|
177 | ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
178 | ix +=1 |
---|
179 | sizer_output.Add(cu_ka_sld_units_txt |
---|
180 | ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
181 | iy += 1 |
---|
182 | ix = 0 |
---|
183 | sizer_output.Add(mo_ka_sld_txt,(iy, ix),(1,1), |
---|
184 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
185 | ix +=1 |
---|
186 | sizer_output.Add(self.mo_ka_sld_reel_ctl,(iy, ix),(1,1), |
---|
187 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
188 | ix +=1 |
---|
189 | sizer_output.Add(wx.StaticText(self, -1, i_complex) |
---|
190 | ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
191 | ix +=1 |
---|
192 | sizer_output.Add(self.mo_ka_sld_im_ctl |
---|
193 | ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
194 | ix +=1 |
---|
195 | sizer_output.Add(mo_ka_sld_units_txt |
---|
196 | ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
197 | iy += 1 |
---|
198 | ix = 0 |
---|
199 | sizer_output.Add(neutron_inc_txt,(iy, ix),(1,1), |
---|
200 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
201 | ix +=1 |
---|
202 | sizer_output.Add(self.neutron_inc_ctl,(iy, ix),(1,1), |
---|
203 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
204 | ix +=2 |
---|
205 | sizer_output.Add(neutron_inc_units_txt,(iy, ix),(1,1), |
---|
206 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
207 | iy += 1 |
---|
208 | ix = 0 |
---|
209 | sizer_output.Add(neutron_abs_txt,(iy, ix),(1,1), |
---|
210 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
211 | ix +=1 |
---|
212 | sizer_output.Add(self.neutron_abs_ctl,(iy, ix),(1,1), |
---|
213 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
214 | ix +=2 |
---|
215 | sizer_output.Add(neutron_abs_units_txt,(iy, ix),(1,1), |
---|
216 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
217 | iy += 1 |
---|
218 | ix = 0 |
---|
219 | sizer_output.Add(neutron_length_txt,(iy, ix),(1,1), |
---|
220 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
221 | ix +=1 |
---|
222 | sizer_output.Add(self.neutron_length_ctl,(iy, ix),(1,1), |
---|
223 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
224 | ix +=2 |
---|
225 | sizer_output.Add(neutron_length_units_txt,(iy, ix),(1,1), |
---|
226 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
227 | boxsizer2.Add( sizer_output ) |
---|
228 | sizer2.Add(boxsizer2,0, wx.EXPAND | wx.ALL, 10) |
---|
229 | #-----Button sizer------------ |
---|
230 | id = wx.NewId() |
---|
231 | button_help = wx.Button(self, id, "Help") |
---|
232 | button_help.SetToolTipString("Help SlD calculations and formula.") |
---|
233 | self.Bind(wx.EVT_BUTTON, self.onHelp, id = id) |
---|
234 | |
---|
235 | id = wx.NewId() |
---|
236 | button_calculate = wx.Button(self, id, "Calculate") |
---|
237 | button_calculate.SetToolTipString("Calculate SlD of neutrons.") |
---|
238 | self.Bind(wx.EVT_BUTTON, self.calculateSld, id = id) |
---|
239 | |
---|
240 | sizer_button.Add((200, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
241 | sizer_button.Add(button_help, 0, wx.RIGHT|wx.ADJUST_MINSIZE,10) |
---|
242 | sizer_button.Add(button_calculate, 0, wx.RIGHT|wx.ADJUST_MINSIZE,20) |
---|
243 | sizer3.Add(sizer_button) |
---|
244 | #---------layout---------------- |
---|
245 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
246 | vbox.Add(sizer1) |
---|
247 | vbox.Add(sizer2) |
---|
248 | vbox.Add(sizer3) |
---|
249 | vbox.Fit(self) |
---|
250 | self.SetSizer(vbox) |
---|
251 | |
---|
252 | |
---|
253 | def check_inputs(self): |
---|
254 | """Check validity user inputs""" |
---|
255 | flag= True |
---|
256 | |
---|
257 | if check_float(self.density_ctl): |
---|
258 | self.density = float(self.density_ctl.GetValue()) |
---|
259 | else: |
---|
260 | flag=False |
---|
261 | raise ValueError,"Error for Density value :expect float" |
---|
262 | |
---|
263 | self.wavelength= self.wavelength_ctl.GetValue() |
---|
264 | if self.wavelength.lstrip().rstrip()=="": |
---|
265 | self.wavelength = self.calculator.wavelength |
---|
266 | else: |
---|
267 | if check_float(self.wavelength_ctl): |
---|
268 | self.wavelength= float(self.wavelength) |
---|
269 | else: |
---|
270 | flag = False |
---|
271 | raise ValueError,"Error for wavelenth value :expect float" |
---|
272 | |
---|
273 | self.formulata_text= self.compound_ctl.GetValue().lstrip().rstrip() |
---|
274 | if self.formulata_text!="": |
---|
275 | self.compound_ctl.SetBackgroundColour(wx.WHITE) |
---|
276 | self.compound_ctl.Refresh() |
---|
277 | else: |
---|
278 | self.compound_ctl.SetBackgroundColour("pink") |
---|
279 | self.compound_ctl.Refresh() |
---|
280 | flag=False |
---|
281 | raise ValueError, "Enter a formula" |
---|
282 | return flag |
---|
283 | |
---|
284 | |
---|
285 | def onHelp(self, event): |
---|
286 | """ |
---|
287 | provide more hint on the SLD calculator |
---|
288 | """ |
---|
289 | from helpPanel import HelpWindow |
---|
290 | frame = HelpWindow(None, -1, pageToOpen="doc/sld_calculator_help.html") |
---|
291 | frame.Show(True) |
---|
292 | name = "SLD_calculator" |
---|
293 | if frame.rhelp.HasAnchor(name): |
---|
294 | frame.rhelp.ScrollToAnchor(name) |
---|
295 | else: |
---|
296 | msg= "Cannot find SLD Calculator description " |
---|
297 | msg +="Please.Search in the Help window" |
---|
298 | wx.PostEvent(self.base, StatusEvent(status = msg )) |
---|
299 | |
---|
300 | |
---|
301 | def calculateSld(self, event): |
---|
302 | """ |
---|
303 | Calculate the neutron scattering density length of a molecule |
---|
304 | """ |
---|
305 | try: |
---|
306 | #Check validity user inputs |
---|
307 | if self.check_inputs(): |
---|
308 | #get ready to compute |
---|
309 | try: |
---|
310 | self.calculator.setValue(self.formulata_text,self.density,self.wavelength) |
---|
311 | except: |
---|
312 | if self.base !=None: |
---|
313 | msg= "SLD Calculator: %s" % (sys.exc_value) |
---|
314 | wx.PostEvent(self.base, StatusEvent(status= msg )) |
---|
315 | else: |
---|
316 | raise |
---|
317 | return |
---|
318 | |
---|
319 | # Compute the Cu SLD |
---|
320 | Cu_reel, Cu_im = self.calculator.calculateXRaySld( "Cu") |
---|
321 | self.cu_ka_sld_reel_ctl.SetValue(format_number(Cu_reel*_SCALE)) |
---|
322 | self.cu_ka_sld_im_ctl.SetValue(format_number(Cu_im*_SCALE)) |
---|
323 | |
---|
324 | # Compute the Mo SLD |
---|
325 | Mo_reel, Mo_im = self.calculator.calculateXRaySld( "Mo") |
---|
326 | self.mo_ka_sld_reel_ctl.SetValue(format_number(Mo_reel*_SCALE)) |
---|
327 | self.mo_ka_sld_im_ctl.SetValue(format_number(Mo_im*_SCALE)) |
---|
328 | |
---|
329 | coh,absorp,inc= self.calculator.calculateNSld() |
---|
330 | im = self.calculator.calculateAbsorptionIm() |
---|
331 | length = self.calculator.calculateLength() |
---|
332 | # Neutron SLD |
---|
333 | self.neutron_sld_reel_ctl.SetValue(format_number(coh*_SCALE)) |
---|
334 | self.neutron_sld_im_ctl.SetValue(format_number(im*_SCALE)) |
---|
335 | self.neutron_inc_ctl.SetValue(format_number(inc )) |
---|
336 | self.neutron_abs_ctl.SetValue(format_number(absorp)) |
---|
337 | # Neutron length |
---|
338 | self.neutron_length_ctl.SetValue(format_number(length)) |
---|
339 | # display wavelength |
---|
340 | self.wavelength_ctl.SetValue(str(self.wavelength)) |
---|
341 | except: |
---|
342 | if self.base !=None: |
---|
343 | msg= "SLD Calculator: %s" % (sys.exc_value) |
---|
344 | wx.PostEvent(self.base, StatusEvent(status= msg )) |
---|
345 | else: |
---|
346 | raise |
---|
347 | return |
---|
348 | |
---|
349 | |
---|
350 | |
---|
351 | |
---|
352 | |
---|
353 | |
---|
354 | |
---|
355 | |
---|
356 | |
---|
357 | class SldWindow(wx.Frame): |
---|
358 | def __init__(self, parent=None, id=1, title="SLD Calculator",base=None): |
---|
359 | wx.Frame.__init__(self, parent, id, title, size=( PANEL_SIZE, PANEL_SIZE)) |
---|
360 | |
---|
361 | self.panel = SldPanel(self, base=base) |
---|
362 | self.Centre() |
---|
363 | self.Show(True) |
---|
364 | |
---|
365 | class ViewApp(wx.App): |
---|
366 | def OnInit(self): |
---|
367 | frame = SldWindow(None, -1, 'SLD Calculator') |
---|
368 | frame.Show(True) |
---|
369 | self.SetTopWindow(frame) |
---|
370 | |
---|
371 | return True |
---|
372 | |
---|
373 | |
---|
374 | if __name__ == "__main__": |
---|
375 | app = ViewApp(0) |
---|
376 | app.MainLoop() |
---|