#!/bin/bash

#SBATCH -n 1
#SBATCH --mem 5000
#SBATCH -t 1:00:00

script='/esarchive/scratch/ncortesi/scripts/WRs_subseasonal_chunks.R' ## this script has 52 chunks and needs 5000 MB for chunk and 1:00:00 h for chunk
##script='/esarchive/scratch/ncortesi/scripts/WRs_subseasonal_seasonal_kmeans.R' ## like above, but for seasonal validation instead of weekly (optimized to run from gpfs)

Rscript $script $chk ## $chk2 ## insert it if you use two types of chunks
if [ $? -eq 0 ]; then echo "Job num." $chk "completed"; else echo "Job num." $chk "terminated before it could finish"; fi

## Command to run from terminal (copy and paste):
##
## for chunk in {1..52}; do rm -f chunk$chunk.*; sbatch --job-name=chunk$chunk --output=chunk$chunk.out --error=chunk$chunk.err --export=chk=$chunk /esarchive/scratch/ncortesi/scripts/parallel9.R; done

### Command to check if all jobs have finished correctly:
### squeue -u bsc32842 -t RUNNING | wc -l   ### count the number of running jobs of the user (+1 because it also counts the table header)

### Command to re-execute only chunks that were terminated before completion:
### for chunk in 4 9 17 26; do sbatch --job-name=chunk$chunk --output=chunk$chunk.out --error=chunk$chunk.err --export=chk=$chunk /esarchive/scratch/ncortesi/scripts/parallel9.R; done
