From fb2a10d75c9ece33b62b0b85bb4e61531bc7a46b Mon Sep 17 00:00:00 2001
From: Karim Ahmed <karim.ahmed@xfel.eu>
Date: Wed, 22 Jul 2020 20:47:57 +0200
Subject: [PATCH] change in correction notebook

---
 .../pnCCD/Characterize_pnCCD_Dark_NBC.ipynb   |  35 ++--
 notebooks/pnCCD/Correct_pnCCD_NBC.ipynb       | 174 +++++++++++-------
 2 files changed, 123 insertions(+), 86 deletions(-)

diff --git a/notebooks/pnCCD/Characterize_pnCCD_Dark_NBC.ipynb b/notebooks/pnCCD/Characterize_pnCCD_Dark_NBC.ipynb
index d06759a56..a0063669d 100644
--- a/notebooks/pnCCD/Characterize_pnCCD_Dark_NBC.ipynb
+++ b/notebooks/pnCCD/Characterize_pnCCD_Dark_NBC.ipynb
@@ -55,9 +55,9 @@
     "\n",
     "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 = 233.  # fix temperature in K, set to 0. to use value from slow data\n",
-    "gain = 1  # the detector's gain setting, only 1 and 64 is available.\n",
-    "bias_voltage = 300  # detector's bias voltage\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",
+    "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",
     "commonModeBlockSize = [512, 512] # size of the detector in pixels for common mode calculations\n",
@@ -200,15 +200,15 @@
    "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_path = h5path_ctrl.format(karabo_id)\n",
-    "    mdl_ctrl_path = \"/CONTROL/{}/MDL/\".format(karabo_id)\n",
-    "\n",
+    "    mdl_ctrl_path = f\"/CONTROL/{karabo_id}/MDL/\"\n",
     "    with h5py.File(ctrl_fname, \"r\") as f:\n",
-    "        bias_voltage = abs(f[os.path.join(mdl_ctrl_path, \"DAQ_MPOD/u0voltage/value\")][0])\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",
-    "        fix_temperature = f[os.path.join(ctrl_path, \"inputA/krdg/value\")][0]"
+    "        if fix_temperature == 0.:\n",
+    "            fix_temperature = f[os.path.join(ctrl_path, \"inputA/krdg/value\")][0]"
    ]
   },
   {
@@ -223,7 +223,6 @@
    "outputs": [],
    "source": [
     "# Reading Parameters such as Detector Bias, Gain, etc. from the Data:\n",
-    "\n",
     "memoryCells = 1 # pnCCD has 1 memory cell\n",
     "sensorSize = [pixels_x, pixels_y]\n",
     "blockSize = [sensorSize[0]//2, sensorSize[1]//2]# sensor area will be analysed according to blocksize\n",
@@ -240,21 +239,11 @@
    "outputs": [],
    "source": [
     "# Printing the Parameters Read from the Data File:\n",
-    "\n",
     "display(Markdown('### Detector Parameters'))\n",
-    "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",
-    "\n",
-    "if fix_temperature != 0.:\n",
-    "    print(f\"Using a fixed temperature of {fix_temperature} K\")\n",
-    "    temperature_k = fix_temperature\n",
-    "else:\n",
-    "    print(\"Temperature is not fixed.\")\n",
-    "    #TODO: remove this line after properly saving the temperature in control data.\n",
-    "    temperature_k = 233.\n",
-    "    print(f\"Using a fixed temperature of {fix_temperature} K\")\n",
-    "    \n",
+    "print(f\"Bias voltage is {bias_voltage} V.\")\n",
+    "print(f\"Detector gain is set to {gain}.\")\n",
+    "print(f\"Detector integration time is set to {integration_time} ms\") \n",
+    "print(f\"Using a fixed temperature of {fix_temperature} K\")\n",
     "print(\"Number of dark images to analyze:\", nImages) "
    ]
   },
diff --git a/notebooks/pnCCD/Correct_pnCCD_NBC.ipynb b/notebooks/pnCCD/Correct_pnCCD_NBC.ipynb
index a41a37a6a..e592a7cf0 100644
--- a/notebooks/pnCCD/Correct_pnCCD_NBC.ipynb
+++ b/notebooks/pnCCD/Correct_pnCCD_NBC.ipynb
@@ -27,12 +27,15 @@
     "run = 365 # which run to read data from\n",
     "sequences = [-1] # sequences to correct, set to -1 for all, range allowed\n",
     "\n",
+    "db_module = \"PnCCD1\"\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_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",
     "\n",
     "overwrite = True # keep this as True to not overwrite the output \n",
     "use_dir_creation_date = True # required to obtain creation time of the run\n",
@@ -51,9 +54,9 @@
     "seq_num = 0  # sequence number for which the last plot at the end of the notebook is plotted\n",
     "\n",
     "# pnCCD parameters:\n",
-    "fix_temperature = 233.\n",
-    "gain = 1\n",
-    "bias_voltage = 300\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",
+    "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",
     "\n",
@@ -134,56 +137,6 @@
     "    sequences = None"
    ]
   },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "# Each xcal.HistogramCalculator requires a total number of bins and a binning range. We define these using a \n",
-    "# dictionary:\n",
-    "\n",
-    "# For all xcal histograms:\n",
-    "if gain == 1:\n",
-    "    Hist_Bin_Dict = {\n",
-    "        \"bins\": 70000, # number of bins \n",
-    "        \"bin_range\": [0, 70000]\n",
-    "    }\n",
-    "\n",
-    "    # For the numpy histograms on the last cell of the notebook:\n",
-    "    Event_Bin_Dict = {\n",
-    "        \"event_bins\": 1000, # number of bins \n",
-    "        \"b_range\": [0, 50000] # bin range \n",
-    "    }\n",
-    "    \n",
-    "elif gain == 64:\n",
-    "    # For all xcal histograms:\n",
-    "    Hist_Bin_Dict = {\n",
-    "        \"bins\": 25000, # number of bins \n",
-    "        \"bin_range\": [0, 25000] \n",
-    "    }\n",
-    "    # For the numpy histograms on the last cell of the notebook:\n",
-    "    Event_Bin_Dict = {\n",
-    "        \"event_bins\": 1000, # number of bins \n",
-    "        \"b_range\": [0, 3000] # bin range \n",
-    "    }\n",
-    "    \n",
-    "bins = Hist_Bin_Dict[\"bins\"]\n",
-    "bin_range = Hist_Bin_Dict[\"bin_range\"]\n",
-    "event_bins = Event_Bin_Dict[\"event_bins\"]\n",
-    "b_range = Event_Bin_Dict[\"b_range\"]\n",
-    "\n",
-    "# On the singles spectrum (uploaded in the middle of this notebook), the ADU values correspoding to the boundaries\n",
-    "# of the first peak region are used as cti_limit_low and cti_limit_high:\n",
-    "\n",
-    "if gain == 1:\n",
-    "    cti_limit_low = 3000 # lower limit of cti\n",
-    "    cti_limit_high = 10000 # higher limit of cti\n",
-    "elif gain == 64:\n",
-    "    cti_limit_low = 50\n",
-    "    cti_limit_high = 170"
-   ]
-  },
   {
    "cell_type": "code",
    "execution_count": null,
@@ -224,9 +177,15 @@
     "    creation_time = get_dir_creation_date(in_folder, run)\n",
     "\n",
     "\n",
-    "print(f\"Creation time: {creation_time}\")\n",
-    "    \n",
-    "\n",
+    "print(f\"Creation time: {creation_time}\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
     "# Reading all sequences of the run:\n",
     "file_list = []\n",
     "total_sequences = 0\n",
@@ -249,6 +208,42 @@
     "print(f\"This run has a total number of {total_sequences} sequences.\\n\")"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "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_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]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# Printing the Parameters Read from the Data File:\n",
+    "\n",
+    "display(Markdown('### Detector Parameters'))\n",
+    "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\")"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
@@ -297,6 +292,58 @@
     "    raise AttributeError(\"Output path exists! Exiting\")    "
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# Each xcal.HistogramCalculator requires a total number of bins and a binning range. We define these using a \n",
+    "# dictionary:\n",
+    "\n",
+    "# For all xcal histograms:\n",
+    "if gain == 1:\n",
+    "    Hist_Bin_Dict = {\n",
+    "        \"bins\": 70000, # number of bins \n",
+    "        \"bin_range\": [0, 70000]\n",
+    "    }\n",
+    "\n",
+    "    # For the numpy histograms on the last cell of the notebook:\n",
+    "    Event_Bin_Dict = {\n",
+    "        \"event_bins\": 1000, # number of bins \n",
+    "        \"b_range\": [0, 50000] # bin range \n",
+    "    }\n",
+    "    \n",
+    "#TODO: make it more adaptive for more than only 2 gains [below was for gain==64 only]\n",
+    "else:\n",
+    "    # For all xcal histograms:\n",
+    "    Hist_Bin_Dict = {\n",
+    "        \"bins\": 25000, # number of bins \n",
+    "        \"bin_range\": [0, 25000] \n",
+    "    }\n",
+    "    # For the numpy histograms on the last cell of the notebook:\n",
+    "    Event_Bin_Dict = {\n",
+    "        \"event_bins\": 1000, # number of bins \n",
+    "        \"b_range\": [0, 3000] # bin range \n",
+    "    }\n",
+    "    \n",
+    "bins = Hist_Bin_Dict[\"bins\"]\n",
+    "bin_range = Hist_Bin_Dict[\"bin_range\"]\n",
+    "event_bins = Event_Bin_Dict[\"event_bins\"]\n",
+    "b_range = Event_Bin_Dict[\"b_range\"]\n",
+    "\n",
+    "# On the singles spectrum (uploaded in the middle of this notebook), the ADU values correspoding to the boundaries\n",
+    "# of the first peak region are used as cti_limit_low and cti_limit_high:\n",
+    "\n",
+    "if gain == 1:\n",
+    "    cti_limit_low = 3000 # lower limit of cti\n",
+    "    cti_limit_high = 10000 # higher limit of cti\n",
+    "#TODO: make it more adaptive for more than only 2 gains [below was for gain==64 only\n",
+    "else:\n",
+    "    cti_limit_low = 50\n",
+    "    cti_limit_high = 170"
+   ]
+  },
   {
    "cell_type": "markdown",
    "metadata": {},
@@ -310,7 +357,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "def get_dark(db_parms, bias_voltage, integration_time, gain, fix_temperature, creation_time, run):\n",
+    "def get_dark(db_parms, bias_voltage, integration_time, gain, temperature_k, 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",
@@ -331,13 +378,13 @@
     "        condition = Conditions.Dark.CCD(bias_voltage=bias_voltage,\n",
     "                            integration_time=integration_time,\n",
     "                            gain_setting=gain,\n",
-    "                            temperature=fix_temperature,\n",
+    "                            temperature=temperature_k,\n",
     "                            pixels_x=pixels_x,\n",
     "                            pixels_y=pixels_y)\n",
     "        \n",
     "        for const in constants.keys():\n",
     "            constants[const], when[const] = \\\n",
-    "                get_constant_from_db_and_time(Detectors.PnCCD1,\n",
+    "                get_constant_from_db_and_time(getattr(Detectors, db_module),\n",
     "                                      getattr(Constants.CCD(DetectorTypes.pnCCD), const)(),\n",
     "                                      condition,\n",
     "                                      np.zeros((pixels_x, pixels_y, 1)),\n",
@@ -367,7 +414,7 @@
     "\n",
     "db_parms = cal_db_interface, cal_db_timeout\n",
     "\n",
-    "constants = get_dark(db_parms, bias_voltage, integration_time, gain, fix_temperature, creation_time, run)\n",
+    "constants = get_dark(db_parms, bias_voltage, integration_time, gain, temperature_k, 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",
@@ -402,13 +449,13 @@
     "    condition = Conditions.Illuminated.CCD(bias_voltage=bias_voltage,\n",
     "                                           integration_time=integration_time,\n",
     "                                           gain_setting=gain,\n",
-    "                                           temperature=fix_temperature,\n",
+    "                                           temperature=temperature_k,\n",
     "                                           pixels_x=pixels_x,\n",
     "                                           pixels_y=pixels_y, \n",
     "                                           photon_energy=photon_energy)\n",
     "\n",
     "    constants[\"RelativeGain\"], relgain_time = \\\n",
-    "        get_constant_from_db_and_time(Detectors.PnCCD1,\n",
+    "        get_constant_from_db_and_time(getattr(Detectors, db_module),\n",
     "                                      Constants.CCD(DetectorTypes.pnCCD).RelativeGain(),\n",
     "                                      condition,\n",
     "                                      np.zeros((pixels_x, pixels_y)),\n",
@@ -806,7 +853,8 @@
     "\n",
     "if gain == 1:\n",
     "    x_range = (0, 30000)\n",
-    "elif gain == 64:\n",
+    "#TODO: make it more adaptive for more than only 2 gains [below was for gain==64 only\n",
+    "else:\n",
     "    x_range = (0, 1000)"
    ]
   },
-- 
GitLab