From cfa33b8bf85da4c3f721e9d680b03c4242c8cbf9 Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Thu, 28 Apr 2022 00:58:28 +0200
Subject: [PATCH] pulse_idx_preview and fix for FXE Gotthard2

---
 .../Gotthard2/Correction_Gotthard2_NBC.ipynb  | 46 +++++++++----------
 src/cal_tools/gotthard2algs.pyx               |  4 +-
 2 files changed, 25 insertions(+), 25 deletions(-)

diff --git a/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb b/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
index 8c5407431..c9b609567 100644
--- a/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
+++ b/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
@@ -28,8 +28,8 @@
     "# Parameters used to access raw data.\n",
     "karabo_id = \"DET_LAB_G2\"  # karabo prefix of Jungfrau devices\n",
     "karabo_da = [\"DA01\"]  # data aggregators\n",
-    "receiver_template = \"GOT{:02d}\"  # receiver template used to read INSTRUMENT keys.\n",
-    "control_template = \"CTRL{:02d}\"  # control template used to read CONTROL keys.\n",
+    "receiver_template = \"GOT{}\"  # receiver template used to read INSTRUMENT keys.\n",
+    "control_template = \"CTRL{}\"  # control template used to read CONTROL keys.\n",
     "instrument_source_template = '{}/DET/{}:daqOutput'  # template for source name (filled with karabo_id & receiver_id). e.g. 'SPB_IRDA_JF4M/DET/JNGFR01:daqOutput'\n",
     "ctrl_source_template = '{}/DET/{}'  # template for control source name (filled with karabo_id_control)\n",
     "karabo_id_control = \"\"  # if control is on a different ID, set to empty string if it is the same a karabo-id\n",
@@ -41,10 +41,11 @@
     "\n",
     "# Parameters affecting corrected data.\n",
     "relative_gain = True  # do relative gain correction\n",
+    "constants_file = \"/gpfs/exfel/data/scratch/ahmedk/dont_remove/gotthard2/constants/GH2-0124/calibration_constants_GH2-0124.h5\"\n",
     "\n",
     "# Parameters for plotting\n",
     "skip_plots = False  # exit after writing corrected files\n",
-    "pulse_preview = 3 # pulseId to preview. The following even/odd pulseId is used for preview.\n",
+    "pulse_idx_preview = 3 # pulse index to preview. The following even/odd pulse index is used for preview. # TODO: update to pulseId preview.\n",
     "\n",
     "def balance_sequences(in_folder, run, sequences, sequences_per_node, karabo_da):\n",
     "    from xfel_calibrate.calibrate import balance_sequences as bs\n",
@@ -95,8 +96,6 @@
     "out_folder = Path(out_folder)\n",
     "out_folder.mkdir(parents=True, exist_ok=True)\n",
     "\n",
-    "run_dc = RunDirectory(in_folder / f'r{run:04d}')\n",
-    "\n",
     "instrument_src = instrument_source_template.format(\n",
     "    karabo_id, receiver_template)\n",
     "ctrl_src = ctrl_source_template.format(\n",
@@ -121,21 +120,24 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "# Select only process detector.\n",
+    "# Select only sequence files to process for the selected detector.\n",
+    "\n",
+    "run_dc = RunDirectory(in_folder / f'r{run:04d}')\n",
     "\n",
     "if sequences != [-1]:\n",
     "    seq_files = []\n",
-    "    for f in run_dc.select(f\"*{karabo_id}*\").files:\n",
+    "    # Using * to match for sources for the selected karabo data aggregators.\n",
+    "    for f in run_dc.select(f\"*{instrument_src.format('*')}*\").files:\n",
     "        raw_f = Path(f.filename)\n",
     "        for s in sequences:\n",
     "            if raw_f.match(f\"*-S{s:05d}.h5\"):\n",
     "                seq_files.append(raw_f)\n",
     "else:\n",
-    "    seq_files = [Path(f.filename) for f in run_dc.select(f\"*{karabo_id}*\").files]\n",
+    "    seq_files = [Path(f.filename) for f in run_dc.select(f\"*{instrument_src.format('*')}*\").files]\n",
     "\n",
     "seq_files = sorted(seq_files)\n",
     "\n",
-    "if not len(seq_files):\n",
+    "if not seq_files:\n",
     "    raise IndexError(\"No sequence files available for the selected sequences.\")\n",
     "\n",
     "print(f\"Processing a total of {len(seq_files)} sequence files\")"
@@ -177,9 +179,6 @@
    "outputs": [],
    "source": [
     "# load constants temporarely using defined local paths.\n",
-    "\n",
-    "constants_file = \"/gpfs/exfel/data/user/mramilli/gotthard2/constants/GH2-0124/calibration_constants_GH2-0124.h5\"\n",
-    "\n",
     "with h5py.File(constants_file, 'r') as cfile:\n",
     "    offset_map = cfile[\"offset_map\"][()]\n",
     "    gain_map = cfile[\"gain_map\"][()]\n",
@@ -224,9 +223,10 @@
    "outputs": [],
    "source": [
     "for mod in karabo_da:\n",
-    "    instr_mod_src = instrument_src.format(int(mod[-2:]))\n",
+    "    # This is used in case receiver template consists of\n",
+    "    # karabo data aggregator index. e.g. detector at DETLAB\n",
+    "    instr_mod_src = instrument_src.format(mod[-2:])\n",
     "    data_path = \"INSTRUMENT/\"+instr_mod_src+\"/data\"\n",
-    "\n",
     "    for raw_file in seq_files:\n",
     "        step_timer.start()\n",
     "\n",
@@ -361,24 +361,24 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "# Validate given \"pulse_preview\"\n",
+    "# Validate given \"pulse_idx_preview\"\n",
     "\n",
-    "if pulse_preview + 1 > data.shape[1]:\n",
-    "    print(f\"WARNING: selected pulse_preview {pulse_preview} is not available in data.\"\n",
+    "if pulse_idx_preview + 1 > data.shape[1]:\n",
+    "    print(f\"WARNING: selected pulse_idx_preview {pulse_idx_preview} is not available in data.\"\n",
     "    \" Previewing 1st pulse.\")\n",
-    "    pulse_preview = 1\n",
+    "    pulse_idx_preview = 1\n",
     "\n",
     "if data.shape[1] == 1:\n",
     "    odd_pulse = 1\n",
     "    even_pulse = None\n",
     "else:\n",
-    "    odd_pulse = pulse_preview if pulse_preview % 2 else pulse_preview + 1\n",
-    "    even_pulse = pulse_preview if not (pulse_preview % 2) else pulse_preview + 1\n",
+    "    odd_pulse = pulse_idx_preview if pulse_idx_preview % 2 else pulse_idx_preview + 1\n",
+    "    even_pulse = pulse_idx_preview if not (pulse_idx_preview % 2) else pulse_idx_preview + 1\n",
     "\n",
-    "if pulse_preview + 1 > data.shape[1]:\n",
-    "    pulse_preview = 1\n",
+    "if pulse_idx_preview + 1 > data.shape[1]:\n",
+    "    pulse_idx_preview = 1\n",
     "    if data.shape[1] > 1:\n",
-    "        pulse_preview = 2"
+    "        pulse_idx_preview = 2"
    ]
   },
   {
diff --git a/src/cal_tools/gotthard2algs.pyx b/src/cal_tools/gotthard2algs.pyx
index 0d4e8de12..a65c62c49 100644
--- a/src/cal_tools/gotthard2algs.pyx
+++ b/src/cal_tools/gotthard2algs.pyx
@@ -14,7 +14,7 @@ def convert_to_10bit(
     """Convert 12bit RAW data to 10bit data."""
 
     cdef:
-        unsigned short d_10bit, pulse, train, x, raw_val
+        unsigned short x, cell, d_10bit, pulse, raw_val, train
 
     for pulse in range(data.shape[0]):
         cell = pulse % 2
@@ -41,7 +41,7 @@ def correct_train(
 
     cdef:
         float raw_val
-        unsigned short d_10bit, pulse, train, x, g
+        unsigned short g, x, cell, d_10bit, pulse, train
 
     for pulse in range(data.shape[0]):
         cell = pulse % 2
-- 
GitLab