Source code for rta_reconstruction.dl2_io

from pathlib import Path

import tables
from pandas import DataFrame


[docs] def init_dl2_file(dl2_file_path: Path, dl1_file_path: Path): if dl2_file_path.exists(): raise FileExistsError("{} already exists, refusing to overwrite it.".format(dl2_file_path)) # TODO: make these configurable somehow not_copy_list = [ "/dl1/event/telescope/image", "/dl1/event/telescope/image/LST_LSTCam", "/dl1/event/telescope/parameters", "/dl1/event/telescope/parameters/LST_LSTCam", "/dl1/event/telescope/parameters_src_dependent/LST_LSTCam", "/dl1/event/telescope/likelihood_parameters/LST_LSTCam", ] with tables.open_file(dl1_file_path, "r") as dl1_f, tables.open_file(dl2_file_path, "w") as dl2_f: for node in dl1_f.walk_nodes(): if node._v_pathname not in not_copy_list: if node._v_parent._v_pathname not in dl2_f: parent = dl2_f.create_group( node._v_parent._v_parent._v_pathname, node._v_parent._v_name, createparents=True ) else: parent = dl2_f.get_node(node._v_parent._v_pathname) dl2_f.copy_node(node, parent)
# TODO: global metadata in file global attributes ? (soft version, contact)
[docs] def write_dl2_df( dl2_file_path: Path, dl2_df: DataFrame, dl2_table_path: str = "/dl2/event/telescope/parameters/LST_LSTCam", attributes=None, filters=tables.Filters( # TODO: make those configurable, benchmark performances and compare with pandas "to_hdf" complevel=1, complib="blosc:zstd", fletcher32=True, bitshuffle=False, ), ): with tables.open_file(dl2_file_path, "a") as dl2_f: path, table_name = dl2_table_path.rsplit("/", maxsplit=1) # TODO: use pathlib split ? dl2_table = dl2_f.create_table( path, table_name, dl2_df.to_records(index=False), createparents=True, filters=filters, ) if attributes is not None: for key, value in attributes.items(): dl2_table.attrs[key] = value