#!/bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exdgex_prep.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"

LENGTH=03
MODEL=nmm

export PS4='DGEX_PREP_T$SECONDS + '

cd $DATA

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

mkdir -p $DATA/ungrib
mkdir -p $DATA/metgrid
mkdir -p $DATA/nemsinterp

echo DATEXX${CYCLE} > 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

cd $DATA/metgrid
rm met*.dio
rm FILE*

cd $DATA/nemsinterp
rm test*d01
rm boco*
rm GWD.bin

cd $DATA/ungrib
sh /nwprod/util/ush/setup.sh
rm GRIBFILE.*
rm FILE*

cp $DATA/nmcdate .
cp $DATA/nmcdate.dgex .

PDY=`cat nmcdate | cut -c7-14`
yy=`cat nmcdate | cut -c7-10`
mm=`cat nmcdate | cut -c11-12`
dd=`cat nmcdate | cut -c13-14`
cyc=`cat nmcdate | cut -c15-16`

PDYdgex=`cat nmcdate.dgex | cut -c7-14`
yydgex=`cat nmcdate.dgex | cut -c7-10`
mmdgex=`cat nmcdate.dgex | cut -c11-12`
dddgex=`cat nmcdate.dgex | cut -c13-14`
cycdgex=`cat nmcdate.dgex | cut -c15-16`

###chmod 755 envir.sh

whr=78
count=0
last=81
incr=3

PDY=`cat nmcdate | cut -c7-14`
yy=`cat nmcdate| cut -c7-10`
mm=`cat nmcdate | cut -c11-12`
dd=`cat nmcdate | cut -c13-14`
cyc=`cat nmcdate | cut -c15-16`

ystart=`cat nmcdate.dgex | cut -c7-10`
mstart=`cat nmcdate.dgex | cut -c11-12`
dstart=`cat nmcdate.dgex | cut -c13-14`
hstart=`cat nmcdate.dgex | cut -c15-16`

end=`/nwprod/util/exec/ndate +03 $CYCdgex`

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

cat $PARMdgex/dgex_${REGION}_namelist.nps | 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: > namelist.nps

DATE=`echo $start | cut -c1-8`

rm Vtable

if [ $REGION = conus ] ; then
type=awip32
else
type=awipak
fi

cp $PARMdgex/dgex_Vtable.NAM Vtable

hrs="78 81"

for fhr in $hrs
do
  cp $COMNAM/nam.t${cyc}z.${type}${fhr}.tm00 .
done

ln -sf nam.t${cyc}z.${type}78.tm00 GRIBFILE.AAA
ln -sf nam.t${cyc}z.${type}81.tm00 GRIBFILE.AAB

export pgm=dgex_ungrib
. prep_step
                                                                                
startmsg
$EXECdgex/dgex_ungrib >> $pgmout 2>errfile
export err=$?;err_chk

mv errfile $DATA/errfile.ungrib
                                                                                
### run metgrid
                                                                                
cd $DATA/metgrid
sh /nwprod/util/ush/setup.sh

cp $DATA/nmcdate .
cp $DATA/nmcdate.dgex .

### modify namelist file
ystart=`cat nmcdate.dgex | cut -c7-10`
mstart=`cat nmcdate.dgex | cut -c11-12`
dstart=`cat nmcdate.dgex | cut -c13-14`
hstart=`cat nmcdate.dgex | cut -c15-16`

end=`/nwprod/util/exec/ndate +03 $CYCdgex`

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

cat $PARMdgex/dgex_${REGION}_namelist.nps | 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: > namelist.nps

STARTHR=00
ENDHR=03
INCRHR=03

FHR=$STARTHR

while [ $FHR -le $ENDHR ]
do

time=`/nwprod/util/exec/ndate +${FHR} ${CYCdgex}`
yy=`echo $time | cut -c1-4`
mm=`echo $time | cut -c5-6`
dd=`echo $time | cut -c7-8`
hh=`echo $time | cut -c9-10`

cp $DATA/ungrib/FILE:${yy}-${mm}-${dd}_${hh} .
echo GRABBED $FHR

FHR=`expr $FHR + $INCRHR`

echo $FHR
done

cp $PARMdgex/dgex_METGRID.TBL.NMM METGRID.TBL
cp $FIXdgex/dgex_${REGION}_geo_nmb.d01.dio geo_nmb.d01.dio

export pgm=dgex_metgrid
. prep_step

startmsg
$EXECdgex/dgex_metgrid >> $pgmout 2>errfile
export err=$?;err_chk

mv errfile $DATA/errfile.metgrid

### run nemsinterp

cd $DATA/nemsinterp
sh /nwprod/util/ush/setup.sh

rm input_domain_01*

cp $DATA/metgrid/$pgmout .
cp $DATA/metgrid/met_nmb*dio .
cp $DATA/metgrid/namelist.nps .

export pgm=nam_nemsinterp
. prep_step

startmsg
$EXECdgex/dgex_nemsinterp >> $pgmout 2>errfile
export err=$?;err_chk

mv errfile $DATA/errfile.nemsinterp

#
# write out 78-h NAM boundary into a binary file that can be read by COMBC

rm input_domain_01
mv input_domain_01_nemsio $COMOUT/dgex_${REGION}.t${cyc}z.input_nemsio_precoldstart 

mv boco.000 boco_fromnps

export pgm=dgex_extract_nemsbdy_${REGION}
. prep_step

export XLFUNIT_33=boco_fromnps
export XLFUNIT_51=nemsbdy_extract.bin

startmsg
$EXECdgex/dgex_extract_nemsbdy_${REGION} >> $pgmout 2> errfile
export err=$?;err_chk

mv errfile $DATA/errfile.nemsbdy
mv nemsbdy_extract.bin $COMOUT/dgex_${REGION}.t${cyc}z.nemsbdy_nam78h.bin

mv $pgmout $DATA/.

cd $DATA
cat errfile.ungrib errfile.metgrid errfile.nemsinterp errfile.nemsbdy> errfile

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

exit

