#! /bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         nam_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 GDAS input GRIB diles
# to the WRF-NMM domains, creating input to the REAL code which computes
# the WRF-NMM based GDAS wrfout file 
#
# Script history log:
# 2003-11-01  Matt Pyle - Original script for parallel
# 2004-07-07  Eric Rogers - Preliminary modifications for production.
# 2006-02-01  Eric Rogers - Modified HIRESW script for NDAS cold start
                                                                                                                                   
set -x

NUMATONCE=2
INT=3

mod=NAM

DOMNAM=$1
CYC=$2
MULTINUM=$3
MODEL=$4
MEMBER=$5

MULTINUM_IN=$MULTINUM

DATAROOT=$DATA/dataroot${MULTINUM}

PERINSTALLROOT=/nw${envir}
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 $FIXnam/nam_static.wrfsi.rotlat_nonetcdf $DATAROOT/static/static.wrfsi.rotlat_nonetcdf
cat $PARMnam/nam_wrfsi.nl wrfsi.nl.piece > $DATAROOT/static/wrfsi.nl

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

DATEnam=`cat $DATA/nmcdate | cut -c7-16`
DATEtm12=`/nwprod/util/exec/ndate -12 $DATEnam` 

DATE=`echo $DATEtm12 | cut -c 1-8`
CYC=`echo $DATEtm12 | cut -c 9-10`

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}`

echo starttime $stime

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

if [ $MULTINUM_IN = 6 ] ; then
  $USHnam/nam_wrfprep.pl -f 3 -s $stime -d $DATAROOT -e $PEREROOT -i $INSTALLROOT
else
  $USHnam/nam_wrfprep.pl -f $timerun -s $stime -d $DATAROOT -e $PEREROOT -i $INSTALLROOT
fi
export err=$?; $DATA/err_chk

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 # ${MULTINUM_IN} done" > $DATA/wrfsidone.${MULTINUM_IN}

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

echo EXITING $0
exit
