#!/bin/ksh

#################################################################
# 9/20/2009, Julia Zhu   Scripts are modified to be sharable
#                        between EMC and NCO
#         Please note that variable "RUN_ENVIR" is set and used
#         in the development enviroment only.
# 3/07/2022, Jun Du: Modified to WCOSS2
#################################################################

############################################
# Set up environment for ensemble products job
############################################

set -xa
# #### 04/16/2001 ###################
# SET SHELL PROCESSING VARIABLES
# ###################################
export PS4='$SECONDS + ' 
date

export RUN_ENVIR=${RUN_ENVIR:-nco}

export NET=sref
export RUN=sref


###########################################
# Run sref_config to get input parameters
###########################################
if [ "$RUN_ENVIR" != nco ]    ### For Developers
then
  . /lfs/h2/emc/lam/noscrub/$LOGNAME/sref${NWROOTp1}/parm/sref_para_config_cray
  export DATA_IN=${DATA_IN:-/emc/ptmp/$LOGNAME${DATAROOT}}

  export SENDCOM=${SENDCOM:-YES}
  export SENDDBN=${SENDDBN:-NO}
  export SENDECF=${SENDECF:-YES}

  export HOMEsref=${HOMEsref:-${PACKAGEROOT}}
  export EXECsref=${EXECsref:-$HOMEsref/exec}
  export FIXsref=${FIXsref:-$HOMEsref/fix}
  export PARMsref=${PARMsref:-$HOMEsref/parm}
  export USHsref=${USHsref:-$HOMEsref/ush}

  export com=${com:-/lfs/h2/emc/lam/noscrub/$LOGNAME${COMROOT}/${NET}/${envir}}
  export COM_IN=${COM_IN:-/lfs/h2/emc/lam/noscrub/$LOGNAME${COMROOT}/${NET}/${envir}}
  export COM_OUT=${COM_OUT:-/lfs/h2/emc/lam/noscrub/$LOGNAME${COMROOT}/${NET}/${envir}}

  export COMIN=$COM_IN/${RUN}.${PDY}/${cyc}/ensprod
  export COMOUT=$COM_OUT/${RUN}.${PDY}/${cyc}/misc
  export pcom_in=${pcom_in:-/pcom}
  export pcom=$pcom_in/${RUN}.${PDY}

fi

###############################################################
# This block can be modified for different test environment
###############################################################
# 
# obtain unique process id (pid) and make temp directories
#
export pid=$$
export DATA_IN=${DATA_IN:-${DATAROOT}}
export DATA=$DATA_IN/${jobid:?}

mkdir -p $DATA
cd $DATA

####################################
# File To Log Msgs
####################################
export jlogfile=${jlogfile:-$DATA/jlogfile.${job}.$$}

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

export cycle=t${cyc}z 

# Run setpdy and initialize PDY variables
setpdy.sh
. ./PDY

##################################################
# SAVEGES  - Copy Files From TMPDIR to $GESdir
# SENDECF  - Flag Events on ecFflow
# SENDCOM  - Copy Files From TMPDIR to $COMOUT
# SENDDBN  - Issue DBNet Client Calls
# RERUN    - Rerun fcst from beginning (default no)
##################################################
export SENDCOM=${SENDCOM:-YES}
export SENDECF=${SENDECF:-YES}
export SENDDBN=${SENDDBN:-YES}

export HOMEsref=${HOMEsref:-${PACKAGEROOT}/${NET}.${sref_ver}}
export EXECsref=${EXECsref:-$HOMEsref/exec}
export FIXsref=${FIXsref:-$HOMEsref/fix}
export PARMsref=${PARMsref:-$HOMEsref/parm}
export USHsref=${USHsref:-$HOMEsref/ush}
export PARMshared=${PARMsref}                 # for util_shared.v?.?.?/ush/mkawpgrb.sh

###################################
# Set up the UTILITIES
###################################

msg="JOB $job HAS BEGUN"
postmsg $jlogfile "$msg"

export COM_IN=${COM_IN:-$(compath.py ${NET}/${sref_ver})}
export COMIN=$COM_IN/${RUN}.${PDY}/${cyc}/ensprod
export COMINpgrb=$COM_IN/${RUN}.${PDY}/${cyc}/pgrb

export COM_OUT=${COM_OUT:-$(compath.py ${NET}/${sref_ver})}
export COMOUT=${COMOUT:-$COM_OUT/${RUN}.${PDY}/${cyc}/wmo}  # used for dbnlog
export COMOUTwmo=${COMOUTwmo:-$COM_OUT/${RUN}.${PDY}/${cyc}/wmo}

mkdir -p $COMOUT $COMOUTwmo
 
###################################################
# Execute the Script
#
$HOMEsref/ush/sref_awips.sh
###################################################

cat $pgmout

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

cd $DATA_IN
if [ ${KEEPDATA:-NO} != YES ] ; then rm -rf $DATA ; fi

date