#!/usr/bin/env sh

#### UNIX Script Documentation Block
#
# Script Name:  JNWM_RFC_TIMESERIES
####

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

##########################
# Specify NET and RUN Name 
##########################
export NET=nwm
export RUN=nwm

########################################################
# Make working directory and file To Log Msgs 
########################################################
export DATA=${DATA:-${DATAROOT:?}/${jobid}}

if [ ! -e $DATA ]; then
  mkdir -p $DATA
fi

cd ${DATA}

export cycle=t${cyc}z

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


####################################
# Specify Application Areas
####################################
export HOMEnwm=${HOMEnwm:-${PACKAGEROOT:?}/${NET}.${nwm_ver}}
export USHnwm=${USHnwm:-${HOMEnwm}/ush}
export SCRIPTnwm=${SCRIPTnwm:-${HOMEnwm}/scripts}

###################################
# define dbn_alert type
###################################
export DBN_ALERT_TYPE=${DBN_ALERT_TYPE:-NWM_RFC_TIMESERIES}
export SENDDBN=NO		#No need to alert per developer

###################################
# Input data directory
###################################
export COMIN=${COMIN:-$(compath.py -o ${RUN}/${nwm_ver})/$RUN.$PDY}
export COMINm1=${COMINm1:-$(compath.py -o ${RUN}/${nwm_ver})/$RUN.$PDYm1}
export COMINm2=${COMINm2:-$(compath.py -o ${RUN}/${nwm_ver})/$RUN.$PDYm2}
export COMINm3=${COMINm3:-$(compath.py -o ${RUN}/${nwm_ver})/$RUN.$PDYm3}
export DCOM=${DCOM:-${DCOMROOT}/RFC_data/Reservoir}

###################################
# Output data directory
###################################
export COMOUT=${COMOUT:-$(compath.py -o ${RUN}/${nwm_ver})/$RUN.$PDY}
export COMOUT_ROOT=${COMOUT_ROOT:-$(compath.py -o ${RUN}/${nwm_ver})}

if [ ! -e $COMOUT ]; then
  mkdir -p $COMOUT
fi

####################################
# File To Log Msgs
####################################
export DATAlogs=$DATA/logs
export pgmout="${DATAlogs}/${job}_OUTPUT_${PDY}.$$"

if [ ! -e $DATAlogs ]; then
  mkdir -p $DATAlogs
fi

export COMOUTlogs=${COMOUTlogs:-${COMOUT}/logs}

if [ ! -e $COMOUTlogs ]; then
   mkdir -p $COMOUTlogs
fi

env   


########################################################
# Execute the script.
########################################################
${SCRIPTnwm}/exnwm_rfc_timeseries.sh

msg="JOB $job FOR NWM RFC TIME SERIES HAS COMPLETED NORMALLY"
postmsg $pgmout "$msg"

if [ -e $pgmout ]; then
   cat $pgmout
   cpfs $pgmout $COMOUTlogs/
fi

cd $DATA/..
if [ "$KEEPDATA" != YES ]; then
  rm -rf $DATA
fi

date