#!/bin/sh

############################################################
# Utility Script plotvpap.sh to generate the Tape Lable file
############################################################

type=$1
parm=$2
region=$3

cd $DATA

########################################
set -x
msg="HAS BEGUN!"
postmsg "$jlogfile" "$msg"
########################################

if test $cycle = 't00z'
then
 comprev=$COMINm1
 pdy=$PDYm1
 prevcycle="12"
else
 comprev=$COMIN
 prevcycle="00"
 pdy=$PDY
fi

############################################################################
# Execute the PREPUPA 
############################################################################

cp $PARMgraph/graph_upaprep.ft22.aircft upaprep.ft22.aircft
cp $PARMgraph/graph_upaprep.ft23.satwnd upaprep.ft23.satwnd
cp $PARMgraph/graph_upaprep.ft24.tiros upaprep.ft24.tiros
cp $PARMgraph/graph_upaprep.ft9.${region}control upaprep.ft9.control

if [ $model = "nam" ]
then
   cp $COMIN/${RUN}.${cycle}.prepbufr.tm00 ${RUN}.${cycle}.prepbufr
else
   cp $COMIN/${RUN}.${cycle}.prepbufr ${RUN}.${cycle}.prepbufr
fi

echo ${PDY}${cyc} > upaprep.input

export pgm=upaprep
. prep_step

export XLFUNIT_9="upaprep.ft9.control"
export XLFUNIT_10="${RUN}.$cycle.prepbufr"

if test -r $comprev/${RUN}.$pdy$prevcycle.upaprep
then
 cp $comprev/${RUN}.$pdy$prevcycle.upaprep ${RUN}.$pdy$prevcycle.upaprep
 export XLFUNIT_11="${RUN}.$pdy$prevcycle.upaprep"
fi

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

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

if test "$SENDCOM" = 'YES'
then
   cp ${RUN}.${PDY}${cyc}.upaprep $COMOUT/.
fi

###################################################################
# This step will read the observation which is prepared by previous
#  step and invoke plotpap to generate tape55 for next step.
###################################################################
#  Execute PLOTVPAP
###################################################################
cp $PARMgraph/graph_plotvpap.${type}.${model}${region} plotvpap.${type}.${model}
cp $PARMgraph/graph_plotvpap.plotmlty.ft26 plotvpap.plotmlty.ft26
  
export pgm=plotvpap
. prep_step

export XLFUNIT_15="plotvpap.${type}.${model}"
export XLFUNIT_26="graph_plotvpap.plotmlty.ft26"
export XLFUNIT_41="${RUN}.${PDY}${cyc}.upaprep"
export XLFUNIT_42="${RUN}.${PDY}${cyc}.aircft"
export XLFUNIT_43="${RUN}.${PDY}${cyc}.satwnd"
export XLFUNIT_44="${RUN}.${PDY}${cyc}.satell"
export XLFUNIT_55="OLDlabel55"
export XLFUNIT_60="label60"
export XLFUNIT_61="label61"
export XLFUNIT_62="label62"
export XLFUNIT_63="label63"
export XLFUNIT_75="LOGMSG"

startmsg
if [ $RUN = "nam" ]
then 
   plotparm='NUMF=07,CARD=ON,AFOS=ON,SEND=ON,TSW1=ON,TSW2=ON,MARG=ON'
   $EXECgraph/plotvpap parm=$plotparm>> $pgmout 2> errfile
else
   $EXECgraph/plotvpap >> $pgmout 2> errfile
fi
export err=$?;err_chk

msg="Leaving plotvpap.sh"
postmsg "$jlogfile" "$msg"

exit

