diff --git a/notebooks/pnCCD/Correct_pnCCD_NBC.ipynb b/notebooks/pnCCD/Correct_pnCCD_NBC.ipynb index 66db3ffb2b00ab49d9efd42a700d2c2bba9ec6ca..a41a37a6a61bf9716311114bf3bb961b1494edd1 100644 --- a/notebooks/pnCCD/Correct_pnCCD_NBC.ipynb +++ b/notebooks/pnCCD/Correct_pnCCD_NBC.ipynb @@ -126,7 +126,7 @@ "from XFELDetAna.detectors.fastccd import readerh5 as fastccdreaderh5\n", "from iCalibrationDB import ConstantMetaData, Constants, Conditions, Detectors, Versions\n", "from iCalibrationDB.detectors import DetectorTypes\n", - "from cal_tools.tools import get_dir_creation_date, get_random_db_interface\n", + "from cal_tools.tools import get_dir_creation_date, get_random_db_interface, get_constant_from_db_and_time\n", "\n", "%matplotlib inline\n", "\n", @@ -326,34 +326,32 @@ " constants['Offset'] = Offset\n", " constants['Noise'] = Noise\n", " constants['BadPixelsDark'] = BadPixelsDark\n", - "\n", + " when = {}\n", + " \n", + " condition = Conditions.Dark.CCD(bias_voltage=bias_voltage,\n", + " integration_time=integration_time,\n", + " gain_setting=gain,\n", + " temperature=fix_temperature,\n", + " pixels_x=pixels_x,\n", + " pixels_y=pixels_y)\n", + " \n", " for const in constants.keys():\n", - " metadata = ConstantMetaData()\n", - " dconst = getattr(Constants.CCD(DetectorTypes.pnCCD), const)()\n", - " dconst.data = constants[const]\n", - " metadata.calibration_constant = dconst\n", - "\n", - " condition = Conditions.Dark.CCD(bias_voltage=bias_voltage,\n", - " integration_time=integration_time,\n", - " gain_setting=gain,\n", - " temperature=fix_temperature,\n", - " pixels_x=pixels_x,\n", - " pixels_y=pixels_y)\n", - "\n", - " device = Detectors.PnCCD1\n", - " metadata.detector_condition = condition\n", - "\n", - " metadata.calibration_constant_version = Versions.Timespan(device=device, start=creation_time)\n", - " metadata.retrieve(cal_db_interface, when=creation_time.isoformat(), timeout=cal_db_timeout)\n", - "\n", - " constants[const] = dconst.data \n", + " constants[const], when[const] = \\\n", + " get_constant_from_db_and_time(Detectors.PnCCD1,\n", + " getattr(Constants.CCD(DetectorTypes.pnCCD), const)(),\n", + " condition,\n", + " np.zeros((pixels_x, pixels_y, 1)),\n", + " cal_db_interface,\n", + " creation_time=creation_time)\n", + " for parm in condition.parameters:\n", + " if parm.name == \"Sensor Temperature\":\n", + " print(f\"For run {run}: dark images for offset calculation \" \n", + " f\"were taken at temperature: {parm.value:0.2f} \"\n", + " f\"K @ {when['Offset']}\")\n", " except Exception as e:\n", " print(f\"Error: {e}\")\n", - " for parm in condition.parameters:\n", - " if parm.name == \"Sensor Temperature\":\n", - " print(f\"For run {run}: dark images for offset calculation \" \n", - " f\"were taken at temperature: {parm.value:0.2f} \"\n", - " f\"K @ {metadata.calibration_constant_version.begin_at}\") \n", + "\n", + "\n", " return constants" ] }, @@ -400,32 +398,25 @@ " metadata = ConstantMetaData()\n", " relgain = Constants.CCD(DetectorTypes.pnCCD).RelativeGain()\n", " metadata.calibration_constant = relgain\n", - " try:\n", - " # set the operating condition\n", - " condition = Conditions.Illuminated.CCD(bias_voltage=bias_voltage,\n", - " integration_time=integration_time,\n", - " gain_setting=gain,\n", - " temperature=fix_temperature,\n", - " pixels_x=pixels_x,\n", - " pixels_y=pixels_y, \n", - " photon_energy=photon_energy)\n", - "\n", - " device = Detectors.PnCCD1\n", - " metadata.detector_condition = condition\n", - "\n", - "\n", - " # specify the a version for this constant\n", - " metadata.calibration_constant_version = Versions.Now(device=device)\n", - " cal_db_interface = get_random_db_interface(cal_db_interface)\n", - " metadata.retrieve(cal_db_interface, timeout=cal_db_timeout) \n", - " constants[\"RelativeGain\"] = relgain.data\n", - " print(\"Retrieved RelativeGain constant with creation time:\"\n", - " f\"{metadata.calibration_constant_version.begin_at}\")\n", - " except Exception as e:\n", - " print(f\"Error: {e}.\\n\")\n", - " if relgain.data is None:\n", - " constants[\"RelativeGain\"] = np.ones((pixels_x, pixels_y))\n", - " print(\"Relative was not retrieved and an empty constant of ones will be used.\")\n", + " # set the operating condition\n", + " condition = Conditions.Illuminated.CCD(bias_voltage=bias_voltage,\n", + " integration_time=integration_time,\n", + " gain_setting=gain,\n", + " temperature=fix_temperature,\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", + " Constants.CCD(DetectorTypes.pnCCD).RelativeGain(),\n", + " condition,\n", + " np.zeros((pixels_x, pixels_y)),\n", + " cal_db_interface,\n", + " creation_time=creation_time)\n", + "\n", + " print(f\"Retrieved RelativeGain constant with creation time: {relgain_time}\")\n", + "\n", " display(Markdown('### Relative Gain Map Retrieval'))\n", " fig = xana.heatmapPlot(constants[\"RelativeGain\"], figsize=(8, 8), x_label='Columns', y_label='Rows', lut_label='Relative Gain', \n", " aspect=1, x_range=(0, pixels_y), y_range=(0, pixels_x), vmin=0.8, vmax=1.2, \n",