Skip to content

Commit a2b6072

Browse files
authored
Merge pull request #2451 from cta-observatory/path_input_h5_datalevels
Enable str/Path as input to get_hdf5_datalevels
2 parents c05b493 + 279614f commit a2b6072

3 files changed

Lines changed: 39 additions & 12 deletions

File tree

ctapipe/io/hdf5eventsource.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import logging
2+
from contextlib import ExitStack
23
from functools import lru_cache
34
from pathlib import Path
4-
from typing import Dict
5+
from typing import Dict, Union
56

67
import astropy.units as u
78
import numpy as np
@@ -76,24 +77,28 @@
7677
]
7778

7879

79-
def get_hdf5_datalevels(h5file):
80+
def get_hdf5_datalevels(h5file: Union[tables.File, str, Path]):
8081
"""Get the data levels present in the hdf5 file"""
8182
datalevels = []
8283

83-
if "/r1/event/telescope" in h5file.root:
84-
datalevels.append(DataLevel.R1)
84+
with ExitStack() as stack:
85+
if not isinstance(h5file, tables.File):
86+
h5file = stack.enter_context(tables.open_file(h5file))
8587

86-
if "/dl1/event/telescope/images" in h5file.root:
87-
datalevels.append(DataLevel.DL1_IMAGES)
88+
if "/r1/event/telescope" in h5file.root:
89+
datalevels.append(DataLevel.R1)
8890

89-
if "/dl1/event/telescope/parameters" in h5file.root:
90-
datalevels.append(DataLevel.DL1_PARAMETERS)
91+
if "/dl1/event/telescope/images" in h5file.root:
92+
datalevels.append(DataLevel.DL1_IMAGES)
9193

92-
if "/dl1/event/telescope/muon" in h5file.root:
93-
datalevels.append(DataLevel.DL1_MUON)
94+
if "/dl1/event/telescope/parameters" in h5file.root:
95+
datalevels.append(DataLevel.DL1_PARAMETERS)
9496

95-
if "/dl2" in h5file.root:
96-
datalevels.append(DataLevel.DL2)
97+
if "/dl1/event/telescope/muon" in h5file.root:
98+
datalevels.append(DataLevel.DL1_MUON)
99+
100+
if "/dl2" in h5file.root:
101+
datalevels.append(DataLevel.DL2)
97102

98103
return tuple(datalevels)
99104

ctapipe/io/tests/test_hdf5.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import enum
2+
from pathlib import Path
23

34
import numpy as np
45
import pandas as pd
@@ -17,6 +18,7 @@
1718
)
1819
from ctapipe.core.container import Container, Field
1920
from ctapipe.io import read_table
21+
from ctapipe.io.datalevels import DataLevel
2022
from ctapipe.io.hdf5tableio import HDF5TableReader, HDF5TableWriter
2123

2224

@@ -1014,3 +1016,21 @@ class Container1(Container):
10141016
assert c1.prefix == "bar"
10151017
assert c2.value == value
10161018
assert c2.prefix == "foo"
1019+
1020+
1021+
@pytest.mark.parametrize("input_type", (str, Path, tables.File))
1022+
def test_hdf5_datalevels(input_type, dl2_shower_geometry_file):
1023+
from ctapipe.io import get_hdf5_datalevels
1024+
1025+
if input_type is tables.File:
1026+
with tables.open_file(dl2_shower_geometry_file) as h5file:
1027+
datalevels = get_hdf5_datalevels(h5file)
1028+
else:
1029+
path = input_type(dl2_shower_geometry_file)
1030+
datalevels = get_hdf5_datalevels(path)
1031+
1032+
assert set(datalevels) == {
1033+
DataLevel.DL1_IMAGES,
1034+
DataLevel.DL1_PARAMETERS,
1035+
DataLevel.DL2,
1036+
}

docs/changes/2451.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add support for using ``str`` and ``Path`` objects as input
2+
to ``ctapipe.io.get_hdf5_datalevels``.

0 commit comments

Comments
 (0)