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 ()
0 commit comments