source: sasview/calculator/slit_length_calculator.py @ afeb37a

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

cleaned up some docs

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