#!/bin/bash

# 1. Submit the Array and capture the ID
# The --parsable flag is the secret sauce here

#1. Prepare observation to submit obsrvation 
# Assume NMME SST and Precip were already processed

cat allSetParams.sh allObsFcstCodes.sh > prepareObs.sh

bash ./prepareObs.sh  

bash ./allPrepareFcstPlotScript.sh


source ./allSetParams.sh

if [[ $LoadManager == "SLURM" ]]; then

#### Total number of predictors X number of models (for parallelization)
numprdct=$(( ${#slctprdctors[@]}))
nPRDCT=$(( ${numprdct} * 7 ))

OBS_ID=$(sbatch --parsable submitPrecip.sh)
echo "obsPrecip is submitted with ID: $OBS_ID"

ARRAY_ID=$(sbatch --array=1-$nPRDCT --parsable --dependency=afterok:$OBS_ID submitFcst.sh)
echo "R Array ${nPRDCT} processors submitted with ID: $ARRAY_ID"

COLLECTOR_ID=$(sbatch --parsable --dependency=afterok:$ARRAY_ID submitBestModel.sh)

echo "Collector submitted with ID: $COLLECTOR_ID. It will start when the array finishes."

plot_ID=$(sbatch --parsable --dependency=afterok:$COLLECTOR_ID submitPlots.sh)

echo "Plot submitted with ID: $plot_ID. It will start when the collector finishes."

elif [[ $LoadManager == "QSUB" ]]; then
  echo "$LoadManager"


elif [[ $LoadManager == "LINUX" ]]; then
  echo "$LoadManager"

bash submitPrecip.sh >& zoutPrec.txt
bash submitFcst.sh >& zoutFcst.txt
bash submitPlots.sh >& zoutPlots.txt

fi

