
#######################################################################
#
# D. Keyser - NP22
#
# This script changes the date in all Section 1 BUFR messages in
#  a PREPBUFR file.  There are two different methods for doing this:
#
#    1) Given the specified input PREPBUFR file, this script will change
#       each of its Section 1 dates (year, month, day, and hour) to be a
#       specified new date (year, month, day, and hour) determined from
#       imported positional parameters.  This new date will be written
#       to each Section 1 BUFR message in the specified output PREPBUFR
#       file.  Note that all BUFR messages in the output file will contain
#       the same Section 1 date in this case.
#              -- or --
#    2) Given the specified input PREPBUFR file, this script will update
#       each of its Section 1 dates by EXACTLY 2 or 3 years (the 2 or 3
#       determined from imported positional parameter).  This new date
#       will be written to each Section 1 BUFR message in the specified
#       output PREPBUFR file.  Note that each BUFR message in the output
#       file will contain the same date as the corresponding message in
#       the input PREPBUFR file, except it will be updated by 2 or 3
#       years.
#
#    In each case, the output PREPBUFR file is otherwise identical to
#     the input PREPBUFR file, with the exception that the output
#     PREPBUFR file is always y2k compliant (century indicator is stored
#     in formerly reserved byte 18 of Section 1) while the input PREPBUFR
#     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).
#
#
# 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.prepbufr.sh \
#       <Input PREPBUFR file (full directory path)> \
#       <Year (UTC)  for output PREPBUFR file (must always be 4-digits)> \
#       <Month (UTC) for output PREPBUFR file (must always be 2-digits)> \
#       <Day (UTC)   for output PREPBUFR file (must always be 2-digits)> \
#       <Hour (UTC)  for output PREPBUFR file (must always be 2-digits)> \
#       <Output PREPBUFR 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.prepbufr.sh \
#       <Input PREPBUFR file (full directory path)> \
#       <Number of years to update (must be 2 or 3)> \
#       <Output PREPBUFR file (full directory path)> \
#
#
# Examples:
#
#     1) I want to update an input prepbufr file by exactly three
#        years ...
#         sh /nwprod/util/scripts/overdate.prepbufr.sh \
#          /com/nam/prod/erl.19980807/nam.t00z.prepbufr.tm00  3 \
#          /tmp/wd22dk/update_ERL.prepda/2001080700.prepda.tm00 
#
#     2) I want to update an input prepbufr file by exactly two
#        years ...
#         sh /nwprod/util/scripts/overdate.prepbufr.sh \
#          /com/gfs/prod/gfs.19980806/gfs.t06z.prepbufr  2 \
#          /tmp/wd22dk/update_GFS.prepda/2000080606.prepda.tm00 
#
#     3) I want to change the date of an input prepbufr file to
#        contain the date 2100 UTC March 4, 2003 ...
#         sh /nwprod/util/scripts/overdate.prepbufr.sh \
#          /com/gfs/prod/gdas.19980807/gdas1.t12z.prepbufr  \
#          2003 03 04 21 \
#          /tmp/wd22dk/update_GDAS.prepda/2003030421.prepda.tm00 
#
#######################################################################


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=/ptmp/tmp$$
mkdir -p $DATA
cd $DATA

prepin=$1

# prepou0 - rewrites current year
# prepou2 - rewrites current year + 2
# prepou3 - rewrites current year + 3
# prepouU - rewrites user-designated date

prepou0=/dev/null
prepou2=/dev/null
prepou3=/dev/null
prepouU=/dev/null

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

export XLFRTEOPTS="unit_vars=yes:xrf_messages=no"
export XLFUNIT_20="$prepin"
export XLFUNIT_50="$prepou0"
export XLFUNIT_51="$prepou2"
export XLFUNIT_52="$prepou3"
export XLFUNIT_53="$prepouU"
/nwprod/util/exec/ovrdprep < datecard
err=$?
set +x
echo
echo "The foreground status code for the running of OVRDPREP is $err"
echo
set -x

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

exit 0
