#! /bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exdgex_combc.sh.sms
# Script description:  Runs code to combine binary WRF-NMM boundary files from GDAS/GFS
#                      into a single WRF-format boundary file. This needs to run as a
#                      parallel code.
#
# Author:        Eric Rogers       Org: NP22         Date: 2006-01-13
#
# Script history log:
# 2006-04-21  Eric Rogers
#

set -xa

export PS4='$SECONDS + '

cd $DATA

#
# Get needed variables from exdgex_prelim.sh.sms
#
. $COMOUT/${RUN}_${REGION}.t${cyc}z.envir.sh

export MODEL=nmm
export ERRSCRIPT=${ERRSCRIPT:-'eval [[ $err = 0 ]]'}

echo $CYCLEdgex > nmcdate.dgexbc
echo $CYCLEnam > nmcdate.bc

SDATE=`cut -c 1-10 nmcdate.dgexbc`
CDATE=`cut -c 1-10 nmcdate.bc`

echo SDATE $SDATE

echo $CDATE > cycledate.bc
CYC=`cut -c 9-10 cycledate.bc`

### modify namelist file

export restval=false

LENGTH=114

 rhour=$LENGTH
 resint=-1
 rday=0

 ystart=`echo $SDATE | cut -c1-4`
 mstart=`echo $SDATE | cut -c5-6`
 dstart=`echo $SDATE | cut -c7-8`
 hstart=`echo $SDATE | cut -c9-10`

end=$(/nwprod/util/exec/ndate +$LENGTH $SDATE)

yend=`echo $end | cut -c1-4`
mend=`echo $end | cut -c5-6`
dend=`echo $end | cut -c7-8`
hend=`echo $end | cut -c9-10`

NLNAME=dgex_${REGION}_wrfnmm_namelist.input.in_mkbnd

cat $PARMdgex/$NLNAME | sed s:YSTART:$ystart: | sed s:MSTART:$mstart: \
 | sed s:DSTART:$dstart: | sed s:HSTART:$hstart: | sed s:YEND:$yend: \
 | sed s:MEND:$mend:     | sed s:DEND:$dend: | sed s:HEND:$hend:  \
 | sed s:RH:$rhour:      | sed s:RD:$rday: \
 | sed s:RESINT:$resint: | sed s:REST:$restval: > namelist.input

args="0 1 2 3 4 5 6 7 8 9 10 11 12"

#### wait for completion of mkbnd jobs

for nm in $args
do
echo checking $nm
while [ ! -s $DATA/mkbnddone.$nm ]
do
echo still not done $nm
sleep 10
done
done

# Check on existence of wrfbdy file from real job, wait until it is done

while [ ! -s $COMOUT/dgex_${REGION}.t${CYC}z.wrfbdy_nam78h.bin ]
do
echo real wrfbdy file not done
sleep 10
done

cp $COMOUT/dgex_${REGION}.t${CYC}z.wrfbdy_nam78h.bin wrfbdy_nam78h.bin

cp $PARMdgex/nam_combc_gribmap.txt .

export pgm=dgex_combc_${REGION}
. prep_step

export XLFUNIT_11=dgexbcs00_0
export XLFUNIT_12=dgexbcs00_1
export XLFUNIT_13=dgexbcs00_2
export XLFUNIT_14=dgexbcs00_3
export XLFUNIT_15=dgexbcs00_4
export XLFUNIT_16=dgexbcs00_5
export XLFUNIT_17=dgexbcs00_6
export XLFUNIT_18=dgexbcs00_7
export XLFUNIT_19=dgexbcs00_8
export XLFUNIT_20=dgexbcs00_9
export XLFUNIT_21=dgexbcs00_10
export XLFUNIT_22=dgexbcs00_11
export XLFUNIT_23=dgexbcs00_12
export XLFUNIT_51=dgexbcs00
export XLFUNIT_99=wrfbdy_nam78h.bin

echo "&NUMAVN NTIMES=39,NPIECE=3" > tmpin
echo "/" >> tmpin

startmsg
$EXECdgex/dgex_combc_${REGION} < tmpin >> $pgmout
export err=$?;err_chk

cat rsl.error.0000 rsl.out.0000 > $COMOUT/dgex_${REGION}.t${CYC}z.wrfmkbndlog.${SDATE}

mv wrfbdy_d01 $COMOUT/dgex_${REGION}.t${CYC}z.wrfbdy_d01

exit

