
#######################################################################
#
# D. Keyser - NP22
#
# This script changes the date in a BUFR data dump file.  All Section 1
#  and Section 4 (DATA) BUFR messages are updated.  In Section 4, the report
#  and receipt time in each subset is updated.  There are two different
#  methods for doing this:
#
#    1) 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.  All other
#       dates in the BUFR data dump file {dump date (BUFR message number 2
#       Section 1 date), all remaining BUFR message Section 1 dates, and all
#       BUFR message Section 4 (data) report and receipt date/times} are
#       updated by the same number of hours as was the center date.  This
#       ensures that all report dates in the updated file remain the same
#       relative to each other, to the center date, and to the message dates.
#       The script then writes the updated BUFR data dump file to a specified
#       output location.
#              -- or --
#    2) Given the specified input BUFR data dump file, this script will
#       update ALL dates (in Section 1 and in Section 4 of each BUFR message)
#       by EXACTLY 2 or 3 years (the 2 or 3 determined from imported
#       positional parameter) and write the updated BUFR data dump file to a
#       specified output location.
#
#    In either case, 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.  The decision to perform either 1) or 2)
#     above is determined by the number of postional parameters imported by
#     this script (more to follow on this).
#
#   NOTE: In 1) above, for a particular subset, up to 100 "levels" of report
#         time and receipt time can be updated.  However, any occurrences of
#         report time or receipt time beyond the first group of "levels"
#         will not be updated.  In 2) above, for a particular subset, all
#         occurences of year are updated (report and receipt).  (Normally,
#         one would expect only one occurrence of report time in a subset in
#         the BUFR data dump files, i.e., appearing once and with only one
#         "level".)
#
#   IMPORTANT!!!!! - While 2) above will also work fine for a BUFR data tank,
#                    1) above will not because there is no center date
#                    messsage (number 1) in a tank and also, there may be
#                    more than one set of report/receipt date "levels" in a
#                    single subset in a tank.
#
#
# Usage:
#
#   The decision to choose to Case 1 or 2 above is based on the number of
#    positional parameters imported.
#
#   ===> If 6 positional parameters are imported, case 1 is chosen:
#
#      sh /nwprod/util/scripts/overdate.bufr_dump.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 ).
#
#   ===> If 3 positional parameters are imported, case 2 is chosen:
#
#      sh /nwprod/util/scripts/overdate.bufr_dump..sh \
#       <Input BUFR data dump file (full directory path)> \
#       <Number of years to update (must be 2 or 3)> \
#       <Output BUFR data dump file (full directory path)> \
#
#
# Examples:
#
#     1) I want to update an input ADPUPA BUFR data dump file by exactly
#        three years ...
#         sh /nwprod/util/scripts/overdate.bufr_dump.sh \
#          /com/nam/prod/erl.19981019/nam.t00z.adpupa.tm00.bufr_d  3 \
#          /tmp/wd22dk/update_ERL/2001101900.adpupa.tm00.bufr_d
#
#     2) I want to update an input ADPSFC BUFR data dump file by exactly
#        two years ...
#         sh /nwprod/util/scripts/overdate.bufr_dump.sh \
#          /com/gfs/prod/gfs.19981019/gfs.t06z.adpsfc.tm00.bufr_d  2 \
#          /tmp/wd22dk/update_GFS/2000101906.adpsfc.tm00.bufr_d
#
#     3) I want to update an input SATWND BUFR data dump file to contain
#        the center date 2100 UTC February 22, 2003 (all other dates
#        in the output file will be changed by the same number of hours as
#        the center date) ...
#         sh /nwprod/util/scripts/overdate.bufr_dump.sh \
#          /com/gfs/prod/gdas.19981019/gdas1.t12z.satwnd.tm00.bufr_d  \
#          2003 02 22 21 \
#          /tmp/wd22dk/update_GDAS/2003022221.satwnd.tm00.bufr_d
#
#######################################################################


set -uax

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


if [ "$#" -eq '3' ]; then
   if [ "$2" -ne '2' -a "$2" -ne '3' ]; then
      set +x
      echo
      echo " ===> Positional parameter 2 must be 2 or 3"
      echo
      echo "See "Usage" in Docblock for scripts $0 "
      echo
      exit 2
   fi
fi

if [ "$#" -eq '6' ]; then
   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
   fi
   if [ "${#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
   fi
   if [ "${#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
   fi
   if [ "${#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
fi


DATA=${DATA:-/ptmp/tmp$$}
mkdir -p $DATA
cd $DATA

bufrin=$1
cp $bufrin bufr_in

# bufrou2 - rewrites current year + 2
# bufrou3 - rewrites current year + 3
# bufrouU - rewrites based on user-designated date

bufrou2=/dev/null
bufrou3=/dev/null
bufrouU=/dev/null

if [ "$#" -eq '3' ]; then
   cp /dev/null datecard
   if [ "$2" -eq '2' ]; then
      bufrou2=$3
   else
      bufrou3=$3
   fi
else
   echo " $2 $3 $4 $5" > datecard
##-temp
   cat datecard
##-temp
   bufrouU=$6
fi

export XLFRTEOPTS="unit_vars=yes"
export XLFUNIT_20="bufr_in"
export XLFUNIT_50="$bufrou2"
export XLFUNIT_51="$bufrou3"
export XLFUNIT_53="$bufrouU"
/nwprod/util/exec/ovrddump < datecard
err=$?
set +x
echo
echo "The foreground status code for the running of OVRDDUMP is $err"
echo
set -x

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

exit 0
