diff --git a/src/cal_tools/tools.py b/src/cal_tools/tools.py index 2d15866ac2517bc0925e2b6ad5bad10560f5ab36..88c533282c4abe94a9b8ee60640b37807e40b23f 100644 --- a/src/cal_tools/tools.py +++ b/src/cal_tools/tools.py @@ -880,6 +880,41 @@ def save_constant_metadata( const_mdata["creation-time"] = None +def load_constants_dict( + retrieved_constants: dict, + empty_constants: Optional[dict] = None, + ) -> Tuple[dict, dict]: + """Load constant data from metadata in the + retrieved_constants dictionary. + + :param retrieved_constants: A dict. with the constant filepaths and + dataset-name to read the constant data arrays. + { + 'Constant Name': { + 'file-path': '/gpfs/.../*.h5', + 'dataset-name': '/module_name/...', + 'creation-time': str(datetime),}, + } + :param empty_constants: A dict of constant names keys and + the empty constant array to use in case of not non-retrieved constants. + :return constant_data: A dict of constant names keys and their data. + """ + const_data = dict() + when = dict() + + for cname, mdata in retrieved_constants.items(): + const_data[cname] = dict() + when[cname] = mdata["creation-time"] + if when[cname]: + with h5py.File(mdata["path"], "r") as cf: + const_data[cname] = np.copy( + cf[f"{mdata['dataset']}/data"]) + else: + const_data[cname] = ( + empty_constants[cname] if empty_constants else None) + return const_data, when + + def load_specified_constants( retrieved_constants: dict, empty_constants: Optional[dict] = None,