77import logging
88from datetime import datetime
99from pathlib import Path
10+ import re
1011from typing import Union
1112
1213import numpy as np
2627BPM_PLANES : str = "MonPlanes"
2728
2829
29- def read_tbt (file_path : Union [str , Path ]) -> TbtData :
30+ def read_tbt (file_path : Union [str , Path ], remove_trailing_bpm_plane : bool = True ) -> TbtData :
3031 """
3132 Reads turn-by-turn data from the ``SPS``'s **SDDS** format file.
3233 Will first determine if it is in ASCII format to figure out which reading method to use.
3334
3435 Args:
3536 file_path (Union[str, Path]): path to the turn-by-turn measurement file.
37+ remove_trailing_bpm_plane (bool, optional): if ``True``, will remove the trailing
38+ BPM plane ('.H', '.V') from the BPM-names.
39+ This makes the measurement data compatible with the madx-models.
40+ Defaults to ``True``.
3641
3742 Returns:
3843 A ``TbTData`` object with the loaded data.
@@ -41,8 +46,7 @@ def read_tbt(file_path: Union[str, Path]) -> TbtData:
4146 LOGGER .debug (f"Reading SPS file at path: '{ file_path .absolute ()} '" )
4247
4348 if is_ascii_file (file_path ):
44- matrices , date = read_ascii (file_path )
45- return TbtData (matrices , date , [0 ], matrices [0 ].X .shape [1 ])
49+ return read_ascii (file_path )
4650
4751 sdds_file = sdds .read (file_path )
4852
@@ -53,30 +57,39 @@ def read_tbt(file_path: Union[str, Path]) -> TbtData:
5357 bpm_names = np .array (sdds_file .values [BPM_NAMES ])
5458 bpm_planes = np .array (sdds_file .values [BPM_PLANES ]).astype (bool )
5559
56- ver_bpms = bpm_names [bpm_planes ]
57- hor_bpms = bpm_names [~ bpm_planes ]
60+ bpm_names_y = bpm_names [bpm_planes ]
61+ bpm_names_x = bpm_names [~ bpm_planes ]
5862
59- tbt_data_x = [sdds_file .values [bpm ] for bpm in hor_bpms ]
60- tbt_data_y = [sdds_file .values [bpm ] for bpm in ver_bpms ]
63+ tbt_data_x = [sdds_file .values [bpm ] for bpm in bpm_names_x ]
64+ tbt_data_y = [sdds_file .values [bpm ] for bpm in bpm_names_y ]
65+
66+ if remove_trailing_bpm_plane :
67+ pattern = re .compile ("\.[HV]$" , flags = re .IGNORECASE )
68+ bpm_names_x = [pattern .sub ("" , bpm ) for bpm in bpm_names_x ]
69+ bpm_names_y = [pattern .sub ("" , bpm ) for bpm in bpm_names_y ]
6170
6271 matrices = [
6372 TransverseData (
64- X = pd .DataFrame (index = hor_bpms , data = tbt_data_x , dtype = float ),
65- Y = pd .DataFrame (index = ver_bpms , data = tbt_data_y , dtype = float ),
73+ X = pd .DataFrame (index = bpm_names_x , data = tbt_data_x , dtype = float ),
74+ Y = pd .DataFrame (index = bpm_names_y , data = tbt_data_y , dtype = float ),
6675 )
6776 ]
6877
6978 return TbtData (matrices , date , [0 ], nturns )
7079
7180
72- def write_tbt (output_path : Union [str , Path ], tbt_data : TbtData ) -> None :
81+ def write_tbt (output_path : Union [str , Path ], tbt_data : TbtData , add_trailing_bpm_plane : bool = True ) -> None :
7382 """
7483 Write a ``TbtData`` object's data to file, in a ``SPS``'s **SDDS** format.
7584 The format is reduced to the necessary parameters used by the reader.
7685
7786 Args:
7887 output_path (Union[str, Path]): path to a the disk location where to write the data.
7988 tbt_data (TbtData): the ``TbtData`` object to write to disk.
89+ add_trailing_bpm_plane (bool, optional): if ``True``, will add the trailing
90+ BPM plane ('.H', '.V') to the BPM-names. This assures that all BPM-names are unique,
91+ and that the measurement data is compatible with the sdds files from the FESA-class.
92+ Defaults to ``True``.
8093 """
8194 output_path = Path (output_path )
8295 LOGGER .info (f"Writing TbTdata in binary SDDS (SPS) format at '{ output_path .absolute ()} '" )
@@ -85,6 +98,11 @@ def write_tbt(output_path: Union[str, Path], tbt_data: TbtData) -> None:
8598
8699 # bpm names
87100 bpm_names_x , bpm_names_y = df_x .index .to_list (), df_y .index .to_list ()
101+
102+ if add_trailing_bpm_plane :
103+ bpm_names_x = [f"{ bpm_name } .H" if not bpm_name .endswith (".H" ) else bpm_name for bpm_name in bpm_names_x ]
104+ bpm_names_y = [f"{ bpm_name } .V" if not bpm_name .endswith (".V" ) else bpm_name for bpm_name in bpm_names_y ]
105+
88106 bpm_names = bpm_names_x + bpm_names_y
89107
90108 # bpm planes
0 commit comments