From 52b28c9b644fc550edd883db3d55e02aa3639ec4 Mon Sep 17 00:00:00 2001 From: x06da Date: Thu, 5 Nov 2026 11:26:14 +0100 Subject: [PATCH] Update PXIII DCCM theta2 calibration utilities --- pxiii_bec/macros/calibrate.py | 73 +++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 21 deletions(-) diff --git a/pxiii_bec/macros/calibrate.py b/pxiii_bec/macros/calibrate.py index c194ac6..b31afe3 100644 --- a/pxiii_bec/macros/calibrate.py +++ b/pxiii_bec/macros/calibrate.py @@ -1,24 +1,55 @@ -from datetime import datetime +"""Calibrate theta2 for PXIII DCCM""" + +from pxiii_bec.scripts import beamline_context as bl + + +def generate_dccm_lut(start_energy=6000, end_energy=16000, step=1000): + """Measure DCCM theta1 and optimised theta2 across the energy range.""" + + filename = bl.env.lut_dir / f"dccm_{filenow(date_only=True)}.csv" -def generate_dccm_lut(start_energy=5000, end_energy=14000, step=1000): - """Generate a lookup table for the dcm from start_energy to end_energy in steps of step""" - now = datetime.now() - fnow = now.strftime("%d%m%H%M") - filename = f"luts/{fnow}_lut.csv" with open(filename, "w", encoding="utf-8") as f: - # f.write("energy,pitch,roll,perp,ss_xicam_x,ss_xicam_y\n") - f.write("energy, pitch\n") + f.write("energy,theta1,theta2,diff\n") - for energy in range(start_energy, end_energy, step): - print(f"Moving to {energy: .0f}eV") - bl_energy(energy) - pitch = EnergyDefaults.mono_pitch.position - # auto_exposure(cam="ss_xicam", max_iter=25) - # x = dev.ss_xicam_x.read()["ss_xicam_x"]["value"] - # y = dev.ss_xicam_y.read()["ss_xicam_y"]["value"] - with open(filename, "a", encoding="utf-8") as f: - f.write( - # f"{energy:.0f},{pitch:.5g},,{x:.4g},{y:.4g}\n" - (f"{energy:.0f},{pitch:.7g}\n") - ) - bl_energy(13000) + try: + for energy in range(start_energy, end_energy + 1, step): + print(f"Moving to {energy:.0f} eV") + + result = bl_energy(energy) + + if not result or not result.get("success", False): + raise RuntimeError(f"DCCM pitch scan failed at {energy} eV") + + theta1 = get_value(dev.dccm_theta1) + theta2 = get_value(dev.dccm_theta2) + diff = theta1 + theta2 + + with open(filename, "a", encoding="utf-8") as f: + f.write(f"{energy:.0f},{theta1:.5f},{theta2:.5f},{diff:.5f}\n") + + finally: + print(f"Calibration data saved to {filename}") + try: + bl_energy(13000) + except Exception as exc: + print(f"WARNING: Could not restore 13 keV: {exc}") + + return filename + + +def theta2_correction(filename, fit_order=1, save=False): + """Fit and plot the DCCM theta2 calibration.""" + + df = get_data(filename) + + pitch = df[["theta1", "theta2"]] + + fit = lut_plot( + pitch.to_numpy(), + xname="theta1", + yname="theta2", + title="DCCM_theta2_Calibration", + fit_order=fit_order, + save=save, + ) + return fit