#!/usr/bin/env sh

#### UNIX Script Documentation Block
#
# Script Name:  JNWM_FORCING_LONG_RANGE
####

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

##########################
# Specify NET and RUN Name 
##########################
export NET=${NET:-nwm}
export RUN=${RUN:-nwm}

export cycle=${cycle:-t${cyc}z}

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

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

cd ${DATA}

###################################
# Set up the PDY
###################################
setpdy.sh
. ./PDY

###################################
# set PDY for 06 and 18z cycles
###################################
#if [ $mem = '01' ]; then
#  if [[ $cyc = '06' || $cyc == '18' ]]; then
#    expor. ./PDY=$PDYm1;
#    setpdy.sh
#    . ./PDY
#  fi
#fi

####################################
# Determine Job Output Name on System
####################################
export pgmout="OUTPUT.${job}_${PDY}${cyc}.$$"

####################################
# Specify Application Areas
####################################
export HOMEnwm=${HOMEnwm:-${PACKAGEROOT:?}/${model}.${nwm_ver}}
export LIBnwm=${LIBnwm:-${HOMEnwm}/lib}
export USHnwm=${USHnwm:-${HOMEnwm}/ush}
export PARMnwm=${PARMnwm:-${HOMEnwm}/parm}
export SCRIPTnwm=${SCRIPTnwm:-${HOMEnwm}/scripts}

###################################
# define dbn_alert type
###################################
export DBN_ALERT_TYPE=${DBN_ALERT_TYPE:-NWM_NCDF}

#####################################
# Input data directory
#####################################
if [ "$InputDataFormat" == "GRIB2" ]; then
   export COMINcfs=${COMINcfs:-$(compath.py ${envir}/cfs/$cfs_ver)}
else
   export COMINcfs=${COMINcfs:-$(compath.py ${envir}/$NET/${nwm_ver})/$RUN.grib2netcdf}
fi

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

export LSB_OUTPUTFILE="${PBS_O_WORKDIR}/${PBS_JOBNAME}.o${PBS_JOBID%.*}"

if [ "$SENDCOM" = YES ]; then
   mkdir -p $COMOUT $GESOUT 
fi

####################################
# File To Log Msgs
####################################
export COMOUTlogs=${COMOUTlogs:-${COMOUT}/logs}

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

env   


########################################################
# Execute the script.
########################################################
export MEM=${mem}

${SCRIPTnwm}/exnwm_forcing_long_range.sh
export err=$?; err_chk

msg="JOB $job HAS COMPLETED NORMALLY."
postmsg $pgmout "$msg"

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

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

date