From 181da0a937357cb93d58b1cb87b86074dfab4c03 Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Wed, 24 Jan 2024 13:32:12 +0100
Subject: [PATCH] new paramater to reverse second GH2 25um modul

---
 .../Gotthard2/Correction_Gotthard2_NBC.ipynb  | 22 +++++++++++++++----
 src/cal_tools/gotthard2/gotthard2lib.py       |  6 +++++
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb b/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
index bd1940cf3..c0ea887d2 100644
--- a/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
+++ b/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
@@ -85,6 +85,7 @@
     "exposure_period = -1.  # Detector exposure period, set to -1 to use value in raw file.\n",
     "acquisition_rate = -1.  # Detector acquisition rate (1.1/4.5), set to -1 to use value in raw file.\n",
     "single_photon = -1  # Detector single photon mode (High/Low CDS), set to -1 to use value in raw file.\n",
+    "reverse_second_module = False  # Reverse 25um GH2 second module before interleaving. set to -1 to use value in raw file reverse based on `CTRL/reverseSlaveReadOutMode`'s value.\n",
     "\n",
     "# Parameters for plotting\n",
     "skip_plots = False  # exit after writing corrected files\n",
@@ -187,7 +188,11 @@
     "    single_photon = g2ctrl.get_single_photon()\n",
     "\n",
     "gh2_detector = g2ctrl.get_det_type()\n",
-    "\n",
+    "if reverse_second_module and gh2_detector == \"25um\":\n",
+    "    reverse_second_module = not g2ctrl.second_module_reversed()\n",
+    "    print(\n",
+    "        \"Second module is not reversed. \"\n",
+    "        \"Reversing second module before interleaving.\")\n",
     "print(\"Bias Voltage:\", bias_voltage)\n",
     "print(\"Exposure Time:\", exposure_time)\n",
     "print(\"Exposure Period:\", exposure_period)\n",
@@ -429,7 +434,7 @@
     "    # Select module INSTRUMENT sources and deselect empty trains.\n",
     "    dc = H5File(raw_file).select(data_sources, require_all=True)\n",
     "    n_trains = len(dc.train_ids)\n",
-    "    \n",
+    "\n",
     "    if n_trains == 0:\n",
     "        warning(f\"Skipping correction. No trains to correct for this sequence file: {raw_file}.\")\n",
     "        continue\n",
@@ -469,6 +474,9 @@
     "\n",
     "        # Create CORR files and add corrected data sections.\n",
     "        image_counts = dc[src, \"data.adc\"].data_counts(labelled=False)\n",
+    "        if reverse_second_module and i == 1:\n",
+    "            data_corr = np.flip(data_corr, axis=-1)\n",
+    "            gain = np.flip(gain, axis=-1)\n",
     "\n",
     "        if gh2_detector == \"25um\":\n",
     "            data_stored[..., i::2] = data_corr.copy()\n",
@@ -568,8 +576,14 @@
     "    with H5File(first_seq_raw) as in_dc:\n",
     "        train_dict = in_dc.train_from_id(tid)[1][src]\n",
     "        if gh2_detector == \"25um\":\n",
-    "            mod_dcs[corr_data_source][\"train_raw_data\"][..., i::2] = train_dict[\"data.adc\"]\n",
-    "            mod_dcs[corr_data_source][\"train_raw_gain\"][..., i::2] = train_dict[\"data.gain\"]\n",
+    "            if reverse_second_module and i == 1:\n",
+    "                data = np.flip(train_dict[\"data.adc\"], axis=-1)\n",
+    "                gain = np.flip(train_dict[\"data.gain\"], axis=-1)\n",
+    "            else:\n",
+    "                data = train_dict[\"data.adc\"]\n",
+    "                gain = train_dict[\"data.gain\"]\n",
+    "            mod_dcs[corr_data_source][\"train_raw_data\"][..., i::2] = data\n",
+    "            mod_dcs[corr_data_source][\"train_raw_gain\"][..., i::2] = gain\n",
     "        else:\n",
     "            mod_dcs[corr_data_source][\"train_raw_data\"] = train_dict[\"data.adc\"]\n",
     "            mod_dcs[corr_data_source][\"train_raw_gain\"] = train_dict[\"data.gain\"]"
diff --git a/src/cal_tools/gotthard2/gotthard2lib.py b/src/cal_tools/gotthard2/gotthard2lib.py
index cd37535e4..586c4af01 100644
--- a/src/cal_tools/gotthard2/gotthard2lib.py
+++ b/src/cal_tools/gotthard2/gotthard2lib.py
@@ -52,3 +52,9 @@ class Gotthard2Ctrl():
 
         hostname = self.run_dc.get_run_value(self.ctrl_src, "rxHostname")
         return "25um" if hostname[1].decode("utf-8") else "50um"
+
+    def second_module_reversed(self):
+        """Check if reverseSlaveReadOutMode is True."""
+        return bool(
+                self.run_dc[
+                    self.ctrl_src, "reverseSlaveReadOutMode"].as_single_value())
-- 
GitLab