#!/bin/sh

########################### EXCMCENS ##################################################
echo "--------------------------------------------------------------------------------"
echo "CMC Ensemble Postprocessing, Convert CMC Bias Corrected Ens from Grib2 to Grib1 "
echo "--------------------------------------------------------------------------------"
echo "History: Oct 2011 - First implementation of this new script."
echo "AUTHOR: Bo Cui  (wx20cb)"
########################### EXCMCENS ##################################################

set -x

export MP_LABELIO=yes
export MP_PROCS=21
export MP_PGMMODEL=mpmd

#####################################
# Define Script/Exec Variables
#####################################

export CNVGRIB=$EXECUTIL/cnvgrib

export NDATE=$EXECUTIL/ndate
export ENSANOMALY=$USHCMCE/cmce_climate_anomaly.sh
export ENSWEIGHTS=$USHCMCE/cmce_weights.sh
export ENSAVGSPR=$USHCMCE/cmce_bc_avgspr.sh

#####################################
# START TO DUMP DATA FOR $cycle CYCLE
#####################################

msg="Starting postprocessing for $cycle Ensemble memebers"
postmsg "$jlogfile" "$msg"

RUN="cmcens"

########################################################
# Begin Processing Ensemble Forecast Data at 00z and 12z
########################################################

if [ $cyc -eq 00 -o $cyc -eq 12 ]; then

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"

memberlist="00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 "

###################################################
## Interpolation in copygb added here (01/03/2006)
###################################################

for IFHR in $hourlist;do

  cmdfile=cmd.$IFHR   
>$cmdfile

  for RUNNAME in $memberlist; do
    if [ $IFHR -le 99 ];then
      PGBF=$DCOM_IN/${PDY}${cyc}_CMC_naefsbc_latlon1p0x1p0_P0${IFHR}_0${RUNNAME}.grib2
    else
      PGBF=$DCOM_IN/${PDY}${cyc}_CMC_naefsbc_latlon1p0x1p0_P${IFHR}_0${RUNNAME}.grib2
    fi
    if [ ! -s $PGBF ]; then
       echo "File $PGBF is missing, please wait until it is available and rerun this job"
       err_exit
    else
      PGBO=cmc_gep${RUNNAME}.t${cyc}z.pgrba_bcf${IFHR} 
      if [ $RUNNAME -eq 00 ]; then
        PGBO=cmc_gec00.t${cyc}z.pgrba_bcf${IFHR} 
      fi
      cat >>$cmdfile << EOF
      $CNVGRIB -g21 $PGBF $PGBO                                          
EOF
    fi
  done

  # /usr/bin/poe -cmdfile $cmdfile -stdoutmode ordered -ilevel 3   
  poe -cmdfile $cmdfile   
  export err=$?; err_chk
done

### waiting for finish all processes

wait

################################################
# move pgrba_bc data to /com/gens/prod directory
################################################

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

if [ "$SENDCOM" = "YES" ]; then
  for IFHR in $hourlist; do
    for RUNNAME in $memberlist; do
      if [ $IFHR -ge 06 ]; then
        PGBO=cmc_ge${RUNNAME}.t${cyc}z.pgrba_bcf${IFHR} 
        cp $PGBO $COMOUTBC/
      fi
    done
  done
fi

#############################
#  calculate anomaly forecast                  
#############################

if [ "$SENDCOM" = "YES" ]; then
  for IFHR in $hourlist; do
    for RUNNAME in $memberlist; do
      if [ $IFHR -ge 06 ]; then
        export MEMLIST=$RUNNAME
        $ENSANOMALY $PDY$cyc $IFHR  
        PGBO=cmc_ge${RUNNAME}.t${cyc}z.pgrba_anf${IFHR} 
        mv $PGBO $COMOUTAN/
      fi
    done
  done
fi

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

$ENSAVGSPR

###############################################################
#  release the NAEFS products generation job jnaefs_prob_avgspr
###############################################################
$SMSBIN/setev release_naefs_avgspr

############################################
#  calculate weight for each ensemble member
############################################

if [ "$SENDCOM" = "YES" ]; then
  for IFHR in $hourlist; do
    for RUNNAME in $memberlist; do
      ifile_ens=$COMIN/cmc_ge${RUNNAME}.t${cyc}z.pgrbaf${IFHR}
      icnt=0
      while [ icnt -le 30 ]; do
        if [ -s $ifile_ens ]; then
          icnt=31
          export MEMLIST=$RUNNAME
          $ENSWEIGHTS $PDY$cyc $IFHR
          PGBO=cmc_ge${RUNNAME}.t${cyc}z.pgrba_wtf${IFHR}
          mv $PGBO $COMOUTWT/
        else
          sleep 10
          icnt=`expr $icnt + 1`
          echo $icnt
        fi
      done
    done
  done
fi

cat $pgmout.24.p02_wt
cat output_p02.24
cat output_p02.360
cat output_avgspr_24
cat output_avgspr_360

#######################################################
# End Processing Ensemble Forecast Data at 00z and 12z
#######################################################
fi

msg="HAS COMPLETED NORMALLY!"
postmsg "$jlogfile" "$msg"
