#!/bin/sh
###############################################################################
#                                                                             #
# This script is the SPC SREF GEMPAK processing script                        #
#                                                                             #
# 3/20/2006  Luke Lin      modified from SPC DAVID BRIGHT's script            #
# 10/16/2008 David Bright  modified for SREF upgrade including hourly data    #
#                                                                             #
###############################################################################

set -x
cd $DATA

export workdir=$DATA

if [ $fcst_hr = '01' -o $fcst_hr = '02' -o $fcst_hr = '04' -o $fcst_hr = '05' -o $fcst_hr = '07' -o $fcst_hr = '08' -o $fcst_hr = '10' -o $fcst_hr = '11' -o $fcst_hr = '13' -o $fcst_hr = '14' -o $fcst_hr = '16' -o $fcst_hr = '17' -o $fcst_hr = '19' -o $fcst_hr = '20' -o $fcst_hr = '22' -o $fcst_hr = '23' -o $fcst_hr = '25' -o $fcst_hr = '26' -o $fcst_hr = '28' -o $fcst_hr = '29' -o $fcst_hr = '31' -o $fcst_hr = '32' -o $fcst_hr = '34' -o $fcst_hr = '35' -o $fcst_hr = '37' -o $fcst_hr = '38' ]
then
cp ${FIXspc}/spc_fields.ens.off_hrs    fields.ens
cp ${PARMspc}/spc_sref_members.21      spc_sref_members
else
cp ${FIXspc}/spc_fields.ens            fields.ens
cp ${PARMspc}/spc_sref_members.22      spc_sref_members
fi
cp ${FIXspc}/spc_fields.mne            fields.mne
cp ${FIXspc}/spc_hicape_model_flag_gem hicape_model_flag_gem.out
#DRB cp ${PARMspc}/spc_sref_members .

fcsthr=$fcst_hr
sed s/_PDY_/$PDY/g spc_sref_members > ./tmp1
sed s/_cycle_/$cycle/g ./tmp1 > ./tmp2
sed s/_fcsthrp3_/$fcsthrp3/g ./tmp2 > ./tmp3
sed s/_fcsthr_/$fcsthr/g ./tmp3 > ./sref.input_files

#DRB for member in `cat sref.input_files | grep -v f$fcsthrp3.gem`
for member in `ls -1 ${COMIN}/ | grep .gem |grep "t${cyc}z"`
do 
#DRB   if [ ! -f ${COMIN}/$member ]
#DRB   then
#DRB        err=1;export err; err_chk
#DRB   else
#DRB        cp ${COMIN}/$member .
#DRB   fi
ln -s ${COMIN}/$member ./$member
done

if [ "${fcsthrp3}" -le "84" ]
then
#DRB   cp ${COMIN_NAM}/nam40_${PDY}${lag_hr}f0${fcsthrp3} \
#DRB                      ${PDY}_sref_eta.${cycle}.pgrb212.tl.f${fcsthrp3}.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f000 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f00.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f003 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f03.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f006 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f06.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f009 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f09.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f012 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f12.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f015 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f15.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f018 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f18.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f021 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f21.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f024 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f24.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f027 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f27.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f030 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f30.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f033 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f33.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f036 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f36.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f039 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f39.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f042 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f42.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f045 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f45.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f048 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f48.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f051 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f51.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f054 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f54.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f057 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f57.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f060 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f60.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f063 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f63.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f066 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f66.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f069 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f69.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f072 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f72.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f075 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f75.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f078 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f78.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f081 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f81.gem
   ln -s ${COMIN_NAM}/nam40_${PDY}${lag_hr}f084 ${PDY}_sref_eta.${cycle}.pgrb212.tl.f84.gem
fi

export pgm=spc_getens_data
. prep_step

msg="$job: spc_getens_data $the_run f$fcst_hr started"
postmsg "$jlogfile" "$msg"

${EXECspc}/spc_getens_data full >>$pgmout 2>errfile
export err=$?;err_chk

if [ $fcst_hr = '00' -o $fcst_hr = '03' -o $fcst_hr = '06' -o $fcst_hr = '09' -o $fcst_hr = '12' -o $fcst_hr = '15' -o $fcst_hr = '18' -o $fcst_hr = '21' -o $fcst_hr = '24' -o $fcst_hr = '27' -o $fcst_hr = '30' -o $fcst_hr = '33' -o $fcst_hr = '36' -o $fcst_hr = '39' -o $fcst_hr = '42' -o $fcst_hr = '45' -o $fcst_hr = '48' -o $fcst_hr = '51' -o $fcst_hr = '54' -o $fcst_hr = '57' -o $fcst_hr = '60' -o $fcst_hr = '63' -o $fcst_hr = '66' -o $fcst_hr = '69' -o $fcst_hr = '72' -o $fcst_hr = '75' -o $fcst_hr = '78' -o $fcst_hr = '81' -o $fcst_hr = '84' -o $fcst_hr = '87' ] 
then

${USHspc}/spc_cal_cptp.sh $fcst_hr $PDY $cyc

${USHspc}/spc_cal_cptp_hrly.sh $fcst_hr $PDY $cyc

${USHspc}/spc_cal_cptp_hrly_rgn3.sh $fcst_hr $PDY $cyc

${USHspc}/spc_cal_cptp_mid.sh $fcst_hr $PDY $cyc

${USHspc}/spc_cal_cptp_high.sh $fcst_hr $PDY $cyc

${USHspc}/spc_cal_cptp_dryt_hrly_rgn3.sh $fcst_hr $PDY $cyc

${USHspc}/spc_cal_cptp_dryt.sh $fcst_hr $PDY $cyc

${USHspc}/spc_cal_cptp_12hr_ccs.sh $fcst_hr $PDY $cyc

${USHspc}/spc_cal_cptp_24hr_ccs.sh $fcst_hr $PDY $cyc

${USHspc}/spc_cal_sevr.sh $fcst_hr $PDY $cyc

${USHspc}/spc_cal_int.sh $fcst_hr $PDY $cyc

${USHspc}/spc_cal_roadsnow.sh $fcst_hr $PDY $cyc

${USHspc}/spc_rename_snow.sh $fcst_hr $PDY $cyc

fi


if [ "$SENDCOM" = 'YES' ]
then
    cp spcsref_${PDY}${cyc}f0${fcst_hr}  $COMOUT/spcsref_${PDY}${cyc}f0${fcst_hr}.tmp
    mv $COMOUT/spcsref_${PDY}${cyc}f0${fcst_hr}.tmp  $COMOUT/spcsref_${PDY}${cyc}f0${fcst_hr}
#   cp spcsref_${PDY}*f0$fcst_hr* $COMOUT
fi
if [ "$SENDDBN" = 'YES' ]
then
     $DBNROOT/bin/dbn_alert MODEL SREF_GEMPAK SPC $COMOUT/spcsref_${PDY}${cyc}f0${fcst_hr}
fi

msg="$job completed normally"
postmsg "$jlogfile" "$msg"

# End of SPC SREF GEMPAK script -------------------------------------- #


