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

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

#-----------------------------------------------------
# This script will run the HWRF post processing. 
# This script will run in parallel to the HWRF Forecast
# for both MOVE (12 hour run) and FCST  (126 hour forecast).
# This script will provide final grib output files for 
# the PARENT, NEST and COMBINED domains.
# Be sure that there is a holdvars.txt file, and source 
# it because it contains needed enviromental variables 
# from the JHWRF_PRE_MASTER job that need to be passed
# into this script. 
#----------------------------------------------------- 
sh ${utilscript}/setup.sh
export pgm=exhwrf_post.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 model ran coupled 
# to the ocean or the atmosphere 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 parameter passed to hwr_post for postprocessing  
# of MODEL = ATMOS or COUPLED output. This overides (only for postprocessing)
# the value of MODEL specified in the holdvars.txt file at start of run.  

if [ ${RUN_COUPLED} = YES ] 
then
   MODEL=COUPLED
else
   MODEL=ATMOS
fi
echo 'MODEL = ' ${MODEL}

echo "Script exhwrf_post.sh.sms"  

# set some diagnostics parameters 
# DIAG=0  no diagnostics; DIAG=1 selected diagnostics (default)
# IDIAG=0 coarse grid; IDIAG=1 high resolution grid (default)
DIAG=$DIAG
IDIAG=$IDIAG

if [[ $MODEL == COUPLED ]]; then
    OUTDIR=${DATA}/$STORM.${YYYYMMDDHH}/COUPLED_FCST
else
    OUTDIR=${DATA}/$STORM.${YYYYMMDDHH}/ATMOS_FCST
fi
echo "For MODEL = ${MODEL}:  OUTDIR =  ${OUTDIR}" 

if [[ "$PARAFLAG" == YES && "$OLD_POST" == YES ]] ; then
    if [[ "$POST_MODE" == SAT ]] ; then
        postmsg "$jlogfile" Exiting because old post does not run in satellite-only mode.
        exit 0
    fi
    ${USHhwrf}/hwrf_post_old.sh  ${STORM} ${YYYYMMDDHH} ${CENLA} ${CENLO} ${CASE} \
        ${MODEL} ${DIAG} ${IDIAG} ${STORMID}   
else
    ${USHhwrf}/hwrf_post_master.sh "${POST_FIRSTHOUR:-0}" "${POST_LASTHOUR:-126}" \
        "${POST_INCREMENT:-3}" "$STORM" "$STORMID" "$YYYYMMDDHH" "$POST_MODE" "$OUTDIR" \
        "$CENLA" "$CENLO"
fi

# end of script 
