source: sasview/calculatorview/perspectives/calculator/sld_panel.py @ 25a2ee3

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 25a2ee3 was 25a2ee3, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on documentation

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