From 0b2fd55c66992c4be91eaf075202fcb371cb0384 Mon Sep 17 00:00:00 2001 From: Claudio Cirelli Date: Thu, 7 May 2026 11:43:02 +0200 Subject: [PATCH] First scripts --- channels_reduce.yaml | 70 ++++++++++++++++++++++++++++++++++++++++++ reduce.py | 72 ++++++++++++++++++++++++++++++++++++++++++++ run_reduce.sh | 10 ++++++ 3 files changed, 152 insertions(+) create mode 100644 channels_reduce.yaml create mode 100644 reduce.py create mode 100644 run_reduce.sh diff --git a/channels_reduce.yaml b/channels_reduce.yaml new file mode 100644 index 0000000..d39348a --- /dev/null +++ b/channels_reduce.yaml @@ -0,0 +1,70 @@ +channels: + Eventcode: SAR-CVME-TIFALL4:EvtSet + delay_motor: SLAAR11-L-BECKBS:PR1_CH4_VAL_GET + PulseE: SARFE10-PBPG050:FAST-PULSE-ENERGY + diode1: SARES11-GES1:PR1_CH1_VAL_GET + diode2: SARES11-GES1:PR1_CH2_VAL_GET + diode3: SAROP11-GES1:PR1_CH2_VAL_GET + laser: SAROP11-GES1:PR1_CH1_VAL_GET + laserIzero: SLAAR11-L-BECKBS:PR1_CH0_VAL_GET + Izero103: SAROP21-PBPS103:INTENSITY + Izero122: SAROP11-PBPS122:INTENSITY + Izero110: SAROP11-PBPS110:INTENSITY + PosX103: SAROP21-PBPS103:XPOS + PosY103: SAROP21-PBPS103:YPOS + PosX110: SAROP11-PBPS110:XPOS + PosY110: SAROP11-PBPS110:YPOS + PosX122: SAROP11-PBPS122:XPOS + PosY122: SAROP11-PBPS122:YPOS + energy: SARES11-CVME-EVR0:DUMMY_PV3_NBS + + arrTimes126: SARES11-SPEC125-M1.edge_position2 + arrTimesAmp126: SARES11-SPEC125-M1.edge_amplitude2 + + arrTimes124: SARES11-SPEC125-M1.edge_position + arrTimesAmp124: SARES11-SPEC125-M1.edge_amplitude + +channels_pump: + - Eventcode + - delay_motor + - PulseE + - diode1 + - diode2 + - diode3 + - laser + - laserIzero + - Izero103 + - Izero122 + - Izero110 + - PosX103 + - PosY103 + - PosX110 + - PosY110 + - PosX122 + - PosY122 + - energy + - arrTimes126 + - arrTimesAmp126 + - arrTimes124 + - arrTimesAmp124 + +channels_unpump: + - Eventcode + - delay_motor + - PulseE + - diode1 + - diode2 + - diode3 + - laser + - laserIzero + - Izero103 + - Izero122 + - Izero110 + - energy + - PosX103 + - PosY103 + - PosX110 + - PosY110 + - PosX122 + - PosY122 + diff --git a/reduce.py b/reduce.py new file mode 100644 index 0000000..6dd2cc0 --- /dev/null +++ b/reduce.py @@ -0,0 +1,72 @@ +import numpy as np +import json, os, glob, yaml +import argparse, pickle, h5py +from pathlib import Path +from sfdata import SFDataFile, SFDataFiles + +from alvra_tools.load_data import load_data_compact_pump_probe +#from alvra_tools.channels import * +#from alvra_tools.utils import * +#from alvra_tools.XAS_functions import * +#from alvra_tools.XAS_utils import * + +def get_acq_from_pth(path): + import re + return re.search(r"acq(\d+)", path).group(1).zfill(4) + +parser = argparse.ArgumentParser() +parser.add_argument("--input", "-i", required=True) +parser.add_argument("channels_file") +args = parser.parse_args() + +filename = Path(args.input) +output_file = Path(str(filename).replace("/raw/", "/res/processed/").replace(".h5",".PROCESSED.h5") ) +output_file.parent.mkdir(parents=True, exist_ok=True, mode=0o775) + +acq = get_acq_from_pth(args.input) +acq_meta_pth = Path(str(filename).replace("/data/ac", "/meta/ac")).parent +acq_info_pth = acq_meta_pth.joinpath(f"acq{acq}.json") + +#if not acq_info_pth.exists(): +# write_apo_meta(apo_meta_pth, apo_meta_dic) +# msg = f"Acq info(.json) file does not exist: {acq_info_pth}" +# raise IOError(msg) + +with open(acq_info_pth, "r") as f: + acq_parameters = json.load(f) + rbk_value = acq_parameters.get("scan_info", {}).get("scan_readbacks", 0) + scan_value = acq_parameters.get("scan_info", {}).get("scan_values", 0) + xlabel = acq_parameters.get("scan_info", {}).get("name", 'None') + units = acq_parameters.get("scan_info", {}).get("units", 'None') + +with open(args.channels_file, "r") as f: + channels_aliases = yaml.safe_load(f) + +channels_mapping = channels_aliases["channels"] +channels_pp = list(channels_mapping[ch] for ch in channels_aliases["channels_pump"]) +channels_all = channels_pp + +with SFDataFile(args.input) as step: + resultsPP, results, _, _ = load_data_compact_pump_probe(channels_pp, channels_all, step) + +res = {} +for name, chan in channels_mapping.items(): + ch = resultsPP[chan] + if name in channels_aliases["channels_pump"]: + res[name + "_pump"] = ch.pump + if name in channels_aliases["channels_unpump"]: + res[name + "_unpump"] = ch.unpump + +with h5py.File(output_file, 'w') as f: + gr1 = f.create_group("allShots") + for key, value in results.items(): + gr1[key] = value + gr2 = f.create_group("OnOff") + for key, value in res.items(): + gr2[key] = value + gr3 = f.create_group("meta") + gr3["readback_value"] = np.atleast_1d(rbk_value) + gr3["scan_value"] = np.atleast_1d(scan_value) + gr3["xlabel"] = xlabel + gr3["units"] = units + diff --git a/run_reduce.sh b/run_reduce.sh new file mode 100644 index 0000000..089b7e1 --- /dev/null +++ b/run_reduce.sh @@ -0,0 +1,10 @@ +#! /bin/bash + +acq=$2 +echo "$acq" + +source /sf/daq/source-conda +conda activate alvra-apo + +python /sf/alvra/code/apo_scripts/reduce.py -i $acq channels_reduce.yaml +