#!/bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exdgex_wrffcst.sh.sms
# Script description:  Runs WRF-NMM forecast model for DGEX
#
# Author:        Eric Rogers       Org: NP22         Date: 2006-04-21
#
# SPECIAL INSTRUCTIONS : For best WRF-NMM performance, standard out and standard error
# files for both script and model MUST be written to a separate location from that in
# which this script runs, for example:

set -x

export MP_SHARED_MEMORY=yes
export MEMORY_AFFINITY=MCM

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

export tmmark=tm00

echo $CYCLEnam > cycledate
CYC=`cut -c 9-10 cycledate`
CYCFREE=`cut -c 9-10 cycledate`

iy=$(echo $CYCLEdgex|cut -c1-4)
im=$(echo $CYCLEdgex|cut -c5-6)
id=$(echo $CYCLEdgex|cut -c7-8)
ih=$(echo $CYCLEdgex|cut -c9-10)

# Build DGEX namelist

lasthour=`ls -1 restartdone.* | tail -1 | cut -c 14-15`
LENGTH=114
rhour=$LENGTH

tstart=0

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

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

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

if [ -z "$lasthour" -o "$RERUN" = "YES" ] ; then

### This is the "restart from non-zero forecast hour" switch which should always be set to NO here

RESTARTNZF=NO
rstout=true

else

### If we end up here, the "restart from non-zero forecast hour" switch which should always be set to yes
### For NEMS, no need to change start/end date in config file, only make sure RESTART=TRUE and
### move last restart file to default name expected by code (restart_file_DOMAINNUMBER_nemsio)

RESTARTNZF=YES
rstout=false

### Since model can only be restarted if modolo(fhr,3)=0, compare last fcstdone file to last
### restartdone file. If they are not at the same forecast hour, delete all fcstdone files with fhr > then
### the last restartdone file so that the restarted post and postsnd jobs are in synch with the restarted
### model run

fcstlasthr=`ls -1 fcstdone.0* | tail -1 | cut -c 11-12`
restartlasthr=`ls -1 restartdone.0* | tail -1 | cut -c 14-15`

if [ $fcstlasthr -gt $restartlasthr ]
then
   let "diffhr=fcstlasthr-restartlasthr"
   if [ $diffhr -eq 1 ]
   then
     rm fcstdone.0${fcstlasthr}*
   fi
   if [ $diffhr -eq 2 ]
   then
     let "fhrm1=fcstlasthr-1"
     typeset -Z2 fhrm1
     rm fcstdone.0${fhrm1}*
     rm fcstdone.0${fcstlasthr}*
   fi
fi

mv nmm_b_restart_nemsio.0${restartlasthr}h_00m_00.00s restart_file_01_nemsio

#end test on non-zero fcst hour
fi

NLNAME=dgex_${REGION}_configfile

restval=false

if [ $RESTARTNZF = "NO" ]
then
  export restval=false
else
  export restval=true
fi

cat $PARMdgex/$NLNAME | sed s:YSTART:$ystart: | sed s:MSTART:$mstart: \
 | sed s:DSTART:$dstart: | sed s:HSTART:$hstart: | sed s:TSTART:$tstart: \
 | sed s:LENGTH:$rhour: | sed s:REST:$restval: | sed s:OUTRST:$rstout: > configure_file

cp configure_file $COMOUT/${RUN}.t${cyc}z.configure_file.${tmmark}
cp configure_file configure_file_01
cp configure_file model_configure

if [ $RESTARTNZF = "NO" ] ; then

if [ -s $COMOUT/${RUN}_${REGION}.t${cyc}z.input_nemsio.${tmmark} ] ; then
   ln -s -f $COMOUT/${RUN}_${REGION}.t${cyc}z.input_nemsio.${tmmark} input_domain_01_nemsio
fi

else

### If we end up here we are restarting the forecast from a non-zero forecast hour;
### assume file " nmm_b_restart_nemsio.0${restartlasthr}" is in the working directory

echo "restarting the model from ${iy}-${im}-${id}_${ih}:00:00, forecast hour=${lasthour}"
echo "assume file nmm_b_restart_nemsio.0${restartlasthr} is in the working directory"

fi

hour=0

while [ $hour -le 114 ] ; do
  if [ $hour -lt 10 ]
  then
    ifhr=00${hour}
  fi
  if [ $hour -ge 10 -a $hour -lt 100 ]
  then
    ifhr=0${hour}
  fi
  if [ $hour -ge 100 ]
  then
    ifhr=${hour}
  fi
  if [ -s $COMIN/${RUN}_${REGION}.t${cyc}z.boco.01.${ifhr}.${tmmark} ]
  then
     ln -s -f $COMIN/${RUN}_${REGION}.t${cyc}z.boco.01.${ifhr}.${tmmark} boco.${ifhr}
  fi
  let "hour=hour+3"
done

export OMP_MAX_THREADS=1
export XLSMPOPTS="parthds=1:spins=0:yields=0:stack=128000000:schedule=static"

ln -s -f $FIXdgex/dgex_${REGION}_GWD.bin GWD_bin_01

cp $FIXdgex/dgex_LANDUSE.TBL LANDUSE.TBL
cp $PARMdgex/dgex_ETAMPNEW_DATA ETAMPNEW_DATA
cp $FIXdgex/dgex_VEGPARM.TBL VEGPARM.TBL
cp $FIXdgex/dgex_SOILPARM.TBL SOILPARM.TBL
cp $FIXdgex/dgex_GENPARM.TBL GENPARM.TBL

cp $FIXdgex/hiresw_RRTM_DATA RRTM_DATA
cp $FIXdgex/hiresw_tr49t67 tr49t67
cp $FIXdgex/hiresw_tr49t85 tr49t85
cp $FIXdgex/hiresw_tr67t85 tr67t85

cp $FIXdgex/nam_micro_lookup.dat ETAMPNEW_DATA
cp $FIXdgex/nam_RRTM_DATA_DBL RRTM_DATA_DBL

cp $PARMdgex/nam_atmos.configure_nmm atmos.configure
cp $PARMdgex/nam_dyn_state.txt dyn_state.txt
cp $PARMdgex/nam_phy_state.txt phy_state.txt

export pgm=dgex_nems_nmmb_fcst
. prep_step

startmsg
$EXECdgex/dgex_nems_nmmb_fcst >>$pgmout 2>>errfile
export err=$?

if [ $err -ne 0 ]
then
   err_chk
else
   msg="$pgm completed normally"
   sh $utilscript/postmsg.sh "$jlogfile" "$msg"
fi

exit
