1+
2+ from datetime import datetime
3+
4+ import numpy as np
5+ import pandas as pd
6+ import pytest
7+
8+ from tests .test_lhc_and_general import INPUTS_DIR , compare_tbt
9+ from turn_by_turn import madng , read_tbt , write_tbt
10+ from turn_by_turn .structures import TbtData , TransverseData
11+
12+
13+ def test_read_ng (_ng_file ):
14+ original = _original_simulation_data ()
15+
16+ # Check directly from the module
17+ new = madng .read_tbt (_ng_file )
18+ compare_tbt (original , new , no_binary = True )
19+
20+ # Check from the main function
21+ new = read_tbt (_ng_file , datatype = "madng" )
22+ compare_tbt (original , new , no_binary = True )
23+
24+ def test_write_ng (_ng_file , tmp_path ):
25+ original_tbt = _original_simulation_data ()
26+
27+ # Write the data
28+ from_tbt = tmp_path / "from_tbt.tfs"
29+ madng .write_tbt (from_tbt , original_tbt )
30+
31+ # Read the written data
32+ new_tbt = madng .read_tbt (from_tbt )
33+ compare_tbt (original_tbt , new_tbt , no_binary = True )
34+
35+ # Check from the main function
36+ original_tbt = read_tbt (_ng_file , datatype = "madng" )
37+ write_tbt (from_tbt , original_tbt , datatype = "madng" )
38+
39+ new_tbt = read_tbt (from_tbt , datatype = "madng" )
40+ compare_tbt (original_tbt , new_tbt , no_binary = True )
41+ assert original_tbt .date == new_tbt .date
42+
43+ def test_error_ng (_error_file ):
44+ with pytest .raises (ValueError ):
45+ read_tbt (_error_file , datatype = "madng" )
46+
47+ # ---- Helpers ---- #
48+ def _original_simulation_data () -> TbtData :
49+ # Create a TbTData object with the original data
50+ names = np .array (["BPM1" , "BPM3" , "BPM2" ])
51+ bpm1_p1_x = np .array ([ 1e-3 , 0.002414213831 ,- 0.0009999991309 ])
52+ bpm1_p1_y = np .array ([- 1e-3 , 0.0004142133507 , 0.001000000149 ])
53+ bpm1_p2_x = np .array ([- 1e-3 ,- 0.002414213831 , 0.0009999991309 ])
54+ bpm1_p2_y = np .array ([ 1e-3 ,- 0.0004142133507 ,- 0.001000000149 ])
55+
56+ bpm2_p1_x = np .array ([- 0.0009999999503 ,- 0.0004142138307 , 0.0009999998012 ])
57+ bpm2_p1_y = np .array ([ 0.00100000029 ,- 0.002414213351 ,- 0.001000001159 ])
58+ bpm2_p2_x = np .array ([ 0.0009999999503 , 0.0004142138307 ,- 0.0009999998012 ])
59+ bpm2_p2_y = np .array ([- 0.00100000029 , 0.002414213351 , 0.001000001159 ])
60+
61+ bpm3_p1_x = np .array ([ 0.002414213831 ,- 0.0009999991309 ,- 0.002414214191 ])
62+ bpm3_p1_y = np .array ([ 0.0004142133507 , 0.001000000149 ,- 0.0004142129907 ])
63+ bpm3_p2_x = np .array ([- 0.002414213831 , 0.0009999991309 , 0.002414214191 ])
64+ bpm3_p2_y = np .array ([- 0.0004142133507 ,- 0.001000000149 , 0.0004142129907 ])
65+
66+ matrix = [
67+ TransverseData ( # first particle
68+ X = pd .DataFrame (index = names , data = [bpm1_p1_x , bpm2_p1_x , bpm3_p1_x ]),
69+ Y = pd .DataFrame (index = names , data = [bpm1_p1_y , bpm2_p1_y , bpm3_p1_y ]),
70+ ),
71+ TransverseData ( # second particle
72+ X = pd .DataFrame (index = names , data = [bpm1_p2_x , bpm2_p2_x , bpm3_p2_x ]),
73+ Y = pd .DataFrame (index = names , data = [bpm1_p2_y , bpm2_p2_y , bpm3_p2_y ]),
74+ ),
75+ ]
76+ return TbtData (matrices = matrix , bunch_ids = [1 , 2 ], nturns = 3 )
77+
78+
79+ # ---- Fixtures ---- #
80+ @pytest .fixture
81+ def _ng_file (tmp_path ):
82+ return INPUTS_DIR / "madng" / "fodo_track.tfs"
83+
84+ @pytest .fixture
85+ def _error_file (tmp_path ):
86+ return INPUTS_DIR / "madng" / "fodo_track_error.tfs"
0 commit comments