
# This script encodes the analysis into the PREPBUFR reports
#  (interpolated to obs. locations)
#
# It is normally executed by the script ????????????????????.sh
#  but can also be executed from a checkout parent script
# -------------------------------------------------------------

set -aux

qid=$$

# Positional parameters passed in:
#   1 - path to COPY OF input prepbufr file --> becomes output prepbufr
#       file upon successful completion of this script (note that input
#       prepbufr file is NOT saved by this script)
#   2 - expected center date in PREPBUFR file (YYYYMMDDHH)

# Imported variables that must be passed in:
#   DATA  - path to working directory
#   SANL  - path to COPY OF global simga analysis file 1 (valid at either
#            center date of PREPBUFR file or nearest cycle time prior to
#            center date of PREPBUFR file which is a multiple of 3)
#            (Note: Right now, this file will be an analysis only if
#                   the valid cycle time is 00, 06, 12 or 18, otherwise it will
#                    be a 3-hour forecast valid at this time)
#   SANLA - path to COPY OF global simga analysis file 2 (either null if SANL
#            is valid at center date of PREPBUFR file or valid at nearest
#            cycle time after center date of PREPBUFR file which is a multiple
#            of 3 if SANL is valid at nearest cycle time prior to center date
#            of PREPBUFR file which is a multiple of 3)
#            (Note: Right now, this file will be an analysis only if
#                   the valid cycle time is 00, 06, 12 or 18, otherwise it will
#                    be a 3-hour forecast valid at this time)
#   PSTX  - path to GLOBAL_POSTEVENTS program executable

# Imported variables that can be passed in:
#   pgmout   - string indicating path to for standard output file (skipped
#              over by this script if not passed in)

cd $DATA
PRPI=$1
if [ ! -s $PRPI ] ; then exit 1 ;fi
CDATE10=$2

rm $PRPI.postevents

pgm=`basename  $PSTX`
if [ -s $DATA/prep_step ]; then
   . $DATA/prep_step
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=$PRPI
#####export XLFUNIT_12=$SANL
#####export XLFUNIT_13=$SANLA

# The GLOBAL_POSTEVENTS code will soon, or may now, open GFS spectral
# coefficient analysis files using sigio routines (via W3LIB rouitne
# GBLEVENTS) via explicit open(unit=number,file=filename) statements.  This
# conflicts with the XLFUNIT statements above.  One can either remove the
# explicit open statements in the code or replace the above XLFUNIT lines with
# soft links.  The soft link approach is taken below.

ln -sf $SANL              fort.12
ln -sf $SANLA             fort.13

export XLFUNIT_15=cdate10.dat
export XLFUNIT_51=$PRPI.postevents

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

#This MAY cause a failure
####XLSMPOPTS=parthds=2:stack=64000000
#This works!
XLSMPOPTS=parthds=2:stack=20000000

timex $PSTX < /dev/null > outout  2> errfile
err=$?
###cat errfile
cat errfile >> outout
cat outout >> postevents.out
set +u
[ -n "$pgmout" ]  &&  cat outout >> $pgmout
set -u
rm outout
set +x
echo
echo 'The foreground exit status for GLOBAL_POSTEVENTS is ' $err
echo
set -x
if [ -s $DATA/err_chk ]; then
   $DATA/err_chk
else
   if test "$err" -gt '0'
   then
######kill -9 ${qid}
      exit 555
   fi
fi

if [ "$err" -gt '0' ]; then
   exit 9
else
   mv $PRPI.postevents $PRPI
fi

exit 0
