Changeset 148f0dd in sasview


Ignore:
Timestamp:
Aug 5, 2011 10:18:28 AM (13 years ago)
Author:
Jae Cho <jhjcho@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
44429d0
Parents:
580ee7a
Message:

extended rescal to tof

File:
1 edited

Legend:

Unmodified
Added
Removed
  • calculatorview/perspectives/calculator/resolution_calculator_panel.py

    re91c179 r148f0dd  
    1010import wx 
    1111import sys 
     12import os 
    1213import matplotlib 
     14import math 
    1315#Use the WxAgg back end. The Wx one takes too long to render 
    1416matplotlib.use('WXAgg') 
     
    7476        self.sigma_phi = None 
    7577        self.sigma_1d = None 
     78        # monchromatic or polychromatic 
     79        self.wave_color = 'mono' 
     80        self.num_wave = 10 
     81        self.spectrum_dic = {} 
    7682        # dQ 2d image 
    7783        self.image = None 
     
    143149            self.source_cb.Append(source_list[idx],idx) 
    144150        self.source_cb.SetStringSelection("Neutron")  
    145          
    146         wx.EVT_COMBOBOX(self.source_cb,-1, self._on_source_selection)       
     151        wx.EVT_COMBOBOX(self.source_cb,-1, self._on_source_selection)  
     152         
     153        # combo box for color 
     154        self.wave_color_cb =  wx.ComboBox(self, -1, 
     155                                style=wx.CB_READONLY, 
     156                                name = 'color') 
     157        # two choices 
     158        self.wave_color_cb.Append('Monochromatic') 
     159        self.wave_color_cb.Append('Polychromatic') 
     160        self.wave_color_cb.SetStringSelection("Monochromatic")  
     161        wx.EVT_COMBOBOX(self.wave_color_cb,-1, self._on_source_color)  
     162         
    147163        source_hint = "Source Selection: Affect on" 
    148164        source_hint += " the gravitational contribution.\n" 
     
    151167        self.mass_txt.SetToolTipString(source_hint) 
    152168        self.mass_sizer.AddMany([(self.mass_txt, 0, wx.LEFT, 15), 
    153                                     (self.source_cb, 0, wx.LEFT, 15)])    
     169                                    (self.source_cb, 0, wx.LEFT, 15), 
     170                                    (self.wave_color_cb, 0, wx.LEFT, 15)])    
    154171         
    155172    def _layout_intensity(self): 
     
    177194        """ 
    178195        # get the wavelength 
    179         wavelength_value = str(self.resolution.wavelength) 
     196        wavelength_value = str(self.resolution.get_wavelength()) 
    180197        wavelength_unit_txt = wx.StaticText(self, -1, '[A]') 
    181198        wavelength_txt = wx.StaticText(self, -1,  
     
    186203        self.wavelength_tcl.SetValue(wavelength_value) 
    187204        self.wavelength_tcl.SetToolTipString(wavelength_hint) 
     205 
     206        # get the spectrum 
     207        spectrum_value = self.resolution.get_default_spectrum() 
     208        self.spectrum_dic['Add new'] = '' 
     209        self.spectrum_dic['Flat'] = spectrum_value 
     210         
     211        self.spectrum_txt = wx.StaticText(self, -1,  
     212                                'Spectrum: ') 
     213        self.spectrum_cb = wx.ComboBox(self, -1, 
     214                                style=wx.CB_READONLY, 
     215                                size=(_BOX_WIDTH,-1), 
     216                                name = 'spectrum') 
     217        self.spectrum_cb.Append('Add new') 
     218        self.spectrum_cb.Append('Flat') 
     219        wx.EVT_COMBOBOX(self.spectrum_cb, -1, self._on_spectrum_cb)  
     220        spectrum_hint = "Wavelength Spectrum: Intensity vs. wavelength" 
     221        #self.spectrum_cb.SetValue(spectrum_value) 
     222        self.spectrum_cb.SetStringSelection('Flat')  
     223        self.spectrum_cb.SetToolTipString(spectrum_hint) 
    188224        self.wavelength_sizer.AddMany([(wavelength_txt, 0, wx.LEFT, 15), 
    189                                     (self.wavelength_tcl, 0, wx.LEFT, 15), 
    190                                     (wavelength_unit_txt,0, wx.LEFT, 10)])    
    191           
     225                                    (self.wavelength_tcl, 0, wx.LEFT, 5), 
     226                                    (wavelength_unit_txt,0, wx.LEFT, 5), 
     227                                    (self.spectrum_txt, 0, wx.LEFT, 20), 
     228                                    (self.spectrum_cb, 0, wx.LEFT, 5)])    
     229        self.spectrum_txt.Show(False) 
     230        self.spectrum_cb.Show(False) 
    192231         
    193232    def _layout_wavelength_spread(self): 
     
    196235        """ 
    197236        # get the wavelength 
    198         wavelength_spread_value = str(self.resolution.wavelength_spread) 
     237        wavelength_spread_value = str(self.resolution.get_wavelength_spread()) 
    199238        wavelength_spread_unit_txt = wx.StaticText(self, -1, '') 
    200239        wavelength_spread_txt = wx.StaticText(self, -1,  
     
    481520        hint_msg = "" 
    482521        #hint_msg += "This tool is to approximately compute " 
    483         #hint_msg += "the resolution (dQ)." 
     522        #hint_msg += "the instrumental resolution (dQ)." 
    484523 
    485524        self.hint_txt = wx.StaticText(self, -1, hint_msg) 
     
    490529        Do the layout for the button widgets 
    491530        """  
     531        # coordinate selction removed 
    492532        #outerbox_txt = wx.StaticText(self, -1, 'Outer Box') 
    493533        #self.x_y_rb = wx.RadioButton(self, -1,"Cartesian") 
     
    507547        id = wx.NewId() 
    508548        self.compute_button = wx.Button(self, id, "Compute") 
    509         hint_on_compute = "..." 
     549        hint_on_compute = "Compute... Please wait until finished." 
    510550        self.compute_button.SetToolTipString(hint_on_compute) 
    511551        self.Bind(wx.EVT_BUTTON, self.on_compute, id=id) 
     
    642682                                    wx.EXPAND|wx.TOP|wx.BOTTOM, 5)]) 
    643683        self.main_sizer.Add(self.vertical_l_sizer, 0, wx.ALL, 10) 
     684         
    644685        # Build image plot layout                      
    645686        self._layout_image() 
     
    651692        self.SetSizer(self.main_sizer) 
    652693        self.SetAutoLayout(True) 
     694         
    653695 
    654696    def on_close(self, event): 
     
    695737        qy_min = []  
    696738        qy_max = []  
    697         # Get all the values at set to compute 
    698         #intensity = self.intensity_tcl.GetValue() 
    699         #self.resolution.set_intensity(float(intensity)) 
    700         wavelength = self.wavelength_tcl.GetValue() 
    701         self.resolution.set_wavelength(float(wavelength)) 
    702         source = self.source_cb.GetValue() 
    703         mass = self.source_mass[str(source)] 
    704         self.resolution.set_neutron_mass(float(mass)) 
    705         wavelength_spread = self.wavelength_spread_tcl.GetValue() 
    706         self.resolution.set_wavelength_spread(float(wavelength_spread)) 
    707         source_aperture_size = self.source_aperture_tcl.GetValue() 
    708         source_aperture_size = self._string2list(source_aperture_size) 
    709         self.resolution.set_source_aperture_size(source_aperture_size) 
    710         sample_aperture_size = self.sample_aperture_tcl.GetValue() 
    711         sample_aperture_size = self._string2list(sample_aperture_size) 
    712         self.resolution.set_sample_aperture_size(sample_aperture_size) 
    713         source2sample_distance = self.source2sample_distance_tcl.GetValue() 
    714         source2sample_distance = self._string2list(source2sample_distance) 
    715         self.resolution.set_source2sample_distance(source2sample_distance) 
    716         sample2sample_distance = self.sample2sample_distance_tcl.GetValue() 
    717         sample2sample_distance = self._string2list(sample2sample_distance) 
    718         self.resolution.set_sample2sample_distance(sample2sample_distance) 
    719         sample2detector_distance = self.sample2detector_distance_tcl.GetValue() 
    720         sample2detector_distance = self._string2list(sample2detector_distance) 
    721         self.resolution.set_sample2detector_distance(sample2detector_distance) 
    722         detector_size = self.detector_size_tcl.GetValue() 
    723         detector_size = self._string2list(detector_size) 
    724         self.resolution.set_detector_size(detector_size) 
    725         detector_pix_size = self.detector_pix_size_tcl.GetValue() 
    726         detector_pix_size = self._string2list(detector_pix_size) 
    727         self.resolution.set_detector_pix_size(detector_pix_size) 
    728         self.qx = self._string2inputlist(self.qx_tcl.GetValue()) 
    729         self.qy = self._string2inputlist(self.qy_tcl.GetValue()) 
    730739         
    731740        try: 
     741            # Get all the values at set to compute 
     742            # default num bin of wave list 
     743            self.num_wave = 10 
     744            wavelength = self._str2longlist(self.wavelength_tcl.GetValue()) 
     745            source = self.source_cb.GetValue() 
     746            mass = self.source_mass[str(source)] 
     747            self.resolution.set_neutron_mass(float(mass)) 
     748            wavelength_spread = self._str2longlist(\ 
     749                        self.wavelength_spread_tcl.GetValue().split(';')[0]) 
     750            # Validate the wave inputs 
     751            wave_input = self._validate_q_input(wavelength, wavelength_spread) 
     752            if wave_input != None: 
     753                wavelength, wavelength_spread = wave_input 
     754     
     755            #self.resolution.set_wave(float(wavelength)) 
     756            self.resolution.set_wave(wavelength) 
     757            #self.resolution.set_wave_spread(float(wavelength_spread)) 
     758            self.resolution.set_wave_spread(wavelength_spread) 
     759            source_aperture_size = self.source_aperture_tcl.GetValue() 
     760            source_aperture_size = self._string2list(source_aperture_size) 
     761            self.resolution.set_source_aperture_size(source_aperture_size) 
     762            sample_aperture_size = self.sample_aperture_tcl.GetValue() 
     763            sample_aperture_size = self._string2list(sample_aperture_size) 
     764            self.resolution.set_sample_aperture_size(sample_aperture_size) 
     765            source2sample_distance = self.source2sample_distance_tcl.GetValue() 
     766            source2sample_distance = self._string2list(source2sample_distance) 
     767            self.resolution.set_source2sample_distance(source2sample_distance) 
     768            sample2sample_distance = self.sample2sample_distance_tcl.GetValue() 
     769            sample2sample_distance = self._string2list(sample2sample_distance) 
     770            self.resolution.set_sample2sample_distance(sample2sample_distance) 
     771            sample2detector_distance = self.sample2detector_distance_tcl.GetValue() 
     772            sample2detector_distance = self._string2list(sample2detector_distance) 
     773            self.resolution.set_sample2detector_distance(sample2detector_distance) 
     774            detector_size = self.detector_size_tcl.GetValue() 
     775            detector_size = self._string2list(detector_size) 
     776            self.resolution.set_detector_size(detector_size) 
     777            detector_pix_size = self.detector_pix_size_tcl.GetValue() 
     778            detector_pix_size = self._string2list(detector_pix_size) 
     779            self.resolution.set_detector_pix_size(detector_pix_size) 
     780            self.qx = self._string2inputlist(self.qx_tcl.GetValue()) 
     781            self.qy = self._string2inputlist(self.qy_tcl.GetValue()) 
     782             
     783             
    732784            # Find min max of qs 
    733785            xmin = min(self.qx) 
     
    735787            ymin = min(self.qy) 
    736788            ymax = max(self.qy) 
     789            if not self._validate_q_input(self.qx, self.qy): 
     790                raise 
    737791        except: 
    738792            msg = "An error occured during the resolution computation." 
    739793            msg += "Please check your inputs..." 
    740794            self._status_info(msg, status_type) 
    741             raise ValueError, "Invalid Q Input..." 
     795            wx.MessageBox(msg, 'Warning') 
     796            return 
     797            #raise ValueError, "Invalid Q Input..." 
    742798 
    743799        # Validate the q inputs 
    744         self._validate_q_input(self.qx, self.qy) 
     800        q_input = self._validate_q_input(self.qx, self.qy) 
     801        if q_input != None: 
     802            self.qx, self.qy = q_input 
    745803         
    746804        # Make list of q min max for mapping 
     
    757815            # Clear the image before redraw 
    758816            self.image.clf() 
    759             #self.image.draw() 
    760817            # reset the image 
    761818            self.resolution.reset_image() 
     
    763820        # Compute and get the image plot 
    764821        try: 
    765             self.image = map(self._map_func, self.qx, self.qy,  
    766                              qx_min, qx_max, qy_min, qy_max)[0] 
    767             msg = "Finished the resolution computation..." 
     822            from .resolcal_thread import CalcRes as thread 
     823            cal_res = thread(func = self._map_func, 
     824                         qx = self.qx, 
     825                         qy = self.qy, 
     826                         qx_min = qx_min, 
     827                         qx_max = qx_max, 
     828                         qy_min = qy_min, 
     829                         qy_max = qy_max, 
     830                         image = self.image, 
     831                         completefn = self.complete_cal) 
     832            #self.image = map(self._map_func, self.qx, self.qy,  
     833            #                 qx_min, qx_max, qy_min, qy_max)[0] 
     834            cal_res.queue() 
     835            msg = "Computation is in progress..." 
     836            #msg = "Finished the resolution computation..." 
     837            status_type = 'progress' 
    768838            self._status_info(msg, status_type) 
    769839        except: 
     840            raise 
    770841            msg = "An error occured during the resolution computation." 
    771842            msg += "Please check your inputs..." 
     843            status_type = 'stop' 
    772844            self._status_info(msg, status_type) 
    773             raise ValueError, "Invalid Q Input: Out of detector range..." 
    774          
     845            wx.MessageBox(msg, 'Warning') 
     846             
     847    def complete(self, image, elapsed=None): 
     848        """ 
     849        Callafter complete: wx call after needed for stable output 
     850        """ 
     851        wx.CallAfter(self.complte, image, elapsed)    
     852         
     853    def complete_cal(self, image, elapsed=None): 
     854        """ 
     855        Complete computation 
     856        """ 
     857        self.image = image 
    775858        # Draw lines in image before drawing 
    776         self._draw_lines(self.image) 
     859        wave_list, _ = self.resolution.get_wave_list() 
     860        if len(wave_list) > 1 and wave_list[-1] == max(wave_list): 
     861            # draw a green rectangle(limit for the longest wavelength  
     862            # to be involved) for tof inputs 
     863            self._draw_lines(self.image, color='g') 
     864        self._draw_lines(self.image, color='r') 
    777865        # Draw image 
    778866        self.image.draw() 
     
    790878        self.sigma_lamd_tcl.SetValue(str(sigma_lamd)) 
    791879        self.sigma_1d_tcl.SetValue(str(sigma_1d)) 
    792          
    793     def _draw_lines(self, image = None): 
     880        msg = "Resolution computation is finished." 
     881        status_type = 'stop' 
     882        self._status_info(msg, status_type) 
     883         
     884    def _draw_lines(self, image=None, color='r'): 
    794885        """ 
    795886        Draw lines in image if applicable 
     
    798889        if image == None: 
    799890            return 
    800          
    801         # Get the params from resolution 
    802         # ploting range 
    803         qx_min = self.resolution.qx_min 
    804         qx_max = self.resolution.qx_max 
    805         qy_min = self.resolution.qy_min 
    806         qy_max = self.resolution.qy_max 
    807          
    808         # detector range 
    809         detector_qx_min = self.resolution.detector_qx_min 
    810         detector_qx_max = self.resolution.detector_qx_max 
    811         detector_qy_min = self.resolution.detector_qy_min 
    812         detector_qy_max = self.resolution.detector_qy_max 
     891        if color == 'g': 
     892            # Get the params from resolution 
     893            # ploting range for largest wavelength 
     894            qx_min = self.resolution.qx_min 
     895            qx_max = self.resolution.qx_max 
     896            qy_min = self.resolution.qy_min 
     897            qy_max = self.resolution.qy_max 
     898            # detector range 
     899            detector_qx_min = self.resolution.detector_qx_min 
     900            detector_qx_max = self.resolution.detector_qx_max 
     901            detector_qy_min = self.resolution.detector_qy_min 
     902            detector_qy_max = self.resolution.detector_qy_max 
     903        else: 
     904            qx_min, qx_max, qy_min, qy_max = self.resolution.get_detector_qrange() 
     905            # detector range 
     906            detector_qx_min = self.resolution.qxmin_limit 
     907            detector_qx_max = self.resolution.qxmax_limit 
     908            detector_qy_min = self.resolution.qymin_limit 
     909            detector_qy_max = self.resolution.qymax_limit 
    813910         
    814911        # Draw zero axis lines 
     
    829926                               xmin = x_min, 
    830927                               xmax = x_max,  
    831                                linewidth = 2, color='r') 
     928                               linewidth = 2, color=color) 
    832929        if detector_qy_max <= qy_max: 
    833930            image.axhline(y = detector_qy_max - 0.0002,  
    834931                               xmin = x_min,  
    835932                               xmax = x_max,  
    836                                linewidth = 2, color='r') 
     933                               linewidth = 2, color=color) 
    837934        if detector_qx_min >= qx_min: 
    838935            image.axvline(x = detector_qx_min + 0.0002,  
    839936                               ymin = y_min,  
    840937                               ymax = y_max,  
    841                                linewidth = 2, color='r') 
     938                               linewidth = 2, color=color) 
    842939        if detector_qx_max <= qx_max: 
    843940            image.axvline(x = detector_qx_max - 0.0002,  
    844941                               ymin = y_min,  
    845942                               ymax = y_max,  
    846                                linewidth = 2, color='r') 
    847  
     943                               linewidth = 2, color=color) 
     944        xmin = min(self.qx) 
     945        xmax = max(self.qx) 
     946        ymin = min(self.qy) 
     947        ymax = max(self.qy) 
     948        if color != 'g': 
     949            if xmin < detector_qx_min or xmax > detector_qx_max or \ 
     950                        ymin < detector_qy_min or ymax > detector_qy_max: 
     951                # message 
     952                status_type = 'stop'  
     953                msg = 'At least one q value located out side of\n' 
     954                msg += " the detector range (%s < qx < %s, %s < qy < %s),\n" % \ 
     955                        (self.format_number(detector_qx_min),  
     956                         self.format_number(detector_qx_max), 
     957                         self.format_number(detector_qy_min),  
     958                         self.format_number(detector_qy_max)) 
     959                msg += " is ignored in computation.\n" 
     960                 
     961                self._status_info(msg, status_type) 
     962                wx.MessageBox(msg, 'Warning') 
     963         
    848964    def _map_func(self, qx, qy, qx_min, qx_max, qy_min, qy_max):     
    849965        """ 
     
    853969        : return: image (pylab) 
    854970        """ 
     971        try: 
     972            qx_value = float(qx) 
     973            qy_value = float(qy) 
     974        except: 
     975            raise 
    855976        # calculate 2D resolution distribution image 
    856         image = self.resolution.compute_and_plot(float(qx), float(qy),  
     977        image = self.resolution.compute_and_plot(qx_value, qy_value,  
    857978                                 qx_min, qx_max, qy_min, qy_max,  
    858979                                 self.det_coordinate) 
     980         
     981 
    859982        return image 
    860983     
     
    869992        # check qualifications 
    870993        if qx.__class__.__name__ != 'list': 
    871             return False 
     994            return None 
    872995        if qy.__class__.__name__ != 'list' : 
    873             return False 
     996            return None 
    874997        if len(qx) < 1: 
    875             return False 
     998            return None 
    876999        if len(qy) < 1: 
    877             return False 
     1000            return None 
    8781001        # allow one input 
    8791002        if len(qx) == 1 and len(qy) > 1: 
    8801003            qx = [qx[0] for ind in range(len(qy))] 
    881             self.qx = qx 
     1004            #self.qx = qx 
    8821005        if len(qy) == 1 and len(qx) > 1: 
    8831006            qy = [qy[0] for ind in range(len(qx))] 
    884             self.qy = qy 
     1007            #self.qy = qy 
    8851008        # check length 
    8861009        if len(qx) != len(qy): 
    887             return False 
    888  
    889         return True   
     1010            return None 
     1011        if qx == None or qy == None: 
     1012            return None 
     1013        return qx, qy  
    8901014      
    8911015    def on_reset(self, event): 
     
    9021026        self.source_cb.SetValue('Neutron') 
    9031027        self._on_source_selection(None) 
     1028        self.wave_color_cb.SetValue('Monochromatic') 
     1029        self._on_source_color(None) 
    9041030        #self.intensity_tcl.SetValue(str(self.resolution.intensity)) 
    905         self.wavelength_tcl.SetValue(str(self.resolution.wavelength)) 
    906         self.wavelength_spread_tcl.SetValue(\ 
    907                                         str(self.resolution.wavelength_spread)) 
     1031        self.wavelength_tcl.SetValue(str(6.0)) 
     1032        self.wavelength_spread_tcl.SetValue(str(0.125)) 
     1033        self.resolution.set_spectrum(self.spectrum_dic['Flat']) 
     1034        self.spectrum_txt.Show(False) 
     1035        self.spectrum_cb.Show(False) 
    9081036        source_aperture_value = str(self.resolution.source_aperture_size[0]) 
    9091037        if len(self.resolution.source_aperture_size)>1: 
     
    10121140         
    10131141        return new_string 
     1142     
     1143    def _str2longlist(self, string): 
     1144        """ 
     1145        Change NNN, NNN,... to list, NNN - NNN ; NNN to list, or float to list 
     1146         
     1147        : return new_string: string like list 
     1148        """ 
     1149        new_string = [] 
     1150        msg = "Wrong format of intputs." 
     1151        try: 
     1152            if self.wave_color.lower().count('poly') > 0: 
     1153                wx.MessageBox(msg, 'Warning') 
     1154            else: 
     1155                # is float 
     1156                out = [float(string)] 
     1157                return out 
     1158        except: 
     1159            if self.wave_color.lower().count('mono') > 0: 
     1160                wx.MessageBox(msg, 'Warning') 
     1161            else: 
     1162                try: 
     1163                    # has a '-'  
     1164                    if string.count('-') > 0: 
     1165                        value = string.split('-') 
     1166                        if value[1].count(';') > 0: 
     1167                            # has a ';' 
     1168                            last_list = value[1].split(';') 
     1169                            num =  math.ceil(float(last_list[1])) 
     1170                            max = float(last_list[0]) 
     1171                            self.num_wave = num 
     1172                        else: 
     1173                            # default num 
     1174                            num = self.num_wave 
     1175                            max = float(value[1]) 
     1176                        min = float(value[0]) 
     1177                        # make a list 
     1178                        bin_size = math.fabs(max - min) / (num - 1) 
     1179                        out = [min + bin_size * bnum for bnum in range(num)] 
     1180                        return out 
     1181                    if string.count(',') > 0: 
     1182                        out = self._string2inputlist(string) 
     1183                        return out 
     1184                except: 
     1185                    pass 
     1186                #    wx.MessageBox(msg, 'Warning') 
    10141187 
    10151188    def _on_xy_coordinate(self,event=None): 
     
    10411214        Status msg 
    10421215        """ 
     1216        if type == "stop": 
     1217            label = "Compute" 
     1218            able = True 
     1219        else:    
     1220            label = "Wait..." 
     1221            able = False 
     1222        self.compute_button.Enable(able) 
     1223        self.compute_button.SetLabel(label) 
     1224        self.compute_button.SetToolTipString(label) 
    10431225        if self.parent.parent != None: 
    10441226                wx.PostEvent(self.parent.parent,  
     
    10711253        self.mass_txt.ToolTip.SetTip(source_hint) 
    10721254         
     1255    def _on_source_color(self, event = None): 
     1256        """ 
     1257        On source color combobox selection 
     1258        """ 
     1259        if event != None: 
     1260            #combo = event.GetEventObject() 
     1261            event.Skip() 
     1262        #else: 
     1263        combo = self.wave_color_cb 
     1264        selection = combo.GetValue() 
     1265        self.wave_color = selection 
     1266        if self.wave_color.lower() == 'polychromatic': 
     1267            list = self.resolution.get_wave_list() 
     1268            minw = min(list[0]) 
     1269            if len(list[0]) < 2: 
     1270                maxw = 2 * minw 
     1271            else: 
     1272                maxw = max(list[0]) 
     1273            self.wavelength_tcl.SetValue('%s - %s' % (minw, maxw)) 
     1274            minw = min(list[1]) 
     1275            maxw = max(list[1]) 
     1276            self.wavelength_spread_tcl.SetValue('%s - %s' % (minw, maxw)) 
     1277            spectrum_val = self.spectrum_cb.GetValue() 
     1278            self.resolution.set_spectrum(self.spectrum_dic[spectrum_val]) 
     1279            self.spectrum_txt.Show(True) 
     1280            self.spectrum_cb.Show(True) 
     1281           
     1282        else: 
     1283            wavelength = self.resolution.get_wavelength() 
     1284            wavelength_spread = self.resolution.get_wavelength_spread() 
     1285            self.wavelength_tcl.SetValue(str(wavelength)) 
     1286            self.wavelength_spread_tcl.SetValue(str(wavelength_spread)) 
     1287            self.resolution.set_spectrum(self.spectrum_dic['Flat']) 
     1288            self.spectrum_txt.Show(False) 
     1289            self.spectrum_cb.Show(False) 
     1290        self.wavelength_sizer.Layout()  
     1291        self.Layout() 
     1292         
     1293    def _on_spectrum_cb(self, event=None): 
     1294        """ 
     1295        On spectrum ComboBox event 
     1296        """ 
     1297        if event != None: 
     1298            combo = event.GetEventObject() 
     1299            event.Skip() 
     1300        else: 
     1301            raise 
     1302        selection = self.spectrum_cb.GetValue() 
     1303        if selection == 'Add new': 
     1304            path = self._selectDlg() 
     1305            if path == None: 
     1306                self.spectrum_cb.SetValue('Flat') 
     1307                self.resolution.set_spectrum(self.spectrum_dic['Flat']) 
     1308                msg = "No file has been chosen." 
     1309                wx.MessageBox(msg, 'Info') 
     1310                return 
     1311            try: 
     1312                basename  = os.path.basename(path) 
     1313                if basename not in self.spectrum_dic.keys(): 
     1314                    self.spectrum_cb.Append(basename) 
     1315                self.spectrum_dic[basename] = self._read_file(path) 
     1316                self.spectrum_cb.SetValue(basename) 
     1317                self.resolution.set_spectrum(self.spectrum_dic[basename]) 
     1318                return 
     1319            except: 
     1320                raise 
     1321                msg = "Failed to load the spectrum data file." 
     1322                wx.MessageBox(msg, 'Warning') 
     1323                raise ValueError, "Invalid spectrum file..." 
     1324         
     1325        self.resolution.set_spectrum(self.spectrum_dic[selection]) 
     1326                              
     1327    def _selectDlg(self): 
     1328        """ 
     1329        open a dialog file to select a customized spectrum 
     1330        """ 
     1331        import os 
     1332        dlg = wx.FileDialog(self,  
     1333                "Choose a wavelength spectrum file: Intensity vs. wavelength", 
     1334                self.parent.parent._default_save_location , "",  
     1335                "*.*", wx.OPEN) 
     1336        path = None 
     1337        if dlg.ShowModal() == wx.ID_OK: 
     1338            path = dlg.GetPath() 
     1339        dlg.Destroy() 
     1340        return path 
     1341         
     1342    def _read_file(self, path): 
     1343        """ 
     1344        Read two columns file as tuples of numpy array 
     1345         
     1346        :param path: the path to the file to read 
     1347         
     1348        """ 
     1349        try: 
     1350            if path==None: 
     1351                wx.PostEvent(self.parent.parent, StatusEvent(status=\ 
     1352                            " Selected Distribution was not loaded: %s"%path)) 
     1353                return None, None 
     1354            input_f = open(path, 'r') 
     1355            buff = input_f.read() 
     1356            lines = buff.split('\n') 
     1357             
     1358            wavelength = [] 
     1359            intensity = [] 
     1360            for line in lines: 
     1361                toks = line.split() 
     1362                try: 
     1363                    wave = float(toks[0]) 
     1364                    intens = float(toks[1]) 
     1365                    wavelength.append(wave) 
     1366                    intensity.append(intens) 
     1367                except: 
     1368                    # Skip non-data lines 
     1369                    pass 
     1370                 
     1371            return [wavelength, intensity] 
     1372        except: 
     1373            raise  
     1374         
    10731375class ResolutionWindow(wx.Frame): 
    10741376    def __init__(self, parent = None, title = "SANS Resolution Estimator", 
Note: See TracChangeset for help on using the changeset viewer.