Skip to content

Commit 4fb38eb

Browse files
authored
Merge pull request #19 from pylhc/doros_update
Update Doros for new entries
2 parents f75a49c + a5f90c7 commit 4fb38eb

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

tests/inputs/doros_shrink_data.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
Quick script to remove some data from the DOROS files, to make them smaller for the tests.
3+
"""
4+
from pathlib import Path
5+
import shutil
6+
import subprocess
7+
import h5py
8+
9+
from turn_by_turn.doros import DEFAULT_OSCILLATION_DATA, N_ORBIT_SAMPLES, N_OSCILLATION_SAMPLES, OSCILLATIONS, POSITIONS
10+
11+
file_in = "/afs/cern.ch/work/j/jdilly/dorostest/DOROS-2024-09-29_01_37_13_522358-NO_USER.h5"
12+
file_out = "./tests/inputs/test_doros_2024-09-29.h5"
13+
14+
# Values need to coincide with the values in the test_doros.py
15+
N_BPMS= 3
16+
NTURNS = 50000
17+
18+
# Copy file to temporary location and "delete" unneccessary data (only removes references to data)
19+
file_temp = file_out + ".tmp"
20+
shutil.copyfile(file_in, file_temp)
21+
with h5py.File(file_temp, "r+", track_order=True) as hdf_file:
22+
bpms = [name for name in hdf_file["/"].keys() if N_ORBIT_SAMPLES in hdf_file[f"/{name}"].keys()]
23+
24+
for bpm in bpms[:N_BPMS]:
25+
del hdf_file[bpm][N_ORBIT_SAMPLES]
26+
hdf_file[bpm].create_dataset(N_ORBIT_SAMPLES, data=[NTURNS])
27+
data_x = hdf_file[bpm][POSITIONS["X"]][:NTURNS]
28+
data_y = hdf_file[bpm][POSITIONS["Y"]][:NTURNS]
29+
30+
del hdf_file[bpm][POSITIONS["X"]]
31+
del hdf_file[bpm][POSITIONS["Y"]]
32+
hdf_file[bpm].create_dataset(POSITIONS["X"], data=data_x)
33+
hdf_file[bpm].create_dataset(POSITIONS["Y"], data=data_y)
34+
35+
del hdf_file[bpm][N_OSCILLATION_SAMPLES]
36+
del hdf_file[bpm][OSCILLATIONS["X"]]
37+
del hdf_file[bpm][OSCILLATIONS["Y"]]
38+
hdf_file[bpm].create_dataset(N_OSCILLATION_SAMPLES, data=0)
39+
hdf_file[bpm].create_dataset(OSCILLATIONS["X"], data=[DEFAULT_OSCILLATION_DATA])
40+
hdf_file[bpm].create_dataset(OSCILLATIONS["Y"], data=[DEFAULT_OSCILLATION_DATA])
41+
42+
for bpm in bpms[N_BPMS:]:
43+
del hdf_file[bpm]
44+
45+
# Repack file (which actually removes the data in the file)
46+
subprocess.run(["h5repack", file_temp, file_out])
47+
Path(file_temp).unlink()
2.28 MB
Binary file not shown.

tests/test_doros.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@
77
import pytest
88
import h5py
99

10-
from turn_by_turn.constants import PRINT_PRECISION
11-
from turn_by_turn.errors import DataTypeError
1210
from turn_by_turn.structures import TbtData, TransverseData
1311
from tests.test_lhc_and_general import create_data, compare_tbt
1412

1513
from turn_by_turn.doros import N_ORBIT_SAMPLES, read_tbt, write_tbt, DEFAULT_BUNCH_ID, POSITIONS
1614

1715
INPUTS_DIR = Path(__file__).parent / "inputs"
1816

19-
20-
def test_read_write_real_data(tmp_path):
21-
tbt = read_tbt(INPUTS_DIR / "test_doros.h5", bunch_id=10)
17+
@pytest.mark.parametrize("filename", ["test_doros.h5", "test_doros_2024-09-29.h5"])
18+
def test_read_write_real_data(tmp_path, filename):
19+
tbt = read_tbt(INPUTS_DIR / filename, bunch_id=10)
2220

2321
assert tbt.nbunches == 1
2422
assert len(tbt.matrices) == 1

turn_by_turn/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
__title__ = "turn_by_turn"
66
__description__ = "Read and write turn-by-turn measurement files from different particle accelerator formats."
77
__url__ = "https://github.com/pylhc/turn_by_turn"
8-
__version__ = "0.7.0"
8+
__version__ = "0.7.1"
99
__author__ = "pylhc"
1010
__author_email__ = "pylhc@github.com"
1111
__license__ = "MIT"

turn_by_turn/doros.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
DEFAULT_BUNCH_ID: int = 0 # bunch ID not saved in the DOROS file
5151

5252
METADATA: str = "METADATA"
53+
BPM_NAME_END: str = "_DOROS"
5354

5455
# tiemstamps
5556
BST_TIMESTAMP: str = "bstTimestamp" # microseconds
@@ -85,7 +86,8 @@ def read_tbt(file_path: str|Path, bunch_id: int = DEFAULT_BUNCH_ID) -> TbtData:
8586
LOGGER.debug(f"Reading DOROS file at path: '{file_path.absolute()}'")
8687
with h5py.File(file_path, "r") as hdf_file:
8788
# use "/" to keep track of bpm order, see https://github.com/h5py/h5py/issues/1471
88-
bpm_names = [name for name in hdf_file["/"].keys() if name != METADATA]
89+
bpm_names = [name for name in hdf_file["/"].keys() if N_ORBIT_SAMPLES in hdf_file[f"/{name}"].keys()]
90+
LOGGER.debug(f"Found BPMs in DOROS-type file: {bpm_names}")
8991

9092
_check_data_lengths(hdf_file, bpm_names)
9193

0 commit comments

Comments
 (0)