#!/bin/bash

########################################
# WAFS UPP PRODUCT GENERATION
########################################

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

####################################
# make temp directory
####################################
export DATA=${DATA:-${DATAROOT}/${jobid:?}}
mkdir -p "${DATA}" && cd "${DATA}"

###########################################
# Run setpdy and initialize PDY variables
###########################################
export cycle=${cycle:-"t${cyc}z"}
setpdy.sh
. ./PDY

####################################
# SENDCOM  - Copy Files From TMPDIR to $COMOUT
####################################
export SENDCOM=${SENDCOM:-"YES"}

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

##############################################
# Define COM directories
##############################################
export COMINgfs=${COMINgfs:-$(compath.py "${envir}/gfs/${gfs_ver}")"/gfs.${PDY}/${cyc}/atmos"}
export COMOUT=${COMOUT:-$(compath.py -o "${NET}/${wafs_ver}")"/${RUN}.${PDY}/${cyc}/upp"}
mkdir -p "${COMOUT}"

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

####################################
# Specify Execution Areas
####################################
export EXECwafs="${HOMEwafs}/exec"
export PARMwafs="${HOMEwafs}/parm"

#########################################################
# print current environment
#########################################################
env

#########################################################
# Execute the Script
#########################################################
# Run UPP with 1 thread
export OMP_NUM_THREADS=1

"${HOMEwafs}/scripts/exwafs_upp.sh"
export err=$?
err_chk

if [[ -e "${pgmout}" ]]; then
  cat "${pgmout}"
fi

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

date