From 3faf72c375b4277fd8c937b942d7aa0be7bf62f9 Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Mon, 28 Nov 2022 13:00:32 +0100
Subject: [PATCH] epix100 changes

---
 .../ePix100/Correction_ePix100_NBC.ipynb      | 41 +++++++++----------
 ...100_retrieve_constants_precorrection.ipynb | 18 ++++----
 2 files changed, 31 insertions(+), 28 deletions(-)

diff --git a/notebooks/ePix100/Correction_ePix100_NBC.ipynb b/notebooks/ePix100/Correction_ePix100_NBC.ipynb
index 829a24b3d..0a0fec78d 100644
--- a/notebooks/ePix100/Correction_ePix100_NBC.ipynb
+++ b/notebooks/ePix100/Correction_ePix100_NBC.ipynb
@@ -270,16 +270,6 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "epix_cal = EPIX100_CalibrationData(\n",
-    "    detector_name=karabo_id,\n",
-    "    sensor_bias_voltage=bias_voltage,\n",
-    "    integration_time=integration_time,\n",
-    "    sensor_temperature=temperature_k,\n",
-    "    in_vacuum=in_vacuum,\n",
-    "    source_energy=gain_photon_energy,\n",
-    "    event_at=creation_time,\n",
-    "    snapshot_at=None,\n",
-    "    )\n",
     "constant_names = [\"OffsetEPix100\", \"NoiseEPix100\"]\n",
     "if relative_gain:\n",
     "    constant_names += [\"RelativeGainEPix100\"]\n",
@@ -288,17 +278,26 @@
     "    print(f\"Using stored constants in {metadata.filename}\")\n",
     "    const_data, _ = load_constants_dict(const_yaml[karabo_da][\"constants\"])\n",
     "else:\n",
-    "    const_data = dict()\n",
-    "    for cname in constant_names:\n",
-    "        try:\n",
-    "            const_data[cname] = epix_cal.ndarray(module=1, calibration=cname)\n",
-    "        except CalCatError as e:\n",
-    "            if cname != \"RelativeGainEPix100\":\n",
-    "                raise\n",
-    "\n",
-    "# Set gain bools to False if constant is not available.\n",
-    "if relative_gain and \"RelativeGainEPix100\" not in const_data.values():\n",
-    "    warning(\"RelativeGainEPix100 is not found in CALCAT. Gain correction wont be applied.\")\n",
+    "    epix_cal = EPIX100_CalibrationData(\n",
+    "        detector_name=karabo_id,\n",
+    "        sensor_bias_voltage=bias_voltage,\n",
+    "        integration_time=integration_time,\n",
+    "        sensor_temperature=temperature_k,\n",
+    "        in_vacuum=in_vacuum,\n",
+    "        source_energy=gain_photon_energy,\n",
+    "        event_at=creation_time,\n",
+    "    )\n",
+    "    const_data = epix_cal.ndarray_map(calibrations=constant_names)[karabo_da]\n",
+    "\n",
+    "# Validate the constants availability and raise/warn correspondingly. \n",
+    "missing_dark_constants = set(\n",
+    "    c for c in [\"OffsetEPix100\", \"NoiseEPix100\"] if c not in const_data.keys())\n",
+    "if missing_dark_constants:\n",
+    "    raise KeyError(\n",
+    "        f\"Dark constants {missing_dark_constants} are not available for correction.\")\n",
+    "\n",
+    "if relative_gain and \"RelativeGainEPix100\" not in const_data.keys():\n",
+    "    warning(\"RelativeGainEPix100 is not found in the calibration database.\")\n",
     "    relative_gain = False\n",
     "    absolute_gain = False"
    ]
diff --git a/notebooks/ePix100/ePix100_retrieve_constants_precorrection.ipynb b/notebooks/ePix100/ePix100_retrieve_constants_precorrection.ipynb
index 67ce2eb00..cc37766bb 100644
--- a/notebooks/ePix100/ePix100_retrieve_constants_precorrection.ipynb
+++ b/notebooks/ePix100/ePix100_retrieve_constants_precorrection.ipynb
@@ -154,7 +154,6 @@
     "    in_vacuum=in_vacuum,\n",
     "    source_energy=gain_photon_energy,\n",
     "    event_at=creation_time,\n",
-    "    snapshot_at=None,  # creation_time,  # TODO:.. why None works\n",
     "    )\n",
     "\n",
     "mdata_dict = {\"constants\": dict()}\n",
@@ -164,9 +163,17 @@
     "    constant_names += [\"RelativeGainEPix100\"]\n",
     "\n",
     "# Retrieve metadata for all epix100 constants.\n",
-    "# Error out if dark constants are not retrieved.\n",
-    "raise_error = False if relative_gain else True\n",
-    "epix_metadata = epix_cal.metadata(constant_names, raise_error=raise_error)\n",
+    "\n",
+    "epix_metadata = epix_cal.metadata(constant_names)\n",
+    "\n",
+    "# Validate the constants availability and raise/warn correspondingly. \n",
+    "missing_dark_constants = set(\n",
+    "    c for c in [\"OffsetEPix100\", \"NoiseEPix100\"] if c not in epix_metadata[karabo_da].keys())\n",
+    "if missing_dark_constants:\n",
+    "    raise KeyError(\n",
+    "        f\"Dark constants {missing_dark_constants} are not available for correction.\")\n",
+    "if relative_gain and \"RelativeGainEPix100\" not in list(epix_metadata.values())[0].keys():\n",
+    "    warning(\"RelativeGainEPix100 is not found in CALCAT.\")\n",
     "\n",
     "for cname, ccv_metadata in list(epix_metadata.values())[0].items():\n",
     "    mdata_dict[\"constants\"][cname] = {\n",
@@ -176,9 +183,6 @@
     "    }\n",
     "    print(f\"Retrieved {cname} with creation-time: {ccv_metadata['begin_validity_at']}\")\n",
     "\n",
-    "if relative_gain and \"RelativeGainEPix100\" not in mdata_dict[\"constants\"]:\n",
-    "    warning(\"RelativeGainEPix100 is not found in CALCAT.\")\n",
-    "\n",
     "mdata_dict[\"physical-name\"] = ccv_metadata[\"physical_name\"]\n",
     "retrieved_constants[karabo_da] = mdata_dict\n",
     "metadata.save()\n",
-- 
GitLab