diff --git a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb index 497bb4d1bd6f2944a738a88ca5e63a0c6cfa60e8..761119c47bb08571eb3afe97a467dc326abf1610 100644 --- a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb +++ b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb @@ -33,9 +33,9 @@ "karabo_id_control = \"\" # if control is on a different ID, set to empty string if it is the same a karabo-id\n", "\n", "# Parameters for calibration database.\n", - "use_dir_creation_date = True # use the creation data of the input dir for database queries\n", "cal_db_interface = \"tcp://max-exfl016:8017#8025\" # the database interface to use\n", "cal_db_timeout = 180000 # timeout on caldb requests\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", "\n", "# Parameters affecting corrected data.\n", "relative_gain = True # do relative gain correction.\n", @@ -96,7 +96,7 @@ "from cal_tools.files import DataFile\n", "from cal_tools.step_timing import StepTimer\n", "from cal_tools.tools import (\n", - " get_dir_creation_date,\n", + " calcat_creation_time,\n", " map_seq_files,\n", " CalibrationMetadata,\n", ")\n", @@ -129,10 +129,9 @@ "print(f\"Instrument H5File source: {instrument_src}\")\n", "print(f\"Process modules: {karabo_da}\")\n", "\n", - "creation_time = None\n", - "if use_dir_creation_date:\n", - " creation_time = get_dir_creation_date(in_folder, run)\n", - " print(f\"Using {creation_time} as creation time\")\n", + "# Run's creation time:\n", + "creation_time = calcat_creation_time(in_folder, run, creation_time)\n", + "print(f\"Creation time: {creation_time}\")\n", "\n", "if karabo_id_control == \"\":\n", " karabo_id_control = karabo_id\n", @@ -203,10 +202,6 @@ " gain_setting = ctrl_data.get_gain_setting()\n", " gain_mode = ctrl_data.get_gain_mode()\n", "\n", - "# A workound for correcting runs of forceswitchg1 and forceswitchg2\n", - "# of single cell mode.\n", - "memory_cells = 1 if memory_cells == 2 and \"forceswitch\" in ctrl_data.run_settings else 16\n", - "\n", "print(f\"Integration time is {integration_time} us\")\n", "print(f\"Gain setting is {gain_setting} (run settings: {ctrl_data.run_settings})\")\n", "print(f\"Gain mode is {gain_mode} ({ctrl_data.run_mode})\")\n", @@ -256,7 +251,8 @@ " detector_name=karabo_id,\n", " sensor_bias_voltage=bias_voltage,\n", " event_at=creation_time,\n", - " modules=[int(x[-2:]) for x in karabo_da],\n", + " snapshot_at=creation_time,\n", + " modules=karabo_da,\n", " memory_cells=memory_cells,\n", " integration_time=integration_time,\n", " gain_setting=gain_setting,\n", @@ -269,7 +265,21 @@ " const_data = jf_cal.ndarray_map(calibrations=constant_names)\n", " \n", " for mod_info in jf_cal.physical_detector_units.values():\n", - " da_to_pdu[mod_info[\"karabo_da\"]] = mod_info[\"physical_name\"]" + " da_to_pdu[mod_info[\"karabo_da\"]] = mod_info[\"physical_name\"]\n", + "\n", + "# Validate the constants availability and raise/warn correspondingly. \n", + "for mod, calibrations in const_data.items():\n", + " missing_dark_constants = set(\n", + " c for c in [\"Offset10Hz\", \"BadPixelsDark10Hz\"] if c not in calibrations.keys())\n", + " missing_gain_constants = set(\n", + " c for c in [\"BadPixelsFF10Hz\", \"RelativeGain10Hz\"] if relative_gain and c not in calibrations.keys()) # noqa\n", + " if missing_dark_constants:\n", + " raise KeyError(\n", + " f\"Dark constants {missing_dark_constants} are not available for correction. Module: {mod}\") # noqa\n", + " if missing_gain_constants:\n", + " warning(\n", + " f\"Gain constants {missing_gain_constants} were not retrieved. Module: {mod}\")\n", + " relative_gain = False" ] }, { @@ -295,9 +305,9 @@ " except KeyError as e:\n", " # Fail processing if dark constants are not available.\n", " raise KeyError(f\"Dark constant is not available for correction for {module}. {e}\")\n", - "\n", - " gain_map = constant_arrays[\"RelativeGain10Hz\"]\n", - " mask_ff = constant_arrays[\"BadPixelsFF10Hz\"]\n", + " \n", + " gain_map = constant_arrays.get(\"RelativeGain10Hz\")\n", + " mask_ff = constant_arrays.get(\"BadPixelsFF10Hz\")\n", "\n", " # Combine masks\n", " if mask_ff is not None:\n", @@ -333,10 +343,6 @@ "constants = {}\n", "for offset_map, mask, gain_map, k_da in r:\n", "\n", - " if gain_map is None:\n", - " warning(\"No gain map found. Relative gain correction is disabled.\")\n", - " relative_gain = False\n", - "\n", " constants[k_da] = (offset_map, mask, gain_map)\n", "\n", "const_data.clear()\n", @@ -881,7 +887,7 @@ "metadata": {}, "outputs": [], "source": [ - "for i, (pdu, mod) in enumerate(zip(db_modules, karabo_da)):\n", + "for i, (pdu, mod) in enumerate(da_to_pdu.items()): \n", " h, ex, ey = np.histogram2d(\n", " raw[i].flatten(),\n", " gain[i].flatten(),\n", @@ -910,7 +916,7 @@ "metadata": {}, "outputs": [], "source": [ - "for i, (pdu, mod) in enumerate(zip(db_modules, karabo_da)): \n", + "for i, (pdu, mod) in enumerate(da_to_pdu.items()): \n", " fig, axs = plt.subplots(nrows=2, ncols=1, figsize=(18, 10))\n", " corrected_flatten = corrected[i].flatten()\n", " for ax, hist_range in zip(axs, [(-100, 1000), (-1000, 10000)]):\n", diff --git a/notebooks/Jungfrau/Jungfrau_retrieve_constants_precorrection_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_retrieve_constants_precorrection_NBC.ipynb index 808ff32290aeeac2a4897d599fd05ae326a535aa..7a56e49a6b02794821509dac1e41f0435afe5a18 100644 --- a/notebooks/Jungfrau/Jungfrau_retrieve_constants_precorrection_NBC.ipynb +++ b/notebooks/Jungfrau/Jungfrau_retrieve_constants_precorrection_NBC.ipynb @@ -33,9 +33,9 @@ "karabo_id_control = \"\" # if control is on a different ID, set to empty string if it is the same a karabo-id\n", "\n", "# Parameters for calibration database.\n", - "use_dir_creation_date = True # use the creation data of the input dir for database queries\n", "cal_db_interface = \"tcp://max-exfl016:8017#8025\" # the database interface to use\n", "cal_db_timeout = 180000 # timeout on cal db requests\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", "\n", "# Parameters affecting corrected data.\n", "relative_gain = True # do relative gain correction\n", @@ -55,6 +55,8 @@ "metadata": {}, "outputs": [], "source": [ + "from logging import warning\n", + "\n", "from extra_data import RunDirectory\n", "from pathlib import Path\n", "\n", @@ -62,7 +64,7 @@ "from cal_tools.jungfraulib import JungfrauCtrl\n", "from cal_tools.step_timing import StepTimer\n", "from cal_tools.tools import (\n", - " get_dir_creation_date,\n", + " calcat_creation_time,\n", " CalibrationMetadata,\n", ")" ] @@ -85,10 +87,9 @@ "\n", "out_folder.mkdir(parents=True, exist_ok=True)\n", "\n", - "creation_time = None\n", - "if use_dir_creation_date:\n", - " creation_time = get_dir_creation_date(in_folder, run)\n", - " print(f\"Using {creation_time} as creation time\")\n", + "# Run's creation time:\n", + "creation_time = calcat_creation_time(in_folder, run, creation_time)\n", + "print(f\"Creation time: {creation_time}\")\n", "\n", "if karabo_id_control == \"\":\n", " karabo_id_control = karabo_id" @@ -145,37 +146,44 @@ " detector_name=karabo_id,\n", " sensor_bias_voltage=bias_voltage,\n", " event_at=creation_time,\n", - " modules=None,\n", + " snapshot_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", ")\n", - "constant_names = [\n", - " \"Offset10Hz\", \"BadPixelsDark10Hz\",\n", - " \"BadPixelsFF10Hz\", \"RelativeGain10Hz\",\n", - "]\n", + "constant_names = [\"Offset10Hz\", \"BadPixelsDark10Hz\"]\n", "\n", - "mdata_dict = {\"constants\": dict()}\n", + "if relative_gain:\n", + " constant_names += [\"BadPixelsFF10Hz\", \"RelativeGain10Hz\"]\n", "\n", - "# Don't raise errors for now if relative_gain is on\n", - "# and constant was not retrieved.\n", - "raise_error = False if relative_gain else True\n", - "jf_metadata = jf_cal.metadata(constant_names, raise_error=raise_error)\n", + "jf_metadata = jf_cal.metadata(constant_names)\n", "\n", + "# Add constants metadata in retrieved_constants dict.\n", "for mod, ccv_dict in jf_metadata.items():\n", " mod_dict = retrieved_constants.setdefault(mod, dict())\n", " const_dict = mod_dict.setdefault(\"constants\", dict())\n", - " if [\"Offset10Hz\", \"BadPixelsDark10Hz\"] not in ccv_dict.keys():\n", - " # Fail processing if dark constants are not available.\n", - " raise KeyError(f\"Dark constant is not available for correction for {module}. {e}\")\n", " for cname, ccv_metadata in ccv_dict.items():\n", " const_dict[cname] = {\n", " \"path\": str(jf_cal.caldb_root / ccv_metadata[\"path\"]),\n", " \"dataset\": ccv_metadata[\"dataset\"],\n", " \"creation-time\": ccv_metadata[\"begin_validity_at\"],\n", " }\n", - " mod_dict[\"physical-name\"] = ccv_metadata[\"physical_name\"]" + " mod_dict[\"physical-name\"] = ccv_metadata[\"physical_name\"]\n", + "\n", + "# Validate the constants availability and raise/warn correspondingly. \n", + "for mod, calibrations in jf_metadata.items():\n", + " missing_dark_constants = set(\n", + " c for c in [\"Offset10Hz\", \"BadPixelsDark10Hz\"] if c not in calibrations.keys())\n", + " missing_gain_constants = set(\n", + " c for c in [\"BadPixelsFF10Hz\", \"RelativeGain10Hz\"] if relative_gain and c not in calibrations.keys()) # noqa\n", + " if missing_dark_constants:\n", + " raise KeyError(\n", + " f\"Dark constants {missing_dark_constants} are not available for correction. Module: {mod}\") # noqa\n", + " if missing_gain_constants:\n", + " warning(\n", + " f\"Gain constants {missing_gain_constants} were not retrieved. Module: {mod}\")" ] }, {