###########################################################################################################
# Script: cmce_avgspr.sh
# Abstract: this script produces mean and spread of CMC 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
#              Bo Cui add hourlist and memberlist,let it be independent to script excmcens_debias.sh.sms- Jan 2008
###########################################################################################################

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

lfm=66000
nenspost=0

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

export 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"

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

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/cmc_ge${mem}.t${cyc}z.pgrba_bcf${nfhrs}'," >>namin
    echo " iskip($ifile)=${iskip}," >>namin
  done

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

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

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

  echo " /" >>namin

  startmsg
  $EXECCMCE/gefs_ensstat <namin > output_avgspr_$nfhrs   
  export err=$?;err_chk
  rm namin

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

done

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

