diff --git a/src/toolbox_scs/detectors/hrixs.py b/src/toolbox_scs/detectors/hrixs.py index 615e88f60ba84dd4d72aa111edbb3f9eb7d3a67e..6b9f7ea08e949e7781a27d8bebe00e717ccaceeb 100644 --- a/src/toolbox_scs/detectors/hrixs.py +++ b/src/toolbox_scs/detectors/hrixs.py @@ -392,23 +392,63 @@ class hRIXS: self.CURVE_B, self.CURVE_A, *_ = args return self.CURVE_A, self.CURVE_B + def centroid(self, data, bins=None, return_hits=False): + #************************************************************* + # Carry out hit finding on data and bin them in grid + # Allows for + # - redifining the bins + # - extraction of the hit positions from each image + # The function will use dark images and hot pixel mask as + # given by + # self.USE_DARK + # self.USE_DARK_MASK + #************************************************************* + #************************************************************* + # If new bins are given, use them + #************************************************************* if bins is None: bins = self.BINS + #************************************************************* + # Define empty arrays and matrix for the output data + #************************************************************* hit_x = [] hit_y = [] hits = [] ret = np.zeros((len(data["hRIXS_det"]), bins)) + #************************************************************* + # Handle each Aq image separately + #************************************************************* for image, r in zip(data["hRIXS_det"], ret): use_image = image.to_numpy() - if self.USE_DARK_MASK: - use_image[self.dark_mask] = np.mean(use_image[self.MASK_AVG_Y, - self.MASK_AVG_X], (0, 1)) - if self.USE_DARK: - use_image = use_image - self.dark_im_array_m + #************************************************************* + # Treat background by optionally + # -subtracting dark image (self.USE_DARK) + # -masking the hot pixels (self.USE_DARK_MASK) + # Diffrent combinations of the two flags result + # in different actions + #************************************************************* + if self.USE_DARK: + #*************************************** + # subtract dark image + #*************************************** + use_image = use_image - self.dark_im_array + if self.USE_DARK_MASK: + #*************************************** + # set masked pixels 0 + #*************************************** + use_image[self.dark_mask] = 0 else: - if self.USE_DARK: - use_image = use_image - self.dark_im_array + #*************************************** + # dont subtract dark image, but set hot + # pixels to a dark baseline value + #*************************************** + if self.USE_DARK_MASK: + use_image[self.dark_mask] = np.mean(use_image[self.MASK_AVG_Y, + self.MASK_AVG_X], (0, 1)) + #************************************************************* + # Run centroiding on the preprocessed image + #************************************************************* c = centroid( use_image[self.X_RANGE, self.Y_RANGE].T, threshold=self.THRESHOLD, @@ -416,19 +456,26 @@ class hRIXS: curvature=(self.CURVE_A, self.CURVE_B)) if not len(c): continue - rc = np.array(c) - + #************************************************************* + # If hits have been requested, append the hit data of the + # image to the lists of hit lists + #************************************************************* if return_hits: hit_x.append(rc[:, 0]) hit_y.append(rc[:, 1]) hits.append(rc) - + #************************************************************* + # Assign the spectrum to the spectrum matrix ret. Iteration + # variable r points to the proper location of ret. + #************************************************************* hy, hx = np.histogram( rc[:, 0], bins=bins, range=(0, self.Y_RANGE.stop - self.Y_RANGE.start)) r[:] = hy - + #************************************************************* + # Setup and assing a linear energy grid + #************************************************************* data = data.assign_coords( energy=np.linspace(self.Y_RANGE.start, self.Y_RANGE.stop, bins) * self.ENERGY_SLOPE + self.ENERGY_INTERCEPT) @@ -444,7 +491,7 @@ class hRIXS: #********************************************** data = data.assign(spectrum=(("trainId", "energy"), ret)) return data - + def integrate(self, data): bins = self.Y_RANGE.stop - self.Y_RANGE.start ret = np.zeros((len(data["hRIXS_det"]), bins - 20))