@@ -32,6 +32,8 @@ def read_tbt(df: tfs.TfsDataFrame) -> TbtData:
3232 A ``TbTData`` object with the loaded data.
3333 """
3434 # df = tfs.read(file_path)
35+ LOGGER .info ("Starting to read TBT data from dataframe" )
36+
3537 nturns = int (df .iloc [- 1 ].loc ["turn" ])
3638 npart = int (df .iloc [- 1 ].loc ["id" ])
3739 LOGGER .info (f"Number of turns: { nturns } , Number of particles: { npart } " )
@@ -46,29 +48,34 @@ def read_tbt(df: tfs.TfsDataFrame) -> TbtData:
4648 matrices = []
4749 for particle_id in range (npart ):
4850 LOGGER .info (f"Processing particle ID: { particle_id + 1 } " )
51+
4952 # Filter the dataframe for the current particle and set index to the matrix dims
5053 subdf = df .loc [particle_id + 1 ] # Particle ID starts from 1 (not 0)
51- print (len (subdf ["name" ]), nbpms , nturns )
54+
55+ # Check if the number of BPMs is consistent for all particles/turns (i.e. no lost particles)
5256 assert (
5357 len (subdf ["name" ]) / nturns == nbpms
5458 ), "The number of BPMs is not consistent for all particles/turns. Simulation may have lost particles."
55- subdf .set_index (
56- ["eidx" ], inplace = True
57- ) # Must set index after getting unique BPMs
5859
59- # Create a dictionary of the TrackingData fields
60+ # Set the index to the element index, which are unique for every BPM and turn
61+ subdf .set_index (["eidx" ], inplace = True )
62+
63+ # Create a dictionary of the TransverseData fields
6064 tracking_data_dict = {
6165 field : pd .DataFrame (
6266 index = bpms ,
63- data = subdf [field .lower ()]
67+ data = subdf [field .lower ()] # MAD-NG uses lower case field names
6468 .to_numpy ()
65- .reshape (nbpms , nturns , order = "F" ), # Number of BPMs x Number of turns
69+ .reshape (nbpms , nturns , order = "F" ),
70+ #^ Number of BPMs x Number of turns, Fortran order (So that the BPMs are the rows)
6671 )
6772 for field in TransverseData .fieldnames ()
6873 }
6974
70- # Append the TrackingData object to the matrices list
75+ # Append the TransverseData object to the matrices list
76+ # We don't use TrackingData, as MAD-NG does not provide energy
7177 matrices .append (TransverseData (** tracking_data_dict ))
7278
7379 LOGGER .info ("Finished reading TBT data" )
80+ # Should we also provide date? (jgray 2024)
7481 return TbtData (matrices = matrices , bunch_ids = list (range (npart )), nturns = nturns )
0 commit comments