Skip to content

Commit 3a4deee

Browse files
authored
Merge pull request #212 from cta-observatory/ignore_missing_end
Ignore missing TrackEnd when parsin TargetLog file by default
2 parents 1285c6a + a87eb50 commit 3a4deee

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/ctapipe_io_lst/pointing.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pathlib import Path
2+
import warnings
23

34
import numpy as np
45
from scipy.interpolate import interp1d
@@ -69,7 +70,7 @@ def __init__(self, subarray, config=None, parent=None, **kwargs):
6970
self.interp_alt = {}
7071

7172
@staticmethod
72-
def _read_target_log(path):
73+
def read_target_log(path, ignore_missing_end=True):
7374
path = Path(path)
7475

7576
def parse_start(tokens):
@@ -103,10 +104,19 @@ def parse_end(tokens):
103104

104105
tokens = line.strip().split(" ")
105106
if tokens[1] == "TrackStart":
107+
start = parse_start(tokens)
108+
106109
if tracking:
107-
raise ValueError(f"Expected TrackingEnd, got {line}")
110+
msg = f"Expected TrackingEnd, got {line}"
111+
if ignore_missing_end:
112+
warnings.warn(msg)
113+
# let previous target end one second before new one
114+
targets[-1].update({"end_unix": start["start_unix"] - 1})
115+
else:
116+
raise ValueError(msg)
117+
108118
tracking = True
109-
targets.append(parse_start(tokens))
119+
targets.append(start)
110120

111121
elif tokens[1] == "TrackEnd":
112122
if not tracking:
@@ -282,7 +292,7 @@ def get_target(self, tel_id, time):
282292
if path is None:
283293
self.target_log[tel_id] = None
284294
else:
285-
self.target_log[tel_id] = self._read_target_log(path)
295+
self.target_log[tel_id] = self.read_target_log(path)
286296

287297
targets = self.target_log[tel_id]
288298
if targets is None:

src/ctapipe_io_lst/tests/test_pointing.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
from pathlib import Path
33
import numpy as np
4+
import pytest
45
from astropy.time import Time
56
import astropy.units as u
67
from ctapipe.core import Provenance
@@ -69,7 +70,7 @@ def test_load_position_and_bending_corrections():
6970
def test_read_target_log(tmp_path):
7071
from ctapipe_io_lst.pointing import PointingSource
7172

72-
targets = PointingSource._read_target_log(test_target_log)
73+
targets = PointingSource.read_target_log(test_target_log)
7374
assert len(targets) == 7
7475
assert targets.colnames == ["start_unix", "ra", "dec", "name", "end_unix", "start", "end"]
7576

@@ -83,7 +84,7 @@ def test_read_target_log(tmp_path):
8384
# test with empty file
8485
empty_log = (tmp_path / "Target_log.txt")
8586
empty_log.open("w").close()
86-
targets = PointingSource._read_target_log(empty_log)
87+
targets = PointingSource.read_target_log(empty_log)
8788
assert len(targets) == 0
8889
assert targets.colnames == ["start_unix", "ra", "dec", "name", "end_unix", "start", "end"]
8990
assert targets["ra"].unit == u.deg
@@ -99,13 +100,28 @@ def test_read_target_log(tmp_path):
99100
tokens = tokens[:-1]
100101
f.write(" ".join(tokens) + "\n")
101102

102-
targets = PointingSource._read_target_log(log_no_names)
103+
targets = PointingSource.read_target_log(log_no_names)
103104
assert len(targets) == 7
104105
assert targets.colnames == ["start_unix", "ra", "dec", "name", "end_unix", "start", "end"]
105106

106107
np.testing.assert_array_equal(targets["name"], ["unknown"] * 7)
107108
np.testing.assert_array_equal(targets["ra"], [83.6296, 86.6333] * 3 + [79.1725])
108109

110+
# test with missing TrackEnd line
111+
log_missing_end = tmp_path / "target_log_missing_end.txt"
112+
log_lines.pop(1)
113+
log_missing_end.write_text('\n'.join(log_lines))
114+
115+
with pytest.warns(match="Expected TrackingEnd"):
116+
targets = PointingSource.read_target_log(log_missing_end)
117+
118+
assert len(targets) == 7
119+
assert targets[0]['end_unix'] == targets[1]['start_unix'] - 1
120+
121+
with pytest.raises(ValueError, match="Expected TrackingEnd"):
122+
targets = PointingSource.read_target_log(log_missing_end, ignore_missing_end=False)
123+
124+
109125

110126
def test_targets():
111127
from ctapipe_io_lst import PointingSource, LSTEventSource

0 commit comments

Comments
 (0)