1 | """ |
---|
2 | This module is a small tool to allow user to quickly |
---|
3 | determine the slit length value of data. |
---|
4 | """ |
---|
5 | from numpy import * |
---|
6 | |
---|
7 | class SlitlengthCalculator(object): |
---|
8 | """ |
---|
9 | compute slit length from SAXSess beam profile (1st col. Q , 2nd col. I , |
---|
10 | and 3rd col. dI.: don't need the 3rd) |
---|
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 | |
---|
21 | # The unit is unknown from SAXSess profile: |
---|
22 | # It seems 1/nm but it could be not fixed, |
---|
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 | """ |
---|
28 | set data |
---|
29 | :param x, y: x array and y array |
---|
30 | """ |
---|
31 | self.x = x |
---|
32 | self.y = y |
---|
33 | |
---|
34 | def calculate_slit_length(self): |
---|
35 | """ |
---|
36 | Calculate slit length. |
---|
37 | """ |
---|
38 | # None data do nothing |
---|
39 | if self.y == None or self.x == None: |
---|
40 | return |
---|
41 | # set local variable |
---|
42 | y = self.y |
---|
43 | x = self.x |
---|
44 | |
---|
45 | # find max y |
---|
46 | max_y = y.max() |
---|
47 | |
---|
48 | # initial values |
---|
49 | y_sum = 0.0 |
---|
50 | y_max = 0.0 |
---|
51 | ind = 0.0 |
---|
52 | |
---|
53 | # sum 10 or more y values until getting max_y, |
---|
54 | while (True): |
---|
55 | if ind >= 10 and y_max == max_y: |
---|
56 | break |
---|
57 | y_sum = y_sum + y[ind] |
---|
58 | if y[ind] > y_max: y_max = y[ind] |
---|
59 | ind += 1 |
---|
60 | |
---|
61 | # find the average value/2 of the top values |
---|
62 | y_half = y_sum/(2.0*ind) |
---|
63 | |
---|
64 | # defaults |
---|
65 | y_half_d = 0.0 |
---|
66 | ind = 0.0 |
---|
67 | # find indices where it crosses y = y_half. |
---|
68 | while (True): |
---|
69 | ind += 1 # no need to check when ind == 0 |
---|
70 | y_half_d = y[ind] # y value and ind just after passed the spot of the half height |
---|
71 | if y[ind] < y_half: break |
---|
72 | |
---|
73 | y_half_u = y[ind-1] # y value and ind just before passed the spot of the half height |
---|
74 | |
---|
75 | # get corresponding x values |
---|
76 | x_half_d = x[ind] |
---|
77 | x_half_u = x[ind-1] |
---|
78 | |
---|
79 | # calculate x at y = y_half using linear interpolation |
---|
80 | if y_half_u == y_half_d: |
---|
81 | x_half = (x_half_d + x_half_u)/2.0 |
---|
82 | else: |
---|
83 | x_half = (x_half_u * (y_half - y_half_d) + x_half_d*(y_half_u-y_half))/(y_half_u - y_half_d) |
---|
84 | |
---|
85 | # multiply by 2 due to the beam profile is for half beam |
---|
86 | slit_length = 2.0 * x_half |
---|
87 | |
---|
88 | # set slit_length |
---|
89 | self.slit_length = slit_length |
---|
90 | return self.slit_length |
---|
91 | |
---|
92 | def get_slit_length_unit(self): |
---|
93 | """ |
---|
94 | return the slit length unit. |
---|
95 | """ |
---|
96 | return self.slit_length_unit |
---|