#!/bin/ksh
# Run under ksh

#############################################################################
echo "----------------------------------------------------------------------"
echo "exrtma_makeprepbufr.sh - RTMA model prepbufr processing"
echo "----------------------------------------------------------------------"
echo "History: Mar  1 2013 - Original script.                               "
echo "         July   2017 - enabled potential for minute-precison cycles   "
#############################################################################

set -x

# Make sure we are in the $DATA directory
cd $DATA

msg="HAS BEGUN on `hostname`"
postmsg "$jlogfile" "$msg"

cat break > $pgmout

CHGRP_RSTPROD=${CHGRP_RSTPROD:-YES}

# Get cycle center date/time (down to hour precision, YYYYMMDDHH)

cdate10=`cut -c7-16 ncepdate`

# Imported variable cycM, if it exists, contains cycle time minutes
#  If cycM is imported, reset cycle in this script only (cycle_here) to 4-digit
#   value t<HHMM>z

cycle_here=$cycle
[ -n "$cycM" ]  &&  cycle_here=t${cyc}${cycM}z

export COMSP=${COMSP:-$COMIN/${RUN}.${cycle_here}.}

msg="CENTER TIME FOR PREPBUFR PROCESSING IS $cdate10"
[ -n "$cycM" ]  &&  msg="$msg:${cycM}"
postmsg "$jlogfile" "$msg"

#  Add variable cycM as 2nd argument below (may or may not be set)

ksh $ushscript_prep/prepobs_makeprepbufr.sh $cdate10 $cycM
errsc=$?

[ "$errsc" -ne '0' ]  &&  exit $errsc

if [ "$CHGRP_RSTPROD" = 'YES' ]; then
   msg="NOTE: These files (if present) are RESTRICTED to rstprod group: \
prepbufr"
   postmsg "$jlogfile" "$msg"
set +x
   echo " "
   echo "$msg"
   echo " "
set -x
fi
warning=no

if [ "$PREPDATA" = 'YES' ]; then

# save snapshot of prepbufr file after PREPOBS_PREPDATA in COMOUT
   cpfs prepda.prepdata $COMOUT/${RUN}.${cycle_here}.prepbufr.$tmmark

   if [ "$CHGRP_RSTPROD" = 'YES' ]; then
      chgrp rstprod $COMOUT/${RUN}.${cycle_here}.prepbufr.$tmmark
      errch=$?
      if [ $errch -eq 0 ]; then
         chmod 640 $COMOUT/${RUN}.${cycle_here}.prepbufr.$tmmark
      else
         cpfs /dev/null $COMOUT/${RUN}.${cycle_here}.prepbufr.$tmmark
         warning=yes
      fi
   fi
#bsm - troubleshooting
echo "is the prepbufr file good?"
echo `ls -l $COMOUT/${RUN}.${cycle_here}.prepbufr.$tmmark`

if [ "$RUN" = "rtma_ru" ] && [ "$SENDDBN" = YES ]; then
   $DBNROOT/bin/dbn_alert MODEL RTMA_RU_BUFR_PREPda $job $COMOUT/${RUN}.${cycle_here}.prepbufr.$tmmark
fi

# save current prepbufr mnemonic table in COMOUT if either it isn't already
#  there for a previous cycle or if it has changed from a previous cycle
   if [ ! -s $COMOUT/*prep.bufrtable ]; then
      cpfs prep.bufrtable  $COMOUT/${RUN}.${cycle_here}.prep.bufrtable
   else
      diff `ls -t  $COMOUT/*prep.bufrtable | head -n1` prep.bufrtable \
       > /dev/null 2>&1
      errdiff=$?
      [ "$errdiff" -ne '0' ]  &&  \
       cpfs prep.bufrtable  $COMOUT/${RUN}.${cycle_here}.prep.bufrtable
   fi
fi

if [ "$warning" = 'yes' ]; then
   msg="**WARNING: Since user $USER is not in rstprod group all RESTRICTED \
files are replaced with a null file"
   postmsg "$jlogfile" "$msg"
set +x
   echo " "
   echo "$msg"
   echo " "
set -x
fi

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

# GOOD RUN
set +x
echo " "
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " "
set -x


# save standard output
cat break $pgmout break > allout
cat allout
# rm allout

sleep 10

msg='ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"

################## END OF SCRIPT #######################
