#!/bin/ksh
######################################################
echo "-----------------------------------------------" 
echo "     exhwrf_coupled_fcst.sh.sms                "
echo "   Hurricane model control script              "
echo "-----------------------------------------------" 
######################################################
set -x

PS4=' + exhwrf_coupled_fcst.sh.sms line $LINENO: ' 

#-----------------------------------------------------
# This script will determine if the coupled atmosphere-
# ocean model or only the atmosphere model will run,
# and calls the appropriate script. 
# 
# Be sure that there is a holdvars.txt file, and source 
# it because it contains needed environmental variables 
# from the JHWRF_PRE_MASTER job that need to be passed 
# into this script
#
# If a coupled atmos-ocean run was requested, this 
# script will source the ocean_status file to see
# if the ocean initialization in hwrf_ocean_init.sh
# was successful. If successful the model will run
# the coupled model. If only atmospheric model requested, 
# or ocean initialization failed, model will run atmosphere
# only.   
#----------------------------------------------------- 
sh ${utilscript}/setup.sh
export pgm=exhwrf_coupled_fcst.sh.sms
. prep_step
startmsg

if test -f ${COMOUT}/${stormlabel}.holdvars.txt
then
   . ${COMOUT}/${stormlabel}.holdvars.txt
else
   set +x
   echo " "
   echo "!!! ERROR: holdvars.txt file missing"
   echo " "
   set -x
   msg="Hurricane to Run But No ${stormlabel}.holdvars.txt file found."
   postmsg "$jlogfile" "$msg"
   export err=911;err_chk
   err_exit "Hurricane to Run But No ${stormlabel}.holdvars.txt file found."
fi
#-------------------------------------------------------
 
if [ -s ${COMOUT}/ocean_status.${STORM}${STORMID}.${YYYYMMDDHH} ]  
then
   echo "Will source file ${COMOUT}/ocean_status.${STORM}${STORMID}.${YYYYMMDDHH}"
   . ${COMOUT}/ocean_status.${STORM}${STORMID}.${YYYYMMDDHH}    
   echo 'RUN_COUPLED = ' ${RUN_COUPLED} 
else
   echo 'No ocean_status file present; set RUN_COUPLED=NO'  
   RUN_COUPLED=NO
fi     

# determine CASE=MOVE or FCST given by value of MODE passed into script. 
CASE=${CASE}
echo "Script exhwrf_coupled_fcst.sh running CASE=${CASE}"  

if [[ ${RUN_COUPLED} = 'YES' ]]; then
if [ ${OCEAN} == POM -o ${OCEAN} == NO ]; then
   ${USHhwrf}/hwrf_coupled_fcst.sh  ${STORM} ${YYYY} ${MM} ${DD} ${CYC} ${CASE} 
elif [ ${OCEAN} == HYCOM ]; then
   ${USHhwrf}/hwrf_rtofs_coupled_fcst.sh  ${STORM} ${YYYY} ${MM} ${DD} ${CYC} ${CASE}
fi
else
   ${USHhwrf}/hwrf_nmm_fcst.sh  ${STORM} ${YYYY} ${MM} ${DD} ${CYC} ${CASE} 
fi 
# end of script 
