#!/bin/sh
#######################################################################
#  Job Name:  exmdl_hrqpf_extpost.sh.sms
#  Purpose:   After fcst step is complete, run all steps necessary to   
#             post process raw forecasts and output the final hrqpf
#             forecasts.
echo "------------------------------------------------"
echo "JGFSMOS_HRQPF_EXTPOST processing                "
echo "------------------------------------------------"
echo "History: May 1, 2011 - First implementation of this new script."
#
#
#######################################################################


echo $PDY $cyc: Date and Cycle - echo PDY and cyc

######################################################################
#
#   DETERMINE SEASON BASED ON PDY
#
######################################################################

month=`echo ${PDY} | cut -c5-6`

case $month in
  04 | 05 | 06 | 07 | 08 | 09 ) season="04010930";;
  01 | 02 | 03 | 10 | 11 | 12 ) season="10010331";;
esac

#######################################################################
#
#     CHECK FOR COMPLETION STATUS OF EXTFCST STEP BEFORE PROCEEDING 
#
#######################################################################

regions="01 02 03 04 05 06 07 08 09 10 11 12 13 14"
for reg in ${regions}
do
  echo "checking region ${reg}"
  while [ ! -s ${COMIN}/mdl_hrqpf_rawxfcst.t${cyc}z.reg${reg} ]
  do
    echo "fcst still not done with region ${reg}, check again in 10 seconds"
    sleep 10
  done
done


while [ ! -s ${COMIN}/mdl_hrqpf_fnlfcst_vecra.t${cyc}z ]
do
  echo "post still not done with file mdl_hrqpf_fnlfcst_vecra.t${cyc}z, check again in 10 seconds"
  sleep 10
done

#######################################################################
#
#    COPY FCST OUTPUT FROM COMIN
#
#######################################################################

cp ${COMIN}/mdl_hrqpf_rawxfcst.t${cyc}z.reg?? .
cp ${COMIN}/mdl_hrqpf_fnlfcst_vecra.t${cyc}z .

#######################################################################
#
# PROGRAM RACREATE - MOS-2000 PROGRAM WHICH
#                   CREATES RANDOM ACCESS FILES; IN THIS CASE, THE
#                   CODE IS USED TO CREATE THE OPERATIONAL MOS
#                   FORECAST FILE.
#
#######################################################################

echo QPFLOG: `date` - begin job RACREATE - CREATE MDL_HRQPF FINAL FORECAST FILE

export pgm="mdl_racreate"
. prep_step
export XLFUNIT_50="${DATA}/mdl_hrqpf_fnlxfcst_vecra.t${cyc}z"

startmsg
${EXECmdl}/mdl_racreate < ${PARMmdl}/mdl_hrqpf_racreate.cn > ${pgmout} 2>errfile
export err=$?; err_chk

echo QPFLOG: `date` -  RACREATE ended

#######################################################################
#
#  PROGRAM RA2RA  - MERGES RANDOM ACCESS FILES FROM RAW REGIONAL 
#                   FORECASTS INTO ONE RANDOM ACCESS FILE FOR USE IN 
#                   FCSTPOST
#
#######################################################################
 
echo QPFLOG: `date` - begin job RA2RA - MERGE RAW FORECAST FILES

export pgm="mdl_hrqpf_ra2ra"
. prep_step
export XLFUNIT_26="${FIXmdl}/mdl_hrqpf_stations.tbl"
export XLFUNIT_27="${FIXmdl}/mdl_hrqpf_stations.tbl"
export XLFUNIT_60="mdl_hrqpf_fnlxfcst_vecra.t${cyc}z"
export XLFUNIT_30="mdl_hrqpf_rawxfcst.t${cyc}z.reg01"
export XLFUNIT_31="mdl_hrqpf_rawxfcst.t${cyc}z.reg02"
export XLFUNIT_32="mdl_hrqpf_rawxfcst.t${cyc}z.reg03"
export XLFUNIT_33="mdl_hrqpf_rawxfcst.t${cyc}z.reg04"
export XLFUNIT_34="mdl_hrqpf_rawxfcst.t${cyc}z.reg05"
export XLFUNIT_35="mdl_hrqpf_rawxfcst.t${cyc}z.reg06"
export XLFUNIT_36="mdl_hrqpf_rawxfcst.t${cyc}z.reg07"
export XLFUNIT_37="mdl_hrqpf_rawxfcst.t${cyc}z.reg08"
export XLFUNIT_38="mdl_hrqpf_rawxfcst.t${cyc}z.reg09"
export XLFUNIT_39="mdl_hrqpf_rawxfcst.t${cyc}z.reg10"
export XLFUNIT_40="mdl_hrqpf_rawxfcst.t${cyc}z.reg11"
export XLFUNIT_41="mdl_hrqpf_rawxfcst.t${cyc}z.reg12"
export XLFUNIT_42="mdl_hrqpf_rawxfcst.t${cyc}z.reg13"
export XLFUNIT_43="mdl_hrqpf_rawxfcst.t${cyc}z.reg14"
export XLFUNIT_44="mdl_hrqpf_fnlfcst_vecra.t${cyc}z"

startmsg
${EXECmdl}/mdl_hrqpf_ra2ra < ${PARMmdl}/mdl_hrqpf_ra2ra-extpost.cn >> ${pgmout}_ra2ra 2>errfile
export err=$?; err_chk

echo QPFLOG: `date` -  RA2RA ended

#######################################################################
#
#    PROGRAM FCSTPOST - POST-PROCESSES ENSMOS FORECASTS
#
#######################################################################

echo QPFLOG: `date` - begin job FCSTPOST - POST PROCESS HRQPF FORECASTS

export pgm="mdl_hrqpf_fcstpost"
. prep_step
export XLFUNIT_10="${DATA}/ncepdate"
export XLFUNIT_27="${FIXmdl}/mdl_hrqpf_stations.tbl"
export XLFUNIT_28="${FIXmdl}/mdl_hrqpf_extpost_idlist.t${cyc}z"
export XLFUNIT_29="${FIXmdl}/mdl_hrqpf_idtable"
export XLFUNIT_45="${FIXmdl}/mdl_hrqpf_mxub-wts.${season}.t${cyc}z"
export XLFUNIT_47="${FIXmdl}/mdl_hrqpf_thresh.${season}.t${cyc}z"
export XLFUNIT_49="${DATA}/mdl_hrqpf_fnlxfcst_vecra.t${cyc}z"
export XLFUNIT_30="${DATA}/mdl_hrqpf_fnlxfcst_vecsq.${PDY}${cyc}"

startmsg
${EXECmdl}/mdl_hrqpf_fcstpost < ${PARMmdl}/mdl_hrqpf_post.cn > ${pgmout} 2>errfile
export err=$?; err_chk

######################################################################
#
#   COPY OUTPUT TO COMPUT
#
#####################################################################

if test ${SENDCOM} = 'YES'
then
  cp mdl_hrqpf_fnlxfcst_vecra.t${cyc}z ${COMOUT}
  cp mdl_hrqpf_fnlxfcst_vecsq.${PDY}${cyc} ${COMOUT}
fi

echo QPFLOG: `date` -  FCSTPOST ended
exit 0


