diff --git a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb index ba512df7877a8868ae79454b68e80a610bb9ca1c..ee5625e7d6f783475005a219f5418ad33f568d71 100644 --- a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb +++ b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb @@ -180,6 +180,7 @@ "else:\n", " print(f\"Gain setting is manually set to {gain_setting}.\")\n", "\n", + "force_fixed_gain_constants_flag = False\n", "if gain_mode < 0:\n", " gain_mode = ctrl_data.get_gain_mode()\n", " print(f\"Gain mode is {gain_mode} ({ctrl_data.run_mode})\")\n", @@ -188,11 +189,80 @@ " if gain_mode == 0 and memory_cells > 1:\n", " print(\"By default fixed gain constant will be retrieved for burst mode data,\"\n", " \" even for dynamic gain data.\")\n", - " gain_mode = 1\n", + " force_fixed_gain_constants_flag = True\n", "else:\n", " print(f\"Gain mode is manually set to {gain_mode}.\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def jungfrau_cal_mdata(gm):\n", + " jf_cal = JUNGFRAU_CalibrationData(\n", + " detector_name=karabo_id,\n", + " sensor_bias_voltage=bias_voltage,\n", + " event_at=creation_time,\n", + " modules=karabo_da,\n", + " memory_cells=memory_cells,\n", + " integration_time=integration_time,\n", + " gain_setting=gain_setting,\n", + " gain_mode=gm,\n", + " client=rest_cfg.calibration_client(),\n", + " )\n", + " da_to_pdu = {}\n", + " for mod_info in jf_cal.physical_detector_units.values():\n", + " da_to_pdu[mod_info[\"karabo_da\"]] = mod_info[\"physical_name\"]\n", + "\n", + " constant_names = [\"Offset10Hz\", \"BadPixelsDark10Hz\"]\n", + " if relative_gain:\n", + " constant_names += [\"BadPixelsFF10Hz\", \"RelativeGain10Hz\"]\n", + " jf_metadata = jf_cal.metadata(calibrations=constant_names) \n", + " # Display retrieved calibration constants timestamps\n", + " jf_cal.display_markdown_retrieved_constants(metadata=jf_metadata)\n", + " return jf_metadata\n", + "\n", + "def force_fixed_gain_constants():\n", + " \"\"\"JF corrections in burst mode are only supported when\n", + " no gain switching occurs. Always retrieve fixed gain\n", + " constant for burst mode.\n", + " https://git.xfel.eu/calibration/planning/-/issues/196\n", + "\n", + " Returns:\n", + " dict: The metadata with the jungfrau retrieved constants.\n", + " {mod: {cname: ccv_metadata}}\n", + " \"\"\"\n", + " from datetime import datetime\n", + "\n", + " from cal_tools.calcat_interface import CalCatError\n", + "\n", + " try:\n", + " jf_metadata = jungfrau_cal_mdata(gm=1)\n", + " except CalCatError as e:\n", + " warning(\n", + " \"No fixed gain constants found. \"\n", + " \"Looking for dynamic gain constant. \"\n", + " f\"(CalCatError: {e}.\")\n", + "\n", + " jf_metadata = jungfrau_cal_mdata(gm=0)\n", + "\n", + " for mod, ccvs in jf_metadata.items():\n", + " offset = ccvs.get(\"Offset10Hz\")\n", + " if not offset: # This module wont be corrected later after validating constants.\n", + " continue\n", + " time_difference = creation_time - datetime.fromisoformat(offset[\"begin_validity_at\"])\n", + " if abs(time_difference.days) > 3:\n", + " warning(\n", + " f\"No dynamic gain constant retrieved for {mod} with the last\"\n", + " \" 3 days from the RAW data create date.\"\n", + " \" Please make sure there are available constants.\")\n", + " jf_metadata[mod].pop(\"Offset10Hz\")\n", + "\n", + " return jf_metadata" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -208,29 +278,10 @@ }, "outputs": [], "source": [ - "jf_cal = JUNGFRAU_CalibrationData(\n", - " detector_name=karabo_id,\n", - " sensor_bias_voltage=bias_voltage,\n", - " event_at=creation_time,\n", - " modules=karabo_da,\n", - " memory_cells=memory_cells,\n", - " integration_time=integration_time,\n", - " gain_setting=gain_setting,\n", - " gain_mode=gain_mode,\n", - " client=rest_cfg.calibration_client(),\n", - ")\n", - "\n", - "da_to_pdu = {}\n", - "for mod_info in jf_cal.physical_detector_units.values():\n", - " da_to_pdu[mod_info[\"karabo_da\"]] = mod_info[\"physical_name\"]\n", - "\n", - "constant_names = [\"Offset10Hz\", \"BadPixelsDark10Hz\"]\n", - "if relative_gain:\n", - " constant_names += [\"BadPixelsFF10Hz\", \"RelativeGain10Hz\"]\n", - "\n", - "jf_metadata = jf_cal.metadata(calibrations=constant_names)\n", - "# Display retrieved calibration constants timestamps\n", - "jf_cal.display_markdown_retrieved_constants(metadata=jf_metadata)" + "if force_fixed_gain_constants_flag:\n", + " jf_metadata = force_fixed_gain_constants()\n", + "else:\n", + " jf_metadata = jungfrau_cal_mdata(gain_mode)" ] }, {