#!/bin/ksh
######################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exnam_fcst_parent.sh
# Script description:  Run NAM forecast
#
# Author:        Eric Rogers       Org: NP22         Date: 1999-06-23
#
# Abstract: This script runs the Nam forecast
#
# Script history log:
# 1999-06-23  Eric Rogers
# 1999-08-02  Brent Gordon  - Modified for production.
# 2006-01-13  Eric Rogers   - Modified for WRF-NMM
# 2010-04-01  Eric Rogers   - Modified to run 60-84 NAM parent domain only
#

set -xa
cd $DATA

export MP_SHARED_MEMORY=yes
export MEMORY_AFFINITY=MCM
export run_firewx=${run_firewx:-NO}
export domain=${domain:-parentonly}
export NODE_CONFIGFILE=${NODE_CONFIGFILE:-$PARMnam/nam_node_decomposition}

export tmmark=tm00

#
# Get needed variables from exndas_prelim.sh.sms
#
. $GESDIR/${RUN}.t${cyc}z.envir.sh

export SDATE=$CYCLE
echo $CYCLE > cycledate

### modify namelist file

lasthour=`ls -1rt restartdone.???h_* | tail -1 | cut -c 13-15`

START=60
LENGTH=84

export restval=true

rhour=$LENGTH
resint=`expr ${LENGTH} \* 60 `

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`

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

tstart=0

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

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

RESTARTNZF=YES
mv nmm_b_restart_nemsio.0${START}h_00m_00.00s restart_file_01_nemsio

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

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

fi

### 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

### 2/1/2012 : Script modified to ensure that all fcstdone files with fhr > restart hour
### are deleted

fcstlasthr=`ls -1 fcstdone.???h_* | tail -1 | cut -c 10-12`

if [ $fcstlasthr -gt $lasthour ]
then
   rm fcstdone.${fcstlasthr}h_* fcstdone.??.${fcstlasthr}h_*
   let "fhrp1=fcstlasthr+1"
   let "fhrp2=fcstlasthr+2"
   let "fhrp3=fcstlasthr+3"
   typeset -Z3 fhrp1
   typeset -Z3 fhrp2
   typeset -Z3 fhrp3
   rm fcstdone.${fhrp1}h_* fcstdone.??.${fhrp1}h_*
   rm fcstdone.${fhrp2}h_* fcstdone.??.${fhrp2}h_*
   rm fcstdone.${fhrp3}h_* fcstdone.??.${fhrp3}h_*
   let "fhrm1=fcstlasthr-1"
   typeset -Z3 fhrm1
   while [ $fhrm1 -gt $lasthour ]
   do
     rm fcstdone.${fhrm1}h_* fcstdone.??.${fhrm1}h_*
     let "fhrm1=fhrm1-1"
   done
fi

numchild=0
wrtlast=false

if [ $cyc = 00 -o $cyc = 12 ] 
then
  nltype=ontime
else
  nltype=offtime
fi

NLNAME=nam_configfile_${nltype}_parent

# Run config file to import node decomposition
set -a; . $NODE_CONFIGFILE; set +a

export restval=true
export rstout=false

cat $PARMnam/$NLNAME | sed s:INPES:$inpes: | sed s:JNPES:$jnpes: \
 | sed s:WRITE_GROUPS:$write_groups: | sed s:WRITE_TASKS:$write_tasks: \
 | 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:  \
 | sed s:WRTLAST:$wrtlast: | sed s:NCHILD:$numchild: > configure_file

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

rm configure_file_02 configure_file_03 configure_file_04 configure_file_05 configure_file_06

export tmmark_prev=tm03

rm fort.*

export XLSMPOPTS=stack=128000000

hour=00

while [ $hour -le 84 ] ; do
  if [ -s $COMIN/${RUN}.t${cyc}z.boco.01.0${hour}.${tmmark} ]
  then
     ln -s -f $COMIN/${RUN}.t${cyc}z.boco.01.0${hour}.${tmmark} boco.0${hour}
  fi
  let "hour=hour+3"
  typeset -Z2 hour
done

ln -s -f $FIXnam/nam_GWD.bin GWD_bin_01

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

cp $FIXnam/nam_micro_lookup.dat ETAMPNEW_DATA
cp $FIXnam/nam_RRTM_DATA_DBL RRTM_DATA_DBL
cp $FIXnam/nam_GENPARM.TBL GENPARM.TBL
cp $FIXnam/nam_LANDUSE.TBL LANDUSE.TBL
cp $FIXnam/nam_SOILPARM.TBL SOILPARM.TBL
cp $FIXnam/nam_VEGPARM.TBL VEGPARM.TBL

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

export pgm=nam_nems_nmmb_fcst
. prep_step

startmsg
poe $EXECnam/nam_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

echo Exiting $0

exit
