diff --git a/pxii_bec/scripts/beamline_context.py b/pxii_bec/scripts/beamline_context.py new file mode 100644 index 0000000..2220889 --- /dev/null +++ b/pxii_bec/scripts/beamline_context.py @@ -0,0 +1,24 @@ +"""Shared beamline context. +Provides access to the current beamline environment, +positioned devices, and beamline state planner.""" + +from init_beamline import init_beamline_environment + +env = None +d = None +planner = None + + +def initialise(): + """Initialise the beamline environment.""" + global env, d, planner + + env, d, planner = init_beamline_environment() + + print("Beamline environment, devices and planner have been initialised") + + +def reload(): + """Re-initialise the beamline environment""" + initialise() + diff --git a/pxii_bec/scripts/init_beamline.py b/pxii_bec/scripts/init_beamline.py new file mode 100755 index 0000000..a698bae --- /dev/null +++ b/pxii_bec/scripts/init_beamline.py @@ -0,0 +1,106 @@ +"""Initialise sample environment devices and beamline states.""" + +import os +import importlib +from dataclasses import dataclass +from pathlib import Path + + +@dataclass +class Environment: + """Define configuration files for a beamline and BEC deployment.""" + + beamline: str + + beamline_configs = { + "x06sa": { + "package": "pxi_bec", + "devices": "pxi-devices.yaml", + "states": "pxi-beamline-states.yaml", + }, + "x10sa": { + "package": "pxii_bec", + "devices": "pxii-devices.yaml", + "states": "pxii-beamline-states.yaml", + }, + "x06da": { + "package": "pxiii_bec", + "devices": "pxiii-devices.yaml", + "states": "pxiii-beamline-states.yaml", + }, + } + + valid_deployments = ("test", "production") + + def __post_init__(self): + + if self.beamline not in self.beamline_configs: + raise ValueError( + f"Unknown beamline '{self.beamline}'. " f"Choose from {list(self.beamline_configs)}" + ) + + cfg = self.beamline_configs[self.beamline] + + # Import whichever deplyment BEV has already made available + package = importlib.import_module(cfg["package"]) + + self.bec_dir = Path(package.__file__).resolve().parent + + self.device_config_dir = self.bec_dir / "device_configs" + self.lut_dir = self.bec_dir / "macros" / "luts" + + self.devices_file = self.device_config_dir / cfg["devices"] + self.states_file = self.device_config_dir / cfg["states"] + + # Determine which deployment is being used + self.deployment = self._get_deployment() + + if not self.devices_file.exists(): + raise FileNotFoundError(f"Device configuration not found: {self.devices_file}") + + if not self.states_file.exists(): + raise FileNotFoundError(f"State configuration not found: {self.states_file}") + + def _get_deployment(self): + """Determine deployment from the loaded package path.""" + + for deployment in ("test", "production"): + if deployment in self.bec_dir.parts: + return deployment + + raise RuntimeError(f"Could not determine BEC deployment from {self.bec_dir}") + + +def init_beamline_environment(): + """ + Initialise beamline positioned devices and state planner. + """ + + beamline = os.environ["BEAMLINE"] + + env = Environment( + beamline=beamline, + ) + + print("=" * 60) + print(f"Beamline : {beamline}") + print(f"BEC deployment : {env.deployment.upper()}") + print(f"Devices file : {env.devices_file}") + print(f"States file : {env.states_file}") + print("=" * 60) + + # Initialise positioned devices + device_manager = DeviceManager() + devices = device_manager.initialize_devices(env.devices_file) + + # Initialise beamline states + state_manager = DefineStatesManager() + states, allow_modifiers = state_manager.initialize_states(env.states_file) + + # Set up dependencies + deps = planner_deps() + + # Set up planner + planner = StateChangePlanner(devices, states, allow_modifiers, deps) + + return env, devices, planner