
# This script performs wind profiler 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
#   PQCX - path to PREPOBS_PROFCQC program executable
#   PQCC - path to PREPOBS_PROFCQC 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

rm $PRPI.profcqc
rm profcqc.monitor profcqc.events

pgm=`basename  $PQCX`
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_14=$PRPI
export XLFUNIT_51=$PRPI.profcqc
export XLFUNIT_52=profcqc.monitor1
export XLFUNIT_53=profcqc.monitor2
export XLFUNIT_54=profcqc.events1
export XLFUNIT_55=profcqc.events2
export XLFUNIT_61=profcqc.stats1
export XLFUNIT_62=profcqc.stats2
TIMEIT=""
[ -s $DATA/timex ] && TIMEIT=$DATA/timex
$TIMEIT $PQCX< $PQCC > outout 2> errfile
err=$?
###cat errfile
cat errfile >> outout
cat profcqc.events2 >> outout
cat outout >> profcqc.out
set +u
[ -n "$pgmout" ]  &&  cat outout >> $pgmout
set -u
rm outout
set +x
echo
echo 'The foreground exit status for PREPOBS_PROFCQC is ' $err
echo
set -x
if [ $err -eq 4 ]; then
   msg="PREPBUFR DATA SET CONTAINS NO "PROFLR" TABLE A MESSAGES  --> non-fatal"
set +u
   [ -n "$jlogfile" ] && postmsg "$jlogfile" "$msg"
set -u
   err=0
fi
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.profcqc $PRPI
fi

exit 0
