#!/bin/sh

##################################################################################
#  UTILITY SCRIPT NAME :  extpc_psurge_awips.sh.sms
#         DATE WRITTEN :  March 03, 2008
#
#  Abstract:  This utility script produces the Probability Storm Surge  and Storm
#             Surge Heights for AWIPS
#
#  History: March 3 2008 - Original script from Boi Vuong  at NCO
#
##################################################################################

##################################################################################
echo "---------------------------------------------------------------------------"
echo "extpc_psurge_awips.sh.sms - Generate the Probability Storm Surge for AWIPS"
echo "---------------------------------------------------------------------------"
##################################################################################

set -x
cd $DATA

msg="TPC psurge post-processing for AWIPS has begun on `hostname` at `date`"
postmsg "$jlogfile" "$msg"
startmsg

#  Retrieve storm-id in psurge
export stormlabel=storm01
cp $COM_ZSURGE/queue/${stormlabel}.txt .

export DBNALERT_TYPE=${DBNALERT_TYPE:-NTC_LOW}
export stormid=`cat ${stormlabel}.txt |awk '{print $1}`
export AdvTime=`cat ${stormlabel}.txt |awk '{print $2}`
export NumThresh=${NumThresh:-25}
export NumExceed=${NumExceed:-90}

echo " ------------------------------------------"
echo " BEGIN MAKING Storm Surge AWIPS PRODUCTS"
echo " ------------------------------------------"


##########################################################
# Process  Probablity of Storm Surge for AWIPS Products
##########################################################


thresh=2
while [ $thresh -le $NumThresh ]
do

    if test -f $COMOUT/${stormid}/${stormid}_${AdvTime}_gt${thresh}_1.grb
    then
        export XLFRTEOPTS="unit_vars=yes"     # Allow overriding default names.
        export XLFUNIT_11=$COMOUT/${stormid}/${stormid}_${AdvTime}_gt${thresh}_1.grb
        export XLFUNIT_31=""
        export XLFUNIT_51=grib2.t${cyc}z.awips_${stormid}_${AdvTime}.gt${thresh}_1
        $utilexec/tocgrib2 < $PARMpsurge/grib2_awps_stormsurge.gt${thresh}_1
        export err=$?; err_chk

    ##############################
    # Post Files to PCOM
    ##############################
 
        if test "$SENDCOM" = 'YES'
        then
            cp grib2.t${cyc}z.awips_${stormid}_${AdvTime}.gt${thresh}_1  $pcom/grib2.t${cyc}z.awips_${stormid}_${AdvTime}.gt${thresh}_1
         fi
     ##############################
     # Distribute Data
     ##############################

         if [ "$SENDDBN" = 'YES' ] ; then
            $DBNROOT/bin/dbn_alert $DBNALERT_TYPE $NET $job $pcom/grib2.t${cyc}z.awips_${stormid}_${AdvTime}.gt${thresh}_1
         else
            msg="File $output_grb.$job not posted to db_net."
            postmsg "$jlogfile" "$msg"
          fi
      fi
    let "thresh=thresh+1"
done

##########################################################
# Process Exceedance of Storm Surge for AWIPS Products
##########################################################

exceed_thresh=10
while [ $exceed_thresh -le $NumExceed ]
do

  if  test -f $COMOUT/${stormid}/${stormid}_${AdvTime}_e${exceed_thresh}_1.grb
  then
       export XLFRTEOPTS="unit_vars=yes"     # Allow overriding default names.
       export XLFUNIT_11=$COMOUT/${stormid}/${stormid}_${AdvTime}_e${exceed_thresh}_1.grb
       export XLFUNIT_31=""
       export XLFUNIT_51=grib2.t${cyc}z.awips_${stormid}_${AdvTime}.e${exceed_thresh}_1
       $utilexec/tocgrib2 < $PARMpsurge/grib2_awps_stormsurge.e${exceed_thresh}_1
       export err=$?; err_chk

  ##############################
  # Post Files to PCOM
  ##############################

      if test "$SENDCOM" = 'YES'
      then
          cp grib2.t${cyc}z.awips_${stormid}_${AdvTime}.e${exceed_thresh}_1 $pcom/grib2.t${cyc}z.awips_${stormid}_${AdvTime}.e${exceed_thresh}_1
      fi
 
  ##############################
  # Distribute Data
  ##############################

       if [ "$SENDDBN" = 'YES' ] ; then
         $DBNROOT/bin/dbn_alert $DBNALERT_TYPE $NET $job  $pcom/grib2.t${cyc}z.awips_${stormid}_${AdvTime}.e${exceed_thresh}_1
       else
         msg="File $output_grb.$job not posted to db_net."
         postmsg "$jlogfile" "$msg"
       fi
  fi
  let "exceed_thresh=exceed_thresh+10"
done
  rm -rf $COM_ZSURGE/queue/${stormlabel}.txt
####################################################################
# GOOD RUN
set +x
echo "**************JOB $job COMPLETED NORMALLY on `hostname` at `date`"
echo "**************JOB $job COMPLETED NORMALLY on `hostname` at `date`"
echo "**************JOB $job COMPLETED NORMALLY on `hostname` at `date`"
set -x
#####################################################################
