
# This script encodes the background (first guess) and observational
#  errors into the PREPBUFR reports (interpolated to obs. locations)
#
# It is normally executed by the script prepobs_makeprepbufr.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
#   NET   - string indicating system network (either "gfs", "gdas", "cdas",
#            "cdc", "nam", "ruc" or "rtma")
#            NOTE1: NET is changed to gdas in the parent Job script for the
#                   RUN=gdas1 (was gfs - NET remains gfs for RUN=gfs).
#            NOTE2: This is read from the program PREPOBS_PREVENTS via a call
#                   to system routine "GETENV".
#   SGES  - path to COPY OF global simga first guess 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)
#   SGESA - path to COPY OF global simga first guess file 2 (either
#            null if SGES 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 SGES is valid at nearest cycle time
#            prior to center date of PREPBUFR file which is a multiple of 3)
#   PRVT  - path to observation error table file
#   PREX  - path to PREPOBS_PREVENTS program executable
#   PREC  - path to PREPOBS_PREVENTS program parm cards

# 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.prevents
rm prevents.filtering

pgm=`basename  $PREX`
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=$SGES
#####export XLFUNIT_13=$SGESA

# The PREPOBS_PREVENTS code will soon, or may now, open GFS spectral
# coefficient guess 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 $SGES              fort.12
ln -sf $SGESA             fort.13

export XLFUNIT_14=$PRVT
export XLFUNIT_15=cdate10.dat
export XLFUNIT_51=$PRPI.prevents
export XLFUNIT_52=prevents.filtering

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

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

TIMEIT=""
[ -s $DATA/timex ] && TIMEIT=$DATA/timex
$TIMEIT $PREX < $PREC > outout  2> errfile
err=$?
###cat errfile
cat errfile >> outout
cat prevents.filtering >> outout
cat outout >> prevents.out
set +u
[ -n "$pgmout" ]  &&  cat outout >> $pgmout
set -u
rm outout
set +x
echo
echo 'The foreground exit status for PREPOBS_PREVENTS 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.prevents $PRPI
fi

exit 0
