############################################################################
echo "---------------------------------------------------------------------"
echo "extropcy_qc_reloc.sh.sms - Tropical Cyclone QC/Relocation Prcocessing"
echo "---------------------------------------------------------------------"
echo "History: Jun 13 2006 - Original script."
############################################################################

set -x

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

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

cat break > $pgmout

export COMSP=$COMOUT/${RUN}.${cycle}.

tmhr=`echo $tmmark|cut -c3-4`
cdate10=`/nwprod/util/exec/ndate -$tmhr $PDY$cyc`

NET_uc=$(echo $NET | tr [a-z] [A-Z])
tmmark_uc=$(echo $tmmark | tr [a-z] [A-Z])

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

iflag=0
if [ $RUN = ndas ]; then
   if [ $DO_RELOCATE = NO ]; then
      msg="CENTER PROCESSING TIME FOR NDAS TROPICAL CYCLONE QC IS $cdate10"
      postmsg "$jlogfile" "$msg"
      msg="Output tcvitals files will be copied forward in time to proper \
output file directory path locations"
      postmsg "$jlogfile" "$msg"
      iflag=1
   else
      msg="CENTER PROCESSING TIME FOR $tmmark_uc NDAS TROPICAL CYCLONE \
RELOCATION IS $cdate10"
      postmsg "$jlogfile" "$msg"
   fi
else
   msg="CENTER PROCESSING TIME FOR $tmmark_uc $NET_uc TROPICAL CYCLONE QC/\
RELOCATION IS $cdate10"
   postmsg "$jlogfile" "$msg"
fi


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

####################################
####################################
#  QC tcvitals for tropical cyclones
####################################
####################################

   $ushscript_qctropcy/syndat_qctropcy.sh $cdate10

   cd $COMOUT
   pwd
   set +x
   ls -ltr *syndata*
   set -x
   cd $ARCHSYND
   pwd;ls -ltr
   cat syndat_dateck
   cd $HOMENHC
   pwd;ls -ltr
   cd $DATA

else

# Copy null files into "syndata.tcvitals" and "jtwc-fnoc.tcvitals"
#  (Note: Only do so if files don't already exist - need because in NDAS this
#         script is run twice, first time with DO_RELOCATE=NO, copying these
#         files, and second time with PROCESS_TROPCY=NO and thus coming here -
#         don't want to wipe out these files)
#         

   [ ! -s ${COMSP}syndata.tcvitals.$tmmark ]  &&  \
    cp /dev/null ${COMSP}syndata.tcvitals.$tmmark
   [ ! -s ${COMSP}jtwc-fnoc.tcvitals.$tmmark ]  &&  \
    cp /dev/null ${COMSP}jtwc-fnoc.tcvitals.$tmmark

#  endif loop $PROCESS_TROPCY
fi


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

###################################################
###################################################
#  Relocate tropical cyclones in global sigma guess
###################################################
###################################################

   $ushscript_relocate/tropcy_relocate.sh $cdate10
   errsc=$?

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


# save global sigma guess file(s) possibly updated by tropical cyclone
#  relocation processing in COMSP path
   qual_last=".$tmmark"  # need this because gfs and gdas don't add $tmmark
                         #  qualifer to end of output sigma guess files
   [ $NET = gfs -o $NET = gdas ]  &&  qual_last=""
   [ -s sgm3prep ]  &&  cp sgm3prep ${COMSP}sgm3prep${qual_last}
   [ -s sgesprep ]  &&  cp sgesprep ${COMSP}sgesprep${qual_last}
   [ -s sgp3prep ]  &&  cp sgp3prep ${COMSP}sgp3prep${qual_last}

# The existence of ${COMSP}tropcy_relocation_status.$tmmark file will tell the
#  subsequent PREP processing that RELOCATION processing occurred, if this file
#  does not already exist at this point, echo "RECORDS PROCESSED" into it to
#  further tell PREP processing that records were processed by relocation and
#  the global sigma guess was modified by tropical cyclone relocation
#  Note: If ${COMSP}tropcy_relocation_status.$tmmark already exists at this
#        point it means that it contains the string "NO RECORDS to process"
#        and was created by the child script tropcy_relocate.sh because records
#        were not processed by relocation and the global sigma guess was NOT
#        modified by tropical cyclone relocation (because no tcvitals records
#        were found in the relocation step)
# ----------------------------------------------------------------------------

   [ ! -s ${COMSP}tropcy_relocation_status.$tmmark ]  &&  \
    echo "RECORDS PROCESSED" > ${COMSP}tropcy_relocation_status.$tmmark

#  endif loop $DO_RELOCATE
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

if [ $iflag -eq 0 ]; then
   msg='ENDED NORMALLY.'
   postmsg "$jlogfile" "$msg"
fi

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