source: sasview/src/sas/sasgui/perspectives/calculator/sld_panel.py @ 64ca561

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.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 64ca561 was d0248bd, checked in by butler, 9 years ago

linked new help to all Tools menu panels remaining.

  • Property mode set to 100644
File size: 21.1 KB
RevLine 
[427fa87]1"""
[b0ab6cb]2This module provide GUI for the neutron scattering length density calculator
3
[427fa87]4"""
5
6import wx
[855546d]7import math
[427fa87]8import sys
9
[d85c194]10from sas.sasgui.guiframe.panel_base import PanelBase
[e696e0a]11
[d85c194]12from sas.sasgui.guiframe.utils import format_number
13from sas.sasgui.guiframe.utils import check_float
14from sas.sasgui.guiframe.events import StatusEvent
[427fa87]15
[810f196]16# the calculator default value for wavelength is 6
[b0ab6cb]17#import periodictable
[810f196]18from periodictable import formula
[b0ab6cb]19from periodictable.xsf import xray_energy
20from periodictable.xsf import xray_sld_from_atoms
21from periodictable.nsf import neutron_scattering
[d85c194]22from sas.sasgui.perspectives.calculator import calculator_widgets as widget
23from sas.sasgui.guiframe.documentation_window import DocumentationWindow
[49ab5d7]24
[810f196]25WAVELENGTH = 6.0
[427fa87]26_BOX_WIDTH = 76
27_STATICBOX_WIDTH = 350
28_SCALE = 1e-6
29
30#SLD panel size
[b0ab6cb]31if sys.platform.count("win32") > 0:
[d5419f7f]32    PANEL_TOP = 0
[427fa87]33    _STATICBOX_WIDTH = 350
34    PANEL_SIZE = 400
35    FONT_VARIANT = 0
36else:
[d5419f7f]37    PANEL_TOP = 60
[427fa87]38    _STATICBOX_WIDTH = 380
[6996942]39    PANEL_SIZE = 410
[427fa87]40    FONT_VARIANT = 1
[49ab5d7]41
[e696e0a]42class SldPanel(wx.Panel, PanelBase):
[427fa87]43    """
[b0ab6cb]44    Provides the SLD calculator GUI.
[427fa87]45    """
46    ## Internal nickname for the window, used by the AUI manager
47    window_name = "SLD Calculator"
48    ## Name to appear on the window title bar
49    window_caption = "SLD Calculator"
50    ## Flag to tell the AUI manager to put this panel in the center pane
51    CENTER_PANE = True
[49ab5d7]52
[44148f0]53    def __init__(self, parent, base=None, *args, **kwds):
[b0ab6cb]54        """
55        """
[44148f0]56        wx.Panel.__init__(self, parent, *args, **kwds)
[e696e0a]57        PanelBase.__init__(self)
[427fa87]58        #Font size
59        self.SetWindowVariant(variant=FONT_VARIANT)
60        # Object that receive status event
[39e49a1]61        self.base = base
[810f196]62        self.wavelength = WAVELENGTH
[d5419f7f]63        self.parent = parent
[b0ab6cb]64        #layout attribute
65        self.compound_ctl = None
66        self.density_ctl = None
[4752c31]67        self.compound = ""
68        self.density = ""
[b0ab6cb]69        self.wavelength_ctl = None
[1e96a66]70        self.neutron_sld_real_ctl = None
[b0ab6cb]71        self.neutron_sld_im_ctl = None
[1e96a66]72        self.mo_ka_sld_real_ctl = None
[b0ab6cb]73        self.mo_ka_sld_im_ctl = None
[1e96a66]74        self.cu_ka_sld_real_ctl = None
[b0ab6cb]75        self.cu_ka_sld_im_ctl = None
76        self.neutron_abs_ctl = None
77        self.neutron_inc_ctl = None
78        self.neutron_length_ctl = None
[66bfacf]79        self.button_calculate = None
[810f196]80        #Draw the panel
[427fa87]81        self._do_layout()
82        self.SetAutoLayout(True)
83        self.Layout()
[49ab5d7]84
[427fa87]85    def _do_layout(self):
86        """
[b0ab6cb]87        Draw window content
[427fa87]88        """
89        unit_a = '[A]'
90        unit_density = '[g/cm^(3)]'
[39e49a1]91        unit_sld = '[1/A^(2)]'
[b0ab6cb]92        unit_cm1 = '[1/cm]'
93        unit_cm = '[cm]'
94        sizer_input = wx.GridBagSizer(5, 5)
95        sizer_output = wx.GridBagSizer(5, 5)
[427fa87]96        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
97        sizer1 = wx.BoxSizer(wx.HORIZONTAL)
98        sizer2 = wx.BoxSizer(wx.HORIZONTAL)
99        sizer3 = wx.BoxSizer(wx.HORIZONTAL)
100        #---------inputs----------------
101        inputbox = wx.StaticBox(self, -1, "Input")
102        boxsizer1 = wx.StaticBoxSizer(inputbox, wx.VERTICAL)
[b0ab6cb]103        boxsizer1.SetMinSize((_STATICBOX_WIDTH, -1))
[49ab5d7]104
[427fa87]105        compound_txt = wx.StaticText(self, -1, 'Compound ')
[49ab5d7]106        self.compound_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH * 2, -1))
[427fa87]107        density_txt = wx.StaticText(self, -1, 'Density ')
[b0ab6cb]108        self.density_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1))
[427fa87]109        unit_density_txt = wx.StaticText(self, -1, unit_density)
110        wavelength_txt = wx.StaticText(self, -1, 'Wavelength ')
[b0ab6cb]111        self.wavelength_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1))
[427fa87]112        self.wavelength_ctl.SetValue(str(self.wavelength))
113        unit_a_txt = wx.StaticText(self, -1, unit_a)
114        iy = 0
115        ix = 0
[b0ab6cb]116        sizer_input.Add(compound_txt, (iy, ix), (1, 1),
[49ab5d7]117                             wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
[b0ab6cb]118        ix += 1
119        sizer_input.Add(self.compound_ctl, (iy, ix), (1, 1),
[49ab5d7]120                            wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[427fa87]121        iy += 1
122        ix = 0
[b0ab6cb]123        sizer_input.Add(density_txt, (iy, ix), (1, 1),
[49ab5d7]124                             wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
[39e49a1]125        ix += 1
[b0ab6cb]126        sizer_input.Add(self.density_ctl, (iy, ix), (1, 1),
[49ab5d7]127                            wx.EXPAND | wx.ADJUST_MINSIZE, 0)
128        ix += 1
129        sizer_input.Add(unit_density_txt, (iy, ix), (1, 1),
130                            wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[427fa87]131        iy += 1
132        ix = 0
[b0ab6cb]133        sizer_input.Add(wavelength_txt, (iy, ix), (1, 1),
[49ab5d7]134                             wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
[39e49a1]135        ix += 1
[b0ab6cb]136        sizer_input.Add(self.wavelength_ctl, (iy, ix), (1, 1),
[49ab5d7]137                            wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[39e49a1]138        ix += 1
[b0ab6cb]139        sizer_input.Add(unit_a_txt, (iy, ix), (1, 1),
[49ab5d7]140                            wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[b0ab6cb]141        boxsizer1.Add(sizer_input)
142        sizer1.Add(boxsizer1, 0, wx.EXPAND | wx.ALL, 10)
[427fa87]143        #---------Outputs sizer--------
144        outputbox = wx.StaticBox(self, -1, "Output")
145        boxsizer2 = wx.StaticBoxSizer(outputbox, wx.VERTICAL)
[b0ab6cb]146        boxsizer2.SetMinSize((_STATICBOX_WIDTH, -1))
[49ab5d7]147
[855546d]148        i_complex = '- i'
[427fa87]149        neutron_sld_txt = wx.StaticText(self, -1, 'Neutron SLD')
[1e96a66]150        self.neutron_sld_real_ctl = wx.TextCtrl(self, -1,
[b0ab6cb]151                                                 size=(_BOX_WIDTH, -1))
[1e96a66]152        self.neutron_sld_real_ctl.SetEditable(False)
153        self.neutron_sld_real_ctl.SetToolTipString("Neutron SLD real.")
[49ab5d7]154        self.neutron_sld_im_ctl = wx.TextCtrl(self, -1,
[b0ab6cb]155                                              size=(_BOX_WIDTH, -1))
[427fa87]156        self.neutron_sld_im_ctl.SetEditable(False)
157        self.neutron_sld_im_ctl.SetToolTipString("Neutron SLD imaginary.")
158        neutron_sld_units_txt = wx.StaticText(self, -1, unit_sld)
[49ab5d7]159
[427fa87]160        cu_ka_sld_txt = wx.StaticText(self, -1, 'Cu Ka SLD')
[1e96a66]161        self.cu_ka_sld_real_ctl = wx.TextCtrl(self, -1,
[b0ab6cb]162                                               size=(_BOX_WIDTH, -1))
[1e96a66]163        self.cu_ka_sld_real_ctl.SetEditable(False)
164        self.cu_ka_sld_real_ctl.SetToolTipString("Cu Ka SLD real.")
[49ab5d7]165        self.cu_ka_sld_im_ctl = wx.TextCtrl(self, -1,
[b0ab6cb]166                                            size=(_BOX_WIDTH, -1))
[427fa87]167        self.cu_ka_sld_im_ctl.SetEditable(False)
168        self.cu_ka_sld_im_ctl.SetToolTipString("Cu Ka SLD imaginary.")
169        cu_ka_sld_units_txt = wx.StaticText(self, -1, unit_sld)
[49ab5d7]170
[427fa87]171        mo_ka_sld_txt = wx.StaticText(self, -1, 'Mo Ka SLD')
[1e96a66]172        self.mo_ka_sld_real_ctl = wx.TextCtrl(self, -1,
[b0ab6cb]173                                               size=(_BOX_WIDTH, -1))
[1e96a66]174        self.mo_ka_sld_real_ctl.SetEditable(False)
175        self.mo_ka_sld_real_ctl.SetToolTipString("Mo Ka SLD real.")
[b0ab6cb]176        self.mo_ka_sld_im_ctl = wx.TextCtrl(self, -1,
177                                             size=(_BOX_WIDTH, -1))
[427fa87]178        self.mo_ka_sld_im_ctl.SetEditable(False)
[017ec0b7]179        self.mo_ka_sld_im_ctl.SetToolTipString("Mo Ka SLD imaginary.")
[427fa87]180        mo_ka_sld_units_txt = wx.StaticText(self, -1, unit_sld)
[49ab5d7]181
[427fa87]182        neutron_inc_txt = wx.StaticText(self, -1, 'Neutron Inc. Xs')
[b0ab6cb]183        self.neutron_inc_ctl = wx.TextCtrl(self, -1,
184                                            size=(_BOX_WIDTH, -1))
[427fa87]185        self.neutron_inc_ctl.SetEditable(False)
186        self.neutron_inc_ctl.SetToolTipString("Neutron Inc. Xs")
[49ab5d7]187        neutron_inc_units_txt = wx.StaticText(self, -1, unit_cm1)
188
189        neutron_abs_txt = wx.StaticText(self, -1, 'Neutron Abs. Xs')
190        self.neutron_abs_ctl = wx.TextCtrl(self, -1,
[b0ab6cb]191                                           size=(_BOX_WIDTH, -1))
[427fa87]192        self.neutron_abs_ctl.SetEditable(False)
193        self.neutron_abs_ctl.SetToolTipString("Neutron Abs. Xs")
[49ab5d7]194        neutron_abs_units_txt = wx.StaticText(self, -1, unit_cm1)
195
[427fa87]196        neutron_length_txt = wx.StaticText(self, -1, 'Neutron 1/e length')
[b0ab6cb]197        self.neutron_length_ctl = wx.TextCtrl(self, -1,
198                                               size=(_BOX_WIDTH, -1))
[427fa87]199        self.neutron_length_ctl.SetEditable(False)
200        self.neutron_length_ctl.SetToolTipString("Neutron 1/e length")
[49ab5d7]201        neutron_length_units_txt = wx.StaticText(self, -1, unit_cm)
202
[427fa87]203        iy = 0
204        ix = 0
[b0ab6cb]205        sizer_output.Add(neutron_sld_txt, (iy, ix), (1, 1),
[49ab5d7]206                             wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
[39e49a1]207        ix += 1
[1e96a66]208        sizer_output.Add(self.neutron_sld_real_ctl, (iy, ix), (1, 1),
[49ab5d7]209                            wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[39e49a1]210        ix += 1
[b0ab6cb]211        sizer_output.Add(wx.StaticText(self, -1, i_complex),
[49ab5d7]212                         (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[b0ab6cb]213        ix += 1
214        sizer_output.Add(self.neutron_sld_im_ctl,
[49ab5d7]215                         (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[b0ab6cb]216        ix += 1
217        sizer_output.Add(neutron_sld_units_txt,
[49ab5d7]218                         (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[427fa87]219        iy += 1
220        ix = 0
[b0ab6cb]221        sizer_output.Add(cu_ka_sld_txt, (iy, ix), (1, 1),
[49ab5d7]222                             wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
[b0ab6cb]223        ix += 1
[1e96a66]224        sizer_output.Add(self.cu_ka_sld_real_ctl, (iy, ix), (1, 1),
[49ab5d7]225                            wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[39e49a1]226        ix += 1
[b0ab6cb]227        sizer_output.Add(wx.StaticText(self, -1, i_complex),
[49ab5d7]228                         (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[b0ab6cb]229        ix += 1
230        sizer_output.Add(self.cu_ka_sld_im_ctl,
[49ab5d7]231                         (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[39e49a1]232        ix += 1
[b0ab6cb]233        sizer_output.Add(cu_ka_sld_units_txt,
[49ab5d7]234                         (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[427fa87]235        iy += 1
236        ix = 0
[49ab5d7]237        sizer_output.Add(mo_ka_sld_txt, (iy, ix), (1, 1),
238                             wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
[b0ab6cb]239        ix += 1
[49ab5d7]240        sizer_output.Add(self.mo_ka_sld_real_ctl, (iy, ix), (1, 1),
241                            wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[39e49a1]242        ix += 1
[b0ab6cb]243        sizer_output.Add(wx.StaticText(self, -1, i_complex),
[49ab5d7]244                         (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[b0ab6cb]245        ix += 1
246        sizer_output.Add(self.mo_ka_sld_im_ctl,
[49ab5d7]247                         (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[b0ab6cb]248        ix += 1
249        sizer_output.Add(mo_ka_sld_units_txt,
[49ab5d7]250                         (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[427fa87]251        iy += 1
252        ix = 0
[b0ab6cb]253        sizer_output.Add(neutron_inc_txt, (iy, ix), (1, 1),
[49ab5d7]254                             wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
[39e49a1]255        ix += 1
[b0ab6cb]256        sizer_output.Add(self.neutron_inc_ctl, (iy, ix), (1, 1),
[49ab5d7]257                            wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[39e49a1]258        ix += 2
[49ab5d7]259        sizer_output.Add(neutron_inc_units_txt, (iy, ix), (1, 1),
260                            wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[427fa87]261        iy += 1
262        ix = 0
[b0ab6cb]263        sizer_output.Add(neutron_abs_txt, (iy, ix), (1, 1),
[49ab5d7]264                             wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
[39e49a1]265        ix += 1
[b0ab6cb]266        sizer_output.Add(self.neutron_abs_ctl, (iy, ix), (1, 1),
[49ab5d7]267                            wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[b0ab6cb]268        ix += 2
269        sizer_output.Add(neutron_abs_units_txt, (iy, ix), (1, 1),
[49ab5d7]270                            wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[427fa87]271        iy += 1
272        ix = 0
[b0ab6cb]273        sizer_output.Add(neutron_length_txt, (iy, ix), (1, 1),
[49ab5d7]274                             wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
[b0ab6cb]275        ix += 1
276        sizer_output.Add(self.neutron_length_ctl, (iy, ix), (1, 1),
[49ab5d7]277                            wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[39e49a1]278        ix += 2
[b0ab6cb]279        sizer_output.Add(neutron_length_units_txt, (iy, ix), (1, 1),
[49ab5d7]280                            wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[b0ab6cb]281        boxsizer2.Add(sizer_output)
[49ab5d7]282        sizer2.Add(boxsizer2, 0, wx.EXPAND | wx.ALL, 10)
[427fa87]283        #-----Button  sizer------------
[49ab5d7]284
[427fa87]285        id = wx.NewId()
[66bfacf]286        self.button_calculate = wx.Button(self, id, "Calculate")
287        self.button_calculate.SetToolTipString("Calculate SLD.")
[49ab5d7]288        self.Bind(wx.EVT_BUTTON, self.calculateSld, id=id)
289
[33f551f]290        id = wx.NewId()
[fc8f132]291        self.button_help = wx.Button(self, id, "HELP")
292        self.button_help.SetToolTipString("help on SLD calculator.")
[49ab5d7]293        self.Bind(wx.EVT_BUTTON, self.on_help, id=id)
294
[d5419f7f]295        self.button_close = wx.Button(self, wx.ID_CANCEL, 'Close')
296        self.button_close.Bind(wx.EVT_BUTTON, self.on_close)
297        self.button_close.SetToolTipString("Close this window.")
298
[49ab5d7]299        sizer_button.Add((150, 20), 1, wx.EXPAND | wx.ADJUST_MINSIZE, 0)
300        sizer_button.Add(self.button_calculate, 0, wx.RIGHT | wx.ADJUST_MINSIZE, 20)
301        sizer_button.Add(self.button_help, 0, wx.RIGHT | wx.ADJUST_MINSIZE, 20)
[d5419f7f]302        sizer_button.Add(self.button_close, 0, wx.RIGHT | wx.ADJUST_MINSIZE, 20)
[427fa87]303        sizer3.Add(sizer_button)
304        #---------layout----------------
[49ab5d7]305        vbox = wx.BoxSizer(wx.VERTICAL)
[427fa87]306        vbox.Add(sizer1)
307        vbox.Add(sizer2)
308        vbox.Add(sizer3)
[49ab5d7]309        vbox.Fit(self)
[427fa87]310        self.SetSizer(vbox)
[49ab5d7]311
312    def on_help(self, event):
[fc8f132]313        """
314        Bring up the SLD Documentation whenever
[49ab5d7]315        the HELP button is clicked.
316
[fc8f132]317        Calls DocumentationWindow with the path of the location within the
[49ab5d7]318        documentation tree (after /doc/ ....".  Note that when using old
319        versions of Wx (before 2.9) and thus not the release version of
320        installers, the help comes up at the top level of the file as
[fc8f132]321        webbrowser does not pass anything past the # to the browser when it is
322        running "file:///...."
[49ab5d7]323
[fc8f132]324    :param evt: Triggers on clicking the help button
325    """
[49ab5d7]326
[d0248bd]327        _TreeLocation = "user/sasgui/perspectives/calculator/"
328        _TreeLocation += "sld_calculator_help.html"
[3db44fb]329        _doc_viewer = DocumentationWindow(self, -1, _TreeLocation, "",
330                                          "General Scattering Calculator Help")
[fc8f132]331
[d5419f7f]332    def on_close(self, event):
333        """
334        close the window containing this panel
335        """
336        self.parent.Close()
337
[810f196]338    def calculate_xray_sld(self, element):
339        """
340        Get an element and compute the corresponding SLD for a given formula
[49ab5d7]341
[810f196]342        :param element:  elements a string of existing atom
[49ab5d7]343
[810f196]344        """
345        myformula = formula(str(element))
346        if len(myformula.atoms) != 1:
[49ab5d7]347            return
348        element = myformula.atoms.keys()[0]
[810f196]349        energy = xray_energy(element.K_alpha)
[49ab5d7]350
[4752c31]351        self.sld_formula = formula(str(self.compound), density=self.density)
[810f196]352        atom = self.sld_formula.atoms
[49ab5d7]353        return xray_sld_from_atoms(atom, density=self.density, energy=energy)
354
[427fa87]355    def check_inputs(self):
356        """Check validity user inputs"""
[39e49a1]357        flag = True
[66bfacf]358        msg = ""
[427fa87]359        if check_float(self.density_ctl):
360            self.density = float(self.density_ctl.GetValue())
361        else:
[39e49a1]362            flag = False
[66bfacf]363            msg += "Error for Density value :expect float"
[49ab5d7]364
[39e49a1]365        self.wavelength = self.wavelength_ctl.GetValue()
[4752c31]366        if str(self.wavelength).lstrip().rstrip() == "":
[810f196]367            self.wavelength = WAVELENGTH
[66bfacf]368            self.wavelength_ctl.SetValue(str(WAVELENGTH))
369            self.wavelength_ctl.SetBackgroundColour(wx.WHITE)
370            self.wavelength_ctl.Refresh()
371            msg += "Default value for wavelength is 6.0"
[427fa87]372        else:
373            if check_float(self.wavelength_ctl):
[39e49a1]374                self.wavelength = float(self.wavelength)
[427fa87]375            else:
376                flag = False
[66bfacf]377                msg += "Error for wavelength value :expect float"
[49ab5d7]378
[810f196]379        self.compound = self.compound_ctl.GetValue().lstrip().rstrip()
380        if self.compound != "":
[66bfacf]381            try :
382                formula(self.compound)
383                self.compound_ctl.SetBackgroundColour(wx.WHITE)
384                self.compound_ctl.Refresh()
385            except:
386                self.compound_ctl.SetBackgroundColour("pink")
387                self.compound_ctl.Refresh()
388                flag = False
389                msg += "Enter correct formula"
[427fa87]390        else:
391            self.compound_ctl.SetBackgroundColour("pink")
392            self.compound_ctl.Refresh()
[39e49a1]393            flag = False
[66bfacf]394            msg += "Enter a formula"
395        return flag, msg
[49ab5d7]396
[810f196]397    def calculate_sld_helper(self, element, density, molecule_formula):
398        """
399        Get an element and compute the corresponding SLD for a given formula
[49ab5d7]400
[810f196]401        :param element:  elements a string of existing atom
[49ab5d7]402
[810f196]403        """
404        element_formula = formula(str(element))
405        if len(element_formula.atoms) != 1:
[49ab5d7]406            return
407        element = element_formula.atoms.keys()[0]
[810f196]408        energy = xray_energy(element.K_alpha)
409        atom = molecule_formula.atoms
410        return xray_sld_from_atoms(atom, density=density, energy=energy)
411
412
[427fa87]413    def calculateSld(self, event):
414        """
415            Calculate the neutron scattering density length of a molecule
416        """
[66bfacf]417        self.clear_outputs()
[427fa87]418        try:
419            #Check validity user inputs
[66bfacf]420            flag, msg = self.check_inputs()
421            if self.base is not None and msg.lstrip().rstrip() != "":
422                msg = "SLD Calculator: %s" % str(msg)
423                wx.PostEvent(self.base, StatusEvent(status=msg))
424            if not flag:
[49ab5d7]425               return
[66bfacf]426            #get ready to compute
427            self.sld_formula = formula(self.compound,
428                                            density=self.density)
429            (sld_real, sld_im, _), (_, absorp, incoh), \
430                        length = neutron_scattering(compound=self.compound,
[49ab5d7]431                                   density=self.density,
432                                   wavelength=self.wavelength)
[66bfacf]433            cu_real, cu_im = self.calculate_sld_helper(element="Cu",
434                                                 density=self.density,
435                                        molecule_formula=self.sld_formula)
[49ab5d7]436            mo_real, mo_im = self.calculate_sld_helper(element="Mo",
[66bfacf]437                                                       density=self.density,
438                                     molecule_formula=self.sld_formula)
439            # set neutron sld values
440            val = format_number(sld_real * _SCALE)
[1e96a66]441            self.neutron_sld_real_ctl.SetValue(val)
[66bfacf]442            val = format_number(math.fabs(sld_im) * _SCALE)
443            self.neutron_sld_im_ctl.SetValue(val)
444            # Compute the Cu SLD
[49ab5d7]445            self.cu_ka_sld_real_ctl.SetValue(format_number(cu_real * _SCALE))
446            val = format_number(math.fabs(cu_im) * _SCALE)
[66bfacf]447            self.cu_ka_sld_im_ctl.SetValue(val)
448            # Compute the Mo SLD
[49ab5d7]449            self.mo_ka_sld_real_ctl.SetValue(format_number(mo_real * _SCALE))
450            val = format_number(math.fabs(mo_im) * _SCALE)
[66bfacf]451            self.mo_ka_sld_im_ctl.SetValue(val)
452            # set incoherence and absorption
453            self.neutron_inc_ctl.SetValue(format_number(incoh))
454            self.neutron_abs_ctl.SetValue(format_number(absorp))
455            # Neutron length
456            self.neutron_length_ctl.SetValue(format_number(length))
457            # display wavelength
458            self.wavelength_ctl.SetValue(str(self.wavelength))
[427fa87]459        except:
[39e49a1]460            if self.base is not None:
[49ab5d7]461                msg = "SLD Calculator: %s" % (sys.exc_value)
[b0ab6cb]462                wx.PostEvent(self.base, StatusEvent(status=msg))
463        if event is not None:
464            event.Skip()
[49ab5d7]465
[66bfacf]466    def clear_outputs(self):
467        """
468        Clear the outputs textctrl
469        """
[1e96a66]470        self.neutron_sld_real_ctl.SetValue("")
[66bfacf]471        self.neutron_sld_im_ctl.SetValue("")
[1e96a66]472        self.mo_ka_sld_real_ctl.SetValue("")
[66bfacf]473        self.mo_ka_sld_im_ctl.SetValue("")
[1e96a66]474        self.cu_ka_sld_real_ctl.SetValue("")
[66bfacf]475        self.cu_ka_sld_im_ctl.SetValue("")
476        self.neutron_abs_ctl.SetValue("")
477        self.neutron_inc_ctl.SetValue("")
478        self.neutron_length_ctl.SetValue("")
[49ab5d7]479
480
[d0923a3]481class SldWindow(widget.CHILD_FRAME):
[b0ab6cb]482    """
483    """
[44148f0]484    def __init__(self, parent=None, title="SLD Calculator",
[49ab5d7]485                  base=None, manager=None,
[ae84427]486                  size=(PANEL_SIZE, PANEL_SIZE), *args, **kwds):
[b0ab6cb]487        """
488        """
[44148f0]489        kwds['title'] = title
490        kwds['size'] = size
[d0923a3]491        widget.CHILD_FRAME.__init__(self, parent, *args, **kwds)
[b0ab6cb]492        """
493        """
[ae84427]494        self.parent = parent
495        self.base = base
496        self.manager = manager
[427fa87]497        self.panel = SldPanel(self, base=base)
[ae84427]498        self.Bind(wx.EVT_CLOSE, self.on_close)
[d5419f7f]499        self.SetPosition((wx.LEFT, PANEL_TOP))
[427fa87]500        self.Show(True)
[49ab5d7]501
[ae84427]502    def on_close(self, event):
503        """
504        On close event
505        """
506        if self.manager != None:
507            self.manager.sld_frame = None
508        self.Destroy()
[49ab5d7]509
510
[427fa87]511class ViewApp(wx.App):
[b0ab6cb]512    """
513    """
[427fa87]514    def OnInit(self):
[b0ab6cb]515        """
516        """
[62af27a9]517        widget.CHILD_FRAME = wx.Frame
[49ab5d7]518        frame = SldWindow(None, title='SLD Calculator')
[427fa87]519        frame.Show(True)
520        self.SetTopWindow(frame)
521        return True
522
[49ab5d7]523
524if __name__ == "__main__":
[427fa87]525    app = ViewApp(0)
[49ab5d7]526    app.MainLoop()
Note: See TracBrowser for help on using the repository browser.