#!/bin/ksh
######################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exndas_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
#
# SPECIAL INSTRUCTIONS : For best WRF-NMM performance, standard out and standard error
# files for both script and model MUST be written to a seperate location from that in
# which this script runs, for example:
#### @ output = /tmpnwprd/stdout.nam/fcstout
#### @ error = /tmpnwprd/stderr.nam/fcsterr
#

set -xa

export MP_SHARED_MEMORY=yes
export MEMORY_AFFINITY=MCM

##export MP_INFOLEVEL=6
##export MP_PMDLOG=yes
##export MP_S_DEBUG=1
##export MP_S_ENABLE_ERR_PRINT=yes

export PS4='$SECONDS + '

cd $DATA

rm fcstdone*
rm restartdone*

export CDATE=$PDY$cyc
export FIXnam_prod=/nwprod/fix
export PARMnam_prod=/nwprod/parm
#
# Get needed variables from exndas_prelim.sh.sms
#
. $GESDIR/${RUN}.t${cyc}z.envir.sh

#
# Run script to get compute precipitation budget for bias correction
# (NDAS ONLY at tm12 for 12z cycle)

if [ $cyc -eq 12 ] ; then
 if [ $tmmark = tm12 ] ; then
  ${USHnam}/nam_h2obudget.sh
 fi
fi

#
# Run script to get hourly NCEP Stage2/4 precipitation interpolated
# to the Nam model grid.  (NDAS ONLY)
${USHnam}/nam_getpcp.sh

tmval=`echo $tmmark | cut -c3-4`

export SDATE=`/nwprod/util/exec/ndate -$tmval $CYCLE`
echo $CYCLE > cycledate

### modify namelist file

export restval=true

LENGTH=3

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

 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`

NLNAME=nam_configfile_ndas
 
if [ $tmmark = tm12 ] ; then
  if [ $GUESS = GDAS ] ; then
    export restval=false
  else
    export restval=true
  fi
else
  export restval=true
fi

filesize1=0
filesize2=0
filesize3=0

if [ -s $COMIN/${RUN}.t${cyc}z.pcp.${tmmark}.hr1.${CDATE}.bin ]
then
  let filesize1=`ls -l $COMIN/${RUN}.t${cyc}z.pcp.${tmmark}.hr1.${CDATE}.bin | awk '{print $5}'`
fi
if [ -s $COMIN/${RUN}.t${cyc}z.pcp.${tmmark}.hr2.${CDATE}.bin ] 
then
  let filesize2=`ls -l $COMIN/${RUN}.t${cyc}z.pcp.${tmmark}.hr2.${CDATE}.bin | awk '{print $5}'`
fi
if [ -s $COMIN/${RUN}.t${cyc}z.pcp.${tmmark}.hr3.${CDATE}.bin ]
then
  let filesize3=`ls -l $COMIN/${RUN}.t${cyc}z.pcp.${tmmark}.hr3.${CDATE}.bin | awk '{print $5}'`
fi

if [ $filesize1 -lt 3000000 -o $filesize2 -lt 3000000 -o $filesize3 -lt 3000000 ]
then
  pflag=false
else
  pflag=true
fi

cat $PARMnam/$NLNAME | sed s:YSTART:$ystart: | sed s:MSTART:$mstart: \
 | sed s:DSTART:$dstart: | sed s:HSTART:$hstart: | sed s:YEND:$yend: \
 | sed s:MEND:$mend:     | sed s:DEND:$dend: | sed s:HEND:$hend:  \
 | sed s:RH:$rhour:      | sed s:RD:$rday: \
 | sed s:RESINT:$resint: | sed s:REST:$restval: | sed s:PFLAG:$pflag: > configure_file

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

case $tmmark in
  tm12) export tmmark_prev=tm12;;
  tm09) export tmmark_prev=tm12;;
  tm06) export tmmark_prev=tm09;;
  tm03) export tmmark_prev=tm06;;
esac

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

rm fort.*

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

if [ -s $COMIN/${RUN}.t${cyc}z.boco.01.000.${tmmark} ]
then
   cp $COMIN/${RUN}.t${cyc}z.boco.01.000.${tmmark} boco.000
fi
#

if [ $tmmark = "tm12" ]
then
  if [ $GUESS = NAM ] 
  then
     cp $GESDIR/${RUN}.t${cyc}z.nmm_b_restart_nemsio_anl.${tmmark} restart_file_01_nemsio
  else
     ln -s -f $GESDIR/${RUN}.t${cyc}z.input_nemsio.anl.${tmmark} input_domain_01_nemsio
  fi
else
  cp $GESDIR/${RUN}.t${cyc}z.nmm_b_restart_nemsio_anl.${tmmark} restart_file_01_nemsio
fi

#
ln -s -f $COMIN/${RUN}.t${cyc}z.pcp.${tmmark}.hr1.${CDATE}.bin  fort.41
ln -s -f $COMIN/${RUN}.t${cyc}z.pcp.${tmmark}.hr2.${CDATE}.bin  fort.42
ln -s -f $COMIN/${RUN}.t${cyc}z.pcp.${tmmark}.hr3.${CDATE}.bin  fort.43

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=$?;err_chk

# Increment SDATE (the start time of this 3-h NDAS forecast) by 3-h to convert it to a
# valid date which is used in the wrfrst file name on /nwges/prod
 
export LENGTH=3

export SDATE=$(/nwprod/util/exec/ndate +$LENGTH $SDATE)
echo "SDATE after : " $SDATE
iy=$(echo $SDATE|cut -c1-4)
im=$(echo $SDATE|cut -c5-6)
id=$(echo $SDATE|cut -c7-8)
ih=$(echo $SDATE|cut -c9-10)

cp nmm_b_restart_nemsio.003h_00m_00.00s $GESDIR/${RUN}.t${cyc}z.nmm_b_restart_nemsio.${tmmark}
cp nmm_b_restart_nemsio.003h_00m_00.00s.ctl $GESDIR/${RUN}.t${cyc}z.nmm_b_restart_nemsio.${tmmark}.ctl

cp fcstdone.003h_00m_00.00s $GESDIR/ndas.t${cyc}z.fcstdone003.${tmmark}

date
echo EXITING $0
