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

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)") 
workdir="`pwd`"

##########
##### 5. Plot individual single model probabilities
##############

constant=".ncl"

for v in "${slctprdctors[@]}"; do
    outfile="plotSingleModel${v}${constant}"

    cat > paramsSingle.ncl <<EOF
PRDCTR        = "${v}"
iLead          = $ilead
iMonth         = $startmon
STATE          = $isstate
currentYr      = $currentyr
SEASON         = "$seasonname"
latS           = $latS
latN           = $latN
lonW           = $lonW
lonE           = $lonE
COUNTRY        = "$mycountry"
Res            = $obsres
printProb      = $printProb
printProbRaw   = $printProbRaw
tlats          = $tlats
tlatn          = $tlatn
tlonw          = $tlonw
tlone          = $tlone
modelFileNames = $allmodelNamesNCL
workingDir     = "$workdir"
dataDir        = "$allcolors"
f1shp          = "$WMOshpfiles"
REGION         = "$region"
obsT           = "$obst"
YearS          = $firstyr
YearE          = $endyr
maskLev        = $masklev
shaderaindry   = $shaderain
useNCLmask     = $UseMask 
RASTER         = $DoRaster
monthly        = "$monthly"
SznLen         = $sznlen              
EOF
    cat paramsSingle.ncl templateShapeFileNames.ncl templateIndividualProb.ncl > "$outfile"
    ncl "$workdir/$outfile"
    echo $outfile
done

############################
##### 6. Plot individual average model probabilities
constant=".ncl"

for v in "${slctprdctors[@]}"; do
    outfile="plotAvgModel${v}${constant}"

    cat > paramsAvg.ncl <<EOF
PRDCTR        = "${v}"
iLead          = $ilead
iMonth         = $startmon
STATE          = $isstate
currentYr      = $currentyr
startmon       = $obsstartmon
SEASON         = "$seasonname"
latS           = $latS
latN           = $latN
lonW           = $lonW
lonE           = $lonE
COUNTRY        = "$mycountry"
Res            = $obsres
printProb      = $printProb
printProbRaw   = $printProbRaw
tlats          = $tlats
tlatn          = $tlatn
tlonw          = $tlonw
tlone          = $tlone
modelFileNames = $allmodelNamesNCL
workingDir     = "$workdir"
dataDir        = "$allcolors"
f1shp          = "$WMOshpfiles"
REGION         = "$region"
obsT           = "$obst"
YearS          = $firstyr
YearE          = $endyr
maskLev        = $masklev
shaderaindry   = $shaderain
useNCLmask     = $UseMask 
RASTER         = $DoRaster 
monthly        = "$monthly"
SznLen         = $sznlen              
EOF

    cat paramsAvg.ncl templateShapeFileNames.ncl templateEnsProb.ncl > "$outfile"
    ncl "$workdir/$outfile"
    echo $outfile
done

############################
##### 7. Plot Average Predictors Skills for indiviual models and ensemble

cat > paramsAllSkill.ncl <<EOF
PRDCTRall      = $slctprdctrNCL
iLead          = $ilead
iMonth         = $startmon
STATE          = $isstate
currentYr      = $currentyr
startmon       = $obsstartmon
SEASON         = "$seasonname"
latS           = $latS
latN           = $latN
lonW           = $lonW
lonE           = $lonE
COUNTRY        = "$mycountry"
Res            = $obsres
printProb      = $printProb
printProbRaw   = $printProbRaw
tlats          = $tlats
tlatn          = $tlatn
tlonw          = $tlonw
tlone          = $tlone
modelFileNames = $allmodelNamesNCL
workingDir     = "$workdir"
dataDir        = "$allcolors"
f1shp          = "$WMOshpfiles"
REGION         = "$region"
obsT           = "$obst"
YearS          = $firstyr
YearE          = $endyr
maskLev        = $masklev
shaderaindry   = $shaderain
useNCLmask     = $UseMask 
RASTER         = $DoRaster 
monthly        = "$monthly"
SznLen         = $sznlen              
EOF

cat paramsAllSkill.ncl templateShapeFileNames.ncl templateSingleAvgPrdctors.ncl > plotAvgPrdctorsSkill.ncl

ncl $workdir/plotAvgPrdctorsSkill.ncl
    
############################
##### 8. Plot Average Predictors Skills for indiviual models and ensemble

cat > paramsSkillWtd.ncl <<EOF
PRDCTRall      = $slctprdctrNCL
iLead          = $ilead
iMonth         = $startmon
STATE          = $isstate
currentYr      = $currentyr
startmon       = $obsstartmon
SEASON         = "$seasonname"
latS           = $latS
latN           = $latN
lonW           = $lonW
lonE           = $lonE
COUNTRY        = "$mycountry"
Res            = $obsres
printProb      = $printProb
printProbRaw   = $printProbRaw
tlats          = $tlats
tlatn          = $tlatn
tlonw          = $tlonw
tlone          = $tlone
nprdFirst      = $nprdFirst
modelFileNames = $allmodelNamesNCL
workingDir     = "$workdir"
dataDir        = "$allcolors"
f1shp          = "$WMOshpfiles"
REGION         = "$region"
obsT           = "$obst"
YearS          = $firstyr
YearE          = $endyr
maskLev        = $masklev
shaderaindry   = $shaderain
useNCLmask     = $UseMask 
RASTER         = $DoRaster 
monthly        = "$monthly"
SznLen         = $sznlen              
EOF

cat paramsSkillWtd.ncl templateShapeFileNames.ncl templateEnsSkill.ncl > plotSkillWtdAvg.ncl

ncl $workdir/plotSkillWtdAvg.ncl


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


echo `date`
