
#######################################################################
#
# D. Keyser - NP22
#
# Given the specified input BUFR data dump file, this script will change
#  the "center" date/time (BUFR message number 1 Section 1 year, month,
#  day, and hour) to be a specified new date (year, month, day, and hour)
#  determined from imported positional parameters.  In addition, the dump
#  date (BUFR message number 2 Section 1 date) is updated by the same number
#  of hours as was the center date.  The script then writes the updated BUFR
#  data dump file to a specified output location.
#
#  The output BUFR data dump file is otherwise identical to the input BUFR
#  data dump file, with the exception that the output file is always y2k
#  compliant (century indicator is stored in formerly reserved byte 18 of
#  Section 1) while the input BUFR data dump file may or may not be y2k
#  compliant.
#
#   IMPORTANT!!!!! - This will not work with BUFR "tanks" because there is
#                    no center date messsage (number 1) in a tank.
#
#
# Usage:
#
#   ===> There are either 5 or 6 positional parameters are imported:
#          (positional parameter 6 is OPTIONAL)
#
#      sh /nwprod/util/ush/overdate.bufr_dump_2.sh \
#       Input BUFR data dump file (full directory path) \
#       "Center" Year (UTC)  for output BUFR data dump file (must always be
#         4-digits) \
#       "Center" Month (UTC) for output BUFR data dump file (must always be
#         2-digits) \
#       "Center" Day (UTC)   for output BUFR data dump file (must always be
#         2-digits) \
#       "Center" Hour (UTC)  for output BUFR data dump file (must always be
#         2-digits) \
#       <Output BUFR data dump file (full directory path)> \
#
#      NOTE THAT POSITIONAL PARAMETER 2 (YEAR) MUST BE 4-DIGITS, AND
#       POSITONAL PARAMETERS 3-5 (MONTH, DAY, AND HOUR) MUST EACH BE
#       2-DIGITS (i.e., if month is July, specify positional parameter 3
#       as 07 , not as 7 ).
#
#
# Examples:
#
#        I want to update an input SATWND BUFR data dump file to contain
#        the center date 2100 UTC February 22, 2003 (the dump date in the
#        output file will be changed by the same number of hours as the
#        center date) ...
#         sh /nwprod/util/ush/overdate.bufr_dump_2.sh \
#          /com/gfs/prod/gdas.20000503/gdas1.t12z.satwnd.tm00.bufr_d  \
#          2003 02 22 21 \
#          /tmp/wd22dk/update_GDAS/2003022221.satwnd.tm00.bufr_d
#
#######################################################################


set -uax

if [ "$#" -ne '5' -a "$#" -ne '6' ]; then
   set +x
   echo
   echo " ===> Must have 5 or 6 positional parameters ..."
   echo
   echo "See "Usage" in Docblock for scripts $0 "
   echo
   exit 1
fi


if [ "${#2}" -ne '4' ]; then
   set +x
   echo
   echo " ===> Positional parameter 2 (year) must be 4-digits"
   echo
   echo "See "Usage" in Docblock for scripts $0 "
   echo
   exit 3
elif [ "${#3}" -ne '2' ]; then
   set +x
   echo
   echo " ===> Positional parameter 3 (month) must be 2-digits"
   echo
   echo "See "Usage" in Docblock for scripts $0 "
   echo
   exit 4
elif [ "${#4}" -ne '2' ]; then
   set +x
   echo
   echo " ===> Positional parameter 4 (day) must be 2-digits"
   echo
   echo "See "Usage" in Docblock for scripts $0 "
   echo
   exit 5
elif [ "${#5}" -ne '2' ]; then
   set +x
   echo
   echo " ===> Positional parameter 5 (hour) must be 2-digits"
   echo
   echo "See "Usage" in Docblock for scripts $0 "
   echo
   exit 6
fi


DATA=${DATA:-$TMPDIR/tmp$$}
UTILEXEC=${UTILEXEC:-/nwprod/util/exec}
mkdir -p $DATA
cd $DATA

bufrin=$1
cp $bufrin bufr_in

echo " $2 $3 $4 $5" > datecard
##-temp
cat datecard
##-temp

export XLFRTEOPTS="unit_vars=yes"
export XLFUNIT_20="bufr_in"
export XLFUNIT_53="bufr_out"
timex $UTILEXEC/ovrddump_2 < datecard
err=$?
set +x
echo
echo "The foreground status code for the running of OVRDDUMP_2 is $err"
echo
set -x

[ "$#" -eq '6' ]  &&  cp bufr_out $6

[ "$err" -ne '0' ]  &&  exit 3

exit 0
