Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option single_ob to HDF5Merger for merging chunks of the same ob #2436

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions ctapipe/io/hdf5merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ class HDF5Merger(Component):
True, help="Whether to include processing statistics in merged output"
).tag(config=True)

single_ob = traits.Bool(
False,
help=(
"If true, input files are assumed to be multiple chunks from the same"
" observation block and the ob / sb blocks will only be copied from "
" the first input file"
),
).tag(config=True)

def __init__(self, output_path=None, **kwargs):
# enable using output_path as posarg
if output_path not in {None, traits.Undefined}:
Expand Down Expand Up @@ -202,6 +211,7 @@ def __init__(self, output_path=None, **kwargs):
focal_length_choice=FocalLengthKind.EQUIVALENT,
)
self.required_nodes = _get_required_nodes(self.h5file)
self._n_merged = 0

def __call__(self, other: Union[str, Path, tables.File]):
"""
Expand All @@ -213,7 +223,7 @@ def __call__(self, other: Union[str, Path, tables.File]):

with exit_stack:
# first file to be merged
if self.meta is None:
if self._n_merged == 0:
self.meta = self._read_meta(other)
self.data_model_version = self.meta.product.data_model_version
metadata.write_to_hdf5(self.meta.to_dict(), self.h5file)
Expand All @@ -229,6 +239,7 @@ def __call__(self, other: Union[str, Path, tables.File]):
self.log.info(
"Updated required nodes to %s", sorted(self.required_nodes)
)
self._n_merged += 1
finally:
self._update_meta()

Expand Down Expand Up @@ -272,13 +283,15 @@ def _append(self, other):
# Configuration
self._append_subarray(other)

config_keys = [
"/configuration/observation/scheduling_block",
"/configuration/observation/observation_block",
]
for key in config_keys:
if key in other.root:
self._append_table(other, other.root[key])
# in case of "single_ob", we only copy sb/ob blocks for the first file
if not self.single_ob or self._n_merged == 0:
config_keys = [
"/configuration/observation/scheduling_block",
"/configuration/observation/observation_block",
]
for key in config_keys:
if key in other.root:
self._append_table(other, other.root[key])

# Simulation
simulation_table_keys = [
Expand Down
4 changes: 4 additions & 0 deletions ctapipe/tools/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class MergeTool(Tool):
}

flags = {
"single-ob": (
{"HDF5Merger": {"single_ob": True}},
"Only copy observation config of first file to be merged.",
),
"progress": (
{"MergeTool": {"progress_bar": True}},
"Show a progress bar for all given input files",
Expand Down
Loading