#!/bin/sh
#
# excdccdas.sh.sms
#
# This is the main driver script for the early CDAS model.
# It runs the file prep, analysis (ssi), forecast (mrf),
# and post processing of the model.
#
#

#####################################################################
echo
echo "------------------------------------------------"
echo "excdccdas.sh.sms - CDAS analysis and forcast"
echo "------------------------------------------------"
echo "History: Jul 20 2000 - Original script."
echo
#####################################################################

set -x
date
cd $DATA

msg="HAS BEGUN on `hostname`"
postmsg "$jlogfile" "$msg"

cat break > $pgmout

msg="CYCLE TIME FOR CDC CDAS ANALYSIS IS $PDY$cyc"
postmsg "$jlogfile" "$msg"


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


#                                    CYCLE files

# input files
export snow=${RUN}.t${cyc}z.snogrb
export sst=${RUN}.t${cyc}z.sstgrb
export ice=${RUN}.t${cyc}z.engicegrb
cp $COMIN/$snow $snow
cp $COMIN/$sst $sst
cp $COMIN/$ice $ice

# output file
export sfcanl=${RUN}.t${cyc}z.sfcanl


#                                    SSI files

# input files
export sges=${RUN}.t${cycm6hr}z.sf06
export bges=${RUN}.t${cycm6hr}z.bf06
export oldsig=${RUN}.t${cycm6hr}z.sanl
cp $COMINm6hr/$sges $sges
cp $COMINm6hr/$bges $bges
cp $COMINm6hr/$oldsig $oldsig

export prepqm=${RUN}.t${cyc}z.prepbufr
cp $COMIN/$prepqm $prepqm

# output files
export sanl=cdas.t${cyc}z.sanl

#                                    MODEL files

# output files
export newsges=${RUN}.t${cyc}z.sf06
export newbges=${RUN}.t${cyc}z.bf06
export newflx=${RUN}.t${cyc}z.sfluxgrbf06
export newdg3=${RUN}.t${cyc}z.dg3f06
export newznl00=${RUN}.t${cyc}z.znlf00
export newznl06=${RUN}.t${cyc}z.znlf06
export pgbf00=${RUN}.t${cyc}z.pgrbf00
export pgbf06=${RUN}.t${cyc}z.pgrbf06


########################## CYCLE ##########################

# check bges for right date code
$USHcdas/cdas_sigmatime.sh $bges $PDY$cyc


# run cycle
$USHcdas/cdas_cycle.sh $bges $sfcanl $ice $snow $sst $PDY$cyc
export err=$?; export pgm=${RUN}_cycle.sh; err_chk

########################### SSI ###########################

# check files for right date code

$USHcdas/cdas_sigmatime.sh $sges $PDY$cyc

$USHcdas/cdas_sigmatime.sh $oldsig $PDYm6hr$cycm6hr

export pgm=cdas_prepdate; . prep_step
export XLFUNIT_11="$prepqm"
export XLFUNIT_51=prepdate.out
startmsg
timex $EXECcdas/cdas_prepdate >> $pgmout 2> errfile
export err=$?; err_chk
cat errfile >> $pgmout
chkdate=`cat prepdate.out`
if [ "$chkdate" -ne "$PDY$cyc" ] ; then
   echo "bufr file has wrong date"
   err_exit "bufr file has wrong date"
fi


$USHcdas/cdas_ssi.sh

#cp SSI output to COM
cp $sanl $COMOUT/$sanl
cp $sfcanl $COMOUT/$sfcanl

########################### run model ###########################

endhour=6
$USHcdas/cdas_model.sh $sanl $sfcanl $endhour

# save output in COMOUT

cp sigft.$PDY${cyc}ft6 $COMOUT/$newsges
mv sigft.$PDY${cyc}ft6 $newsges
cp bges.$PDY${cyc}ft6 $COMOUT/$newbges
mv bges.$PDY${cyc}ft6 $newbges
cp flx.$PDY${cyc}ft6 $COMOUT/$newflx
mv flx.$PDY${cyc}ft6 $newflx

# GOOD RUN
set +x
echo " "
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " "
set -x


# save standard output
cat break $pgmout break > allout
cat allout

sleep 10

msg='ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"

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

