
set +x

#          SCRIPT NAME :  syndat_qctropcy.sh
#               AUTHOR :  Steven Lord/Hua-Lu pan/Dennis Keyser
#         DATE WRITTEN :  04/21/97; MODIFIED 05/27/1997 (Keyser)
#                                   MODIFIED 07/06/1997 (Keyser)
#                                   MODIFIED 03/03/2000 (Keyser)
#
#   Abstract: This script handles the pre-processing of the tcvital 
#             files that are made by NHC and other tropical
#             prediction centers by the executable syndat_qctropcy
#
#
echo "History: JUN     1997 - First implementation of this utility script"
echo "         JUL     1997 - Added tcvitals made manually by SDM; Added "
echo "                        jtwc/fnoc tcvitals                         "
echo "         MAR     2000   Converted to IBM-SP                        "
#
#
# Positional parameters passed in:
#   1 - Run date (YYYYMMDDHH)
#        (NOTE: If $tmmark below is 'tm00', then RUN date is the same as
#                CYCLE date; otherwise RUN date is earlier than CYCLE date
#                by "hh" hours where "hh" is "tmhh" in $tmmark)

# Imported variables that must be passed in:
#   envir  - processing environment ('prod' or 'test')
#   DATA   - path to working directory
#   pgmout - string indicating path to for standard output file
#   NET    - string indicating network ('nam', 'gfs', or 'gdas')
#            NOTE: NET is changed to gdas in the parent Job script for
#                  the gdas1 RUN (was gfs - NET remains gfs for gfs RUN)
#   RUN    - string indicating run ('nam', 'ndas', 'gfs', or 'gdas1')
#   cyc    - cycle hour (e.g., '00', '06', '12', or '18')
#   tmmark - string indicating relative time of run to cycle time
#             (e.g., 'tm00', 'tm03', 'tm06', 'tm09', or 'tm12')
#   COMSP  - path to both output jtwc-fnoc file and output tcvitals file (this
#             tcvitals file is read by subsequent relocation processing and/or
#             subsequent program SYNDAT_SYNDATA)

# Imported variables that can be passed in:
#   ARCHSYND  - path to syndat archive directory
#                (Default: /com/arch/${envir}/syndat)
#   PARMSYND  - path to syndat parm field directory
#                (Default: /nw${envir}/parm)
#   EXECSYND  - path to syndat executable directory
#                (Default: /nw${envir}/exec)
#   FIXSYND   - path to syndat fix field directory
#                (Default: /nw${envir}/fix)
#   USHSYND   - path to syndat ush directory
#                (Default: /nw${envir}/ush)
#   HOMENHC   - path to NHC directory containing tcvitals records
#                (Default: /tpc/save/guidance/storm-data/ncep)
#   TANK_TROPCY
#             - path to home directory containing tropical cyclone record
#                data base
#                (Default: /dcom/us007003)
#   slmask    - path to t126 32-bit gaussian land/sea mask file
#                (Default: /nw${envir}/fix/syndat_slmask.t126.gaussian)
#   copy_back - switch to copy updated files back to archive directory and
#                to tcvitals directory
#                (Default: YES)
#   jlogfile  - path to job log file (skipped over by this script if not
#                 passed in)
#   SENDCOM     switch  copy output files to $COMSP
#                (Default: YES)

set -xua

ARCHSYND=${ARCHSYND:-/com/arch/${envir}/syndat}
PARMSYND=${PARMSYND:-/nw${envir}/parm}
EXECSYND=${EXECSYND:-/nw${envir}/exec}
FIXSYND=${FIXSYND:-/nw${envir}/fix}
USHSYND=${USHSYND:-/nw${envir}/ush}
HOMENHC=${HOMENHC:-/tpc/save/guidance/storm-data/ncep}
TANK_TROPCY=${TANK_TROPCY:-/dcom/us007003}
slmask=${slmask:-/nw${envir}/fix/syndat_slmask.t126.gaussian}
copy_back=${copy_back:-YES}
SENDCOM=${SENDCOM:-YES}

cd $DATA

msg="Tropical Cyclone tcvitals QC processing has begun"
set +x
echo
echo $msg
echo
set -x
echo $msg >> $pgmout
set +u
[ -n "$jlogfile" ]  && postmsg "$jlogfile" "$msg"
set -u

if [ "$#" -ne '1' ]; then
   msg="**NON-FATAL ERROR PROGRAM  SYNDAT_QCTROPCY  run date not in \
positional parameter 1"
   set +x
   echo
   echo $msg
   echo
   set -x
   echo $msg >> $pgmout
   set +u
   [ -n "$jlogfile" ]  &&  postmsg "$jlogfile" "$msg"
   set -u
   msg="**NO TROPICAL CYCLONE tcvitals processed --> non-fatal"
   set +x
   echo
   echo $msg
   echo
   set -x
   echo $msg >> $pgmout
   set +u
   [ -n "$jlogfile" ]  &&  postmsg "$jlogfile" "$msg"
   set -u

# Copy null files into "${COMSP}syndata.tcvitals.$tmmark" and
#  "${COMSP}jtwc-fnoc.tcvitals.$tmmark" so later ftp attempts will find and
#  copy the zero-length file and avoid wasting time with multiple attempts
#  to remote machine(s)
#  (Note: Only do so if files don't already exist)

   if [ $SENDCOM = YES ]; then
      [ ! -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
   fi

   exit
fi

CDATE10=$1

set +x
echo
echo "Run date is $CDATE10"
echo
set -x

year=`echo $CDATE10 | cut -c1-4`

 
#  Copy the seasonal statistics from archive directory to local
 
cp $ARCHSYND/syndat_akavit akavit; touch akavit
cp $ARCHSYND/syndat_dateck dateck
cp $ARCHSYND/syndat_stmcat.scr stmcat.scr; touch stmcat.scr
cp $ARCHSYND/syndat_stmcat stmcat; touch stmcat
cp $ARCHSYND/syndat_sthisto sthisto
cp $ARCHSYND/syndat_sthista sthista
 
#  Generate the correct RUNID and FILES value based on $NET, $RUN and $cyc
#  Copy this into parm file, then cat the remaining switches in the parm file
#     Note: FILES=T for 00Z GDAS at tm00 (last run of day centered on 00Z)
#                       06Z GDAS at tm00 (last run of day centered on 06Z)
#                       12Z GDAS at tm00 (last run of day centered on 12Z)
#                       18Z GDAS at tm00 (last run of day centered on 18Z)
 
net=$NET
files=F,
if [ "$RUN" = 'ndas' ]; then
   net=ndas
elif [ "$NET" = 'gdas' ]; then
   files=T,
fi

echo " &INPUT  RUNID = '${net}_${tmmark}_${cyc}', FILES = $files " > vitchk.inp
cat $PARMSYND/syndat_qctropcy.${NET}.parm >> vitchk.inp
 
#  Copy the fixed fields from FIXSYND
 
cp $FIXSYND/syndat_fildef.vit fildef.vit
cp $FIXSYND/syndat_stmnames stmnames


rm nhc fnoc gtsbtab gtsbufr human.btab lthistry


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

#  There are five possible sources of tropical cyclone bogus messages
#  All are input to program syndat_qctropcy
#  ------------------------------------------------------------------

# NHC ... copy into working directory as nhc; copy to archive
cp $HOMENHC/tcvitals nhc
touch nhc
[ "$copy_back" = 'YES' ]  &&  cat nhc >> $ARCHSYND/syndat_tcvitals.$year
cp -p nhc nhc.ORIG


# JTWC/FNOC ... execute syndat_getjtbul script to write into working directory
#               as fnoc; copy to archive
$USHSYND/syndat_getjtbul.sh $CDATE10
touch fnoc
[ "$copy_back" = 'YES' ]  &&  cat fnoc >> $ARCHSYND/syndat_tcvitals.$year


##cp $???????????????? gtsbtab


##cp $???????????????? gtsbufr


# Manual ... copy into working directory as human.btab; copy to archive
cp $TANK_TROPCY/maksynrc human.btab
touch human.btab
[ "$copy_back" = 'YES' ]  &&  cat human.btab >> $ARCHSYND/syndat_tcvitals.$year
cp -p human.btab human.btab.ORIG

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


cp $slmask slmask.126
 
 
#  Execute program syndat_qctropcy

pgm=`basename $EXECSYND/syndat_qctropcy`
export pgm
if [ -s prep_step ]; then
   set +u
   . prep_step
   set -u
else
   [ -f errfile ] && rm errfile
   export XLFUNITS=0
   unset `env | grep XLFUNIT | awk -F= '{print $1}'`

   set +u
   if [ -z "$XLFRTEOPTS" ]; then
     export XLFRTEOPTS="unit_vars=yes"
   else
     export XLFRTEOPTS="${XLFRTEOPTS}:unit_vars=yes"
   fi
   set -u
fi

echo "$CDATE10"      > cdate10.dat
export XLFUNIT_11=slmask.126
export XLFUNIT_12=cdate10.dat
timex $EXECSYND/syndat_qctropcy >> $pgmout 2> errfile
errqct=$?
###cat errfile
cat errfile >> $pgmout
rm errfile
set +x
echo
echo "The foreground exit status for SYNDAT_QCTROPCY is " $errqct
echo
set -x
if [ "$errqct" -gt '0' ];then
   msg="**NON-FATAL ERROR PROGRAM  SYNDAT_QCTROPCY  RETURN CODE $errqct"
   set +x
   echo
   echo $msg
   echo
   set -x
   echo $msg >> $pgmout
   set +u
   [ -n "$jlogfile" ]  &&  postmsg "$jlogfile" "$msg"
   set -u
   msg="**NO TROPICAL CYCLONE tcvitals processed --> non-fatal"
   set +x
   echo
   echo $msg
   echo
   set -x
   echo $msg >> $pgmout
   set +u
   [ -n "$jlogfile" ]  &&  postmsg "$jlogfile" "$msg"
   set -u

# In the event of a ERROR in PROGRAM SYNDAT_QCTROPCY, copy null files into
#  "${COMSP}syndata.tcvitals.$tmmark" and "${COMSP}jtwc-fnoc.tcvitals.$tmmark"
#  so later ftp attempts will find and copy the zero-length file and avoid
#  wasting time with multiple attempts to remote machine(s)
#  (Note: Only do so if files don't already exist)

   if [ $SENDCOM = YES ]; then
      [ ! -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
   fi

   exit
fi
set +x
echo
echo "----------------------------------------------------------"
echo "**********  COMPLETED PROGRAM syndat_qctropcy   **********"
echo "----------------------------------------------------------"
echo
set -x

if [ -s current ]; then
   msg="program  SYNDAT_QCTROPCY  completed normally - tcvitals records \
processed"
else
msg="no records available for program  SYNDAT_QCTROPCY - null tcvitals file \
produced"
fi
set +u
[ -n "$jlogfile" ]  &&  postmsg "$jlogfile" "$msg"
set -u


if [ "$copy_back" = 'YES' ]; then
   cat lthistry>>$ARCHSYND/syndat_lthistry.$year
   cp akavit $ARCHSYND/syndat_akavit
   cp dateck $ARCHSYND/syndat_dateck
   cp stmcat.scr $ARCHSYND/syndat_stmcat.scr
   cp stmcat $ARCHSYND/syndat_stmcat
   cp sthisto $ARCHSYND/syndat_sthisto
   cp sthista $ARCHSYND/syndat_sthista
fi


diff nhc nhc.ORIG > /dev/null
errdiff=$?

###################################
#  Update NHC file in $HOMENHC
###################################

if test "$errdiff" -ne '0'
then

   if [ "$copy_back" = 'YES' ]; then
      cp nhc $HOMENHC/tcvitals
      err=$?

      if [ "$err" -ne '0' ]; then
         msg="###ERROR: Previous NHC Synthetic Data Record File \
$HOMENHC/tcvitals not updated by syndat_qctropcy"
      else
         msg="Previous NHC Synthetic Data Record File \
$HOMENHC/tcvitals successfully updated by syndat_qctropcy"
      fi

      set +x
      echo
      echo $msg
      echo
      set -x
      echo $msg >> $pgmout
      set +u
      [ -n "$jlogfile" ]  &&  postmsg "$jlogfile" "$msg"
      set -u
   fi

else

   msg="Previous NHC Synthetic Data Record File $HOMENHC/tcvitals \
not changed by syndat_qctropcy"
   set +x
   echo
   echo $msg
   echo
   set -x
   echo $msg >> $pgmout
   set +u
   [ -n "$jlogfile" ]  &&  postmsg "$jlogfile" "$msg"
   set -u

fi


diff human.btab human.btab.ORIG > /dev/null
errdiff=$?

###############################################################################
#  Update Manual file in $TANK_TROPCY if it has been changed by syndat_qctropcy
###############################################################################

if test "$errdiff" -ne '0'
then

   if [ "$copy_back" = 'YES' ]; then
      cp human.btab $TANK_TROPCY/maksynrc
      err=$?

      if test "$err" -ne '0'
      then
         msg="###ERROR: Previous Manual Synthetic Data Record File \
$TANK_TROPCY/maksynrc not updated by syndat_qctropcy"
      else
         msg="Previous Manual Synthetic Data Record File $TANK_TROPCY/maksynrc \
successfully updated by syndat_ qctropcy"
      fi
      set +x
      echo
      echo $msg
      echo
      set -x
      echo $msg >> $pgmout
      set +u
      [ -n "$jlogfile" ]  &&  postmsg "$jlogfile" "$msg"
      set -u
   fi

else

   msg="Previous Manual Synthetic Data Record File $TANK_TROPCY/maksynrc \
not changed by syndat_qctropcy"
   set +x
   echo
   echo $msg
   echo
   set -x
   echo $msg >> $pgmout
   set +u
   [ -n "$jlogfile" ]  &&  postmsg "$jlogfile" "$msg"
   set -u

fi

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


#  This is the file that connects to the later RELOCATE and/or PREP scripts
[ $SENDCOM = YES ]  &&  cp current ${COMSP}syndata.tcvitals.$tmmark

#  Create the DBNet alert 
if [ $SENDDBN = "YES" ]
then
   $DBNROOT/bin/dbn_alert MODEL GDAS_TCVITALS $job ${COMSP}syndata.tcvitals.$tmmark
fi
    
#  Write JTWC/FNOC Tcvitals to /com path since not saved anywhere else
[ $SENDCOM = YES ]  &&  cp fnoc ${COMSP}jtwc-fnoc.tcvitals.$tmmark

msg="TROPICAL CYCLONE TCVITALS QC PROCESSING HAS COMPLETED FOR $CDATE10"
set +x
echo
echo $msg
echo
set -x
echo $msg >> $pgmout
echo " "  >> $pgmout
set +u
[ -n "$jlogfile" ]  &&  postmsg "$jlogfile" "$msg"
set -u

exit
