From 74beb5c973aabb9c97ee36a9d74321988aafbae8 Mon Sep 17 00:00:00 2001 From: ahmedk <karim.ahmed@xfel.eu> Date: Wed, 27 Dec 2023 10:57:34 +0100 Subject: [PATCH] send constant to database notebook --- .../Jungfrau/gainCal_JF_Fit_sendDB_NBC.ipynb | 276 ++++++++++++++++++ 1 file changed, 276 insertions(+) create mode 100755 notebooks/Jungfrau/gainCal_JF_Fit_sendDB_NBC.ipynb diff --git a/notebooks/Jungfrau/gainCal_JF_Fit_sendDB_NBC.ipynb b/notebooks/Jungfrau/gainCal_JF_Fit_sendDB_NBC.ipynb new file mode 100755 index 000000000..2f5266eae --- /dev/null +++ b/notebooks/Jungfrau/gainCal_JF_Fit_sendDB_NBC.ipynb @@ -0,0 +1,276 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Send constants from file to the DB" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "in_folder = '/gpfs/exfel/data/user/mramilli/jungfrau/module_PSI_gainmaps/M109'\n", + "raw_folder = '/gpfs/exfel/exp/SPB/202330/900322/raw'\n", + "gain_map_file = '/gainMaps_M109_Burst_Fix_20230523.h5' # path\n", + "g0_run = 94\n", + "\n", + "# Detector module parameters.\n", + "karabo_id = 'SPB_CFEL_JF1M'\n", + "da_name = 'JNGFR09'\n", + "db_module = \"Jungfrau_M109\" # ID of module in calibration database\n", + "\n", + "# Parameter conditions\n", + "bias_voltage = 180 # detector bias voltage\n", + "integration_time = 10 # the detector acquisition rate, use 0 to try to auto-determine\n", + "gain_setting = 0\n", + "gain_mode = 0\n", + "memory_cells = 0 # number of memory cells used, use 0 to auto-derive\n", + "\n", + "# Condition limits\n", + "bias_voltage_lims = [0, 200]\n", + "integration_time_lims = [0.1, 1000]\n", + "\n", + "gain_map_name = 'gain_map_g0'\n", + "db_output = True\n", + "send_bpix = False\n", + "\n", + "# CALCAT API parameters\n", + "cal_db_interface = \"tcp://max-exfl016:8020\" # the database interface to use\n", + "creation_time = \"\" # To overwrite the measured creation_time. Required Format: YYYY-MM-DD HR:MN:SC e.g. \"2022-06-28 13:00:00\"\\\n", + "cal_db_timeout = 180000" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# imports, usually no need to change anything here\n", + "import os\n", + "from h5py import File as h5file\n", + "import numpy as np\n", + "import datetime\n", + "\n", + "import dateutil.parser\n", + "from iCalibrationDB import (\n", + " Constants,\n", + " Conditions,\n", + ")\n", + "from cal_tools.tools import (\n", + " send_to_db,\n", + " calcat_creation_time\n", + ")\n", + "from pathlib import Path\n", + "import matplotlib.pyplot as plt\n", + "from XFELDetAna.plotting.heatmap import heatmapPlot\n", + "%matplotlib inline" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if gain_setting == 1:\n", + " gain_map_name = 'gain_map_hg0'\n", + "\n", + "in_folder = Path(in_folder)\n", + "raw_folder = Path(raw_folder)\n", + "\n", + "in_file = in_folder / gain_map_file" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Read constants from file\n", + "constant_data = {}\n", + "\n", + "# Run's creation time:\n", + "creation_time = calcat_creation_time(in_folder, g0_run, creation_time)\n", + "print(f\"Creation time: {creation_time}\")\n", + "\n", + "gain_map = None\n", + "bad_pixel_ff = None\n", + "\n", + "with h5file(in_file, 'r') as f:\n", + " gain_map = np.array(f[gain_map_name])\n", + " \n", + " if memory_cells == 0:\n", + " memory_cells = int(gain_map.shape[-2])\n", + " print(f'Memory Cells: {memory_cells}')\n", + " if 'bad_pixel_fit' in f.keys():\n", + " bad_pixel_ff = np.array(f['bad_pixel_fit'])\n", + " print(bad_pixel_ff.shape)\n", + " else:\n", + " print('BadPixelsFF not found')\n", + " \n", + " if 'integration_time' in f.attrs.keys():\n", + " integration_time = np.float32(f.attrs['integration_time'])\n", + " print(f'Integration time: {integration_time} us')\n", + " else:\n", + " print('integration_time not found, using default {} us'.format(integration_time))\n", + " \n", + " if 'bias_voltage' in f.attrs.keys():\n", + " bias_voltage = np.float32(f.attrs['bias_voltage'])\n", + " print(f'Bias voltage: {bias_voltage} V')\n", + " else:\n", + " print('bias_voltage not found, using default {} V'.format(bias_voltage))\n", + "\n", + " if 'dir_date_iso' in f.attrs.keys():\n", + " dir_date_iso = str(f.attrs['dir_date_iso'])\n", + " creation_time = dateutil.parser.isoparse(dir_date_iso)\n", + " print(f'Acquisition Time: {creation_time}')\n", + " else:\n", + " print(f'Acquisition date not found!\\nUsing this date: {creation_time}')\n", + "\n", + " if 'karabo_id' in f.attrs.keys():\n", + " if isinstance(f.attrs['karabo_id'], str):\n", + " karabo_id = str(f.attrs['karabo_id'])\n", + " print(f'Karabo ID: {karabo_id}')\n", + " else:\n", + " print(f'karabo id not a string, using {karabo_id}')\n", + " else:\n", + " print(f'karabo id not found, using {karabo_id}')\n", + " \n", + " if 'da_name' in f.attrs.keys():\n", + " if isinstance(f.attrs['da_name'], str):\n", + " da_name = str(f.attrs['da_name'])\n", + " print(f'DA name: {da_name}')\n", + " else:\n", + " print(f'DA name not a string, using {da_name}')\n", + " else:\n", + " print(f'DA name not found, using {da_name}')\n", + "\n", + " if 'gain_mode' in f.attrs.keys():\n", + " gain_mode = np.int(f.attrs['gain_mode'])\n", + " print(f'Gain mode: {gain_mode}')\n", + " else:\n", + " print(f'gain mode not found, using default {gain_mode}')\\\n", + "\n", + " f.close()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "gain = Constants.jungfrau.RelativeGain()\n", + "gain.data = gain_map\n", + "\n", + "condition = Conditions.Dark.jungfrau(\n", + " memory_cells=memory_cells, \n", + " bias_voltage=bias_voltage,\n", + " integration_time=integration_time,\n", + " gain_mode=gain_mode,\n", + " gain_setting=gain_setting)\n", + "\n", + "for parm in condition.parameters:\n", + " if parm.name == \"Integration Time\":\n", + " print('setting integration time limits')\n", + " parm.lower_deviation = integration_time - integration_time_lims[0]\n", + " parm.upper_deviation = integration_time_lims[1] - integration_time\n", + "\n", + "for parm in condition.parameters:\n", + " if parm.name == \"Sensor Bias Voltage\":\n", + " print('setting bias voltage limits')\n", + " parm.lower_deviation = bias_voltage - bias_voltage_lims[0]\n", + " parm.upper_deviation = bias_voltage_lims[1] - bias_voltage" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print('Creating time: ', creation_time)\n", + "\n", + "if db_output:\n", + " send_to_db(\n", + " db_module=db_module,\n", + " karabo_id=karabo_id, \n", + " constant=gain, \n", + " condition=condition, \n", + " file_loc=in_file, \n", + " report_path='',\n", + " cal_db_interface=cal_db_interface,\n", + " creation_time=creation_time,\n", + " )\n", + "\n", + "if bad_pixel_ff is not None and send_bpix:\n", + " bpix_ff = Constants.jungfrau.BadPixelsFF()\n", + " # WHY CONDITION DEVIATIONS ARE NOT CONSIDERED FOR THE BADPIXELS\n", + " condition = Conditions.Dark.jungfrau(\n", + " memory_cells=memory_cells, \n", + " bias_voltage=bias_voltage,\n", + " integration_time=integration_time,\n", + " gain_setting=gain_setting,\n", + " )\n", + "\n", + " bpix_ff.data = bad_pixel_ff\n", + " send_to_db(\n", + " db_module=db_module,\n", + " karabo_id=karabo_id,\n", + " constant=bpix_ff, \n", + " condition=condition, \n", + " file_loc=in_file,\n", + " report_path='',\n", + " cal_db_interface=cal_db_interface,\n", + " creation_time=creation_time,\n", + " )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for g in range(0, gain_map.shape[3]):\n", + " f_im = heatmapPlot(\n", + " np.swapaxes(gain_map[..., 0, g], 0, 1), \n", + " y_label=\"Row\",\n", + " x_label=\"Column\",\n", + " lut_label=\"G{:01d}[ADCu/keV]\".format(g),\n", + " aspect=1.,\n", + " vmin=np.min(gain_map[..., 0, g].ravel()),\n", + " vmax=np.max(gain_map[..., 0, g].ravel()),\n", + " )\n", + " plt.show()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.11" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} -- GitLab