source: sasview/src/sas/perspectives/calculator/sld_panel.py @ a6f3613

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 a6f3613 was d5419f7f, checked in by butler, 10 years ago
  • Fixed problem with mac vs pc positioning of tool panels for now -

opened ticket to completely rethink panels in mac vs pc

  • Added close button/activated close method for tool panels that did not

have it. Tool panels are now much more consistant.

  • Property mode set to 100644
File size: 21.0 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
[79492222]10from sas.guiframe.panel_base import PanelBase
[e696e0a]11
[79492222]12from sas.guiframe.utils import format_number
13from sas.guiframe.utils import check_float
[49ab5d7]14from sas.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
[49ab5d7]22from sas.perspectives.calculator import calculator_widgets as widget
[fc8f132]23from sas.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
[fc8f132]327        _TreeLocation = "user/perspectives/calculator/sld_calculator_help.html"
328        _doc_viewer = DocumentationWindow(self, -1, \
[49ab5d7]329             _TreeLocation, "General Scattering Calculator Help")
[fc8f132]330
[d5419f7f]331    def on_close(self, event):
332        """
333        close the window containing this panel
334        """
335        self.parent.Close()
336
[810f196]337    def calculate_xray_sld(self, element):
338        """
339        Get an element and compute the corresponding SLD for a given formula
[49ab5d7]340
[810f196]341        :param element:  elements a string of existing atom
[49ab5d7]342
[810f196]343        """
344        myformula = formula(str(element))
345        if len(myformula.atoms) != 1:
[49ab5d7]346            return
347        element = myformula.atoms.keys()[0]
[810f196]348        energy = xray_energy(element.K_alpha)
[49ab5d7]349
[4752c31]350        self.sld_formula = formula(str(self.compound), density=self.density)
[810f196]351        atom = self.sld_formula.atoms
[49ab5d7]352        return xray_sld_from_atoms(atom, density=self.density, energy=energy)
353
[427fa87]354    def check_inputs(self):
355        """Check validity user inputs"""
[39e49a1]356        flag = True
[66bfacf]357        msg = ""
[427fa87]358        if check_float(self.density_ctl):
359            self.density = float(self.density_ctl.GetValue())
360        else:
[39e49a1]361            flag = False
[66bfacf]362            msg += "Error for Density value :expect float"
[49ab5d7]363
[39e49a1]364        self.wavelength = self.wavelength_ctl.GetValue()
[4752c31]365        if str(self.wavelength).lstrip().rstrip() == "":
[810f196]366            self.wavelength = WAVELENGTH
[66bfacf]367            self.wavelength_ctl.SetValue(str(WAVELENGTH))
368            self.wavelength_ctl.SetBackgroundColour(wx.WHITE)
369            self.wavelength_ctl.Refresh()
370            msg += "Default value for wavelength is 6.0"
[427fa87]371        else:
372            if check_float(self.wavelength_ctl):
[39e49a1]373                self.wavelength = float(self.wavelength)
[427fa87]374            else:
375                flag = False
[66bfacf]376                msg += "Error for wavelength value :expect float"
[49ab5d7]377
[810f196]378        self.compound = self.compound_ctl.GetValue().lstrip().rstrip()
379        if self.compound != "":
[66bfacf]380            try :
381                formula(self.compound)
382                self.compound_ctl.SetBackgroundColour(wx.WHITE)
383                self.compound_ctl.Refresh()
384            except:
385                self.compound_ctl.SetBackgroundColour("pink")
386                self.compound_ctl.Refresh()
387                flag = False
388                msg += "Enter correct formula"
[427fa87]389        else:
390            self.compound_ctl.SetBackgroundColour("pink")
391            self.compound_ctl.Refresh()
[39e49a1]392            flag = False
[66bfacf]393            msg += "Enter a formula"
394        return flag, msg
[49ab5d7]395
[810f196]396    def calculate_sld_helper(self, element, density, molecule_formula):
397        """
398        Get an element and compute the corresponding SLD for a given formula
[49ab5d7]399
[810f196]400        :param element:  elements a string of existing atom
[49ab5d7]401
[810f196]402        """
403        element_formula = formula(str(element))
404        if len(element_formula.atoms) != 1:
[49ab5d7]405            return
406        element = element_formula.atoms.keys()[0]
[810f196]407        energy = xray_energy(element.K_alpha)
408        atom = molecule_formula.atoms
409        return xray_sld_from_atoms(atom, density=density, energy=energy)
410
411
[427fa87]412    def calculateSld(self, event):
413        """
414            Calculate the neutron scattering density length of a molecule
415        """
[66bfacf]416        self.clear_outputs()
[427fa87]417        try:
418            #Check validity user inputs
[66bfacf]419            flag, msg = self.check_inputs()
420            if self.base is not None and msg.lstrip().rstrip() != "":
421                msg = "SLD Calculator: %s" % str(msg)
422                wx.PostEvent(self.base, StatusEvent(status=msg))
423            if not flag:
[49ab5d7]424               return
[66bfacf]425            #get ready to compute
426            self.sld_formula = formula(self.compound,
427                                            density=self.density)
428            (sld_real, sld_im, _), (_, absorp, incoh), \
429                        length = neutron_scattering(compound=self.compound,
[49ab5d7]430                                   density=self.density,
431                                   wavelength=self.wavelength)
[66bfacf]432            cu_real, cu_im = self.calculate_sld_helper(element="Cu",
433                                                 density=self.density,
434                                        molecule_formula=self.sld_formula)
[49ab5d7]435            mo_real, mo_im = self.calculate_sld_helper(element="Mo",
[66bfacf]436                                                       density=self.density,
437                                     molecule_formula=self.sld_formula)
438            # set neutron sld values
439            val = format_number(sld_real * _SCALE)
[1e96a66]440            self.neutron_sld_real_ctl.SetValue(val)
[66bfacf]441            val = format_number(math.fabs(sld_im) * _SCALE)
442            self.neutron_sld_im_ctl.SetValue(val)
443            # Compute the Cu SLD
[49ab5d7]444            self.cu_ka_sld_real_ctl.SetValue(format_number(cu_real * _SCALE))
445            val = format_number(math.fabs(cu_im) * _SCALE)
[66bfacf]446            self.cu_ka_sld_im_ctl.SetValue(val)
447            # Compute the Mo SLD
[49ab5d7]448            self.mo_ka_sld_real_ctl.SetValue(format_number(mo_real * _SCALE))
449            val = format_number(math.fabs(mo_im) * _SCALE)
[66bfacf]450            self.mo_ka_sld_im_ctl.SetValue(val)
451            # set incoherence and absorption
452            self.neutron_inc_ctl.SetValue(format_number(incoh))
453            self.neutron_abs_ctl.SetValue(format_number(absorp))
454            # Neutron length
455            self.neutron_length_ctl.SetValue(format_number(length))
456            # display wavelength
457            self.wavelength_ctl.SetValue(str(self.wavelength))
[427fa87]458        except:
[39e49a1]459            if self.base is not None:
[49ab5d7]460                msg = "SLD Calculator: %s" % (sys.exc_value)
[b0ab6cb]461                wx.PostEvent(self.base, StatusEvent(status=msg))
462        if event is not None:
463            event.Skip()
[49ab5d7]464
[66bfacf]465    def clear_outputs(self):
466        """
467        Clear the outputs textctrl
468        """
[1e96a66]469        self.neutron_sld_real_ctl.SetValue("")
[66bfacf]470        self.neutron_sld_im_ctl.SetValue("")
[1e96a66]471        self.mo_ka_sld_real_ctl.SetValue("")
[66bfacf]472        self.mo_ka_sld_im_ctl.SetValue("")
[1e96a66]473        self.cu_ka_sld_real_ctl.SetValue("")
[66bfacf]474        self.cu_ka_sld_im_ctl.SetValue("")
475        self.neutron_abs_ctl.SetValue("")
476        self.neutron_inc_ctl.SetValue("")
477        self.neutron_length_ctl.SetValue("")
[49ab5d7]478
479
[d0923a3]480class SldWindow(widget.CHILD_FRAME):
[b0ab6cb]481    """
482    """
[44148f0]483    def __init__(self, parent=None, title="SLD Calculator",
[49ab5d7]484                  base=None, manager=None,
[ae84427]485                  size=(PANEL_SIZE, PANEL_SIZE), *args, **kwds):
[b0ab6cb]486        """
487        """
[44148f0]488        kwds['title'] = title
489        kwds['size'] = size
[d0923a3]490        widget.CHILD_FRAME.__init__(self, parent, *args, **kwds)
[b0ab6cb]491        """
492        """
[ae84427]493        self.parent = parent
494        self.base = base
495        self.manager = manager
[427fa87]496        self.panel = SldPanel(self, base=base)
[ae84427]497        self.Bind(wx.EVT_CLOSE, self.on_close)
[d5419f7f]498        self.SetPosition((wx.LEFT, PANEL_TOP))
[427fa87]499        self.Show(True)
[49ab5d7]500
[ae84427]501    def on_close(self, event):
502        """
503        On close event
504        """
505        if self.manager != None:
506            self.manager.sld_frame = None
507        self.Destroy()
[49ab5d7]508
509
[427fa87]510class ViewApp(wx.App):
[b0ab6cb]511    """
512    """
[427fa87]513    def OnInit(self):
[b0ab6cb]514        """
515        """
[62af27a9]516        widget.CHILD_FRAME = wx.Frame
[49ab5d7]517        frame = SldWindow(None, title='SLD Calculator')
[427fa87]518        frame.Show(True)
519        self.SetTopWindow(frame)
520        return True
521
[49ab5d7]522
523if __name__ == "__main__":
[427fa87]524    app = ViewApp(0)
[49ab5d7]525    app.MainLoop()
Note: See TracBrowser for help on using the repository browser.