From df2666dfa66cb656bcf2599da42887570f5554da Mon Sep 17 00:00:00 2001
From: David Hammer <dhammer@mailbox.org>
Date: Mon, 20 Dec 2021 16:54:44 +0100
Subject: [PATCH] Handle fixed gain mode as operating condition, too

---
 src/calng/AgipdCorrection.py | 48 ++++++++++++++----------------------
 1 file changed, 18 insertions(+), 30 deletions(-)

diff --git a/src/calng/AgipdCorrection.py b/src/calng/AgipdCorrection.py
index 3b8072a5..1a7e5ecf 100644
--- a/src/calng/AgipdCorrection.py
+++ b/src/calng/AgipdCorrection.py
@@ -93,6 +93,8 @@ class AgipdGpuRunner(base_gpu.BaseGpuRunner):
         g_gain_value=1,
     ):
         self.gain_mode = gain_mode
+        # default gain only matters when not thresholding (missing constant or fixed)
+        # note: gain stage (result of thresholding) is 0, 1, or 2
         if self.gain_mode is AgipdGainMode.ADAPTIVE_GAIN:
             self.default_gain = cupy.uint8(gain_mode)
         else:
@@ -279,10 +281,6 @@ class AgipdGpuRunner(base_gpu.BaseGpuRunner):
     def correct(self, flags):
         if flags & CorrectionFlags.BLSHIFT:
             raise NotImplementedError("Baseline shift not implemented yet")
-        if self.gain_mode is not AgipdGainMode.ADAPTIVE_GAIN and (
-            flags & CorrectionFlags.THRESHOLD
-        ):
-            raise ValueError("Cannot do gain thresholding in fixed gain mode")
 
         self.correction_kernel(
             self.full_grid,
@@ -376,11 +374,11 @@ class AgipdCalcatFriend(calcat_utils.BaseCalcatFriend):
             .reconfigurable()
             .commit(),
 
-            DOUBLE_ELEMENT(schema)
+            STRING_ELEMENT(schema)
             .key(f"{param_prefix}.gainMode")
             .assignmentOptional()
-            .defaultValue(0)
-            .reconfigurable()
+            .defaultValue("ADAPTIVE_GAIN")
+            .options("ADAPTIVE_GAIN,FIXED_HIGH_GAIN,FIXED_MEDIUM_GAIN,FIXED_LOW_GAIN")
             .commit(),
 
             DOUBLE_ELEMENT(schema)
@@ -404,25 +402,25 @@ class AgipdCalcatFriend(calcat_utils.BaseCalcatFriend):
         res["Pixels X"] = self._get_param("pixelsX")
         res["Pixels Y"] = self._get_param("pixelsY")
         res["Acquisition rate"] = self._get_param("acquisitionRate")
-        # TODO: make configurable whether or not to include gain setting?
+
+        # TODO: remove this workaround after CalCat update
         integration_time = self._get_param("integrationTime")
         if integration_time != 12:
             res["Integration Time"] = integration_time
+
+        gain_mode = AgipdGainMode[self._get_param("gainMode")]
+        if gain_mode is not AgipdGainMode.ADAPTIVE_GAIN:
+            res["Gain Mode"] = gain_mode.value
+
+        # TODO: make configurable whether or not to include gain setting?
         res["Gain Setting"] = self._get_param("gainSetting")
+
         return res
 
     def illuminated_condition(self):
-        res = calcat_utils.OperatingConditions()
-        res["Memory cells"] = self._get_param("memoryCells")
-        res["Sensor Bias Voltage"] = self._get_param("biasVoltage")
-        res["Pixels X"] = self._get_param("pixelsX")
-        res["Pixels Y"] = self._get_param("pixelsY")
+        res = self.dark_condition()
         res["Source Energy"] = self._get_param("photonEnergy")
-        res["Acquisition rate"] = self._get_param("acquisitionRate")
-        integration_time = self._get_param("integrationTime")
-        if integration_time != 12:
-            res["Integration Time"] = integration_time
-        res["Gain Setting"] = self._get_param("gainSetting")
+
         return res
 
 
@@ -454,16 +452,6 @@ class AgipdCorrection(BaseCorrection):
             .setNewDefaultValue("cell")
             .commit(),
         )
-        # WIP: fixed gain mode
-        (
-            STRING_ELEMENT(expected)
-            .key("gainMode")
-            .displayedName("Gain mode")
-            .assignmentOptional()
-            .defaultValue("ADAPTIVE_GAIN")
-            .options("ADAPTIVE_GAIN,FIXED_HIGH_GAIN,FIXED_MEDIUM_GAIN,FIXED_LOW_GAIN")
-            .commit(),
-        )
 
         (
             OUTPUT_CHANNEL(expected)
@@ -595,8 +583,8 @@ class AgipdCorrection(BaseCorrection):
 
     def __init__(self, config):
         super().__init__(config)
-        # TODO: consider different gpu runner for fixed gain mode
-        self.gain_mode = AgipdGainMode[config.get("gainMode")]
+        # note: gain mode single sourced from constant retrieval node
+        self.gain_mode = AgipdGainMode[config.get("constantParameters.gainMode")]
 
         try:
             self.bad_pixel_mask_value = np.float32(
-- 
GitLab