#!/bin/sh

#
set -xa

date
###### 02/02/2004 ####################################################
echo "------------------------------------------------"
echo "XXXX -  mkrpt2gempak.sh Processing"
echo "------------------------------------------------"
echo "This script takes a model prepbufr file and its"
echo "corresponding Jack Woollen preptable and converts "
echo "it to a gempak sounding or surface file depending"
echo "on the data type for any model ruc, nam, avn (gfs),"
echo "gdas. The user has to provide the input table with
echo "the list of prepbufr parameters together with the
echo "gempak equivalent and a packing parameter. "
echo "To run sh mkrpt2gempak.sh $NET $cyc          "  
echo "Example:  sh mkrpt2gempak.sh gfs 12          "
echo "Author: V. Krishna Kumar NCO/Data Management & Quality Assessment Branch"
echo "History: MAR 2004 - First implementation of this new script."
#####################################################################

cd $DATA


##############################
# Set up the UTILITIES
##############################

pdyhh=${PDY}${cyc}

PREPIN=prepbufr.${RUN}

export XLFRTEOPTS="unit_vars=yes"     # Allow overriding default names.

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


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

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

export pgm=upaprep
. prep_step

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


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

###################################################################
# This step will read the observation which is prepared by previous
#  step and invoke thinpap to generate tape55 for next step.
#  Execute THINPAP
###################################################################

export pgm=thinpap
. prep_step


export XLFUNIT_15=$DATACARD
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"
export XLFUNIT_88="thinlist.$MAP"

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

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

export XLFUNIT_12="$DATA/thinlist.$MAP"


$GEMEXE/bfr2gp << EOF10 >> record
SNEFIL  = $PREPIN|preptable
SFFSRC  = adpupa|cat=0,1,2,3,4,5
SNOUTF  = $DATA/${RUN}.${MAP}.adpupa.${pdyhh}.snd
SFOUTF  =
SNPRMF  = adpupa.mass.wind.tbl
SFPRMF  = anysnd_sfc.tbl
AREA    =
DATTIM  =
TIMSTN  = 13/3999
r

EOF10

$GEMEXE/bfr2gp << EOF11 >> record
SNEFIL  = $PREPIN|preptable         
SFFSRC  = thncft        
SNOUTF  =
SFOUTF  = $DATA/aircft_reports
SNPRMF  =
SFPRMF  = aircft_wind.tbl
AREA    = $LAREA  
DATTIM  = all
TIMSTN  = 3999
r

EOF11

$GEMEXE/bfr2gp << EOF12 >> record
SNEFIL  = $PREPIN|preptable         
SFFSRC  = thnwnd        
SNOUTF  =
SFOUTF  = $DATA/satwnd_reports
SNPRMF  =
SFPRMF  = satwnd_wind.tbl
AREA    = $LAREA  
DATTIM  = all
TIMSTN  = 3999
r

EOF12

exit
