Skip to content
Snippets Groups Projects
Commit a6c9a7ce authored by Karim Ahmed's avatar Karim Ahmed
Browse files

Merge branch 'fix/uncommented_line' into 'master'

Uncommented line causing syntax error

See merge request detectors/pycalibration!728
parents 49a1eee0 11c5e3da
No related branches found
No related tags found
1 merge request!728Uncommented line causing syntax error
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# JUNGFRAU Retrieving Constants Pre-correction # # JUNGFRAU Retrieving Constants Pre-correction #
Author: European XFEL Detector Group, Version: 1.0 Author: European XFEL Detector Group, Version: 1.0
Retrieving Required Constants for Offline Calibration of the JUNGFRAU Detector Retrieving Required Constants for Offline Calibration of the JUNGFRAU Detector
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
in_folder = "/gpfs/exfel/exp/SPB/202130/p900204/raw" # the folder to read data from, required in_folder = "/gpfs/exfel/exp/SPB/202130/p900204/raw" # the folder to read data from, required
out_folder = "/gpfs/exfel/data/scratch/ahmedk/test/remove" # the folder to output to, required out_folder = "/gpfs/exfel/data/scratch/ahmedk/test/remove" # the folder to output to, required
metadata_folder = "" # Directory containing calibration_metadata.yml when run by xfel-calibrate metadata_folder = "" # Directory containing calibration_metadata.yml when run by xfel-calibrate
run = 112 # run to process, required run = 112 # run to process, required
sequences = [-1] # sequences to correct, set to [-1] for all, range allowed sequences = [-1] # sequences to correct, set to [-1] for all, range allowed
sequences_per_node = 1 # number of sequence files per cluster node if run as slurm job, set to 0 to not run SLURM parallel sequences_per_node = 1 # number of sequence files per cluster node if run as slurm job, set to 0 to not run SLURM parallel
# Parameters used to access raw data. # Parameters used to access raw data.
karabo_id = "SPB_IRDA_JF4M" # karabo prefix of Jungfrau devices karabo_id = "SPB_IRDA_JF4M" # karabo prefix of Jungfrau devices
karabo_da = ['JNGFR01', 'JNGFR02', 'JNGFR03', 'JNGFR04', 'JNGFR05', 'JNGFR06', 'JNGFR07', 'JNGFR08'] # data aggregators karabo_da = ['JNGFR01', 'JNGFR02', 'JNGFR03', 'JNGFR04', 'JNGFR05', 'JNGFR06', 'JNGFR07', 'JNGFR08'] # data aggregators
receiver_template = "JNGFR{:02d}" # Detector receiver template for accessing raw data files. e.g. "JNGFR{:02d}" receiver_template = "JNGFR{:02d}" # Detector receiver template for accessing raw data files. e.g. "JNGFR{:02d}"
instrument_source_template = '{}/DET/{}:daqOutput' # template for source name (filled with karabo_id & receiver_id). e.g. 'SPB_IRDA_JF4M/DET/JNGFR01:daqOutput' instrument_source_template = '{}/DET/{}:daqOutput' # template for source name (filled with karabo_id & receiver_id). e.g. 'SPB_IRDA_JF4M/DET/JNGFR01:daqOutput'
ctrl_source_template = '{}/DET/CONTROL' # template for control source name (filled with karabo_id_control) ctrl_source_template = '{}/DET/CONTROL' # template for control source name (filled with karabo_id_control)
karabo_id_control = "" # if control is on a different ID, set to empty string if it is the same a karabo-id karabo_id_control = "" # if control is on a different ID, set to empty string if it is the same a karabo-id
# Parameters for calibration database. # Parameters for calibration database.
use_dir_creation_date = True # use the creation data of the input dir for database queries use_dir_creation_date = True # use the creation data of the input dir for database queries
cal_db_interface = "tcp://max-exfl016:8017#8025" # the database interface to use cal_db_interface = "tcp://max-exfl016:8017#8025" # the database interface to use
cal_db_timeout = 180000 # timeout on cal db requests cal_db_timeout = 180000 # timeout on cal db requests
# Parameters affecting corrected data. # Parameters affecting corrected data.
relative_gain = True # do relative gain correction relative_gain = True # do relative gain correction
# Parameters for retrieving calibration constants # Parameters for retrieving calibration constants
manual_slow_data = False # if true, use manually entered bias_voltage, integration_time, gain_setting, and gain_mode values manual_slow_data = False # if true, use manually entered bias_voltage, integration_time, gain_setting, and gain_mode values
integration_time = 4.96 # integration time in us, will be overwritten by value in file integration_time = 4.96 # integration time in us, will be overwritten by value in file
gain_setting = 0 # 0 for dynamic gain, 1 for dynamic HG0, will be overwritten by value in file gain_setting = 0 # 0 for dynamic gain, 1 for dynamic HG0, will be overwritten by value in file
gain_mode = 0 # 0 for runs with dynamic gain setting, 1 for fixgain. It will be overwritten by value in file, if manual_slow_data is set to True. gain_mode = 0 # 0 for runs with dynamic gain setting, 1 for fixgain. It will be overwritten by value in file, if manual_slow_data is set to True.
mem_cells = -1 Set mem_cells to -1 to automatically use the value stored in RAW data.# mem_cells = -1 # Set mem_cells to -1 to automatically use the value stored in RAW data.#
bias_voltage = 180 # will be overwritten by value in file bias_voltage = 180 # will be overwritten by value in file
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import datetime import datetime
from functools import partial from functools import partial
import multiprocessing import multiprocessing
from extra_data import RunDirectory from extra_data import RunDirectory
from pathlib import Path from pathlib import Path
from cal_tools.jungfraulib import JungfrauCtrl from cal_tools.jungfraulib import JungfrauCtrl
from cal_tools.tools import ( from cal_tools.tools import (
get_dir_creation_date, get_dir_creation_date,
get_from_db, get_from_db,
CalibrationMetadata, CalibrationMetadata,
) )
from iCalibrationDB import Conditions, Constants from iCalibrationDB import Conditions, Constants
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
in_folder = Path(in_folder) in_folder = Path(in_folder)
out_folder = Path(out_folder) out_folder = Path(out_folder)
metadata = CalibrationMetadata(metadata_folder or out_folder) metadata = CalibrationMetadata(metadata_folder or out_folder)
run_folder = in_folder / f'r{run:04d}' run_folder = in_folder / f'r{run:04d}'
run_dc = RunDirectory(run_folder) run_dc = RunDirectory(run_folder)
out_folder.mkdir(parents=True, exist_ok=True) out_folder.mkdir(parents=True, exist_ok=True)
creation_time = None creation_time = None
if use_dir_creation_date: if use_dir_creation_date:
creation_time = get_dir_creation_date(in_folder, run) creation_time = get_dir_creation_date(in_folder, run)
print(f"Using {creation_time} as creation time") print(f"Using {creation_time} as creation time")
if karabo_id_control == "": if karabo_id_control == "":
karabo_id_control = karabo_id karabo_id_control = karabo_id
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
ctrl_src = ctrl_source_template.format(karabo_id_control) ctrl_src = ctrl_source_template.format(karabo_id_control)
ctrl_data = JungfrauCtrl(run_dc, ctrl_src) ctrl_data = JungfrauCtrl(run_dc, ctrl_src)
if mem_cells < 0: if mem_cells < 0:
memory_cells, sc_start = ctrl_data.get_memory_cells() memory_cells, sc_start = ctrl_data.get_memory_cells()
mem_cells_name = "single cell" if memory_cells == 1 else "burst" mem_cells_name = "single cell" if memory_cells == 1 else "burst"
print(f"Run is in {mem_cells_name} mode.\nStorage cell start: {sc_start:02d}") print(f"Run is in {mem_cells_name} mode.\nStorage cell start: {sc_start:02d}")
else: else:
memory_cells = mem_cells memory_cells = mem_cells
mem_cells_name = "single cell" if memory_cells == 1 else "burst" mem_cells_name = "single cell" if memory_cells == 1 else "burst"
print(f"Run is in manually set to {mem_cells_name} mode. With {memory_cells} memory cells") print(f"Run is in manually set to {mem_cells_name} mode. With {memory_cells} memory cells")
if not manual_slow_data: if not manual_slow_data:
integration_time = ctrl_data.get_integration_time() integration_time = ctrl_data.get_integration_time()
bias_voltage = ctrl_data.get_bias_voltage() bias_voltage = ctrl_data.get_bias_voltage()
gain_setting = ctrl_data.get_gain_setting() gain_setting = ctrl_data.get_gain_setting()
gain_mode = ctrl_data.get_gain_mode() gain_mode = ctrl_data.get_gain_mode()
print(f"Integration time is {integration_time} us") print(f"Integration time is {integration_time} us")
print(f"Gain setting is {gain_setting} (run settings: " print(f"Gain setting is {gain_setting} (run settings: "
f"{ctrl_data.run_settings.value if ctrl_data.run_settings else ctrl_data.run_settings})") # noqa f"{ctrl_data.run_settings.value if ctrl_data.run_settings else ctrl_data.run_settings})") # noqa
print(f"Gain mode is {gain_mode}") print(f"Gain mode is {gain_mode}")
print(f"Bias voltage is {bias_voltage} V") print(f"Bias voltage is {bias_voltage} V")
print(f"Number of memory cells are {memory_cells}") print(f"Number of memory cells are {memory_cells}")
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
condition = Conditions.Dark.jungfrau( condition = Conditions.Dark.jungfrau(
memory_cells=memory_cells, memory_cells=memory_cells,
bias_voltage=bias_voltage, bias_voltage=bias_voltage,
integration_time=integration_time, integration_time=integration_time,
gain_setting=gain_setting, gain_setting=gain_setting,
gain_mode=gain_mode, gain_mode=gain_mode,
) )
def get_constants_for_module(mod: str): def get_constants_for_module(mod: str):
"""Get calibration constants for given module for Jungfrau.""" """Get calibration constants for given module for Jungfrau."""
retrieval_function = partial( retrieval_function = partial(
get_from_db, get_from_db,
karabo_id=karabo_id, karabo_id=karabo_id,
karabo_da=mod, karabo_da=mod,
cal_db_interface=cal_db_interface, cal_db_interface=cal_db_interface,
creation_time=creation_time, creation_time=creation_time,
timeout=cal_db_timeout, timeout=cal_db_timeout,
verbosity=0, verbosity=0,
meta_only=True, meta_only=True,
load_data=False, load_data=False,
empty_constant=None empty_constant=None
) )
mdata_dict = dict() mdata_dict = dict()
mdata_dict["constants"] = dict() mdata_dict["constants"] = dict()
constants = [ constants = [
"Offset", "BadPixelsDark", "Offset", "BadPixelsDark",
"RelativeGain", "BadPixelsFF", "RelativeGain", "BadPixelsFF",
] ]
for cname in constants: for cname in constants:
mdata_dict["constants"][cname] = dict() mdata_dict["constants"][cname] = dict()
if not relative_gain and cname in ["BadPixelsFF", "RelativeGain"]: if not relative_gain and cname in ["BadPixelsFF", "RelativeGain"]:
continue continue
_, mdata = retrieval_function( _, mdata = retrieval_function(
condition=condition, condition=condition,
constant=getattr(Constants.jungfrau, cname)(), constant=getattr(Constants.jungfrau, cname)(),
) )
mdata_const = mdata.calibration_constant_version mdata_const = mdata.calibration_constant_version
const_mdata = mdata_dict["constants"][cname] const_mdata = mdata_dict["constants"][cname]
# check if constant was successfully retrieved. # check if constant was successfully retrieved.
if mdata.comm_db_success: if mdata.comm_db_success:
const_mdata["file-path"] = ( const_mdata["file-path"] = (
f"{mdata_const.hdf5path}" f"{mdata_const.filename}" f"{mdata_const.hdf5path}" f"{mdata_const.filename}"
) )
const_mdata["dataset-name"] = mdata_const.h5path const_mdata["dataset-name"] = mdata_const.h5path
const_mdata["creation-time"] = f"{mdata_const.begin_at}" const_mdata["creation-time"] = f"{mdata_const.begin_at}"
mdata_dict["physical-detector-unit"] = mdata_const.device_name mdata_dict["physical-detector-unit"] = mdata_const.device_name
else: else:
const_mdata["file-path"] = None const_mdata["file-path"] = None
const_mdata["creation-time"] = None const_mdata["creation-time"] = None
return mdata_dict, mod return mdata_dict, mod
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Constant paths are saved under retrieved-constants in calibration_metadata.yml # Constant paths are saved under retrieved-constants in calibration_metadata.yml
retrieved_constants = metadata.setdefault("retrieved-constants", {}) retrieved_constants = metadata.setdefault("retrieved-constants", {})
# Avoid retrieving constants for available modules in calibration_metadata.yml # Avoid retrieving constants for available modules in calibration_metadata.yml
# This is used during reproducability. # This is used during reproducability.
query_karabo_da = [] query_karabo_da = []
for mod in karabo_da: for mod in karabo_da:
if mod in retrieved_constants.keys(): if mod in retrieved_constants.keys():
print(f"Constant for {mod} already in " print(f"Constant for {mod} already in "
"calibration_metadata.yml, won't query again.") "calibration_metadata.yml, won't query again.")
continue continue
query_karabo_da.append(mod) query_karabo_da.append(mod)
with multiprocessing.Pool() as pool: with multiprocessing.Pool() as pool:
results = pool.map(get_constants_for_module, query_karabo_da) results = pool.map(get_constants_for_module, query_karabo_da)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
timestamps = dict() timestamps = dict()
for md_dict, mod in results: for md_dict, mod in results:
retrieved_constants[mod] = md_dict retrieved_constants[mod] = md_dict
module_timestamps = timestamps[mod] = dict() module_timestamps = timestamps[mod] = dict()
module_constants = retrieved_constants[mod] module_constants = retrieved_constants[mod]
print(f"Module: {mod}:") print(f"Module: {mod}:")
for cname, mdata in module_constants["constants"].items(): for cname, mdata in module_constants["constants"].items():
if hasattr(mdata["creation-time"], 'strftime'): if hasattr(mdata["creation-time"], 'strftime'):
mdata["creation-time"] = mdata["creation-time"].strftime('%y-%m-%d %H:%M') mdata["creation-time"] = mdata["creation-time"].strftime('%y-%m-%d %H:%M')
print(f'{cname:.<12s}', mdata["creation-time"]) print(f'{cname:.<12s}', mdata["creation-time"])
for cname in ["Offset", "BadPixelsDark", "RelativeGain", "BadPixelsFF"]: for cname in ["Offset", "BadPixelsDark", "RelativeGain", "BadPixelsFF"]:
if cname in module_constants["constants"]: if cname in module_constants["constants"]:
module_timestamps[cname] = module_constants["constants"][cname]["creation-time"] module_timestamps[cname] = module_constants["constants"][cname]["creation-time"]
else: else:
module_timestamps[cname] = "NA" module_timestamps[cname] = "NA"
time_summary = retrieved_constants.setdefault("time-summary", {}) time_summary = retrieved_constants.setdefault("time-summary", {})
time_summary = timestamps time_summary = timestamps
metadata.save() metadata.save()
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment