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