##########################################################################
echo "--------------------------------------------------------------------"
echo "excfs_cdas_dump.sh.sms - CFS network"
echo "                       data dump processing"
echo "--------------------------------------------------------------------"
echo "History: Aug 31 2010 - Original script."
###########################################################################
#
#     COMSP       - string indicating the final directory/filename path to
#                   output data destination
#                   (e.g., "/com/cfs/prod/cdas1.20000102/cdas1.t12z.")
#                   {NOTE: If the imported variable "SENDCOM" (see below)
#                          is "NO", then COMSP is hardwired to the string
#                          "$DATA/"}
#     SENDCOM     - string: if = 'NO' will redefine "COMSP" variable to be
#                   "$DATA/", regardless of its imported value - this has
#                   the effect of preventing any files from going to an
#                   operational /com directory path, and instead sending
#                   them to the "DATA" directory
#                   Default is "YES"
###########################################################################

set -x

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

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

cat break > $pgmout

export NET_uc=$(echo $NET | tr [a-z] [A-Z])
export tmmark_uc=$(echo $tmmark | tr [a-z] [A-Z])
export SENDCOM=${SENDCOM:-YES}
export PROCESS_GRIBFLDS=${PROCESS_GRIBFLDS:-YES}
export PROCESS_GDAS_DUMP=${PROCESS_GDAS_DUMP:-YES}
export PROCESS_OCN_DUMP=${PROCESS_OCN_DUMP:-YES}
export COMSP=${COMSP:-$COMOUT/${RUN}.${cycle}.}    # preferred, but don't know how RUN will be set in J-Script
export COMGDAS=${COMGDAS:-/com/gfs/prod/gdas.$PDY} # GDAS file directory

export EXECbufr=${EXECbufr:-/nwprod/exec}

msg="$NET_uc ANALYSIS TIME IS $PDY$cyc"
$DATA/postmsg "$jlogfile" "$msg"

# 
[ "$SENDCOM" = 'NO' ]  &&  COMSP=$DATA/


# get grib fields
if [ "$PROCESS_GRIBFLDS" = 'YES' ]; then

########################################################
########################################################
#  copy sstgrb           from $COM_SSTOIQD
#  generate sstgrb index file
########################################################
########################################################
   sstgrb=${COM_SSTOIQD}.$PDY/sstoiqd.sst.grb
   sstold=${COM_SSTOIQD}.$PDYm1/sstoiqd.sst.grb

   if [ -s $sstgrb ]; then
      cp $sstgrb ${COMSP}sstgrb
      msg="todays sst grib file located and copied to /com"
      $DATA/postmsg "$jlogfile" "$msg"
   elif [ -s $sstold ]; then
      cp $sstold ${COMSP}sstgrb
      msg="**todays sst grib file not located - copy 1-day old file"
      $DATA/postmsg "$jlogfile" "$msg"
   else
      set +x
      echo " "
      echo " #########################################"
      echo " cannot locate sst grib file - fatal error"
      echo " #########################################"
      echo " "
      set -x
      msg="**CANNOT LOCATE SST GRIB FILE --> EXIT DATA DUMP"
      $DATA/postmsg "$jlogfile" "$msg"
      $DATA/err_exit
   fi

   [ -f errfile ] && rm errfile
   $EXECUTIL/grbindex ${COMSP}sstgrb ${COMSP}sstgrb.index 2> errfile
   errindx=$?
   [ "$errindx" -ne '0' ] && cat errfile
   [ -f errfile ] && rm errfile

   if [ $SENDDBN = YES ]; then
     $DBNROOT/bin/dbn_alert MODEL CDAS1_SSTOIQD_GRB $job ${COMSP}sstgrb
   fi
fi   #  endif $PROCESS_GRIBFLDS


# get gdas files
if [ "$PROCESS_GDAS_DUMP" = 'YES' ]; then

# copy the production GDAS DUMP files
#              and rename them to cdas1 files to be saved in the CDAS directory
  
  # bufr_d files
  for gdasfile in `ls $COMGDAS/gdas1.t${cyc}z.*bufr_d |awk ' BEGIN { FS="/"} { print $NF }'`
  do
    cdasfile=`echo $gdasfile |sed -e "s/gdas1/cdas1/g"`
    cp $COMGDAS/$gdasfile $COMOUT/$cdasfile
  done

  # Change the group ID to rstprod for the following files:
  rst_group_list="adpsfc aircar aircft sfcshp"
  for fname in $rst_group_list
  do
    chgrp rstprod $COMOUT/${RUN}.t${cyc}z.${fname}.tm00.bufr_d
  done
 
  # snow and icegrb files
  cp $COMGDAS/gdas1.t${cyc}z.snogrb_t574 $COMOUT/${RUN}.t${cyc}z.snogrb_t574
  cp $COMGDAS/gdas1.t${cyc}z.snogrb $COMOUT/${RUN}.t${cyc}z.snogrb
  cp $COMGDAS/gdas1.t${cyc}z.engicegrb $COMOUT/${RUN}.t${cyc}z.engicegrb
  
  # tcvital files
  cp $COMGDAS/gdas1.t${cyc}z.syndata.tcvitals.tm00 $COMOUT/${RUN}.t${cyc}z.syndata.tcvitals.tm00
  cp $COMGDAS/gdas1.t${cyc}z.jtwc-fnoc.tcvitals.tm00 $COMOUT/${RUN}.t${cyc}z.jtwc-fnoc.tcvitals.tm00

fi  #  endif $PROCESS_GDAS_DUMP

### No need to send out the DUMP files to TOC as they are identical to GDAS dump

# dump ocean bufr data
if [ "$PROCESS_OCN_DUMP" = 'YES' ]; then

### dump ocean data
DUMPMB=${DUMPMB:-/nwprod/ush/dumpmb}
DUMP_LIST="dbuoy bathy tesac"
LOUD=${LOUD:-off}
GODAS_WNDO=${GODAS_WNDO:-10}

export XLFRTEOPTS="unit_vars=yes"

START_DATE=`$utilscript/finddate.sh $PDY d-$GODAS_WNDO`
END_DATE=$PDY

for CTYPE in $DUMP_LIST; do
  $DUMPMB  $START_DATE  $END_DATE  $CTYPE      > dumpmb.output 2>dumpmb.errfile

#  dumpstat=$?
#  echo status from dump is $dumpstat
#  if [ $dumpstat -ne 0 ]; then
#    msg="Fatal error -- $DUMPMB failed for $CTYPE for dates ${START_DATE} to ${END_DATE}"
#    postmsg "$jlogfile" "$msg"
#    export err=$dumpstat; err_chk
#  fi

  flist=$CTYPE.list
  [ -f $flist ] && rm $flist

  DATE=$START_DATE
  while [ $DATE -le $END_DATE ]; do
  if [ -s $CTYPE.$DATE ]; then
    echo $CTYPE.$DATE >> $flist
  else
    msg="Warning:  NO $CTYPE DUMP FOR ${CDATE}"
      postmsg "$jlogfile" "$msg"
    fi
    DATE=`sh $utilscript/finddate.sh $DATE d+1`
  done

  if [ -s $flist ]; then
    sort $flist > $flist.sort
    export XLFUNITS=0
    unset `env | grep XLFUNIT | awk -F= '{print $1}'`
    export XLFUNIT_50="$CTYPE.all"
    $EXECbufr/bufr_combfr < $flist.sort >> $logfile
    msg="Done processing $CTYPE for ${START_DATE} to ${END_DATE}"
    postmsg "$jlogfile" "$msg"

    if [ "$SENDCOM" = 'YES' ]; then
      [ -s $CTYPE.all ] && cp $CTYPE.all ${COMSP}c${CTYPE}.$tmmark.mbufr_d

      # CTYPE could be dbuoy bathy or tesac
      if [ $SENDDBN = YES ]; then
        $DBNROOT/bin/dbn_alert MODEL CDAS1_BUFR_${CTYPE} $job ${COMSP}c${CTYPE}.$tmmark.mbufr_d
      fi
    fi
  fi

done

### end dump ocean data

export STATUS=YES

fi   #  endif $PROCESS_OCN_DUMP

echo " " >> $pgmout
echo "##################################################################\
####################"  >> $pgmout
echo " " >> $pgmout

#================================================================
#================================================================


# 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.'
$DATA/postmsg "$jlogfile" "$msg"

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