#####################################################################
echo "------------------------------------------------"
echo "JAFPLT - early upper air afos plotfile          "
echo "------------------------------------------------"
echo "History: Feb 1998 - First implementation of this new script."
#####################################################################

cd $DATA

######################
# Set up Here Files.
######################


#####################################################################
#
# START FLOW OF CONTROL
#
# 1) Run PREPDATA
#
# 2) Run UPAPREP to put plain language observational data in flat.
#    Includes pressure tendency if 12 hour old data is
#    available.
# 3) Run UPAAFOS
#
# 4) Run AWPUPAIR to generate upper airplot files for AWIPS.
#
# 5) Run PLOTSEND to put communication headers on AWIP plotfiles.
#
#####################################################################

set -x

#####################################################################

if test $cycle = t00z
then
  prevcom=$COMINm1
  prevcycle=${PDYm1}12
else
  prevcom=$COMIN
  prevcycle=${PDY}00
fi

#  run prepro to get prepdata
#  --------------------------
set -xa

dump=bufr
bufrsh=/nwprod/ush
DUMP=${DUMP:-$bufrsh/dumpjb}

postmsg "$jlogfile" "ADPUPA dump has begun"
$DUMP ${PDY}${cyc} 3.25 adpupa
errdmp1=$?
postmsg "$jlogfile" "ADPUPA dump has completed with R.C. $errdmp1"

postmsg "$jlogfile" "AIRCFT dump has begun"
$DUMP ${PDY}${cyc} 3.25 aircft 
errdmp2=$?
postmsg "$jlogfile" "AIRCFT dump has completed with R.C. $errdmp2"

postmsg "$jlogfile" "AIRCAR dump has begun"
$DUMP ${PDY}${cyc} 1.5 aircar
errdmp3=$?
postmsg "$jlogfile" "AIRCAR dump has completed with R.C. $errdmp3"

postmsg "$jlogfile" "SATWND dump has begun"
$DUMP ${PDY}${cyc} 2.1 satwnd
errdmp4=$?
postmsg "$jlogfile" "SATWND dump has completed with R.C. $errdmp4"

cat adpupa.out >> $pgmout
cat aircft.out >> $pgmout
cat aircar.out >> $pgmout
cat satwnd.out >> $pgmout

# Compare AFWA ACARS vs. ARINC ACARS report counts in AIRCAR dump to see
#  which should be processed from the AIRCAR dump in PREPBUFR - normally there
#  are more ARINC ACARS (primary) and ONLY this type will be processed from
#  AIRCAR dump; on rare occasions the ARINC feed may go down and there are more
#  AFWA ACARS (backup) and then ONLY this type will be processed from AIRCAR
#  dump

if [ -s aircar.out ]; then
   count_004004=`grep -Fe "004.004  HAS " aircar.out | \
    awk -F" HAS" '{print$2}' | awk -F" REPORTS" '{print$1}'`
   [ -z "$count_004004" ]  &&  count_004004=0

   count_004007=`grep -Fe "004.007  HAS " aircar.out | \
    awk -F" HAS" '{print$2}' | awk -F" REPORTS" '{print$1}'`
   [ -z "$count_004007" ]  &&  count_004007=0

   if [ $count_004004 -ge $count_004007 ]; then
      msgp=""
      msg="process only message type 004.004 for AIRCAR data in PREPBUFR"
      acar_source=004004
   else
      msgp="***WARNING: ARINC ACARS dump count ($count_004004) < AFWA ACARS \
dump count ($count_004007)"
      postmsg "$jlogfile" "$msgp"
      msg="process only message type 004.007 for AIRCAR data in PREPBUFR"
      acar_source=004007
   fi
   set +x
   echo
   echo $msgp
   echo $msg
   echo
   set -x
fi

sh /nwprod/util/ush/getges.sh -e prod -r high -t sigges -v $PDY$cyc sges

cp /com/date/$cycle nmcdate
 
PREPDATA_ONLY=${PREPDATA_ONLY:-NO}
PRPX=${PRPX:-$EXECprep/prepobs_prepdata}
PRPC=${PRPC:-$PARMprep/prepobs_prepdata.afosplt.parm}
PRPT=${PRPT:-$FIXprep/prepobs_prep.bufrtable}

> prepdata.stdin
cat $PRPC > prepdata.stdin

# Check contents of acar_source switch - if it indicates that there were more
#  AFWA (backup) ACARS reports than ARINC (primary) ACARS reports in the AIRCAR
#  dump, then skip processing of ARINC ACARS messages in PREPOBS_PREPDATA
#  (meaning process ONLY AFWA ACARS messages); otherwise, as is usually the
#  case, skip processing of AFWA ACARS messages (meaning process only ARINC
#  ACARS messages in PREPOBS_PREPDATA)

echo "   SUBSKP(004,007) = TRUE," > insert
if [ $acar_source = 004007 ]; then
   echo "   SUBSKP(004,004) = TRUE," > insert
   msg="***WARNING: Dump count for ARINC ACARS < AFWA ACARS; encode backup \
AFWA ACARS into PREPBUFR"
   postmsg "$jlogfile" "$msg"
fi

grep -q -Fe `cat insert` prepdata.stdin
err_grep=$?
if [ $err_grep -ne 0 ]; then
   nlines=`cat < prepdata.stdin | wc -l`
   line=`grep -n -Fe "&LDTA" prepdata.stdin | cut -f1 -d:`
   head -n $line prepdata.stdin > top_part
   mlines=`expr $nlines - $line`
   tail -n $mlines prepdata.stdin > bottom_part
   [ $mlines -gt 2 ] && cat top_part insert bottom_part > prepdata.stdin
   rm top_part bottom_part
fi
rm insert

export pgm=`basename  $PRPX`
. prep_step

export XLFUNIT_11=nmcdate
export XLFUNIT_12=$PRPT
export XLFUNIT_15=$FIXprep/prepobs_landc
export XLFUNIT_21=adpupa.ibm
export XLFUNIT_22=aircar.ibm
export XLFUNIT_23=aircft.ibm
export XLFUNIT_24=satwnd.ibm
export XLFUNIT_51=prepda
export XLFUNIT_52=prevents.filtering.prepdata

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

startmsg
timex $PRPX < prepdata.stdin >> $pgmout 2> errfile
export err=$?
cat prevents.filtering.prepdata >>  $pgmout
set +x
echo
echo 'The foreground exit status for PREPOBS_PREPDATA is ' $err
echo
set -x
if test $err -le 4
then
  export err=0
fi
err_chk

[ $PREPDATA_ONLY = YES ]  &&  exit

export pgm=upaprep
. prep_step

echo ${PDY}${cyc} > upaprep.input
 
export XLFUNIT_9=$PARMgraph/graph_upaprep.ft9.control
export XLFUNIT_10=prepda

if test -r $prevcom/nam.$prevcycle.upaprep
then
  cp $prevcom/nam.$prevcycle.upaprep nam.$prevcycle.upaprep
  export XLFUNIT_11=nam.$prevcycle.upaprep
fi

export XLFUNIT_22=$PARMgraph/graph_upaprep.ft22.aircft
export XLFUNIT_23=$PARMgraph/graph_upaprep.ft23.satwnd
export XLFUNIT_24=$PARMgraph/graph_upaprep.ft24.tiros
export XLFUNIT_55=nam.${PDY}${cyc}.upaprep
export XLFUNIT_56=nam.${PDY}${cyc}.aircft
export XLFUNIT_57=nam.${PDY}${cyc}.satwnd
export XLFUNIT_58=nam.${PDY}${cyc}.satell
export XLFUNIT_75=LOGMSG

startmsg
$EXECgraph/upaprep < upaprep.input >> $pgmout 2> errfile
export err=$?;err_chk
#
# Save prepared observational file in com.  We will use it next cycle
# to calculate 12 hour pressure tendencies.
#
#----------------------------------------------------------------
# Now create North American upper air plot files for AFOS.
#----------------------------------------------------------------

export pgm=upaafos
. prep_step

AFOSPLOT=afos.early${cyc}.upa.$job
echo $cyc > upaafos.input

export XLFUNIT_17=$FIXgraph/graph_pillist1
export XLFUNIT_19=nam.${PDY}${cyc}.upaprep
export XLFUNIT_20=nam.${PDY}${cyc}.aircft
export XLFUNIT_21=nam.${PDY}${cyc}.satwnd
export XLFUNIT_24=$AFOSPLOT

startmsg
$EXECgraph/upaafos >> $pgmout < upaafos.input  2> errfile
export err=$?;err_chk

if test $SENDCOM = 'YES'
then
  cp $AFOSPLOT $pcom
  if test $SENDDBN = 'YES'
  then
      $utilities/snd2forgn AFOS nam $job $pcom/$AFOSPLOT
  fi
fi
#
#       NOW RUN THE awpupair FILE MAKER
#
export pgm=upawips
. prep_step

AWPLOT=awplot.early${cyc}.upa.$job

export XLFUNIT_17=$FIXgraph/graph_pillist1
export XLFUNIT_19=nam.${PDY}${cyc}.upaprep
export XLFUNIT_20=nam.${PDY}${cyc}.aircft
export XLFUNIT_21=nam.${PDY}${cyc}.satwnd
export XLFUNIT_24=$AWPLOT

startmsg
$EXECgraph/upawips >> $pgmout < upaafos.input  2> errfile
export err=$?; err_chk

if test $SENDCOM = 'YES'
then
  cp $AWPLOT $pcom
fi

export pgm=awpupair
. prep_step

export XLFUNIT_7=$PARMgraph/graph_awpupair.early.control  
export XLFUNIT_18=$AWPLOT
export XLFUNIT_51=awipplot.file
  
startmsg
$EXECgraph/awpupair PARM="P" >> $pgmout 2>errfile
export err=$?; err_chk

AWIPPLOT=awip.early${cyc}.upa
export pgm=plotsend
. prep_step

export XLFUNIT_12=$FIXgraph/graph_awiplist
export XLFUNIT_15=$PARMgraph/graph_awpsndal
export XLFUNIT_13=awipplot.file
export XLFUNIT_51=$AWIPPLOT

startmsg
$EXECgraph/plotsend >> $pgmout 2>errfile
export err=$?; err_chk


if test $SENDDBN = 'YES'
then
  cp $AWIPPLOT $pcom/$AWIPPLOT.$job
  $utilities/make_ntc_bull.pl plot WMO KWBC NONE $DATA/$AWIPPLOT $pcom/$AWIPPLOT.$job
fi

#####################################################################
# GOOD RUN
set +x
echo "**************JOB WWAFP${CYL} COMPLETED NORMALLY ON THE IBM"
echo "**************JOB WWAFP${CYL} COMPLETED NORMALLY ON THE IBM"
echo "**************JOB WWAFP${CYL} COMPLETED NORMALLY ON THE IBM"
set -x
#####################################################################

############## END OF SCRIPT #######################

