#!/bin/bash
#
# GLAMBIE fixed-geometry monthly specific MB — one job per RGI region.
# Submit as an array, e.g.:  sbatch --array=1-19 run_glambie_slurm.sh
#
#SBATCH --job-name=glambie_fixgeo
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=64
#SBATCH --time=12:00:00
#SBATCH --mail-user=fabien.maussion@uibk.ac.at
#SBATCH --qos=normal

set -e

# Two-digit region from SLURM array index
OGGM_RGI_REG=$(printf "%02d" $SLURM_ARRAY_TASK_ID)
export OGGM_RGI_REG
echo "RGI Region: $OGGM_RGI_REG"

# Scratch working dir on the local node
OGGM_WORKDIR="/work/$SLURM_JOB_USER/$SLURM_JOB_ID/wd"
mkdir -p "$OGGM_WORKDIR"
export OGGM_WORKDIR
echo "Workdir: $OGGM_WORKDIR"

# Scratch output dir (rsync'd to permanent storage at the end)
OGGM_OUTDIR="/work/$SLURM_JOB_USER/$SLURM_JOB_ID/out"
mkdir -p "$OGGM_OUTDIR"
export OGGM_OUTDIR
echo "Output dir: $OGGM_OUTDIR"

# Shared download cache (read-only on cluster)
export OGGM_DOWNLOAD_CACHE=/home/data/download
export OGGM_DOWNLOAD_CACHE_RO=1
export OGGM_EXTRACT_DIR="/work/$SLURM_JOB_USER/$SLURM_JOB_ID/oggm_tmp"
export OGGM_USE_MULTIPROCESSING=1

export LRU_MAXSIZE=1000
export NUMBA_NUM_THREADS=1

# Copy netrc for authenticated downloads
cp /home/users/fmaussion/.netrc "$OGGM_WORKDIR/.netrc"

# Link www fmaussion data here to avoid useless downloads
mkdir -p "$OGGM_WORKDIR/cache/cluster.klima.uni-bremen.de"
ln -s /home/www/fmaussion "$OGGM_WORKDIR/cache/cluster.klima.uni-bremen.de/~fmaussion"
ln -s /home/www/lschuster "$OGGM_WORKDIR/cache/cluster.klima.uni-bremen.de/~lschuster"

# Run inside the OGGM singularity container.
srun -n 1 -c "${SLURM_JOB_CPUS_PER_NODE}" singularity exec /home/users/fmaussion/images/oggm_v163.sif bash -s <<EOF
  set -e

  export HOME="$OGGM_WORKDIR/fake_home"
  mkdir "\$HOME"

  # Venv on top of the container's system packages
  python3 -m venv --system-site-packages "$OGGM_WORKDIR/oggm_env"
  source "$OGGM_WORKDIR/oggm_env/bin/activate"

  # Pin the OGGM commit used to build the 2025.6 gdirs for reproducibility.
  # Replace the hash below with the exact commit if known.
  # pip install --quiet --upgrade pip setuptools
  # pip install --quiet --no-deps "git+https://github.com/OGGM/oggm.git@master"

  ulimit -n 65000

  # Script must live next to glambie_helper.py
  python run_fixed_geometry_glambie.py
EOF

# Permanent output location (one folder per run, array index keeps regions separate)
FINAL_DIR="glambie_fixed_geometry"
echo "Copying output to $FINAL_DIR ..."
rsync -avh "$OGGM_OUTDIR/" "$FINAL_DIR"

echo "SLURM DONE"
