#!/bin/ksh

echo "$(date -u) begin ${.sh.file}"

set -xa
if [[ ${STRICT:-NO} == "YES" ]]; then
	# Turn on strict bash error checking
	set -eu
fi
export PS4='$SECONDS + $(basename ${.sh.file})[$LINENO] '

####################################
# Determine Job Output Name on System
####################################
export jobid=${jobid:-"$job.$PBS_JOBID"}
export pgmout="OUTPUT.${PBS_JOBID}"
export pgmerr=errfile

####################################
# Specify and create working directory
####################################
export DATA=${DATA:-${DATAROOT}/${jobid}}
mkdir -p $DATA
cd $DATA

######################################
# Set up compute resources
######################################
export total_tasks=${total_tasks:-6}
export APRUN_MPMD=${gefsmpexec_mpmd:-"mpiexec -n $total_tasks cfp mpmd_cmdfile"}

####################################
# Specify NET and RUN Name and model
####################################
export NET=${NET:-gefs}
export envir=${envir:-prod}
export RUN=${RUN:-gefs}

######################################
# SENDCOM  - Copy Files From TMPDIR to $COMOUT
# SENDDBN  - Issue DBNet Client Calls
# SENDECF  - Flag Events on ECF
# VERBOSE  - Specify Verbose Output
######################################
export SENDCOM=${SENDCOM:-YES}
export SENDDBN=${SENDDBN:-YES}
export SENDECF=${SENDECF:-YES}
export VERBOSE=${VERBOSE:-YES}

######################################
# Set up the date information
######################################
export cycle=t${cyc}z
setpdy.sh
source PDY

####################################
# Specify Execution Areas
####################################
export HOMEgefs=${HOMEgefs:-${PACKAGEROOT}/gefs.${gefs_ver}}
export PARMgefs=${PARMgefs:-$HOMEgefs/parm}

##############################################
# Define COM directories
##############################################
ver=${ver:-$(echo ${gefs_ver}|cut -c1-5)}
export COMPONENT=atmos
export COMOUT=${COMOUT:-$(compath.py -o $NET/${ver})/${RUN}.${PDY}/$cyc/$COMPONENT}
export GESIN=${GESIN:-$(compath.py $envir/com/$NET/${ver})/${RUN}.${PDY}/$cyc/nwges}
export GESOUT=${GESOUT:-$(compath.py -o $NET/${ver})/${RUN}.${PDY}/$cyc/nwges}

#############################
# Source relevant config files
#############################
configs="gefs gefs_init"
config_path=$PARMgefs
for config in $configs; do
    . $config_path/$config.parm
    export err=$?
    if [[ $err != 0 ]]; then
        echo "FATAL ERROR in ${.sh.file}: Error while loading parm file $config_path/$config.parm"
        exit $err
    fi
done

echo "Environment before calling script"
env | sort

#############################################################
# Execute the script
$HOMEgefs/scripts/exgefs_atmos_init_recenter.sh
export err=$?
if [[ $err == 0 ]]; then
    echo "$job completed normally!"
else
    echo "FATAL ERROR in ${.sh.file}: $job failed!"
    exit $err
fi
#############################################################

##############################
# Remove the Temporary working directory
##############################
cd $DATAROOT
if [ ${KEEPDATA:-NO} = NO ] ; then rm -rf $DATA ; fi

echo "$(date -u) end ${.sh.file}"

exit $err