55
66from .exceptions import UnknownAttributeWarning , UnknownElementTypeWarning
77from .parse import parse_elegant , parse_madx
8+ from .validate import schema_version
89
910NAME_MAP = json .loads ((Path (__file__ ).parent / "map.json" ).read_text ())["map" ]
1011TO_ELEGANT = {x : y [0 ][0 ] for x , * y in NAME_MAP }
@@ -58,12 +59,14 @@ def _map_names(lattice_data: dict, name_map: dict):
5859 warn (UnknownAttributeWarning (other_key , name ))
5960
6061 lattices = lattice_data ["lattices" ]
61- lattice_name , main_lattice = lattices .popitem () # use last lattice as main_lattice
62+ root = lattice_data .get ("root" , tuple (lattices .keys ())[- 1 ])
63+ title = lattice_data .get ("title" , "" )
6264 return dict (
63- name = lattice_name ,
64- lattice = main_lattice ,
65- sub_lattices = lattices ,
65+ version = str ( schema_version ) ,
66+ title = title ,
67+ root = root ,
6668 elements = elements ,
69+ lattices = lattices ,
6770 )
6871
6972
@@ -74,21 +77,20 @@ def to_elegant(latticejson: dict) -> str:
7477 :return: string with in elegant lattice file format
7578 """
7679 elements = latticejson ["elements" ]
77- sub_lattices = latticejson ["sub_lattices " ]
80+ lattices = latticejson ["lattices " ]
7881
79- strings = [f"! TITLE: { latticejson ['name ' ]} " ]
82+ strings = [f"! TITLE: { latticejson ['title ' ]} " ]
8083 element_template = "{}: {}, {}" .format
8184 for name , (type_ , attributes ) in elements .items ():
8285 attrs = ", " .join (f"{ TO_ELEGANT [k ]} ={ v } " for k , v in attributes .items ())
8386 elegant_type = TO_ELEGANT [type_ ]
8487 strings .append (element_template (name , elegant_type , attrs ))
8588
8689 lattice_template = "{}: LINE=({})" .format
87- for name in sort_lattices (sub_lattices ):
88- strings .append (lattice_template (name , ", " .join (sub_lattices [name ])))
90+ for name in sort_lattices (lattices ):
91+ strings .append (lattice_template (name , ", " .join (lattices [name ])))
8992
90- strings .append (lattice_template ("__MAIN__" , ", " .join (latticejson ["lattice" ])))
91- strings .append ("USE, __MAIN__\n " )
93+ strings .append (f"USE, { latticejson ['root' ]} \n " )
9294 return "\n " .join (strings )
9395
9496
@@ -99,21 +101,20 @@ def to_madx(latticejson: dict) -> str:
99101 :return: string with in elegant lattice file format
100102 """
101103 elements = latticejson ["elements" ]
102- sub_lattices = latticejson ["sub_lattices " ]
104+ lattices = latticejson ["lattices " ]
103105
104- strings = [f"TITLE, \" { latticejson ['name ' ]} \" ;" ]
106+ strings = [f"TITLE, \" { latticejson ['title ' ]} \" ;" ]
105107 element_template = "{}: {}, {};" .format
106108 for name , (type_ , attributes ) in elements .items ():
107109 attrs = ", " .join (f"{ TO_MADX [k ]} ={ v } " for k , v in attributes .items ())
108110 elegant_type = TO_MADX [type_ ]
109111 strings .append (element_template (name , elegant_type , attrs ))
110112
111113 lattice_template = "{}: LINE=({});" .format
112- for name in sort_lattices (sub_lattices ):
113- strings .append (lattice_template (name , ", " .join (sub_lattices [name ])))
114+ for name in sort_lattices (lattices ):
115+ strings .append (lattice_template (name , ", " .join (lattices [name ])))
114116
115- strings .append (lattice_template ("__MAIN__" , ", " .join (latticejson ["lattice" ])))
116- strings .append ("USE, __MAIN__;\n " )
117+ strings .append (f"USE, { latticejson ['root' ]} ;\n " )
117118 return "\n " .join (strings )
118119
119120
0 commit comments