HOME ESR EXPERIMENTS LABS etc
E121 E127 E132 E125 E143 laser_cooling_2021 E142 Ê128 E146 E0052 E0018 E0028 E0038 E0075 E0203_E0018
  masses & isomeres 2025  Not logged in ELOG logo
Message ID: 158     Entry time: Fri Apr 4 16:24:48 2025
Author: Ruijiu, Yuri 
Category: Analysis 
Subject: PID of 186Hf 
Code used to add up injection data.
(pyroot) gpuuser@appc256:/data.local/G22-00018_00203/scripts/OnlineDataAnalysisSystem/shift/20250331/data/2025.04.04.13.00$ find /data.local/G22-00018_00203/results/analyzers/RSA01 -name "RSA01-2025.04.04.*.tiq_spectrogram.npz" > filelist.txt
(pyroot) gpuuser@appc256:/data.local/G22-00018_00203/scripts/OnlineDataAnalysisSystem/shift/20250331/data/summed_spectrum$ python npz_addup.py --time-cut 0.14 filelist.txt
Attachment 1: Screenshot_2025-04-04_16-24-23.png  153 kB  Uploaded Fri Apr 4 17:25:17 2025  | Hide | Hide all | Show all
Screenshot_2025-04-04_16-24-23.png
Attachment 2: summed_spectrum.npz  512 kB  Uploaded Fri Apr 4 17:28:06 2025
Attachment 3: summed_spectrum.png  23 kB  Uploaded Fri Apr 4 17:28:19 2025  | Hide | Hide all | Show all
summed_spectrum.png
Attachment 4: summed_spectrogram_time_cut.png  83 kB  Uploaded Fri Apr 4 17:28:36 2025  | Hide | Hide all | Show all
summed_spectrogram_time_cut.png
Attachment 5: filelist.txt  14 kB  Uploaded Fri Apr 4 17:28:50 2025  | Show | Hide all | Show all
Attachment 6: npz_addup.py  3 kB  Uploaded Fri Apr 4 17:28:58 2025  | Hide | Hide all | Show all
import argparse
import numpy as np
from loguru import logger
from tqdm import tqdm
import sys
from iqtools import *
import sys

# font settings for plot
font = {"weight": "bold", "size": 6}  #'family' : 'normal',
plt.rc("font", **font)


def process_files(file_list):
    """
    Process the files from the file list, summing up the 'zz' arrays from spectrogram files.
    """
    zz_sum = None
    found_files = False

    with open(file_list, "r") as f:
        files = f.read().splitlines()

    spectrogram_files = [file for file in files if file.endswith("_spectrogram.npz")]

    if not spectrogram_files:
        logger.info(
            "No files ending with '_spectrogram.npz' found. Exiting gracefully."
        )
        return zz_sum, False

    for file in tqdm(spectrogram_files, desc="Processing files"):
        try:
            data = np.load(file)
            xx, yy, zz = data["arr_0"], data["arr_1"], data["arr_2"]
            if zz_sum is None:
                zz_sum = zz
            else:
                zz_sum += zz
            found_files = True
        except Exception as e:
            logger.error(f"Error processing file {file}: {e}")

    return xx, yy, zz_sum, found_files


def main():
    """
    Main function to parse arguments and invoke the processing function.
    """
    parser = argparse.ArgumentParser(
        description="Process a file list and sum the 'zz' arrays from spectrogram files."
    )
    parser.add_argument(
        "file_list",
        type=str,
        help="Path to the file containing the list of file paths.",
    )
    
    parser.add_argument("-t", "--time-cut", type=float, required=False, help="Start time as a float (optional)")
    
    args = parser.parse_args()

    try:
        logger.info("Starting the summation...")
        xx, yy, zz_sum, found_files = process_files(args.file_list)

        if args.time_cut is not None:
            y_idx = (np.abs(yy[:,0] - float(args.time_cut))).argmin()
            filename_suffix = "_time_cut"
        else:
            y_idx = 0
            filename_suffix = ""
            
        logger.info("Saving 3D NPZ sum to file...")
        np.savez(f"summed_spectrogram{filename_suffix}.npz", arr_0=xx[y_idx:,:], arr_1=yy[y_idx:,:], arr_2=zz_sum[y_idx:,:])
        
        logger.info("Plotting the 3D NPZ sum...")
        
        slx = slice (int(np.shape(xx[y_idx:,:])[1]/2) - 500, int(np.shape(xx[y_idx:,:])[1]/2) + 500)
        sly = slice(y_idx, np.shape(yy)[0]) # this one was very tricky until I found it!
        
        plot_spectrogram(
            xx[sly,slx], yy[sly,slx], zz_sum[sly,slx],
            zzmin=10,
            zzmax=500,
            filename=f"summed_spectrogram{filename_suffix}",
            title=f"summed_spectrogram{filename_suffix}",
        )

        logger.info("Creating 2D average...")
        navg = np.shape(zz_sum[y_idx:,:])[0]
        xx_avg, yy_avg, zz_sum_avg = get_averaged_spectrogram(xx[y_idx:,:], yy[y_idx:,:], zz_sum[y_idx:,:], every=navg)

        logger.info("Saving 2D NPZ sum to file...")
        np.savez(f"summed_spectrum{filename_suffix}.npz", arr_0=xx_avg.flatten(), arr_1=zz_sum_avg.flatten())

        logger.info("Plotting the 2D NPZ sum...")
        plot_spectrum(
            xx_avg.flatten(),
            zz_sum_avg.flatten(),
            dbm=True,
            filename=f"summed_spectrum{filename_suffix}",
            title=f"summed_spectrum{filename_suffix}"
        )

        if found_files:
            logger.info(
                "Successfully processed all spectrogram files."
            )
        else:
            logger.info("No valid spectrogram files found to process.")
    except KeyboardInterrupt:
        logger.warning("Process interrupted by user (Ctrl+C). Exiting gracefully.")
        sys.exit(1)


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        logger.warning("Script interrupted (Ctrl+C). See you!")
        sys.exit(1)
Attachment 7: simulation_result.out  168 kB  Uploaded Fri Apr 4 17:32:55 2025  | Show | Hide all | Show all
ELOG V3.1.5-fc6679b