#!/bin/bash
###############################################################################
# UNIX Script Documentation Block                                                         #
#                                                                                         #
# Script name:         model_nmm.ksh                                                      #
# Script description:  Runs WRF-NMM or WRF-EM forecast model.                             #
#                                                                                         #
# Author:        Eric Rogers       Org: NP22         Date: 2004-07-02                     #
#                                                                                         #
# Script history log:                                                                     #
# 2003-11-01  Matt Pyle   - Original script for parallel                                  #
# 2004-07-02  Eric Rogers - Preliminary modifications for production.                     #
# 2005-08-03  Jun Du - Modified it to fit to the SREF system
#
# 2005-10-06  Xiaoxue Wang - Modified to operational standard
# 2006-03-30  Jun Du - save 6-hour instead of 12-hour forecasts for 4xday SREF's
#                      breeding
# 2008-07-18  Jun Du - upgrade to WRFv2.2
# 2010-05-06  Jun Du - generalized the script to run NEMS ensemble (MODEL=NMB)
# 2014-10-10  Jun Du - expanded for SREF.v7.0.0
################################################################################
set -eux


model=`echo $MODEL | tr "[:upper:]" "[:lower:]"`

START_DATE=$PDY$CYC
START_YYYY=`echo $START_DATE | cut -c 1-4`
START_MM=`echo $START_DATE | cut -c 5-6`
START_DD=`echo $START_DATE | cut -c 7-8`
START_HH=`echo $START_DATE | cut -c 9-10`

YEAR=${START_YYYY}
YEARM1=`expr $YEAR - 1`

cd $WORK_DIR

if [ $MEMBER = ctl ];then export PAIR=00;fi
if [ $MEMBER = p01 ];then export PAIR=01;fi
if [ $MEMBER = n01 ];then export PAIR=01;fi
if [ $MEMBER = p02 ];then export PAIR=02;fi
if [ $MEMBER = n02 ];then export PAIR=02;fi
if [ $MEMBER = p03 ];then export PAIR=03;fi
if [ $MEMBER = n03 ];then export PAIR=03;fi
if [ $MEMBER = p04 ];then export PAIR=04;fi
if [ $MEMBER = n04 ];then export PAIR=04;fi
if [ $MEMBER = p05 ];then export PAIR=05;fi
if [ $MEMBER = n05 ];then export PAIR=05;fi
if [ $MEMBER = p06 ];then export PAIR=06;fi
if [ $MEMBER = n06 ];then export PAIR=06;fi

# Run setup to initialize working directory and utility scripts
## setup.sh  # no need, since using module load prod_util

#
# fcst
#

#if [ $MODEL == NMM ] && [ $PAIR == 02 ] ; then
if [ $MODEL == NMM ] && [ $PAIR == 99 ] ; then
   export pgm=sref_wrf_${model}_hwrf
   . prep_step
   startmsg
   if [ $MACHINE == zeus ]; then
   mpiexec_mpt -prefix "[%g]" -np $PBS_NP $EXECsref/sref_wrf_${model}_hwrf >> $pgmout 2>errfile
   elif [ $MACHINE == wcoss ]; then
   mpirun.lsf $EXECsref/sref_wrf_${model}_hwrf >> $pgmout 2>errfile
   elif [ $MACHINE == dell ]; then
   mpirun -l -n $N_TASK $EXECsref/sref_wrf_${model}_hwrf >> $pgmout 2>errfile
   fi
   export err=$?; err_chk
else
   export pgm=sref_wrf_${model}
   . prep_step
   startmsg

   if [ $MODEL == NMB ] ; then
    if [ $MEMBER == n02 -o $MEMBER == p02 -o $MEMBER == n03 -o $MEMBER == p03 -o $MEMBER == n05 -o $MEMBER == p05 ]; then
     if [ $MACHINE == zeus ]; then
     mpiexec_mpt -prefix "[%g]" -np $PBS_NP $EXECsref/sref_wrf_${model}_driersoil >> $pgmout 2>errfile
     elif [ $MACHINE == wcoss ]; then
     mpirun.lsf $EXECsref/sref_wrf_${model}_driersoil >> $pgmout 2>errfile
     elif [ $MACHINE == dell ]; then
     mpirun -l -n $N_TASK $EXECsref/sref_wrf_${model}_driersoil >> $pgmout 2>errfile
     fi
    else
     if [ $MACHINE == zeus ]; then
     mpiexec_mpt -prefix "[%g]" -np $PBS_NP $EXECsref/sref_wrf_${model}_normalsoil >> $pgmout 2>errfile
     elif [ $MACHINE == wcoss ]; then
     mpirun.lsf $EXECsref/sref_wrf_${model}_normalsoil >> $pgmout 2>errfile
     elif [ $MACHINE == dell ]; then
     mpirun -l -n $N_TASK $EXECsref/sref_wrf_${model}_normalsoil >> $pgmout 2>errfile
     fi
    fi
   fi
   if [ $MODEL == ARW ] ; then
#   if [ $MEMBER == n01 -o $MEMBER == n03 -o $MEMBER == p04 -o $MEMBER == n06 ]; then
    if [ $MEMBER == n01 -o $MEMBER == n02 -o $MEMBER == n03 -o $MEMBER == p04 -o $MEMBER == p05 -o $MEMBER == n06 ]; then
     if [ $MACHINE == zeus ]; then
     mpiexec_mpt -prefix "[%g]" -np $PBS_NP $EXECsref/sref_wrf_${model}_driersoil >> $pgmout 2>errfile
     elif [ $MACHINE == wcoss ]; then
     mpirun.lsf $EXECsref/sref_wrf_${model}_driersoil >> $pgmout 2>errfile
     elif [ $MACHINE == dell ]; then
     mpirun -l -n $N_TASK $EXECsref/sref_wrf_${model}_driersoil >> $pgmout 2>errfile
     fi
    else
     if [ $MACHINE == zeus ]; then
     mpiexec_mpt -prefix "[%g]" -np $PBS_NP $EXECsref/sref_wrf_${model}_normalsoil >> $pgmout 2>errfile
     elif [ $MACHINE == wcoss ]; then
     mpirun.lsf $EXECsref/sref_wrf_${model}_normalsoil >> $pgmout 2>errfile
     elif [ $MACHINE == dell ]; then
     mpirun -l -n $N_TASK $EXECsref/sref_wrf_${model}_normalsoil >> $pgmout 2>errfile
     fi
    fi
   fi

   export err=$?; err_chk
fi

#grep "wrf: SUCCESS COMPLETE WRF" rsl.error.0000
err=$?
if [ $err -ne 0 ]; then
   echo "sref_wrf${model}_fcst failed" >> $WORK_DIR/fcst.$MEMBER.failed
   exit 1
fi

# save 12h or 6h wrf fcst for next cycle's breeding calculation
#TOMMORROW=`$NDATE +06 ${PDY}${CYC}`
TOMMORROW=`$NDATE +12 ${PDY}${CYC}`
Y=`echo ${TOMMORROW} | cut -c 1-4`
M=`echo ${TOMMORROW} | cut -c 5-6`
D=`echo ${TOMMORROW} | cut -c 7-8`
C=`echo ${TOMMORROW} | cut -c 9-10`

WRFF=wrfout_d01_${Y}-${M}-${D}_${C}:00:00
if [ $MODEL = NMM ];then
cp ${WRFF} ${GESDIR}/${MODEL}.${MEMBER}.${WRFF}_$RES
fi
if [ $MODEL = ARW ];then
# Note: here MODEL=ARW. If want to apply reg bred, use name "MODEL"; otherwise, use "em"
cp ${WRFF} ${GESDIR}/${MODEL}.${MEMBER}.${WRFF}_$RES
#cp ${WRFF} ${GESDIR}/em.${MEMBER}.${WRFF}_$RES
fi
if [ $MODEL = NMB ];then
cp nmmb_hst_01_nio_0012h_00m_00.00s ${GESDIR}/${MODEL}.${MEMBER}.${WRFF}_$RES
fi

echo "done" > $WORK_DIR/fcst.$MEMBER.done
echo "done" > $WORK_DIR/../fcst.$MEMBER.done
