Skip to content

Commit

Permalink
Remove restriction on existing file in EventSource.input_url
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnoe committed Jul 7, 2022
1 parent 2b08140 commit 4b9d9ee
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
6 changes: 1 addition & 5 deletions ctapipe/io/eventsource.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ class EventSource(Component):
generated events. If None, all available telescopes are used.
"""

input_url = Path(
directory_ok=False,
exists=True,
help="Path to the input file containing events.",
).tag(config=True)
input_url = Path(help="Path to the input file containing events.").tag(config=True)

max_events = Int(
None,
Expand Down
52 changes: 29 additions & 23 deletions ctapipe/io/hdf5eventsource.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
import astropy.units as u
from astropy.utils.decorators import lazyproperty
import logging
from ast import literal_eval
from pathlib import Path

import astropy.units as u
import numpy as np
import tables
from ast import literal_eval
from astropy.utils.decorators import lazyproperty

from ..core import Container, Field
from ..instrument import SubarrayDescription
from ..containers import (
ConcentrationContainer,
ArrayEventContainer,
CameraHillasParametersContainer,
CameraTimingParametersContainer,
ConcentrationContainer,
DL1CameraContainer,
EventIndexContainer,
CameraHillasParametersContainer,
HillasParametersContainer,
ImageParametersContainer,
IntensityStatisticsContainer,
LeakageContainer,
MorphologyContainer,
ParticleClassificationContainer,
PeakTimeStatisticsContainer,
R1CameraContainer,
ReconstructedEnergyContainer,
SimulationConfigContainer,
SimulatedShowerContainer,
ReconstructedGeometryContainer,
SimulatedEventContainer,
PeakTimeStatisticsContainer,
CameraTimingParametersContainer,
SimulatedShowerContainer,
SimulationConfigContainer,
TelescopeImpactParameterContainer,
TelescopeTriggerContainer,
TelEventIndexContainer,
TimingParametersContainer,
TriggerContainer,
ImageParametersContainer,
TelEventIndexContainer,
TelescopeTriggerContainer,
R1CameraContainer,
TelescopeImpactParameterContainer,
ReconstructedGeometryContainer,
)
from ..core import Container, Field
from ..instrument import SubarrayDescription
from ..utils import IndexFinder
from .datalevels import DataLevel
from .eventsource import EventSource
from .hdf5tableio import HDF5TableReader
from .datalevels import DataLevel
from ..utils import IndexFinder

from .tableloader import DL2_SUBARRAY_GROUP, DL2_TELESCOPE_GROUP

__all__ = ["HDF5EventSource"]
Expand Down Expand Up @@ -161,21 +162,26 @@ def close(self):

@staticmethod
def is_compatible(file_path):
path = Path(file_path).expanduser()
if not path.is_file():
return False

with open(file_path, "rb") as f:
with path.open("rb") as f:
magic_number = f.read(8)

if magic_number != b"\x89HDF\r\n\x1a\n":
return False

with tables.open_file(file_path) as f:
with tables.open_file(path) as f:
metadata = f.root._v_attrs
if "CTA PRODUCT DATA LEVEL" not in metadata._v_attrnames:
return False

# we can now read both R1 and DL1
datalevels = set(literal_eval(metadata["CTA PRODUCT DATA LEVEL"]))
if not datalevels.intersection(("R1", "DL1_IMAGES", "DL1_PARAMETERS")):
if not datalevels.intersection(
("R1", "DL1_IMAGES", "DL1_PARAMETERS", "DL2")
):
return False

if "CTA PRODUCT DATA MODEL VERSION" not in metadata._v_attrnames:
Expand Down
5 changes: 4 additions & 1 deletion ctapipe/io/simteleventsource.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,10 @@ def prepare_subarray_info(self, telescope_descriptions, header):

@staticmethod
def is_compatible(file_path):
return is_eventio(Path(file_path).expanduser())
path = Path(file_path).expanduser()
if not path.is_file():
return False
return is_eventio(path)

@property
def subarray(self):
Expand Down

0 comments on commit 4b9d9ee

Please sign in to comment.