#!/usr/bin/env bash
# Wrapper that waits for the brick cataloging arrays to drain, runs the
# cross-filter merge for both basic and iterative daophot outputs, then
# fires the comparison script.
#
# Designed to be submitted as a slurm batch job so it runs autonomously
# after the per-frame catalog jobs finish.
set -euo pipefail

logdir=/blue/adamginsburg/adamginsburg/brick_logs
analysis_dir=/orange/adamginsburg/repos/brick-jwst-2221/brick2221/analysis
python_exe=/blue/adamginsburg/adamginsburg/miniconda3/envs/python313/bin/python
brick_basepath=/blue/adamginsburg/adamginsburg/jwst/brick
compare_script=/orange/adamginsburg/jwst/sickle/compare_brick_iter2_to_old.py

mkdir -p "$logdir"
out_log="$logdir/run_brick_compare_when_done_$(date +%Y%m%dT%H%M%S).log"
exec > "$out_log" 2>&1
echo "[$(date)] starting; logging to $out_log"

# 1. Wait for any remaining per-frame brick array tasks (job-name pattern
#    'webb-cat-...-eachexp-brick' or '-cloudc'); poll squeue.
echo "[$(date)] waiting for brick/cloudc per-frame array tasks to finish..."
while true; do
    pending=$(squeue -u "$USER" -h --array \
        --format='%j %T' 2>/dev/null \
        | awk '/webb-cat-.*-eachexp-(brick|cloudc)/ && ($2=="PENDING" || $2=="RUNNING")' \
        | wc -l)
    if [[ "$pending" -eq 0 ]]; then
        echo "[$(date)] no brick/cloudc per-frame tasks remain"
        break
    fi
    echo "[$(date)] $pending brick/cloudc per-frame tasks still pending/running; sleeping 300s"
    sleep 300
done

# 2. Run merge_catalogs.py for the brick target.  Asking for both
#    'dao' (basic) and 'daoiterative' (iterative-PSF) in one invocation
#    drives the per-filter cross-exposure merge and the cross-filter
#    merge for both modes, producing
#       basic_merged_indivexp_photometry_tables_merged.fits
#       iterative_merged_indivexp_photometry_tables_merged.fits
echo "[$(date)] running merge_catalogs.py (basic + iterative)"
"$python_exe" "$analysis_dir/merge_catalogs.py" \
    --merge-singlefields --modules=merged \
    --indiv-merge-methods=dao,daoiterative --skip-crowdsource \
    --target=brick
echo "[$(date)] merge_catalogs.py finished"

# 3. Run the comparison.
echo "[$(date)] running comparison script"
"$python_exe" "$compare_script" \
    --new-basic "$brick_basepath/catalogs/basic_merged_indivexp_photometry_tables_merged.fits" \
    --new-iterative "$brick_basepath/catalogs/iterative_merged_indivexp_photometry_tables_merged.fits" \
    --summary-out /orange/adamginsburg/jwst/sickle/compare_brick_iter2_summary.ecsv \
    --plot-dir /orange/adamginsburg/jwst/sickle/compare_brick_iter2_plots

echo "[$(date)] DONE"
