
###########################

nprdFirst=$(( ${#slctprdctors[@]} - 1 ))

#######################
join_by_comma() {
  local IFS=","
  echo "$*"
}

quote_items_for_r() {
  local out=()
  for x in "$@"; do
    out+=("\"$x\"")
  done
  local IFS=","
  echo "c(${out[*]})"
}

quote_items_for_ncl() {
  local out=()
  for x in "$@"; do
    out+=("\"$x\"")
  done
  local IFS=","
  echo "(/${out[*]}/)"
}

allmodelNamesR=$(quote_items_for_r "${allmodelNames[@]}")
allmodelNamesNCL=$(quote_items_for_ncl "${allmodelNames[@]}")
slctPrdctrR=$(quote_items_for_r "${slctprdctors[@]}")
allprdctorNCL=$(quote_items_for_ncl "${allprdctors[@]}")
slctprdctrNCL=$(quote_items_for_ncl "${slctprdctors[@]}")

#parentDir=$(dirname "$(pwd)")
parentDir="$allcolors"
workdir="$(pwd)"
##########
##### Get observational validation data
#########
cat > paramsObs.ncl <<EOF
startday = 1
iMonth   = $startmon
startmon = $obsstartmon
endmon   = $obsendmon
SEASON   = "$seasonname"
YearS    = $firstyr
YearE    = $endyr
latS     = $latS
latN     = $latN
lonW     = $lonW
lonE     = $lonE
obsT     = "$obst"
monthly  = $monthly
Res      = $obsres
workingDir  = "$workdir"
dataDir     = "$parentDir"
EOF

if [[ $obst = "LOCAL" ]]; then
 cat paramsObs.ncl templateLocalObs.ncl > obsPrecip.ncl
elif [[ $obst = "ENACT" ]]; then
 cat paramsObs.ncl templateObsENACT.ncl > obsPrecip.ncl
else
 cat paramsObs.ncl templateObs.ncl > obsPrecip.ncl
fi

#######################
#### 2. SST
cat > paramsSST.ncl <<EOF
iMon           = $startmon
YYYY           = $currentyr
SEASON         = "$seasonname"
yrS            = $firstyr
yrE            = $endyr
Leadi          = $leadi
Leadf          = $leadf
latSeof        = $latSeof
latNeof        = $latNeof
lonWeof        = $lonWeof
lonEeof        = $lonEeof
numeof         = $numeof
yrSc           = $yrsstsc
yrEc           = $yrsstec
modelFileNames = $allmodelNamesNCL
workingDir     = "$workdir"
EOF

#2 Get NMME SST data
cat paramsSST.ncl templateSSTDownload.ncl > sstDownloadStdz.ncl

#######################
#### 3. Get Precip
cat > paramsPrec.ncl <<EOF
iMon           = $startmon
YYYY           = $currentyr
SEASON         = "$seasonname"
yrS            = $firstyr
yrE            = $endyr
Leadi          = $leadi
Leadf          = $leadf
latSeof        = $latS
latNeof        = $latN
lonWeof        = $lonW
lonEeof        = $lonE
numeof         = $numeof
yrSc           = $yrsstsc
yrEc           = $yrsstec
modelFileNames = $allmodelNamesNCL
workingDir     = "$workdir"
EOF

#3 Get NMME precip data
cat paramsPrec.ncl templatePrecipDownload.ncl > precDownloadStdz.ncl


#############################
### 4. R script
####################

cat > params.R <<EOF
iMonth <- $startmon
SznLen <- $sznlen
ThisYr <- $currentyr
SEASON <- "$seasonname"
fileprecnc <- "$obsprecip"
FirstYr <- $firstyr
LastYr <- $endyr
iLead <- $ilead
trainLength <- $trainLength
testLength <- $testLength
Leave3Out <- $leave3Out
RUN_SERIAL <- $runserial
modelFileNames <- $allmodelNamesR
slctPrdctr <- $slctPrdctrR
nprdFirst <- $nprdFirst
workingDir  = "$workdir"
dataDir     = "$parentDir"
monthly     = "$monthly"
EOF

####params.R is sourced inside multiNominalRegression.R
if [[ $LoadManager == "SLURM" ]]; then
  cp ./templateDriverSLURM.R  multiNominalRegression.R
  echo "$LoadManager"
elif [[ $LoadManager == "QSUB" ]]; then
  cp ./templateDriverQSUB.R  multiNominalRegression.R
  echo "$LoadManager"
elif [[ $LoadManager == "LINUX" ]]; then
  cp ./templateDriverLINUX.R  multiNominalRegression.R
  echo "$LoadManager"
fi


echo `date`
