source: sasview/src/sas/calculator/slit_length_calculator.py @ 573e7034

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 573e7034 was 79492222, checked in by krzywon, 10 years ago

Changed the file and folder names to remove all SANS references.

  • Property mode set to 100644
File size: 3.0 KB
RevLine 
[0bf2c6f]1"""
[14e9eb3]2This module is a small tool to allow user to quickly
3determine the slit length value of data.
[0bf2c6f]4"""
5
[8a621ac]6
[0bf2c6f]7class SlitlengthCalculator(object):
8    """
[14e9eb3]9    compute slit length from SAXSess beam profile (1st col. Q , 2nd col. I ,
10    and 3rd col. dI.: don't need the 3rd)
[0bf2c6f]11    """
12    def __init__(self):
13       
14        # x data
15        self.x = None
16        # y data
17        self.y = None
18        #default slit length
19        self.slit_length = 0.0
20       
[8a621ac]21        # The unit is unknown from SAXSess profile:
22        # It seems 1/nm but it could be not fixed,
[0bf2c6f]23        # so users should be notified to determine the unit by themselves.
24        self.slit_length_unit = "unknown"
25   
26    def set_data(self, x=None, y=None):
27        """
[8a621ac]28         Receive two vector x, y and prepare the slit calculator for
[69fee24]29         computation.
30       
31        :param x: array
32        :param y: array
[0bf2c6f]33        """
34        self.x = x
35        self.y = y
36       
37    def calculate_slit_length(self):
38        """
[14e9eb3]39        Calculate slit length.
[69fee24]40       
41        :return: the slit length calculated value.
[0bf2c6f]42        """
43        # None data do nothing
44        if self.y == None or self.x == None:
45            return
46        # set local variable
47        y = self.y
48        x = self.x
49
50        # find max y
51        max_y = y.max()
52       
[8a621ac]53        # initial values
[0bf2c6f]54        y_sum = 0.0
55        y_max = 0.0
56        ind = 0.0
57       
[8a621ac]58        # sum 10 or more y values until getting max_y,
[0bf2c6f]59        while (True):
60            if ind >= 10 and y_max == max_y:
61                break
62            y_sum = y_sum + y[ind]
[8a621ac]63            if y[ind] > y_max:
[d267aed]64                y_max = y[ind]
[0bf2c6f]65            ind += 1
66     
[8a621ac]67        # find the average value/2 of the top values
[0bf2c6f]68        y_half = y_sum/(2.0*ind)
69       
70        # defaults
71        y_half_d = 0.0
72        ind = 0.0
73        # find indices where it crosses y = y_half.
74        while (True):
[afeb37a]75            # no need to check when ind == 0
[8a621ac]76            ind += 1
77            # y value and ind just after passed the spot of the half height
78            y_half_d = y[ind]
79            if y[ind] < y_half:
[afeb37a]80                break
81       
82        # y value and ind just before passed the spot of the half height
[8a621ac]83        y_half_u = y[ind-1]
[0bf2c6f]84       
85        # get corresponding x values
86        x_half_d = x[ind]
[8a621ac]87        x_half_u = x[ind-1]
[0bf2c6f]88       
89        # calculate x at y = y_half using linear interpolation
90        if y_half_u == y_half_d:
91            x_half = (x_half_d + x_half_u)/2.0
92        else:
[d267aed]93            x_half = (x_half_u * (y_half - y_half_d)  \
[8a621ac]94                       + x_half_d * (y_half_u - y_half)) \
95                        / (y_half_u - y_half_d)
[0bf2c6f]96       
[74b1770]97        # Our slit length is half width, so just give half beam value
98        slit_length = x_half
[0bf2c6f]99       
100        # set slit_length
101        self.slit_length = slit_length
102        return self.slit_length
[14e9eb3]103 
[8a621ac]104    def get_slit_length_unit(self):
[0bf2c6f]105        """
[69fee24]106        :return: the slit length unit.
[0bf2c6f]107        """
108        return self.slit_length_unit
Note: See TracBrowser for help on using the repository browser.