#! /bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         dgex_wrfsi.ll_multi_gen.sh
# Script description:  Runs WRF standard initialization (SI)
#
# Author:        Eric Rogers       Org: NP22         Date: 2004-07-07
#
# Abstract: Runs the main WRF-SI Perl script to interpolate Nam input GRIB files
# to the WRF-NMM creating input to the REAL code which computes
# the WRF initial/boundary conditions. Input variable
#
# Script history log:
# 2004-04-21  Eric Rogers - original modified for NMM DGEX
#

set -x

NUMATONCE=2
INT=3

mod=ETA

REGION=$1
CYC=$2
MULTINUM=$3
MODEL=$4

MULTINUM_IN=$MULTINUM

DATAROOT=$DATA/dataroot${MULTINUM}

PERINSTALLROOT=$HOMEdgex
PEREROOT=$DATA/installroot/wrfsi/extdata
mkdir -p $DATA/installroot/wrfsi/extdata
INSTALLROOT=$PERINSTALLROOT

mkdir -p $DATAROOT
chmod 755 $DATAROOT
cd $DATAROOT

mkdir -p $DATAROOT/siprd/
mkdir -p $DATAROOT/static/
mkdir -p $DATAROOT/log/

# echo out the working directory path names previously hardwired in the wrfsi.nl file

rm wrfsi.nl.piece

echo "&si_paths" > wrfsi.nl.piece
echo "ANALPATH = '$DATA/wrfsi/extprd',">> wrfsi.nl.piece
echo "LBCPATH = '$DATA/wrfsi/extprd',">> wrfsi.nl.piece
echo "LSMPATH = '$DATA/wrfsi/extprd',">> wrfsi.nl.piece
echo "CONSTANTS_PATH = '$DATA/wrfsi/extprd',">> wrfsi.nl.piece
echo "/" >> wrfsi.nl.piece

cat wrfsi.nl.piece

cp $FIXdgex/dgex_${REGION}_static.wrfsi.rotlat_nonetcdf_mod $DATAROOT/static/static.wrfsi.rotlat_nonetcdf
cat $PARMdgex/dgex_${REGION}.wrfsi.nl wrfsi.nl.piece > $DATAROOT/static/wrfsi.nl

chmod 755 $DATAROOT/siprd/
chmod 755 $DATAROOT/static/
chmod 755 $DATAROOT/log/

DATE=`cat $DATA/nmcdate.dgex | cut -c7-14`
DATECYC=`cat $DATA/nmcdate.dgex | cut -c7-16`

echo $DATE
echo $CYC

## $DATE$CYC is start time

let MULTINUM=MULTINUM-1
echo $MULTINUM

let timeskip=MULTINUM*NUMATONCE*INT
let timerun=NUMATONCE*INT-INT
echo timeskip $timeskip
echo timerun $timerun

echo "ARGUMENTS for ndate: " $timeskip ${DATE}${CYC}
#stime=`/nwprod/util/exec/ndate $timeskip ${DATE}${CYC}`
stime=`/nwprod/util/exec/ndate $timeskip ${DATECYC}`

echo starttime $stime

# if this is the last WRF-SI piece (MULTINUM=6) hardwire timerun=3 hours

if [ $MULTINUM_IN = 6 ] ; then
  $USHdgex/dgex_wrfnmm_wrfprep.pl -f 3 -s $stime -d $DATAROOT -e $PEREROOT -i $INSTALLROOT
else
  $USHdgex/dgex_wrfnmm_wrfprep.pl -f $timerun -s $stime -d $DATAROOT -e $PEREROOT -i $INSTALLROOT
fi

cp $DATAROOT/log/${stime}.hinterp $DATA/. 
cp $DATAROOT/log/${stime}.vinterp $DATA/. 
cp $DATAROOT/log/${stime}.wrfprep $DATA/. 

cp $DATAROOT/siprd/wrf_real* $DATA/.
cp $DATAROOT/siprd/real* $DATA/.

echo "WRFSI JOB # ${MULITNUM_IN} done" > $DATA/wrfsidone.${MULTINUM_IN}

rm $DATAROOT/siprd/wrf_real*
rm $DATAROOT/siprd/real*
rm $DATAROOT/siprd/hint*
