source: sasview/calculatorview/perspectives/calculator/sld_panel.py @ 017ec0b7

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 017ec0b7 was 017ec0b7, checked in by Jae Cho <jhjcho@…>, 14 years ago

minor touch on tips

  • 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        iy = 0
161        ix = 0
162        sizer_output.Add(neutron_sld_txt,(iy, ix),(1,1),
163                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
164        ix += 1
165        sizer_output.Add(self.neutron_sld_reel_ctl,(iy, ix),(1,1),
166                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
167        ix += 1
168        sizer_output.Add(wx.StaticText(self, -1, i_complex)
169                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
170        ix +=1
171        sizer_output.Add(self.neutron_sld_im_ctl
172                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
173        ix +=1
174        sizer_output.Add(neutron_sld_units_txt
175                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
176        iy += 1
177        ix = 0
178        sizer_output.Add(cu_ka_sld_txt,(iy, ix),(1,1),
179                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
180        ix +=1
181        sizer_output.Add(self.cu_ka_sld_reel_ctl,(iy, ix),(1,1),
182                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
183        ix +=1
184        sizer_output.Add(wx.StaticText(self, -1, i_complex)
185                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0)
186        ix += 1
187        sizer_output.Add(self.cu_ka_sld_im_ctl
188                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
189        ix += 1
190        sizer_output.Add(cu_ka_sld_units_txt
191                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
192        iy += 1
193        ix = 0
194        sizer_output.Add(mo_ka_sld_txt,(iy, ix),(1,1),
195                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
196        ix +=1
197        sizer_output.Add(self.mo_ka_sld_reel_ctl,(iy, ix),(1,1),
198                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
199        ix +=1
200        sizer_output.Add(wx.StaticText(self, -1, i_complex)
201                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0)
202        ix += 1
203        sizer_output.Add(self.mo_ka_sld_im_ctl
204                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
205        ix +=1
206        sizer_output.Add(mo_ka_sld_units_txt
207                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0)
208        iy += 1
209        ix = 0
210        sizer_output.Add(neutron_inc_txt,(iy, ix),(1,1),
211                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
212        ix += 1
213        sizer_output.Add(self.neutron_inc_ctl,(iy, ix),(1,1),
214                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
215        ix += 2
216        sizer_output.Add(neutron_inc_units_txt,(iy, ix),(1,1),
217                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
218        iy += 1
219        ix = 0
220        sizer_output.Add(neutron_abs_txt, (iy, ix), (1,1),
221                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
222        ix += 1
223        sizer_output.Add(self.neutron_abs_ctl, (iy, ix), (1,1),
224                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
225        ix +=2
226        sizer_output.Add(neutron_abs_units_txt, (iy, ix), (1,1),
227                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
228        iy += 1
229        ix = 0
230        sizer_output.Add(neutron_length_txt, (iy, ix), (1,1),
231                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
232        ix +=1
233        sizer_output.Add(self.neutron_length_ctl, (iy, ix), (1,1),
234                            wx.EXPAND|wx.ADJUST_MINSIZE, 0)
235        ix += 2
236        sizer_output.Add(neutron_length_units_txt, (iy, ix), (1,1),
237                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
238        boxsizer2.Add( sizer_output )
239        sizer2.Add(boxsizer2,0, wx.EXPAND|wx.ALL, 10)
240        #-----Button  sizer------------
241   
242        id = wx.NewId()
243        button_calculate = wx.Button(self, id, "Calculate")
244        button_calculate.SetToolTipString("Calculate SLD.")
245        self.Bind(wx.EVT_BUTTON, self.calculateSld, id = id)   
246       
247        sizer_button.Add((250, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
248        sizer_button.Add(button_calculate, 0, wx.RIGHT|wx.ADJUST_MINSIZE,20)
249        sizer3.Add(sizer_button)
250        #---------layout----------------
251        vbox  = wx.BoxSizer(wx.VERTICAL)
252        vbox.Add(sizer1)
253        vbox.Add(sizer2)
254        vbox.Add(sizer3)
255        vbox.Fit(self) 
256        self.SetSizer(vbox)
257       
258       
259    def check_inputs(self):
260        """Check validity user inputs"""
261        flag = True
262       
263        if check_float(self.density_ctl):
264            self.density = float(self.density_ctl.GetValue())
265        else:
266            flag = False
267            raise ValueError,"Error for Density value :expect float"
268   
269        self.wavelength = self.wavelength_ctl.GetValue()
270        if self.wavelength.lstrip().rstrip() == "":
271            self.wavelength = self.calculator.wavelength
272        else:
273            if check_float(self.wavelength_ctl):
274                self.wavelength = float(self.wavelength)
275            else:
276                flag = False
277                raise ValueError,"Error for wavelenth value :expect float"
278               
279        self.formulata_text = self.compound_ctl.GetValue().lstrip().rstrip()
280        if self.formulata_text != "":
281            self.compound_ctl.SetBackgroundColour(wx.WHITE)
282            self.compound_ctl.Refresh()
283        else:
284            self.compound_ctl.SetBackgroundColour("pink")
285            self.compound_ctl.Refresh()
286            flag = False
287            raise ValueError, "Enter a formula"
288        return flag
289       
290    def calculateSld(self, event):
291        """
292            Calculate the neutron scattering density length of a molecule
293        """
294        try:
295            #Check validity user inputs
296            if self.check_inputs():
297                #get ready to compute
298                try:
299                    self.calculator.set_value(self.formulata_text,
300                                              self.density, self.wavelength)
301                except:
302                    if self.base is not None:
303                        msg = "SLD Calculator: %s" %(sys.exc_value)
304                        wx.PostEvent(self.base, StatusEvent(status=msg))
305                    else:
306                        raise
307                    return
308               
309                # Compute the Cu SLD
310                Cu_reel, Cu_im = self.calculator.calculate_xray_sld( "Cu")
311                self.cu_ka_sld_reel_ctl.SetValue(format_number(Cu_reel *_SCALE))
312                self.cu_ka_sld_im_ctl.SetValue(format_number(Cu_im * _SCALE))
313               
314                # Compute the Mo SLD
315                Mo_reel, Mo_im = self.calculator.calculate_xray_sld( "Mo")
316                self.mo_ka_sld_reel_ctl.SetValue(format_number(Mo_reel *_SCALE))
317                self.mo_ka_sld_im_ctl.SetValue(format_number(Mo_im * _SCALE))
318               
319                coh,absorp,inc = self.calculator.calculate_neutron_sld()
320                im = self.calculator.calculate_coherence_im()
321                length = self.calculator.calculate_length()
322                # Neutron SLD
323                self.neutron_sld_reel_ctl.SetValue(format_number(coh * _SCALE))
324                self.neutron_sld_im_ctl.SetValue(format_number(im * _SCALE))
325                self.neutron_inc_ctl.SetValue(format_number(inc))
326                self.neutron_abs_ctl.SetValue(format_number(absorp))
327                # Neutron length
328                self.neutron_length_ctl.SetValue(format_number(length))
329                # display wavelength
330                self.wavelength_ctl.SetValue(str(self.wavelength))
331        except:
332            if self.base is not None:
333                  msg = "SLD Calculator: %s"%(sys.exc_value)
334                  wx.PostEvent(self.base, StatusEvent(status=msg))
335            else:
336                raise
337            return   
338
339   
340       
341 
342   
343   
344 
345       
346               
347class SldWindow(wx.Frame):
348    def __init__(self, parent=None, id=1, title="SLD Calculator",base=None):
349        wx.Frame.__init__(self, parent, id, title, size=( PANEL_SIZE,  PANEL_SIZE))
350       
351        self.panel = SldPanel(self, base=base)
352        self.Centre()
353        self.Show(True)
354       
355class ViewApp(wx.App):
356    def OnInit(self):
357        frame = SldWindow(None, -1, 'SLD Calculator')   
358        frame.Show(True)
359        self.SetTopWindow(frame)
360       
361        return True
362       
363
364if __name__ == "__main__": 
365    app = ViewApp(0)
366    app.MainLoop()     
Note: See TracBrowser for help on using the repository browser.