PALSParserPy

PALSParserPy is a Python parser for the Particle Accelerator Lattice Standard (PALS). It reads PALS-format lattice files, performs lattice expansion, and translates lattices into SciBmad, Bmad and MAD-X formats.

Under the hood, the package is a thin ctypes wrapper around the C library built by PALSParserCpp (a rapidyaml backend). A parsed document is a tree of YAMLNode values that you index and mutate with familiar Python idioms (node["key"], node[i], key in node, .keys(), len, iteration).

What it does

  1. Parse PALS-format YAML into a YAMLNode tree, or build one from scratch — see Parsing and writing YAML.

  2. Expand a lattice — read a lattice file, resolve its includes, and expand the line into an ordered list of elements — see Reading and expanding lattices.

  3. Evaluate the mathematical expressions in the expanded lattice to numbers — see Evaluating expressions.

  4. Translate a PALS lattice to SciBmad, Bmad or MAD-X format — see Translating to SciBmad, Bmad and MAD-X.

The complete docstring reference is in the API Reference.

Quick example

import palsparserpy as pp

# Read a lattice file and expand it.
lat = pp.parse_and_expand_pals("ex.pals.yaml")

print(pp.to_yaml_string(lat.full_expanded))  # the expanded root lattice as YAML
print(pp.to_yaml_string(lat.adjunct))        # everything else in the document

# Build a document from scratch and write it out.
root = pp.create_empty_tree()
server = root.add_map(key="server")
server["host"] = "localhost"
server["port"] = "8080"

pp.write_yaml(root, "config.pals.yaml")