From 632a12283c1a03a8b4d28a511b45c50b21611e92 Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Thu, 24 Mar 2022 09:31:25 +0100
Subject: [PATCH] Read bias_voltage for AGIPD at SPB and MID from RUN source

---
 src/cal_tools/agipdlib.py | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/src/cal_tools/agipdlib.py b/src/cal_tools/agipdlib.py
index 5f0c1d137..30b2cc158 100644
--- a/src/cal_tools/agipdlib.py
+++ b/src/cal_tools/agipdlib.py
@@ -173,30 +173,39 @@ class AgipdCtrl:
         karabo_id_control: str,
         module: Optional[int] = 0
     ) -> int:
-        """Read the voltage information from the FPGA device of module 0.
+        """Read the voltage information from the RUN source of module 0.
 
         Different modules may operate at different voltages.
         In practice, they all operate at the same voltage.
         As such, it is okay to read a single module's value.
 
-        If the FPGA module source is not available, 300 will be returned.
-        300 is the default bias_voltage value before adding it to slow data.
+        If the FPGA/PSC RUN source is not available, 300 will be returned.
+        300 was the default bias_voltage value for
+        MID_DET_AGIPD1M-1 and SPB_DET_AGIPD1M-1.
 
-        :param karabo_id_control: The detector karabo id, for the control device.
+        :param karabo_id_control: The karabo deviceId for the CONTROL device.
         :param module: defaults to module 0
         :return: voltage, a uint16
         """
-        voltage_src = (
-            f"{karabo_id_control}/FPGA/M_{module}",
-            "highVoltage.actual.value")
+        # TODO: Add a breaking fix by passing the source and key through
+        # get_bias_voltage arguments.
+        if "AGIPD1M" in karabo_id_control:
+            voltage_src = (
+                f"{karabo_id_control[:-1]}/PSC/HV",
+                f"channels.U{module}.measurmentSenseVoltage.value")
+        else:  # AGIPD500K
+            voltage_src = (
+                f"{karabo_id_control}/FPGA/M_{module}",
+                "highVoltage.actual.value")
 
         if (
             voltage_src[0] in self.run_dc.all_sources and
             voltage_src[1] in self.run_dc.keys_for_source(voltage_src[0])
         ):
-
-            return self.run_dc[voltage_src].as_single_value(atol=1, reduce_by='max')
+            return int(self.run_dc.get_run_value(*voltage_src))
         else:
+            # TODO: Validate if removing this and
+            # and using NB value for old RAW data.
             print(
                 "WARNING: Unable to read bias_voltage from"
                 f" {voltage_src[0]}/{voltage_src[1].replace('.','/')} "
-- 
GitLab