From df93edadf4ac4b670cce6c67b5ebde525d19c1d6 Mon Sep 17 00:00:00 2001
From: Karim Ahmed <karim.ahmed@xfel.eu>
Date: Fri, 24 Jul 2020 15:06:32 +0200
Subject: [PATCH] add try/except + test corr nb

---
 .../pnCCD/Characterize_pnCCD_Dark_NBC.ipynb   | 49 +++++++++++-----
 notebooks/pnCCD/Correct_pnCCD_NBC.ipynb       | 56 ++++++++++++-------
 2 files changed, 69 insertions(+), 36 deletions(-)

diff --git a/notebooks/pnCCD/Characterize_pnCCD_Dark_NBC.ipynb b/notebooks/pnCCD/Characterize_pnCCD_Dark_NBC.ipynb
index a0063669d..d01308896 100644
--- a/notebooks/pnCCD/Characterize_pnCCD_Dark_NBC.ipynb
+++ b/notebooks/pnCCD/Characterize_pnCCD_Dark_NBC.ipynb
@@ -34,9 +34,9 @@
     "in_folder = \"/gpfs/exfel/exp/SQS/202031/p900166/raw\"  # input folder, required\n",
     "out_folder = '/gpfs/exfel/data/scratch/ahmedk/test/pnccd'  # output folder, required\n",
     "sequence = 0  # sequence file to use\n",
-    "run = 34 # which run to read data from, required\n",
+    "run = 39 # which run to read data from, required\n",
     "\n",
-    "db_module = \"PnCCD1\"\n",
+    "db_module = \"pnCCD_M205_M206\"\n",
     "karabo_da = ['PNCCD01'] # data aggregators\n",
     "karabo_da_control = \"PNCCD02\" # file inset for control data\n",
     "karabo_id = \"SQS_NQS_PNCCD1MP\" # karabo prefix of PNCCD devices\n",
@@ -56,7 +56,7 @@
     "number_dark_frames = 0  # number of images to be used, if set to 0 all available images are used\n",
     "chunkSize = 100 # number of images to read per chunk\n",
     "fix_temperature = 0.  # fix temperature in K, set to 0. to use value from slow data\n",
-    "gain = 1  # the detector's gain setting, It is later read from file and this value is overwritten\n",
+    "gain = 0.  # the detector's gain setting, It is later read from file and this value is overwritten\n",
     "bias_voltage = 0. # the detector's bias voltage. set to 0. to use value from slow data.\n",
     "integration_time = 70  # detector's integration time\n",
     "commonModeAxis = 0 # axis along which common mode will be calculated (0: along rows, 1: along columns)\n",
@@ -203,12 +203,20 @@
     "    ctrl_fname = os.path.join(ped_dir, path_template.format(run, karabo_da_control)).format(sequence)\n",
     "    ctrl_path = h5path_ctrl.format(karabo_id)\n",
     "    mdl_ctrl_path = f\"/CONTROL/{karabo_id}/MDL/\"\n",
-    "    with h5py.File(ctrl_fname, \"r\") as f:\n",
-    "        if bias_voltage == 0.:\n",
-    "            bias_voltage = abs(f[os.path.join(mdl_ctrl_path, \"DAQ_MPOD/u0voltage/value\")][0])\n",
-    "        gain = f[os.path.join(mdl_ctrl_path, \"DAQ_GAIN/spNCCDGain/value\")][0]\n",
-    "        if fix_temperature == 0.:\n",
-    "            fix_temperature = f[os.path.join(ctrl_path, \"inputA/krdg/value\")][0]"
+    "    try:\n",
+    "        with h5py.File(ctrl_fname, \"r\") as f:\n",
+    "            if bias_voltage == 0.:\n",
+    "                print(\"bias voltage control h5path:\", os.path.join(mdl_ctrl_path, \"DAQ_MPOD/u0voltage/value\"))\n",
+    "                bias_voltage = abs(f[os.path.join(mdl_ctrl_path, \"DAQ_MPOD/u0voltage/value\")][0])\n",
+    "            gain = f[os.path.join(mdl_ctrl_path, \"DAQ_GAIN/pNCCDGain/value\")][0]\n",
+    "            if fix_temperature == 0.:\n",
+    "                fix_temperature = f[os.path.join(ctrl_path, \"inputA/krdg/value\")][0]\n",
+    "    except KeyError:\n",
+    "        print(\"Error !!! during extracting slow data\")\n",
+    "        traceback.print_exc(limit=1)\n",
+    "        print(\"bias voltage control h5path:\", os.path.join(mdl_ctrl_path, \"DAQ_MPOD/u0voltage/value\"))\n",
+    "        print(\"gain control h5path:\", os.path.join(mdl_ctrl_path, \"DAQ_GAIN/pNCCDGain/value\"))\n",
+    "        print(\"fix_temperature control h5path:\", os.path.join(ctrl_path, \"inputA/krdg/value\"))"
    ]
   },
   {
@@ -1003,7 +1011,7 @@
     "    condition = Conditions.Dark.CCD(bias_voltage=bias_voltage,\n",
     "                                    integration_time=integration_time,\n",
     "                                    gain_setting=gain,\n",
-    "                                    temperature=temperature_k,\n",
+    "                                    temperature=fix_temperature,\n",
     "                                    pixels_x=pixels_x,\n",
     "                                    pixels_y=pixels_y)\n",
     "\n",
@@ -1026,10 +1034,14 @@
     "\n",
     "    if db_output:\n",
     "        try:\n",
-    "            metadata.send(cal_db_interface)\n",
-    "            print(f\"Inject {const_name} constants from {metadata.calibration_constant_version.begin_at}\")\n",
-    "        except Exception as e:\n",
-    "            print(e)\n",
+    "            metadata.send(cal_db_interface, timeout=cal_db_timeout)\n",
+    "            print(f\"Inject {const_name} constants from {metadata.calibration_constant_version.begin_at}\\n\")\n",
+    "        except Exception as e:    \n",
+    "            if \"has already been take\" in str(e):\n",
+    "                print(f\"WARN: {const_name} has already been injected with the same parameter conditions\\n\")\n",
+    "            else:\n",
+    "                # To prevent having big error message out of the pdf report's page.\n",
+    "                print(\"\\n\".join(textwrap.wrap(str(e),100)))\n",
     "\n",
     "    if local_output:\n",
     "        save_const_to_h5(metadata, out_folder)\n",
@@ -1037,10 +1049,17 @@
     "\n",
     "print(\"Generated constants with conditions:\\n\")\n",
     "print(f\"• bias_voltage: {bias_voltage}\\n• integration_time: {integration_time}\\n\"\n",
-    "      f\"• gain_setting: {gain}\\n• temperature: {temperature_k}\\n\"\n",
+    "      f\"• gain_setting: {gain}\\n• temperature: {fix_temperature}\\n\"\n",
     "      f\"• creation_time: {creation_time}\\n\")"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  },
   {
    "cell_type": "code",
    "execution_count": null,
diff --git a/notebooks/pnCCD/Correct_pnCCD_NBC.ipynb b/notebooks/pnCCD/Correct_pnCCD_NBC.ipynb
index e592a7cf0..b1484e339 100644
--- a/notebooks/pnCCD/Correct_pnCCD_NBC.ipynb
+++ b/notebooks/pnCCD/Correct_pnCCD_NBC.ipynb
@@ -22,17 +22,17 @@
    },
    "outputs": [],
    "source": [
-    "in_folder = \"/gpfs/exfel/exp/SQS/201930/p900075/raw\" # input folder\n",
+    "in_folder = \"/gpfs/exfel/exp/SQS/202031/p900166/raw\" # input folder\n",
     "out_folder = '/gpfs/exfel/data/scratch/ahmedk/test/pnccd' # output folder\n",
-    "run = 365 # which run to read data from\n",
-    "sequences = [-1] # sequences to correct, set to -1 for all, range allowed\n",
+    "run = 39 # which run to read data from\n",
+    "sequences = [0] # sequences to correct, set to -1 for all, range allowed\n",
     "\n",
-    "db_module = \"PnCCD1\"\n",
+    "db_module = \"pnCCD_M205_M206\"\n",
     "karabo_da = 'PNCCD01' # data aggregators\n",
     "karabo_da_control = \"PNCCD02\" # file inset for control data\n",
     "karabo_id = \"SQS_NQS_PNCCD1MP\" # karabo prefix of PNCCD devices\n",
     "receiver_id = \"PNCCD_FMT-0\" # inset for receiver devices\n",
-    "path_template = 'RAW-R{:04d}-PNCCD01-S{{:05d}}.h5' # the template to use to access data\n",
+    "path_template = 'RAW-R{:04d}-{}-S{{:05d}}.h5' # the template to use to access data\n",
     "path_template_seqs = \"{}/r{:04d}/*PNCCD01-S*.h5\"\n",
     "h5path = '/INSTRUMENT/{}/CAL/{}:output/data/' # path to data in the HDF5 file \n",
     "h5path_ctrl = '/CONTROL/{}/CTRL/TCTRL'\n",
@@ -55,7 +55,7 @@
     "\n",
     "# pnCCD parameters:\n",
     "fix_temperature = 0. # fix temperature in K, set to 0. to use value from slow data.\n",
-    "gain = 1 # the detector's gain setting, It is later read from file and this value is overwritten\n",
+    "gain = 0. # the detector's gain setting, It is later read from file and this value is overwritten\n",
     "bias_voltage = 0. # the detector's bias voltage. set to 0. to use value from slow data.\n",
     "integration_time = 70\n",
     "photon_energy = 1.6 # Al fluorescence in keV\n",
@@ -107,6 +107,7 @@
     "import time\n",
     "import copy\n",
     "import os\n",
+    "import traceback\n",
     "import glob\n",
     "import datetime\n",
     "from datetime import timedelta\n",
@@ -216,17 +217,23 @@
    "source": [
     "# extract slow data\n",
     "if karabo_da_control:\n",
-    "\n",
-    "    ctrl_fname = os.path.join(ped_dir, path_template.format(run, karabo_da_control)).format(sequence)\n",
+    "    ctrl_fname = os.path.join(ped_dir, path_template.format(run, karabo_da_control)).format(sequences[0])\n",
     "    ctrl_path = h5path_ctrl.format(karabo_id)\n",
-    "    mdl_ctrl_path = \"/CONTROL/{}/MDL/\".format(karabo_id)\n",
-    "\n",
-    "    with h5py.File(ctrl_fname, \"r\") as f:\n",
-    "        if bias_voltage == 0 :\n",
-    "            bias_voltage = abs(f[os.path.join(mdl_ctrl_path, \"DAQ_MPOD/u0voltage/value\")][0])\n",
-    "        gain = f[os.path.join(mdl_ctrl_path, \"DAQ_GAIN/spNCCDGain/value\")][0]\n",
-    "        if fix_temperature == 0.:\n",
-    "            fix_temperature = f[os.path.join(ctrl_path, \"inputA/krdg/value\")][0]"
+    "    mdl_ctrl_path = f\"/CONTROL/{karabo_id}/MDL/\"\n",
+    "    try:\n",
+    "        with h5py.File(ctrl_fname, \"r\") as f:\n",
+    "            if bias_voltage == 0.:\n",
+    "                bias_voltage = abs(f[os.path.join(mdl_ctrl_path, \"DAQ_MPOD/u0voltage/value\")][0])\n",
+    "            gain = f[os.path.join(mdl_ctrl_path, \"DAQ_GAIN/pNCCDGain/value\")][0]\n",
+    "            if fix_temperature == 0.:\n",
+    "                fix_temperature = f[os.path.join(ctrl_path, \"inputA/krdg/value\")][0]\n",
+    "    except KeyError:\n",
+    "        print(\"Error !!! during extracting slow data\")\n",
+    "        traceback.print_exc(limit=1)\n",
+    "        print(\"Control file name:\", ctrl_fname)\n",
+    "        print(\"bias voltage control h5path:\", os.path.join(mdl_ctrl_path, \"DAQ_MPOD/u0voltage/value\"))\n",
+    "        print(\"gain control h5path:\", os.path.join(mdl_ctrl_path, \"DAQ_GAIN/pNCCDGain/value\"))\n",
+    "        print(\"fix_temperature control h5path:\", os.path.join(ctrl_path, \"inputA/krdg/value\"))"
    ]
   },
   {
@@ -241,7 +248,7 @@
     "print(\"Bias voltage is {} V.\".format(bias_voltage))\n",
     "print(\"Detector gain is set to {}.\".format(gain))\n",
     "print(\"Detector integration time is set to {} ms\".format(integration_time))\n",
-    "print(f\"Using a fixed temperature of {temperature_k} K\")"
+    "print(f\"Using a fixed temperature of {fix_temperature} K\")"
    ]
   },
   {
@@ -357,7 +364,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "def get_dark(db_parms, bias_voltage, integration_time, gain, temperature_k, creation_time, run):\n",
+    "def get_dark(db_parms, bias_voltage, integration_time, gain, fix_temperature, creation_time, run):\n",
     "# This function is to retrieve the dark constants associated with the run of interest:\n",
     "\n",
     "    cal_db_interface, cal_db_timeout = db_parms\n",
@@ -378,7 +385,7 @@
     "        condition = Conditions.Dark.CCD(bias_voltage=bias_voltage,\n",
     "                            integration_time=integration_time,\n",
     "                            gain_setting=gain,\n",
-    "                            temperature=temperature_k,\n",
+    "                            temperature=fix_temperature,\n",
     "                            pixels_x=pixels_x,\n",
     "                            pixels_y=pixels_y)\n",
     "        \n",
@@ -414,7 +421,7 @@
     "\n",
     "db_parms = cal_db_interface, cal_db_timeout\n",
     "\n",
-    "constants = get_dark(db_parms, bias_voltage, integration_time, gain, temperature_k, creation_time, run)\n",
+    "constants = get_dark(db_parms, bias_voltage, integration_time, gain, fix_temperature, creation_time, run)\n",
     "\n",
     "fig = xana.heatmapPlot(constants[\"Offset\"][:,:,0], x_label='Columns', y_label='Rows', lut_label='Pedestal (ADU)', aspect=1, \n",
     "                       x_range=(0, pixels_y), y_range=(0, pixels_x), vmax=16000, \n",
@@ -449,7 +456,7 @@
     "    condition = Conditions.Illuminated.CCD(bias_voltage=bias_voltage,\n",
     "                                           integration_time=integration_time,\n",
     "                                           gain_setting=gain,\n",
-    "                                           temperature=temperature_k,\n",
+    "                                           temperature=fix_temperature,\n",
     "                                           pixels_x=pixels_x,\n",
     "                                           pixels_y=pixels_y, \n",
     "                                           photon_energy=photon_energy)\n",
@@ -1319,6 +1326,13 @@
     "    l = ax.legend()"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  },
   {
    "cell_type": "code",
    "execution_count": null,
-- 
GitLab