#!/usr/bin/env bash
#SBATCH --job-name=DrSFC_1f
#SBATCH --partition=workq
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
#SBATCH --time=04:00:00
#SBATCH --array=0-394%12
#SBATCH --output=/home/tanpv/Progs/CLI/Dr/logs/slurm_1f_%A_%a.out
#SBATCH --error=/home/tanpv/Progs/CLI/Dr/logs/slurm_1f_%A_%a.err

set -euo pipefail
PROJECT_ROOT=/home/tanpv/Progs/CLI/Dr
MANIFEST="$PROJECT_ROOT/operational_cycles_202101_202607.txt"
PYTHON=/home/hapt/miniconda3/bin/python3
read -r year month cycle day < <(sed -n "$((SLURM_ARRAY_TASK_ID+2))p" "$MANIFEST")
out="$PROJECT_ROOT/outputs/Ope/$year/$month/$day"
echo "$(date -Is) task=$SLURM_ARRAY_TASK_ID init=$year-$month-$day cycle=$cycle status=START"
cd "$PROJECT_ROOT"
if [[ "${FORCE:-0}" != 1 && -f "$out/SUCCESS" ]]; then
 if [[ -f "$out/figures/prob_D4_S1.png" && -f "$out/figures/prob_D4_S3.png" && -f "$out/figures/prob_D4_S6.png" ]] && grep -q 'prob_D4_S6.png' "$out/index.html"; then
  echo "$(date -Is) init=$year-$month-$day status=SKIPPED_ALREADY_COMPLETE"
  exit 0
 fi
 "$PYTHON" operational_forecast.py --year "$year" --month "$month" --cycle "$cycle" --d4-only
else
 "$PYTHON" operational_forecast.py --year "$year" --month "$month" --cycle "$cycle"
fi
echo "$(date -Is) init=$year-$month-$day status=SUCCESS"
