From 047b20c9ce644ae4d433afed26f497311cafc47e Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Thu, 2 Mar 2023 16:38:25 +0100
Subject: [PATCH] update correction to use virtual karabo da

---
 .../Gotthard2/Correction_Gotthard2_NBC.ipynb  | 71 +++++++++----------
 1 file changed, 32 insertions(+), 39 deletions(-)

diff --git a/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb b/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
index b455ca34d..3f7d2368a 100644
--- a/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
+++ b/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
@@ -162,26 +162,12 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "id": "7bf7fb32",
+   "id": "892172d8",
    "metadata": {},
    "outputs": [],
    "source": [
     "run_dc = RunDirectory(run_folder)\n",
     "\n",
-    "# Decide if we are processing 2 25um or 1 50um Gotthard-II\n",
-    "\n",
-    "receivers = list(run_dc.select(f'{karabo_id}/DET/{receiver_template}*').all_sources)\n",
-    "\n",
-    "corr_karabo_da = receivers[0].split(\"/\")[-1].split(\":\")[0][:-2]"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "id": "892172d8",
-   "metadata": {},
-   "outputs": [],
-   "source": [
     "# Read slow data\n",
     "g2ctrl = gotthard2lib.Gotthard2Ctrl(run_dc=run_dc, ctrl_src=ctrl_src)\n",
     "\n",
@@ -206,7 +192,18 @@
     "gh2_hostname = run_dc.get_run_value(ctrl_src, \"rxHostname\")\n",
     "# gh2_hostname is a vector of bytes objects.\n",
     "# GH2 25um has two hostnames unlike 50um which has one.\n",
-    "gh2_detector = \"25um\" if gh2_hostname[1].decode(\"utf-8\") else \"50um\""
+    "gh2_detector = \"25um\" if gh2_hostname[1].decode(\"utf-8\") else \"50um\"\n",
+    "\n",
+    "# Decide if we are processing 2 25um or 1 50um Gotthard-II\n",
+    "\n",
+    "receivers = sorted(list(run_dc.select(f'{karabo_id}/DET/{receiver_template}*').all_sources))\n",
+    "\n",
+    "if gh2_detector == \"25um\":  # For 25um use virtual karabo_das for CALCAT data mapping.\n",
+    "    karabo_da = [f\"{karabo_da[0]}/1\", f\"{karabo_da[0]}/2\"]\n",
+    "    print(\"Processing 25um Gotthard2.\")\n",
+    "    corr_karabo_da = receivers[0].split(\"/\")[-1].split(\":\")[0][:-2]\n",
+    "else:\n",
+    "    corr_karabo_da = receivers[0].split(\"/\")[-1].split(\":\")[0]"
    ]
   },
   {
@@ -249,7 +246,7 @@
     "    db_modules = [None] * len(karabo_da)\n",
     "\n",
     "if constants_file:\n",
-    "    for mod in receivers:\n",
+    "    for mod in karabo_da:\n",
     "        const_data[mod] = dict()\n",
     "        # load constants temporarily using defined local paths.\n",
     "        with h5py.File(constants_file, \"r\") as cfile:\n",
@@ -362,6 +359,8 @@
    "metadata": {},
    "outputs": [],
    "source": [
+    "corr_data_source = f\"{karabo_id}/DET/{corr_karabo_da}:daqOutput\"\n",
+    "\n",
     "for raw_file in seq_files:\n",
     "\n",
     "    out_file = out_folder / raw_file.name.replace(\"RAW\", \"CORR\")\n",
@@ -378,12 +377,12 @@
     "        data_stored = None\n",
     "        gain_stored = None\n",
     "\n",
-    "    for i, rec_mod in enumerate(receivers):\n",
+    "    for i, (receiver, mod) in enumerate(zip(receivers, karabo_da)):\n",
     "        step_timer.start()\n",
-    "        print(f\"Correcting {rec_mod} for {raw_file}\")\n",
+    "        print(f\"Correcting {receiver} for {raw_file}\")\n",
     "\n",
-    "        data = dc[rec_mod, \"data.adc\"].ndarray()\n",
-    "        gain = dc[rec_mod, \"data.gain\"].ndarray()\n",
+    "        data = dc[receiver, \"data.adc\"].ndarray()\n",
+    "        gain = dc[receiver, \"data.gain\"].ndarray()\n",
     "        step_timer.done_step(\"Preparing raw data\")\n",
     "        dshape = data.shape\n",
     "\n",
@@ -402,7 +401,7 @@
     "        data_corr[np.isinf(data_corr)] = np.nan\n",
     "\n",
     "        # Create CORR files and add corrected data sections.\n",
-    "        image_counts = dc[rec_mod, \"data.adc\"].data_counts(labelled=False)\n",
+    "        image_counts = dc[receiver, \"data.adc\"].data_counts(labelled=False)\n",
     "\n",
     "        if gh2_detector == \"25um\":\n",
     "            data_stored[..., i::2] = data_corr.copy()\n",
@@ -421,7 +420,7 @@
     "        )\n",
     "\n",
     "        # Create Instrument section to later add corrected datasets.\n",
-    "        outp_source = ofile.create_instrument_source(f\"{karabo_id}/DET/{corr_karabo_da}:daqOutput\")\n",
+    "        outp_source = ofile.create_instrument_source(corr_data_source)\n",
     "\n",
     "        # Create count/first datasets at INDEX source.\n",
     "        outp_source.create_index(data=image_counts)\n",
@@ -446,8 +445,8 @@
     "        # stored in the corrected file.\n",
     "        for field in [\"bunchId\", \"memoryCell\", \"frameNumber\", \"timestamp\"]:\n",
     "                outp_source.create_key(\n",
-    "                    f\"data.{field}\", data=dc[instr_mod_src, f\"data.{field}\"].ndarray(),\n",
-    "                    chunks=(chunks_data, data_corr.shape[1])\n",
+    "                    f\"data.{field}\", data=dc[receiver, f\"data.{field}\"].ndarray(),\n",
+    "                    chunks=(chunk_size_idim, data_corr.shape[1])\n",
     "            )\n",
     "        outp_source.create_compressed_key(f\"data.mask\", data=mask)\n",
     "        step_timer.done_step(\"Storing data\")\n",
@@ -465,16 +464,6 @@
     "step_timer.print_summary()"
    ]
   },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "id": "7f366352",
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "corr_data_source = f\"{karabo_id}/DET/{corr_karabo_da}:daqOutput\""
-   ]
-  },
   {
    "cell_type": "code",
    "execution_count": null,
@@ -501,7 +490,6 @@
     "first_seq_corr = out_folder / first_seq_raw.name.replace(\"RAW\", \"CORR\")\n",
     "mod_dcs[corr_data_source] = {}\n",
     "with H5File(first_seq_corr) as out_dc:\n",
-    "    print(out_dc.all_sources)\n",
     "    tid, mod_dcs[corr_data_source][\"train_corr_data\"] = next(\n",
     "        out_dc[corr_data_source, \"data.adc\"].trains()\n",
     "    )\n",
@@ -531,12 +519,17 @@
     "display(Markdown(\"### Mean RAW and CORRECTED across pulses for one train:\"))\n",
     "display(Markdown(f\"Train: {tid}\"))\n",
     "\n",
+    "if gh2_detector == \"50um\":\n",
+    "    title = f\"{{}} data for {karabo_da} ({db_modules})\"\n",
+    "else:\n",
+    "    title = f\"Interleaved {{}} data for {karabo_da} ({db_modules})\"\n",
+    "\n",
     "step_timer.start()\n",
     "\n",
     "fig, ax = plt.subplots(figsize=(20, 10))\n",
     "raw_data = mod_dcs[corr_data_source][\"train_raw_data\"]\n",
     "im = ax.plot(np.mean(raw_data, axis=0))\n",
-    "ax.set_title(f\"RAW module {receivers} ({db_modules})\")\n",
+    "ax.set_title(title.format(\"RAW\"))\n",
     "ax.set_xlabel(\"Strip #\", size=20)\n",
     "ax.set_ylabel(\"12-bit ADC output\", size=20)\n",
     "plt.xticks(fontsize=20)\n",
@@ -546,7 +539,7 @@
     "fig, ax = plt.subplots(figsize=(20, 10))\n",
     "corr_data = mod_dcs[corr_data_source][\"train_corr_data\"]\n",
     "im = ax.plot(np.mean(corr_data, axis=0))\n",
-    "ax.set_title(f\"CORRECTED module {receivers} ({db_modules})\")\n",
+    "ax.set_title(title.format(\"CORRECTED\"))\n",
     "ax.set_xlabel(\"Strip #\", size=20)\n",
     "ax.set_ylabel(\"10-bit KeV. output\", size=20)\n",
     "plt.xticks(fontsize=20)\n",
@@ -575,7 +568,7 @@
     "        mod_dcs[corr_data_source][plt_data],\n",
     "        y_label=\"Pulses\",\n",
     "        x_label=\"Strips\",\n",
-    "        title=f\"{dname} module {receivers} ({db_modules})\",\n",
+    "        title=title.format(dname),\n",
     "        use_axis=ax,\n",
     "    )\n",
     "    pass\n",
-- 
GitLab