#!/bin/sh 
date
export PS4=' $SECONDS + '
set -x

####################################
# obtain unique process id (pid) and make temp directory
####################################
export DATA=${DATA:-${DATAROOT:?}/${jobid}}
mkdir -p $DATA
cd $DATA
export cycle=t${cyc}z

####################################
# File To Log Msgs
####################################
#export jlogfile=${jlogfile:-${DATA}/jlogfile.${jobid}}

export ECMWF_FILE_EXT=${ECMWF_FILE_EXT:-1}
####################################
# Specify NET and RUN Name and model
####################################
export NET=${NET:-ens_tracker}
export RUN=${RUN:-fens}

####################################
# Determine Job Output Name on System
####################################
export pgmout="OUTPUT.$$"
export pgmerr=errfile

####################################
# SENDECF  - Flag Events on ecFLOW
# SENDCOM  - Copy Files From TMPDIR to $COMOUT
# SENDDBN  - Issue DBNet Client Calls
####################################
export SENDECF=${SENDECF:-NO}
export SENDCOM=${SENDCOM:-YES}
export SENDDBN=${SENDDBN:-NO}

####################################
# Specify Execution Areas
####################################
export HOMEens_tracker=${HOMEens_tracker:-${PACKAGEHOME}}
export EXECens_tracker=${EXECens_tracker:-$HOMEens_tracker/exec}
export FIXens_tracker=${FIXens_tracker:-$HOMEens_tracker/fix}
export USHens_tracker=${USHens_tracker:-$HOMEens_tracker/ush}

##############################
setpdy.sh
. ./PDY

##############################################
# Define COM directories
##############################################
export JYYYY=`echo ${PDY} | cut -c1-4`
export DCOM=${DCOM:-/lfs/h1/ops/prod/dcom/navgem}
export COMINgfs=${COMINgfs:-$(compath.py $envir/com/gfs/${gfs_ver})/gfs.${PDY}}
export COMINsyn=${COMINsyn:-$(compath.py $envir/com/gfs/${gfs_ver})/syndat}

export COMOUT=${COMOUT:-$(compath.py -o ${NET}/${ens_tracker_ver})/${RUN}.${PDY}/${cyc}/tctrack}
export COMOUThur=${COMOUThur:-$(compath.py -o ${NET}/${ens_tracker_ver})/global}
export COMOUTatcf=${COMOUTatcf:-$(compath.py -o ${NET}/${ens_tracker_ver})/atcf}
mkdir -m 775 -p $COMOUT $COMOUThur $COMOUTatcf

msg="HAS BEGUN on `hostname`"
postmsg "$jlogfile" "$msg"

env

#############################################################
# Execute the script
export cmodel=ngps
export loopnum=1
export ymdh=${PDY}${cyc}

export pert="p01"
pertdir=${DATA}/${cmodel}/${pert}
mkdir -p $pertdir

#-----------input data checking -----------------
${USHens_tracker}/data_check.sh
# exit code 6 = missing data of opportunity
if [ $? -eq 6 ]; then exit; fi
#------------------------------------------------

${USHens_tracker}/extrkr_g2.sh ${loopnum} ${cmodel} ${ymdh} ${pert} ${pertdir} #2>&1 >${outfile}
export err=$?; err_chk

if [ -s "missing_ngps.txt" ]; then
   mail.py -s "Missing NAVGEM data in $job" <<ENDMSG
One or more Navy Global Environmental Model files are missing, including
$(sort -u missing_ngps.txt | awk '$0="<li>"$0"</li>"')

$0 could not process all expected data.
ENDMSG
fi

#if [ ${SENDCOM} = 'NO' ]; then
#  cp ${pertdir}/trak.ngx.atcfunix.${PDY}${cyc} ${COMOUT}/track/ngx.t${cyc}z.cyclone.trackatcfunix
#fi  
#############################################################

msg="JOB COMPLETED NORMALLY"
postmsg "$jlogfile" "$msg"

##############################
# Remove the Temporary working directory
##############################
if [[ $KEEPDATA != "YES" ]]; then
  rm -rf $DATA
fi

date