#!/bin/sh

########################### EXCMCENS ################################
echo "------------------------------------------------"
echo "CMC Ensemble Postprocessing, Convert from Grib2 to Grib1 "
echo "------------------------------------------------"
echo "History: Oct 2010 - 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

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

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

RUN="cmcens"

ANAPRDGEN=YES

#########################################################
# Begin Processing CMC Analysis at 00z, 06z, 12z and 18z
#########################################################

if [ "$ANAPRDGEN" = "YES" ]; then

  PGBF=$COMINm1/${PDYm1}${cyc}_CMC_naefs_latlon1p0x1p0_000_ana.grib2 
  PGBO=CMC_naefs_latlon1p0x1p0_${PDYm1}${cyc}_P000_ana.grib
  $CNVGRIB -g21 $PGBF $PGBO

fi

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

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

hourlist="000 006 012 018 024 030 036 042 048 054 060 066 072 078 084 090 096 \
          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 FHOUR in $hourlist;do
  for RUNNAME in $memberlist; do
    PGBF=$COMIN/${PDY}${cyc}_CMC_naefs_latlon1p0x1p0_P${FHOUR}_0${RUNNAME}.grib2
    if [ ! -s $PGBF ]; then
       echo "File $PGBF is missing, please wait until it is available and rerun this job"
       err_exit
    else
       ln -fs $PGBF PGC.$FHOUR.$RUNNAME
    fi
  done
  if [ $FHOUR -le 99 ];then
    IFHR=`echo $FHOUR | cut -c2-3`
  else
    IFHR=$FHOUR
  fi
  cmdfile=cmd.$FHOUR   
>$cmdfile
  for RUNNAME in $memberlist; do
    PGBO=CMC_naefs_latlon1p0x1p0_${PDY}${cyc}_P${FHOUR}_0${RUNNAME}.grib
    cat >>$cmdfile << EOF
    $CNVGRIB -g21 PGC.$FHOUR.$RUNNAME $PGBO                                          
EOF
  done
  /usr/bin/poe -cmdfile $cmdfile -stdoutmode ordered -ilevel 3   
  export err=$?; err_chk
done

### waiting for finish all processes
wait

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

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