
# This script performs rawinsonde upper-air complex quality control checking
#
# 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)

# Imported variables that must be passed in:
#   DATA - path to working directory
#   CQCS - path to PREPOBS_CQCBUFR program statbge file
#   CQCX - path to PREPOBS_CQCBUFR program executable
#   CQCC - path to PREPOBS_CQCBUFR 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)
#   PRPI_m24 - string indicating path to prepbufr file valid 24-hours previous
#              (only needed if temporal checking is being done)
#              (skipped over by this script if not passed in)
#   PRPI_m12 - string indicating path to prepbufr file valid 12-hours previous
#              (only needed if temporal checking is being done)
#              (skipped over by this script if not passed in)
#   PRPI_p12 - string indicating path to prepbufr file valid 12-hours ahead
#              (only needed if temporal checking is being done)
#              (skipped over by this script if not passed in)
#   PRPI_p24 - string indicating path to prepbufr file valid 24-hours ahead
#              (only needed if temporal checking is being done)
#              (skipped over by this script if not passed in)

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

cp /dev/null $DATA/prepbufr_m24
cp /dev/null $DATA/prepbufr_m12
cp /dev/null $DATA/prepbufr_p12
cp /dev/null $DATA/prepbufr_p24

set +u
[ -n "$PRPI_m24" ]  && cp $PRPI_m24 prepbufr_m24
[ -n "$PRPI_m12" ]  && cp $PRPI_m12 prepbufr_m12
[ -n "$PRPI_p12" ]  && cp $PRPI_p12 prepbufr_p12
[ -n "$PRPI_p24" ]  && cp $PRPI_p24 prepbufr_p24
set -u

rm $PRPI.cqcbufr
rm cqc_events cqc_stncnt cqc_stnlst

pgm=`basename  $CQCX`
if [ -s prep_step ]; then
   . 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

export XLFUNIT_4=cqcbufr.unit04.wrk
export XLFUNIT_12=cqc_events
export XLFUNIT_14=$PRPI
export XLFUNIT_15=cqc_stncnt
export XLFUNIT_16=cqc_stnlst
export XLFUNIT_17=prepbufr_m24
export XLFUNIT_18=prepbufr_m12
export XLFUNIT_19=prepbufr_p12
export XLFUNIT_20=prepbufr_p24
export XLFUNIT_22=cqc_wndpbm
export XLFUNIT_23=$CQCS
export XLFUNIT_51=$PRPI.cqcbufr
export XLFUNIT_52=cqc_sdm
export XLFUNIT_60=cqcbufr.unit60.wrk
export XLFUNIT_61=cqcbufr.unit61.wrk
export XLFUNIT_62=cqcbufr.unit62.wrk
export XLFUNIT_64=cqcbufr.unit64.wrk
export XLFUNIT_68=cqc_radcor
export XLFUNIT_80=cqcbufr.unit80.wrk
TIMEIT=""
[ -s $DATA/timex ] && TIMEIT=$DATA/timex
$TIMEIT $CQCX< $CQCC > outout 2> errfile
err=$?
###cat errfile
cat errfile >> outout
cat outout >> cqcbufr.out
set +u
[ -n "$pgmout" ]  &&  cat outout >> $pgmout
set -u
rm outout
set +x
echo
echo 'The foreground exit status for PREPOBS_CQCBUFR is ' $err
echo
set -x
if [ -s err_chk ]; then
   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.cqcbufr $PRPI
fi

exit 0
