#!/bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exdgex_prelim.sh.sms
# Script description:  Runs WRF Preprocessor System (WPS) to initialize the DGEX 
#
# Author:        Eric Rogers       Org: NP22         Date: 2006-04-21
#

set -x

msg="JOB $job FOR REGION=$REGION HAS BEGUN"
postmsg "$jlogfile" "$msg"

mod=ETA

export CYCLE=$PDY$cyc
echo "export REGION=$REGION" >> $COMOUT/${RUN}_${REGION}.t${cyc}z.envir.sh
echo "export CYCLE=$CYCLE" >> $COMOUT/${RUN}_${REGION}.t${cyc}z.envir.sh

if [ "$SENDSMS" = "YES" ] ; then
  $SMSBIN/smslabel NEST "$REGION"
fi

LENGTH=03
MODEL=nmm

export PS4='DGEX_PRELIM_T$SECONDS + '

cd $DATA

cp /com/date/t${cyc}z .

CYCer=`cat t${cyc}z | cut -c 7-16`
CYCstart=`/nwprod/util/exec/ndate -0 $CYCer`
CYCdgex=`/nwprod/util/exec/ndate 78 $CYCer`

echo DATEXX${CYCstart} > nmcdate
echo DATEXX${CYCdgex} > nmcdate.dgex

echo "export CYCLEdgex=`cat nmcdate.dgex | cut -c7-16`" >> $COMOUT/${RUN}_${REGION}.t${cyc}z.envir.sh
echo "export CYCLEnam=`cat nmcdate | cut -c7-16`" >> $COMOUT/${RUN}_${REGION}.t${cyc}z.envir.sh

#
# get sigma files from GFS for boundary conditions
#

$USHdgex/dgex_getgfsbc.sh $CYCstart 198

hr=078

while [ $hr -le 198 ] ; do

if [ ! -s gfsbc${hr}_ops ] ; then

# WRF-NMM boundary condition makes needs 3-hourly coefficient files, so if 
# one doesn't exist, make one using global_sigavg

let "hrm3=hr-3"
typeset -Z3 hrm3
let "hrp3=hr+3"
typeset -Z3 hrp3

/nwprod/exec/global_sigavg -w 0.5,0.5 gfsbc${hrm3}_ops gfsbc${hrp3}_ops gfsbc${hr}_ops

fi

let "hr=hr+3"
typeset -Z3 hr

done

icnt=0
hr=078
 
while [ $hr -le 198 ] ; do
  mv gfsbc${hr}_ops gfsbc${icnt}
  let "hr=hr+3"
  typeset -Z3 hr
  let "icnt=icnt+1"
done

#
# Release the mkbnd jobs on SMS
#
mkdir -p $DATA/mkbnd

$SMSBIN/setev release_mkbnd

msg="JOB $job FOR DGEX HAS COMPLETED NORMALLY"
postmsg "$jlogfile" "$msg"

exit

