#!/bin/sh

#####################################################################
# Script tpc_psurgeSlosh.sh runs the SLOSH model for the Probability 
# Storm Surge
# usage: tpc_psurgeSlosh.sh <ScriptDir> <stormAdvDir> <runID> <numInst>
#####################################################################
set -x 

if [[ -z $2 ]] || [[ ! -z $3 ]]
then
  echo "usage: $0 <ScriptDir> <stormAdvDir>"
  exit 1
fi

ScriptDir=${1:-$DATA}
stormAdvDir=${2:-$DATA}

export PS4='slosh_T$SECONDS + '

if [ $RUN_ENVIR = "NCO" ]
then
   EXECpsurge=${EXECpsurge:-/nwprod/exec}
   SloshbsnDir=$PARMpsurge/tpc_sloshbsn
else
   SloshbsnDir=${ScriptDir}/../parm/tpc_sloshbsn
   EXECpsurge=${ScriptDir}/../exec
fi

# Setup the working directory for this instance
mkdir -p $DATA/slosh
cd $DATA/slosh

/nwprod/util/ush/setup.sh

#######################################################
# Check for existance of storm list.
#######################################################
listName=${stormAdvDir}/run/master.txt
doneFile=${stormAdvDir}/run/nhctrkDone.txt
ansDir=${stormAdvDir}/ans
while [[ ! -e ${listName} ]]
do
  sleep 60
done

#######################################################
# Run the storms (while tpc_psurgeNhcTrk is running).
#######################################################
${EXECpsurge}/tpc_psurge_slosh env ${SloshbsnDir} \
       ${listName} ${doneFile} ${ansDir} >>slosh.out
export err=$?; err_chk
exit 0
