From f3b4f77ba698e4dacac4af6f20864ad912fa35f3 Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Thu, 4 Aug 2022 15:48:51 +0200
Subject: [PATCH] move reading parameter conditions for ePix100 to epix100lib

---
 .../Characterize_Darks_ePix100_NBC.ipynb      | 21 +++++++--
 .../ePix100/Correction_ePix100_NBC.ipynb      | 45 ++++++++++---------
 ...100_retrieve_constants_precorrection.ipynb | 41 +++++++++--------
 src/cal_tools/epix100/epix100lib.py           | 26 +++++++++++
 4 files changed, 90 insertions(+), 43 deletions(-)
 create mode 100644 src/cal_tools/epix100/epix100lib.py

diff --git a/notebooks/ePix100/Characterize_Darks_ePix100_NBC.ipynb b/notebooks/ePix100/Characterize_Darks_ePix100_NBC.ipynb
index 8fe339e74..592965d84 100644
--- a/notebooks/ePix100/Characterize_Darks_ePix100_NBC.ipynb
+++ b/notebooks/ePix100/Characterize_Darks_ePix100_NBC.ipynb
@@ -77,6 +77,7 @@
     "from XFELDetAna import xfelpyanatools as xana\n",
     "from XFELDetAna.plotting.util import prettyPlotting\n",
     "from cal_tools.enums import BadPixels\n",
+    "from cal_tools.epix100 import epix100lib\n",
     "from cal_tools.tools import (\n",
     "    get_dir_creation_date,\n",
     "    get_pdu_from_db,\n",
@@ -156,11 +157,23 @@
     "        f\"Less than {min_trains} trains are available in RAW data.\"\n",
     "         \" Not enough data to process darks.\")\n",
     "\n",
-    "print(f\"Number of dark images to analyze: {n_trains}\")\n",
-    "\n",
+    "print(f\"Number of dark images to analyze: {n_trains}\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "ctrl_data = epix100lib.epix100Ctrl(\n",
+    "    run_dc=run_dir,\n",
+    "    instrument_src=instrument_src,\n",
+    "    ctrl_src=f\"{karabo_id}/DET/CONTROL\",\n",
+    "    )\n",
     "# Read integration time\n",
     "if fix_integration_time == -1:\n",
-    "    integration_time = run_dir[f\"{karabo_id}/DET/CONTROL\", 'expTime.value'].as_single_value(reduce_by='first')\n",
+    "    integration_time = ctrl_data.get_integration_time()\n",
     "    integration_time_str_add = ''\n",
     "else:\n",
     "    integration_time = fix_integration_time\n",
@@ -168,7 +181,7 @@
     "    \n",
     "# Read temperature    \n",
     "if fix_temperature == -1:\n",
-    "    temperature = run_dir[instrument_src, 'data.backTemp'].ndarray().mean()/100.\n",
+    "    temperature = ctrl_data.get_temprature()\n",
     "    temperature_k = temperature + 273.15\n",
     "    temp_str_add = ''\n",
     "else:\n",
diff --git a/notebooks/ePix100/Correction_ePix100_NBC.ipynb b/notebooks/ePix100/Correction_ePix100_NBC.ipynb
index 76fdce77b..916b66436 100644
--- a/notebooks/ePix100/Correction_ePix100_NBC.ipynb
+++ b/notebooks/ePix100/Correction_ePix100_NBC.ipynb
@@ -46,7 +46,8 @@
     "# Conditions for retrieving calibration constants.\n",
     "bias_voltage = 200  # bias voltage\n",
     "in_vacuum = False  # detector operated in vacuum\n",
-    "fix_temperature = 290.  # fix temperature to this value\n",
+    "integration_time = -1  # Detector integration time, Default value -1 to use the value from the slow data.\n",
+    "fix_temperature = -1  # fixed temperature value in Kelvin, Default value -1 to use the value from files.\n",
     "gain_photon_energy = 9.0  # Photon energy used for gain calibration\n",
     "photon_energy = 0.  # Photon energy to calibrate in number of photons, 0 for calibration in keV\n",
     "\n",
@@ -88,7 +89,7 @@
     "\n",
     "from XFELDetAna import xfelpyanatools as xana\n",
     "from XFELDetAna import xfelpycaltools as xcal\n",
-    "from cal_tools import h5_copy_except\n",
+    "from cal_tools import h5_copy_except, epix100\n",
     "from cal_tools.tools import (\n",
     "    calcat_creation_time,\n",
     "    get_dir_creation_date,\n",
@@ -206,28 +207,32 @@
     "run_parallel = False\n",
     "\n",
     "# Read control data.\n",
-    "integration_time = int(run_dc.get_run_value(\n",
-    "    f\"{karabo_id}/DET/CONTROL\",\n",
-    "    \"expTime.value\"))\n",
-    "temperature = np.mean(run_dc.get_array(\n",
-    "    f\"{karabo_id}/DET/{receiver_template}:daqOutput\",\n",
-    "    f\"data.backTemp\").values) / 100.\n",
-    "\n",
-    "if fix_temperature != 0:\n",
-    "    temperature_k = fix_temperature\n",
-    "    print(\"Temperature is fixed!\")\n",
+    "ctrl_data = epix100.epix100lib.epix100Ctrl(\n",
+    "    run_dc=run_dc,\n",
+    "    instrument_src=f\"{karabo_id}/DET/{receiver_template}:daqOutput\",\n",
+    "    ctrl_src=f\"{karabo_id}/DET/CONTROL\",\n",
+    "    )\n",
+    "\n",
+    "if integration_time < 0:\n",
+    "    integration_time = ctrl_data.get_integration_time()\n",
+    "    integration_time_str_add = \"\"\n",
     "else:\n",
+    "    integration_time_str_add = \"(manual input)\"\n",
+    "\n",
+    "if fix_temperature < 0:\n",
+    "    temperature = ctrl_data.get_temprature()\n",
     "    temperature_k = temperature + 273.15\n",
+    "    temp_str_add = \"\"\n",
+    "else:\n",
+    "    temperature_k = fix_temperature\n",
+    "    temperature = fix_temperature - 273.15\n",
+    "    temp_str_add = \"(manual input)\"\n",
     "\n",
-    "print(f\"Bias voltage is {bias_voltage} V\")\n",
-    "print(f\"Detector integration time is set to {integration_time}\")\n",
-    "print(\n",
-    "    f\"Mean temperature was {temperature:0.2f} °C \"\n",
-    "    f\"/ {temperature_k:0.2f} K at beginning of the run.\"\n",
-    ")\n",
-    "print(f\"Operated in vacuum: {in_vacuum} \")\n",
     "\n",
-    "step_timer.done_step(f'Reading control parameters.')\n"
+    "print(f\"Bias voltage is {bias_voltage} V\")\n",
+    "print(f\"Detector integration time is set to {integration_time} \\u03BCs {integration_time_str_add}\")\n",
+    "print(f\"Mean temperature: {temperature:0.2f}°C / {temperature_k:0.2f} K {temp_str_add}\")\n",
+    "print(f\"Operated in vacuum: {in_vacuum}\")\n"
    ]
   },
   {
diff --git a/notebooks/ePix100/ePix100_retrieve_constants_precorrection.ipynb b/notebooks/ePix100/ePix100_retrieve_constants_precorrection.ipynb
index 6ae7de6a5..d4ee5826b 100644
--- a/notebooks/ePix100/ePix100_retrieve_constants_precorrection.ipynb
+++ b/notebooks/ePix100/ePix100_retrieve_constants_precorrection.ipynb
@@ -37,7 +37,8 @@
     "# Conditions for retrieving calibration constants.\n",
     "bias_voltage = 200  # bias voltage\n",
     "in_vacuum = False  # detector operated in vacuum\n",
-    "fix_temperature = 290.  # fix temperature to this value\n",
+    "fix_temperature = -1  # fixed temperature value in Kelvin. Default value -1 to use the value from files.\n",
+    "integration_time = -1  # Detector integration time, Default value -1 to use the value from the slow data.\n",
     "gain_photon_energy = 9.0  # Photon energy used for gain calibration\n",
     "\n",
     "# Flags to select type of applied corrections.\n",
@@ -54,6 +55,7 @@
     "from extra_data import RunDirectory\n",
     "from pathlib import Path\n",
     "\n",
+    "from cal_tools.epix100 import epix100lib\n",
     "from cal_tools.tools import (\n",
     "    calcat_creation_time,\n",
     "    get_dir_creation_date,\n",
@@ -108,30 +110,31 @@
     "# Read control data.\n",
     "run_dc = RunDirectory(in_folder / f\"r{run:04d}\")\n",
     "\n",
-    "integration_time = int(\n",
-    "    run_dc.get_run_value(f\"{karabo_id}/DET/CONTROL\", \"expTime.value\")\n",
-    ")\n",
-    "temperature = (\n",
-    "    np.mean(\n",
-    "        run_dc.get_array(\n",
-    "            f\"{karabo_id}/DET/{receiver_template}:daqOutput\", f\"data.backTemp\"\n",
-    "        ).values\n",
+    "ctrl_data = epix100lib.epix100Ctrl(\n",
+    "    run_dc=run_dc,\n",
+    "    instrument_src=f\"{karabo_id}/DET/{receiver_template}:daqOutput\",\n",
+    "    ctrl_src=f\"{karabo_id}/DET/CONTROL\",\n",
     "    )\n",
-    "    / 100.0\n",
-    ")\n",
     "\n",
-    "if fix_temperature != 0:\n",
-    "    temperature_k = fix_temperature\n",
-    "    print(\"Temperature is fixed!\")\n",
+    "if integration_time < 0:\n",
+    "    integration_time = ctrl_data.get_integration_time()\n",
+    "    integration_time_str_add = \"\"\n",
     "else:\n",
+    "    integration_time_str_add = \"(manual input)\"\n",
+    "\n",
+    "if fix_temperature < 0:\n",
+    "    temperature = ctrl_data.get_temprature()\n",
     "    temperature_k = temperature + 273.15\n",
+    "    temp_str_add = \"\"\n",
+    "else:\n",
+    "    temperature_k = fix_temperature\n",
+    "    temperature = fix_temperature - 273.15\n",
+    "    temp_str_add = \"(manual input)\"\n",
+    "\n",
     "\n",
     "print(f\"Bias voltage is {bias_voltage} V\")\n",
-    "print(f\"Detector integration time is set to {integration_time}\")\n",
-    "print(\n",
-    "    f\"Mean temperature was {temperature:0.2f} °C \"\n",
-    "    f\"/ {temperature_k:0.2f} K at beginning of the run.\"\n",
-    ")\n",
+    "print(f\"Detector integration time is set to {integration_time} \\u03BCs {integration_time_str_add}\")\n",
+    "print(f\"Mean temperature: {temperature:0.2f}°C / {temperature_k:0.2f} K {temp_str_add}\")\n",
     "print(f\"Operated in vacuum: {in_vacuum}\")"
    ]
   },
diff --git a/src/cal_tools/epix100/epix100lib.py b/src/cal_tools/epix100/epix100lib.py
new file mode 100644
index 000000000..ee501960d
--- /dev/null
+++ b/src/cal_tools/epix100/epix100lib.py
@@ -0,0 +1,26 @@
+import extra_data
+
+
+class epix100Ctrl():
+    def __init__(
+        self,
+        run_dc: extra_data.DataCollection,
+        ctrl_src: str,
+        instrument_src: str,
+    ):
+        """Read epix100 parameters to use later while quering CALCAT.
+        :param run_dir: EXtra-data RunDirectory DataCollection object.
+        :param ctrl_src: CONTROL source for accessing slow data.
+        :param instrument_src: INSTRUMENT source for accessing fast data.
+        """
+        self.run_dc = run_dc
+        self.ctrl_src = ctrl_src
+        self.instrument_src = instrument_src
+
+    def get_integration_time(self):
+        return self.run_dc[
+            self.ctrl_src, 'expTime.value'].as_single_value(reduce_by='first')
+
+    def get_temprature(self):
+        return self.run_dc[
+            self.instrument_src, 'data.backTemp'].ndarray().mean() / 100
-- 
GitLab