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

set -xa
cd $DATA

export MP_SHARED_MEMORY=yes
export MEMORY_AFFINITY=MCM
export run_firewx=${run_firewx:-NO}
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 14-15`
START=36
LENGTH=60

# Need to check of the existence of ALL restart files (parent and nests) to
# determine if we are restarting from a non-zero forecast hour

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

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

# If no parent restartdone files exist, we are starting from 36-h as desired so
# skip the second half of this if test

echo "NAM forecast starting from 36-h forecast"

else

num="02 03 04 05"

for numnest in $num
do
  export lasthour_${numnest}=`ls -1 restartdone.${numnest}.0* | tail -1 | cut -c 17-18`
done

if [ $lasthour -eq $lasthour_02 -a $lasthour -eq $lasthour_03 \
  -a $lasthour -eq $lasthour_04 -a $lasthour -eq $lasthour_05 ]
then
  echo "all restart files available at $lasthour"
else
  echo "not all restart files available at $lasthour; back up all lasthour variables by restart frequency"
  # Restart frequency
  if [ $cyc = 00 -o $cyc = 12 ]
  then
    nltype=ontime
  else
    nltype=offtime
  fi
  fs_rest=`grep "minutes_restart" $PARMnam/nam_configfile_${nltype}_parent |awk -F" " '{print $2}'`
  fh_rest=`expr $fs_rest / 60`
  let "lasthour=lasthour-fh_rest"
  typeset -Z2 lasthour
  num="02 03 04 05"
  for numnest in $num
  do
    export lasthour${numnest}=$lasthour
  done
fi

fi

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

##### Condition 1: Normal restart of model @ 36-h without firewx domsin; 
##### copy restart files and proceed; restart from non-zero forecast hour 
##### should always be set to NO here

RESTARTNZF=YES
rstout=false
mv nmm_b_restart_nemsio.0${START}h_00m_00.00s restart_file_01_nemsio
mv nmm_b_restart.02_nemsio.0${START}h_00m_00.00s restart_file_02_nemsio
mv nmm_b_restart.03_nemsio.0${START}h_00m_00.00s restart_file_03_nemsio
mv nmm_b_restart.04_nemsio.0${START}h_00m_00.00s restart_file_04_nemsio
mv nmm_b_restart.05_nemsio.0${START}h_00m_00.00s restart_file_05_nemsio
rm configure_file_06
   
else

#### Condition 2 : Restart from 36-h < fhr < 60-h. 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

### FOR PARENT + NESTED RUNS: we already checked to ensure that ALL restart files for forecast hour XX
### were written out. If not, we set lasthour back by 3-h for all domains.

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

num="02 03 04 05"

for numdomain in $num
do
  mv nmm_b_restart.${numdomain}_nemsio.0${lasthour}h_00m_00.00s restart_file_${numdomain}_nemsio
done

#end test on non-zero fcst hour
fi

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

NLNAME=nam_configfile_${nltype}_parent

# Hardwire numchild here for testing

numchild=4
wrtlast=true

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

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

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_parent.${tmmark}
cp configure_file configure_file_01
cp configure_file model_configure

# Set up configure file for nests

nest="conus alaska hawaii prico"

for domain in $nest
do
export domain
set -a; . $NODE_CONFIGFILE; set +a

NLNAME=nam_configfile_nest_${domain}

### RESTART for nests is always false UNLESS restarting model from non-zero forecast hour

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

wrtlast=false

if [ $domain = hawaii -o $domain = prico ]
then

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:WRTLAST:$wrtlast: > configure_file_${domain}

else

numchild_firewx=0

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:WRTLAST:$wrtlast: \
 | sed s:NCHILD_FIREWX:$numchild_firewx: > configure_file_${domain}

fi

cp configure_file_${domain} $COMOUT/${RUN}.t${cyc}z.configure_file_${domain}nest.${tmmark}
export numdomain=`grep ${domain} $PARMnam/nam_nestdomains | awk '{print $2}'`

mv configure_file_${domain} configure_file_${numdomain}

# GWD runs in Alaska nest only

if [ $domain = alaska ]
then
   ln -s -f $FIXnam/nam_${domain}nest_GWD.bin GWD_bin_${numdomain}
fi

# end loop over four fixed nested domains
done

export tmmark_prev=tm03

rm fort.*

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

# E.Rogers 2/2007: replace link with copy for wrfrst file since
# the model writes out a wrfrst file at forecast hour = 0 which will
# overwrite the wrfrst_anl file in /nwges/prod/nam.$PDY (They are not bit-identical)

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

if [ $RESTARTNZF = "NO" ] ; then

if [ $GUESS = GDAS ] ; then
    if [ -s $GESDIR/${RUN}.t${cyc}z.input_nemsio.anl.${tmmark} ] ; then
     ln -s -f $GESDIR/${RUN}.t${cyc}z.input_nemsio.anl.${tmmark} input_domain_01_nemsio
  fi
else
  if [ -s $GESDIR/${RUN}.t${cyc}z.nmm_b_restart_nemsio_anl.${tmmark} ] ; then
     cp $GESDIR/${RUN}.t${cyc}z.nmm_b_restart_nemsio_anl.${tmmark} restart_file_01_nemsio
  fi
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"

### 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`
restartlasthr=`ls -1 restartdone.???h_* | tail -1 | cut -c 13-15`

if [ $fcstlasthr -gt $lasthour ]
then
   rm fcstdone.${fcstlasthr}h_* fcstdone.??.${fcstlasthr}h_*
   rm restartdone.${fcstlasthr}h_* restartdone.??.${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 restartdone.${fhrp1}h_* restartdone.??.${fhrp1}h_*
   rm fcstdone.${fhrp2}h_* fcstdone.??.${fhrp2}h_*
   rm restartdone.${fhrp2}h_* restartdone.??.${fhrp2}h_*
   rm fcstdone.${fhrp3}h_* fcstdone.??.${fhrp3}h_*
   rm restartdone.${fhrp3}h_* restartdone.??.${fhrp3}h_*
   let "fhrm1=fcstlasthr-1"
   typeset -Z3 fhrm1
   while [ $fhrm1 -gt $lasthour ]
   do
     rm fcstdone.${fhrm1}h_* fcstdone.??.${fhrm1}h_*
     rm restartdone.${fhrm1}h_* restartdone.??.${fhrm1}h_*
     let "fhrm1=fhrm1-1"
   done
fi

fi

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
