#!/bin/bash
set -x

export RUN_ENVIR=${RUN_ENVIR:-"nco"}
export PS4='$SECONDS + '
date


#############################
# Source relevant config files
#############################
export EXPDIR=${EXPDIR:-$HOMEgfs/parm/config}
config_path=${EXPDIR:-$NWROOT/gfs.${gfs_ver}/parm/config}
configs="base fcst efcs"
for config in $configs; do
    . $config_path/config.$config
    status=$?
    [[ $status -ne 0 ]] && exit $status
done


##########################################
# Source machine runtime environment
##########################################
. $HOMEgfs/env/${machine}.env efcs
status=$?
[[ $status -ne 0 ]] && exit $status


##############################################
# Obtain unique process id (pid) and make temp directory
##############################################
export pid=${pid:-$$}
export outid=${outid:-"LL$job"}
export DATA=${DATA:-${DATAROOT}/${jobid:?}}
mkdir -p $DATA
cd $DATA


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


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


##############################################
# Set variables used in the script
##############################################
export CDATE=${CDATE:-${PDY}${cyc}}
export CDUMP=${CDUMP:-${RUN:-"gdas"}}
export COMPONENT=${COMPONENT:-atmos}


##############################################
# Begin JOB SPECIFIC work
##############################################

export CASE=$CASE_ENKF

# COMOUT is used in script
export COMOUT="$ROTDIR/enkf$CDUMP.$PDY/$cyc/$COMPONENT"


# Forecast length for EnKF forecast
export FHMIN=$FHMIN_ENKF
export FHOUT=$FHOUT_ENKF
export FHMAX=$FHMAX_ENKF


# Get ENSBEG/ENSEND from ENSGRP and NMEM_EFCSGRP
export ENSEND=$((NMEM_EFCSGRP * 10#${ENSGRP}))
export ENSBEG=$((ENSEND - NMEM_EFCSGRP + 1))


###############################################################
# Run relevant script
env
echo "HAS BEGUN on $(hostname)"
$LOGSCRIPT


${ENKFFCSTSH:-$SCRgfs/exgdas_enkf_fcst.sh}
status=$?
[[ $status -ne 0 ]] && exit $status


# Double check the status of members in ENSGRP
EFCSGRP=$COMOUT/efcs.grp${ENSGRP}
npass=0
if [ -f $EFCSGRP ]; then
    npass=$(grep "PASS" $EFCSGRP | wc -l)
fi
echo "$npass/$NMEM_EFCSGRP members successfull in efcs.grp$ENSGRP"
if [ $npass -ne $NMEM_EFCSGRP ]; then
    echo "FATAL ERROR: Failed members in group $ENSGRP, ABORT!"
    cat $EFCSGRP
    exit 99
fi


##############################################
# Send Alerts
##############################################
if [ $SENDDBN = YES ] ; then
   $DBNROOT/bin/dbn_alert MODEL ENKF1_MSC_fcsstat $job $EFCSGRP
fi


##############################################
# End JOB SPECIFIC work
##############################################

##############################################
# Final processing
##############################################
if [ -e "$pgmout" ] ; then
  cat $pgmout
fi


echo "ENDED NORMALLY."


##########################################
# Remove the Temporary working directory
##########################################
cd $DATAROOT
[[ $KEEPDATA = "NO" ]] && rm -rf $DATA

date
exit 0