###########################################################################################################
# Script: gefs_avgspr.sh
# Abstract: this script produces mean and spread of NCEP bias-corrected ensemble forecast
# Author: Richard Wobus ---- March 2006 ( Ensemble Postprocessina Script, exgefs_ensstat.sh.sms )  
# Modified by: Bo Cui choose the mean and spread calculation part for bias-corrected ensemble ---- May 2006
###########################################################################################################

#---------------------------------------
#  calculate ensemble mean and spread
#---------------------------------------

hourlist="     06  12  18  24  30  36  42  48  54  60  66  72  78  84  90  96 \
          102 108 114 120 126 132 138 144 150 156 162 168 174 180 186 192 198 \
          204 210 216 222 228 234 240 246 252 258 264 270 276 282 288 294 300 \
          306 312 318 324 330 336 342 348 354 360 366 372 378 384"

memberlist="c00 p01 p02 p03 p04 p05 p06 p07 p08 p09 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 p20"


#######################################
# define control parameters
# no enspost files produced, nenspost=0
#######################################

lfm=66000
nenspost=0

######################################################
# start mean and spread calculation for each lead time
######################################################

for nfhrs in $hourlist
do

  echo " &namdim" >namin
  echo " lfdim=${lfm}," >>namin
  echo " /" >>namin
  echo " &namens" >>namin

  ifile=0
  for mem in $memberlist
  do
    (( ifile = ifile + 1 ))
    iskip=0
    if [ "$mem" = "c00" ]; then
      iskip=1
    fi
    echo " cfipg($ifile)='$COMOUTBC/ge${mem}.t${cyc}z.pgrba_bcf${nfhrs}'," >>namin
#   echo " cfipi($ifile)='ge${mem}.t${cyc}z.pgrbaif${nfhrs}idx'," >>namin
    echo " iskip($ifile)=${iskip}," >>namin
  done

  echo " nfiles=${ifile}," >>namin
  echo " nenspost=${nenspost}," >>namin

  echo " cfoag='geavg.t${cyc}z.pgrba_bcf${nfhrs}'," >>namin
  echo " cfosg='gespr.t${cyc}z.pgrba_bcf${nfhrs}'," >>namin

  echo " inindxf=.false.," >>namin

  echo " /" >>namin

  startmsg
  $EXECGEFS/gefs_ensstat <namin > $pgmout.${nfhrs}_avgspr  2> errfile

  export err=$?;err_chk
  rm namin

  if [ "$SENDCOM" = "YES" ]; then
    if [ -s geavg.t${cyc}z.pgrba_bcf$nfhrs ]; then
      mv geavg.t${cyc}z.pgrba_bcf$nfhrs  $COMOUTBC/geavg.t${cyc}z.pgrba_bcf$nfhrs
    fi
    if [ -s gespr.t${cyc}z.pgrba_bcf$nfhrs ]; then
      mv gespr.t${cyc}z.pgrba_bcf$nfhrs  $COMOUTBC/gespr.t${cyc}z.pgrba_bcf$nfhrs
    fi
  fi

done

cp $COMIN/geavg.t${cyc}z.pgrbaf00      $COMOUTBC/geavg.t${cyc}z.pgrba_bcf00         
cp $COMIN/gespr.t${cyc}z.pgrbaf00      $COMOUTBC/gespr.t${cyc}z.pgrba_bcf00         

set +x
echo " "
echo "Leaving sub script gefs_avgspr.sh"
echo " "
set -x

