#!/bin/bash
# Submit the round 6 committed-loss runs: each HIST strand of a state tree
# (L5: yearly HIST geometry), from its geometry in COMMIT_START, under a
# random climate of the last 21 years of its HIST climate, for 150 years.
#
#   ./submit_commit.sh <v7 exp> <state tree> <dataset name> [--test]
#   e.g. ./submit_commit.sh sw_pf3.5_mf5_lam12 sw_pf3.5_mf5_lam12_1850 commit_pf3.5_mf5_lam12
#
#   --test     chunk 8, 4 glaciers, from the test state tree, into test_run/
#   COMMIT_START (default 2025: the end of the HIST runs), COMMIT_Y0 (2014)
#   and COMMIT_HALFSIZE (10): the climate years 2004-2024 (the HIST climate
#   ends in 2024), COMMIT_NYEARS (150), COMMIT_SEEDS ("0 1 2 3 4")
#   STATES     the members (default 010, the calibration member: the HIST
#              strands are identical after 1850 and converged by 2025, so one
#              is enough; "123 023 ..." for more)
#   TASKS=ids  resubmit these chunks only
#
# Output: output/<name>/commit<START>_state###_seed#_reg11.nc (merge on the
# login node, as printed).
set -e

HERE=$(cd "$(dirname "$0")" && pwd)
source "$HERE/common.sh"

if [ -z "$3" ]; then
    echo "Usage: $0 <v7 exp> <state tree> <dataset name> [--test]" >&2
    exit 1
fi
EXP=$1; TREE=$2; NAME=$3; TEST=$4
export R6_EXP_DIR=$V7_EXPS_DIR/$EXP
BDIR=$(source "$R6_EXP_DIR/config.sh"; printf "b_%03d" "$BORDER")
export R6_STATES="${STATES:-010}"
export R6_C_START=${COMMIT_START:-2025} R6_C_Y0=${COMMIT_Y0:-2014}
export R6_C_HALFSIZE=${COMMIT_HALFSIZE:-10} R6_C_NYEARS=${COMMIT_NYEARS:-150}
export R6_C_SEEDS="${COMMIT_SEEDS:-0 1 2 3 4}"

if [ "$TEST" = "--test" ]; then
    TREE_DIR=$STATES_DIR/test_run/$TREE
    export R6_STATES_URL="$STATES_URL_BASE/test_run/$TREE/"
    export R6_TEST_IDS="RGI60-11.00897 RGI60-11.00890 RGI60-11.00846 RGI60-11.00807"
    OUT=$HERE/test_run/$NAME; LOGDIR=$HERE/_logs_test/$NAME; TIME=01:00:00
    ARRAY=${TASKS:-8}; N_EXPECT=1
else
    TREE_DIR=$STATES_DIR/$TREE
    export R6_STATES_URL="$STATES_URL_BASE/$TREE/"
    OUT=$HERE/output/$NAME; LOGDIR=$HERE/_logs/$NAME; TIME=04:00:00
    ARRAY=${TASKS:-0-$((PERM_N_CHUNKS - 1))}; N_EXPECT=$PERM_N_CHUNKS
fi
[ -d "$TREE_DIR/RGI62/$BDIR/L5" ] || { echo "ERROR: no L5 in $TREE_DIR" >&2; exit 1; }
mkdir -p "$OUT" "$LOGDIR"

echo "------------------------------------------------------------------"
echo "  dataset       $OUT"
echo "  HIST strands  $R6_STATES from $R6_STATES_URL (L5)"
echo "  commitment    from $R6_C_START, random climate $((R6_C_Y0 - R6_C_HALFSIZE))-$((R6_C_Y0 + R6_C_HALFSIZE)) (unique samples), $R6_C_NYEARS yr, seeds $R6_C_SEEDS"
echo "  tasks         $ARRAY${R6_TEST_IDS:+ (test: $R6_TEST_IDS)}"
echo "  oggm          $OGGM_REPO@$OGGM_COMMIT"
echo "------------------------------------------------------------------"

jid=$(sbatch --parsable --job-name="r6c_${NAME}" --array=$ARRAY \
             --cpus-per-task=$PERM_CPUS --time=$TIME \
             --mail-user=$MAIL --export=ALL \
             --output="$LOGDIR/run_%A_%a.out" \
             "$HERE/run_perm_slurm.sh" "$OUT/chunks" run_commit.py)
echo "$NAME: run=$jid"
echo "When done, merge on the login node:"
echo "  MERGE_JOBS=8 $LOCAL_PY $R5_DIR/merge_ens.py $OUT $N_EXPECT"
