From 85292e6ced5a9ce6d964844f084500c1fba483d3 Mon Sep 17 00:00:00 2001
From: David Hammer <dhammer@mailbox.org>
Date: Fri, 13 May 2022 09:21:23 +0200
Subject: [PATCH] LPD: add gain stage preview

---
 src/calng/LpdCorrection.py   | 24 ++++++++++++++++++++++--
 src/calng/kernels/lpd_gpu.cu |  4 ++++
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/src/calng/LpdCorrection.py b/src/calng/LpdCorrection.py
index fc602da1..94721847 100644
--- a/src/calng/LpdCorrection.py
+++ b/src/calng/LpdCorrection.py
@@ -5,6 +5,7 @@ import numpy as np
 from karabo.bound import (
     DOUBLE_ELEMENT,
     KARABO_CLASSINFO,
+    OUTPUT_CHANNEL,
     OVERWRITE_ELEMENT,
     STRING_ELEMENT,
     VECTOR_STRING_ELEMENT,
@@ -12,7 +13,7 @@ from karabo.bound import (
 
 from . import base_gpu, base_calcat, utils
 from ._version import version as deviceVersion
-from .base_correction import BaseCorrection, add_correction_step_schema
+from .base_correction import BaseCorrection, add_correction_step_schema, preview_schema
 
 
 class LpdConstants(enum.Enum):
@@ -57,6 +58,9 @@ class LpdGpuRunner(base_gpu.BaseGpuRunner):
             input_data_dtype,
             output_data_dtype,
         )
+        self.gain_map_gpu = cupy.empty(self.processed_shape, dtype=cupy.float32)
+        self.preview_buffer_getters.append(self._get_gain_map_for_preview)
+
         self.map_shape = (constant_memory_cells, pixels_x, pixels_y, 3)
         self.offset_map_gpu = cupy.zeros(self.map_shape, dtype=cupy.float32)
         self.gain_amp_map_gpu = cupy.ones(self.map_shape, dtype=cupy.float32)
@@ -73,6 +77,9 @@ class LpdGpuRunner(base_gpu.BaseGpuRunner):
     def _get_corrected_for_preview(self):
         return self.processed_data_gpu
 
+    def _get_gain_map_for_preview(self):
+        return self.gain_map_gpu
+
     def correct(self, flags):
         self.correction_kernel(
             self.full_grid,
@@ -87,6 +94,7 @@ class LpdGpuRunner(base_gpu.BaseGpuRunner):
                 self.flatfield_map_gpu,
                 self.bad_pixel_map_gpu,
                 self.bad_pixel_mask_value,
+                self.gain_map_gpu,
                 self.processed_data_gpu,
             ),
         )
@@ -283,6 +291,13 @@ class LpdCorrection(BaseCorrection):
             .commit(),
         )
 
+        (
+            OUTPUT_CHANNEL(expected)
+            .key("preview.outputGainMap")
+            .dataSchema(preview_schema)
+            .commit(),
+        )
+
         LpdCalcatFriend.add_schema(expected, LpdCorrection._managed_keys)
         add_correction_step_schema(
             expected, LpdCorrection._managed_keys, LpdCorrection._correction_field_names
@@ -401,7 +416,11 @@ class LpdCorrection(BaseCorrection):
                 pulse_table,
                 warn_func=self.log_status_warn,
             )
-            preview_raw, preview_corrected = self.kernel_runner.compute_previews(
+            (
+                preview_raw,
+                preview_corrected,
+                preview_gain_map,
+            ) = self.kernel_runner.compute_previews(
                 preview_slice_index,
             )
 
@@ -415,6 +434,7 @@ class LpdCorrection(BaseCorrection):
                 (
                     ("preview.outputRaw", preview_raw),
                     ("preview.outputCorrected", preview_corrected),
+                    ("preview.outputGainMap", preview_gain_map),
                 ),
                 metadata,
             )
diff --git a/src/calng/kernels/lpd_gpu.cu b/src/calng/kernels/lpd_gpu.cu
index 3a556559..4fb24fb3 100644
--- a/src/calng/kernels/lpd_gpu.cu
+++ b/src/calng/kernels/lpd_gpu.cu
@@ -12,6 +12,7 @@ extern "C" {
 	                        const float* flatfield_map,
 	                        const unsigned int* bad_pixel_map,
 	                        const float bad_pixel_mask_value,
+	                        float* gain_map, // similar to preview for AGIPD
 	                        {{output_data_dtype}}* output) {
 		const size_t X = {{pixels_x}};
 		const size_t Y = {{pixels_y}};
@@ -33,6 +34,7 @@ extern "C" {
 		const unsigned short raw_data_value = data[data_index];
 		const unsigned char gain = (raw_data_value >> 12) & 0x0003;
 		float corrected = (float)(raw_data_value & 0x0fff);
+		float gain_for_preview = (float)gain;
 
 		const size_t gm_map_stride_gain = 1;
 		const size_t gm_map_stride_x = 3 * gm_map_stride_gain;
@@ -49,6 +51,7 @@ extern "C" {
 			if (gain > 2 || ((corr_flags & BPMASK) && bad_pixel_map[gm_map_index])) {
 				// now also checking for illegal gain value
 				corrected = bad_pixel_mask_value;
+				gain_for_preview = bad_pixel_mask_value;
 			} else {
 				// TODO: save GPU memory by combining maps
 				if (corr_flags & OFFSET) {
@@ -68,6 +71,7 @@ extern "C" {
 				}
 			}
 		}
+		gain_map[data_index] = gain_for_preview;
 		{% if output_data_dtype == "half" %}
 		output[data_index] = __float2half(corrected);
 		{% else %}
-- 
GitLab