#!/bin/bash
#
# Script:  R_loops.sh
# Usage: For submitting multiple batch R jobs to LSF,
#    an example of looping over years and models
# Author:  NCSU HPC
#
# 
## To run, type:
#     ./R_loops.sh [# years] [# models]
#  Script must have execute permissions, i.e.,
#     chmod u+x R_loops.sh

module load R

if [ $# -ne 2 ]; then
        echo "Usage: You need to feed two arguments to this program which is"
        echo "the number of years and the number of models. For example,"
        echo "./R_loops.sh 2 2"
        exit 1
fi

# Specify number of jobs to submit
numYears=$1

numModels=$2

# Initialize year loop counter 
year=1

while [ $year -le $numYears ]
do

    # Initialize model loop counter
    model=1
    while [ $model -le $numModels ]
    do
       echo "Submit job year = $year and model = $model"

       bsub -n 1 -W 30 -oo out.year=$year.model=$model -eo err.year=$year.model=$model "Rscript codehpc.R $year $model"

       ((model++))

    done

    ((year++))

done
