Skip to content
Snippets Groups Projects

[GH2][Correct] Remove precorrection notebook and add fragment

Merged Karim Ahmed requested to merge feat/fragment_gh2 into master
3 files
+ 40
244
Compare changes
  • Side-by-side
  • Inline
Files
3
from cal_tools.step_timing import StepTimer
from cal_tools.step_timing import StepTimer
from cal_tools.tools import (
from cal_tools.tools import (
calcat_creation_time,
calcat_creation_time,
CalibrationMetadata,
write_constants_fragment,
)
)
from XFELDetAna.plotting.heatmap import heatmapPlot
from XFELDetAna.plotting.heatmap import heatmapPlot
out_folder = Path(out_folder)
out_folder = Path(out_folder)
out_folder.mkdir(parents=True, exist_ok=True)
out_folder.mkdir(parents=True, exist_ok=True)
metadata = CalibrationMetadata(metadata_folder or out_folder)
# NOTE: this notebook will not overwrite calibration metadata file
const_yaml = metadata.get("retrieved-constants", {})
if not karabo_id_control:
if not karabo_id_control:
karabo_id_control = karabo_id
karabo_id_control = karabo_id
const_data[mod]["RelativeGainGotthard2"] = cfile["gain_map"][()].astype(np.float32)
const_data[mod]["RelativeGainGotthard2"] = cfile["gain_map"][()].astype(np.float32)
const_data[mod]["Mask"] = cfile["bpix_ff"][()].astype(np.uint32)
const_data[mod]["Mask"] = cfile["bpix_ff"][()].astype(np.uint32)
else:
else:
if const_yaml:
constant_names = ["LUTGotthard2", "OffsetGotthard2", "BadPixelsDarkGotthard2"]
const_data = dict()
if gain_correction:
for mod in karabo_da:
constant_names += ["RelativeGainGotthard2", "BadPixelsFFGotthard2"]
const_data[mod] = dict()
for cname, mdata in const_yaml[mod]["constants"].items():
const_data[mod][cname] = dict()
if mdata["creation-time"]:
with h5py.File(mdata["path"], "r") as cf:
const_data[mod][cname] = np.copy(
cf[f"{mdata['dataset']}/data"])
else:
mdata_dict = {"constants": dict()}
constant_names = ["LUTGotthard2", "OffsetGotthard2", "BadPixelsDarkGotthard2"]
if gain_correction:
constant_names += ["RelativeGainGotthard2", "BadPixelsFFGotthard2"]
# Retrieve metadata for all pnccd constants.
g2_metadata = g2_cal.metadata(calibrations=constant_names)
const_data = g2_cal.ndarray_map(constant_names)
# Validate the constants availability and raise/warn correspondingly.
# Validate the constants availability and raise/warn correspondingly.
for mod, calibrations in const_data.items():
for mod, calibrations in g2_metadata.items():
dark_constants = {"LUTGotthard2"}
dark_constants = {"LUTGotthard2"}
if offset_correction:
if offset_correction:
if gain_correction and missing_gain_constants:
if gain_correction and missing_gain_constants:
warning(f"Gain constants {missing_gain_constants} are not retrieved for mod {mod}.")
warning(f"Gain constants {missing_gain_constants} are not retrieved for mod {mod}.")
# Create the mask array.
bpix = const_data[mod].get("BadPixelsDarkGotthard2")
if bpix is None:
bpix = np.zeros((1280, 2, 3), dtype=np.uint32)
if const_data[mod].get("BadPixelsFFGotthard2") is not None:
bpix |= const_data[mod]["BadPixelsFFGotthard2"]
const_data[mod]["Mask"] = bpix
# Prepare empty arrays for missing constants.
if const_data[mod].get("OffsetGotthard2") is None:
const_data[mod]["OffsetGotthard2"] = np.zeros(
(1280, 2, 3), dtype=np.float32)
if const_data[mod].get("RelativeGainGotthard2") is None:
const_data[mod]["RelativeGainGotthard2"] = np.ones(
(1280, 2, 3), dtype=np.float32)
if gain_correction:
gain_correction = False
warning("Gain correction is disabled for this module.")
const_data[mod]["RelativeGainGotthard2"] = const_data[mod]["RelativeGainGotthard2"].astype( # noqa
np.float32, copy=False) # Old gain constants are not float32.
if not karabo_da:
if not karabo_da:
raise ValueError("Dark constants are not available for all modules.")
raise ValueError("Dark constants are not available for all modules.")
 
%% Cell type:code id:ac1cdec5 tags:
 
``` python
 
# Record constant details in YAML metadata.
 
write_constants_fragment(
 
out_folder=(metadata_folder or out_folder),
 
det_metadata=g2_metadata,
 
caldb_root=g2_cal.caldb_root)
 
 
# Load constants data for all constants.
 
const_data = g2_cal.ndarray_map(metadata=g2_metadata)
 
 
# Prepare constant arrays.
 
if not constants_file:
 
# Create the mask array.
 
bpix = const_data[mod].get("BadPixelsDarkGotthard2")
 
if bpix is None:
 
bpix = np.zeros((1280, 2, 3), dtype=np.uint32)
 
if const_data[mod].get("BadPixelsFFGotthard2") is not None:
 
bpix |= const_data[mod]["BadPixelsFFGotthard2"]
 
const_data[mod]["Mask"] = bpix
 
 
# Prepare empty arrays for missing constants.
 
if const_data[mod].get("OffsetGotthard2") is None:
 
const_data[mod]["OffsetGotthard2"] = np.zeros(
 
(1280, 2, 3), dtype=np.float32)
 
 
if const_data[mod].get("RelativeGainGotthard2") is None:
 
const_data[mod]["RelativeGainGotthard2"] = np.ones(
 
(1280, 2, 3), dtype=np.float32)
 
const_data[mod]["RelativeGainGotthard2"] = const_data[mod]["RelativeGainGotthard2"].astype( # noqa
 
np.float32, copy=False) # Old gain constants are not float32.
%% Cell type:code id:23fcf7f4-351a-4df7-8829-d8497d94fecc tags:
%% Cell type:code id:23fcf7f4-351a-4df7-8829-d8497d94fecc tags:
Loading