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

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

#-----------------------------------------------------
# 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
#----------------------------------------------------- 
sh ${utilscript}/setup.sh
export pgm=exhwrf_merge.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
#-------------------------------------------------------
# check the ocean_status file to see if the ocean 
# initialization was successful. If so, run coupled 
# atmos-ocean model; otherwise run atmos model alone. 
 
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 if shell variable MODEL=COUPLED or MODEL=ATMOS 
# will be passed to the script. This overrides (only for this
# script) the value of MODEL specified in the holdvars.txt file
# at the start of the run.    

if [ ${RUN_COUPLED} = YES ] 
then
   MODEL=COUPLED
else
   MODEL=ATMOS
fi
echo "The value MODEL =  ${MODEL} will be passed into the script"
#----------------------------------------------------------------

# Run the merge script  
if [ $INIT == YES -o $GSI == YES ]; then
${USHhwrf}/hwrf_merge.sh $STORM $YYYY $MM $DD $CYC $STORMID
else
echo 'Initialization not requested.  INIT = $INIT GSI = $GSI  This script need not be run'
msg="Hurricane to Run But INIT is not requested."
postmsg "$jlogfile" "$msg"
export err=911;err_chk
exit 99
fi
# end of script 
