#!/usr/bin/ksh
set -x

#######################
# tpc_psurge_blank
#######################

# advTime typically = 3, 9, 15, 21Z
# synTime typically = advTime - 3
# synTime = 0, 6, 12, 18Z
if [[ -z $4 ]] || [[ ! -z $5 ]]
then
  echo "usage: $0 <ScriptDir> <outDir> <advTime> <synTime>"
  exit 1
fi

ScriptDir=$1
# outDir=${stormAdvDir}/output
outDir=$2
advTime=$3
synTime=$4

ExecDir=${ScriptDir}/../exec
SloshbsnDir=${ScriptDir}/../parm/tpc_sloshbsn

#####
# Merge the results together into GRIB files.
#####
year=`echo ${advTime} | cut -c 1-4`
mon=`echo ${advTime} | cut -c 5-6`
day=`echo ${advTime} | cut -c 7-8`
hour=`echo ${advTime} | cut -c 9-10`
time=${year}-${mon}-${day}T${hour}0000

stormid=al0000${year}

if [ -s $COMOUT/${stormid}/*${advTime}* ]; then exit; fi

#####
# Create the blank psurge GRIB files
#####

if [ ! -d $COMOUT/${stormid} ]; then mkdir -p $COMOUT/${stormid}; fi

for thresh in 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
do
   ${ExecDir}/tpc_psurge_envmerge "" "" ${SloshbsnDir} prob ${SloshbsnDir}/conus.txt ${thresh} ${time} ${stormid}_${advTime}_gt${thresh}_1.grb
   cp ${stormid}_${advTime}_gt${thresh}_1.grb $COMOUT/${stormid}/.
   if [ $SENDDBN = YES ]; then
       $DBNROOT/bin/dbn_alert MODEL TPC_PSURGE_GB2 $job $COMOUT/${stormid}/${stormid}_${advTime}_gt${thresh}_1.grb
   fi
done

#####
# Create the blank exceedance GRIB files
#####
for thresh in 10 20 30 40 50 60 70 80 90
do
  exceed=$(expr 100 \- ${thresh})
  ${ExecDir}/tpc_psurge_envmerge "" "" ${SloshbsnDir} exceed ${SloshbsnDir}/conus.txt ${thresh} ${time} ${stormid}_${advTime}_e${exceed}_1.grb
  cp ${stormid}_${advTime}_e${exceed}_1.grb $COMOUT/${stormid}/.
  if [ $SENDDBN = YES ]; then
     $DBNROOT/bin/dbn_alert MODEL TPC_PSURGE_GB2 $job $COMOUT/${stormid}/${stormid}_${advTime}_e${exceed}_1.grb
  fi
done

#####
# Create the blank meta file
#####
metafile=psurge_${advTime}_${stormid}.meta
echo "STRMID, ${stormid}" > ${metafile}
echo "NAME, blank" >> ${metafile}
echo "FILE, blank" >> ${metafile}
echo "SYNOPTICTIME, ${synTime}" >> ${metafile}
echo "ADVNUM, blank" >> ${metafile}
echo "ADVTIME, ${advTime}" >> ${metafile}
echo "TIMEZONE, UTC" >> ${metafile}

cp $metafile $COMOUT/${stormid}/.
if [ $SENDDBN = YES ]; then
   $DBNROOT/bin/dbn_alert MODEL TPC_PSURGE_META $job $COMOUT/${stormid}/$metafile
fi

##########################
# GOOD RUN
exit
