#! /bin/sh

date
export PS4=' $SECONDS + '
set -xue

####################################
# Set default values if not yet set
# values from ecFlow or modules will override these
####################################
export DATA=${DATA:-${DATAROOT:?}/hwrf${storm_num:?}_${cyc:?}_${envir:?}}
export jlogfile=${jlogfile:-${DATA}/jlogfile.${jobid}}
export cycle=${cycle:-t${cyc}z}

####################################
# Initialize work area and cd there
####################################
mkdir -p $DATA
cd $DATA

####################################
# Set HWRF version number if unset
####################################
export HWRF_VERSION=${HWRF_VERSION:-${hwrf_ver/v/}}

####################################
# Variables for logging & DBnet
####################################
export NET=hwrf RUN=hwrf
export EMAIL_SDM=${EMAIL_SDM:-YES}

####################################
# Set threading defaults
####################################
export OMP_NUM_THREADS=1
export OMP_STACKSIZE=128M
export MKL_NUM_THREADS=1

####################################
# Set HWRF-specific vars
####################################
# Top of HWRF installation tree:
export HOMEhwrf=${HOMEhwrf:-${OPSROOT:?}/hwrf.$hwrf_ver}
# source $HOMEhwrf/versions/run.ver

# Do not source the holdvars for the launch job since the holdvars
# does not exist yet!
# We do need to set these variables that holdvars normally sets:
export USHhwrf=$HOMEhwrf/ush
export EXhwrf=$HOMEhwrf/scripts
export PARMhwrf=$HOMEhwrf/parm

# Get PDY
. ./PDY

# Upstream and downstream areas:
export stormlabel=${stormlabel:-storm$storm_num}
COMROOTnhc=${COMROOTnhc:-${COMROOT/nhc}}
export COMINnhc=${COMINnhc:-${COMROOTnhc:?}/storm-data/ncep}
export COMINobs=${COMINobs:-$(compath.py ${envir}/obsproc/${obsproc_ver})}
export COMINgfs=${COMINgfs:-$(compath.py ${envir}/gfs/${gfs_ver})}
export COMINgdas=${COMINgdas:-$(compath.py ${envir}/gfs/${gfs_ver})}
export COMINarch=${COMINarch:-$(compath.py ${envir}/gfs/${gfs_ver})/syndat}
export COMINrtofs=${COMINrtofs:-$(compath.py ${envir}/rtofs/$rtofs_ver)}
export COMINmsg=${COMINmsg:-$(compath.py ${envir}/hafs/${hafs_ver})/inphfsa}

# HWRF areas:
priorymdh=$( $NDATE -6 "$PDY$cyc" )
pre_YMD=`echo $priorymdh |cut -c1-8`
pre_H=`echo $priorymdh |cut -c9-10`
export HISTDATA=${HISTDATA:-$(compath.py ${envir}/${NET}/${hwrf_ver})/$RUN.$pre_YMD/$pre_H}
export COMIN=${COMIN:-$(compath.py ${envir}/${NET}/${hwrf_ver})/$RUN.$PDY/$cyc}
export COMOUT=${COMOUT:-$(compath.py ${envir}/${NET}/${hwrf_ver})/$RUN.$PDY/$cyc}

# Point to stormX.conf generated by launcher job:
export CONFhwrf=$COMIN/storm$storm_num.conf

# Prepend $USHhwrf to python package search path:
export PYTHONPATH=$USHhwrf${PYTHONPATH:+:$PYTHONPATH}

# Make sure all mandatory variables are set: cyc, storm_num
set -u +x
echo "Checking for mandatory variables:"
echo "cyc=$cyc storm_num=$storm_num"
set +u -x

####################################
# Set/check job-specific vars if any
####################################

####################################
# Ensure aprun works
####################################
#aprun -n 1 -d 1 -j 1 -N 1 /bin/echo Aprun Test

####################################
# Pass control to ex-script
####################################

postmsg "$jlogfile" "Starting exhwrf_ensda_relocate_pre.py."
$EXhwrf/exhwrf_ensda_relocate_pre.py
export err=$?
err_chk
postmsg "$jlogfile" "exhwrf_ensda_relocate_pre.py ended normally."