#!/bin/bash

############################################
# Set up environment for WRF BUFR JOB
# Change log:
#
# 08/01/2007, Jun Du: Initial Implementation
# 07/18/2008, Jun Du: upgraded to new mpiio wrfbufr version for 
#                     both WRF cores in order to match WRFv2.2
#
# MODEL(MEMBER ...)=NMM(ctl,p01,n01,p02,n02,p03,n03)
#                  +ARW(ctl,p01,n01,p02,n02,p03,n03)
#                  +NMB(ctl,p01,n01,p02,n02,p03,n03)
# 9/20/2009, Julia Zhu   Scripts are modified to be sharable
#                        between EMC and NCO
# 3/26/2012, Geoff Manikin   adapted for meanbufr
# 3/12/2019, Jun Du  Modified to run on Dell machine
# 
#         Please note that variable "RUN_ENVIR" is set and used
#         in the development enviroment only.
############################################
set -xa

export OMP_NUM_THREADS=1
export MP_EUIDEVICE=sn_all
export MP_EUILIB=us
export MP_MPILIB=mpich2
export MP_TASK_AFFINITY=core:1

# ###################################
# 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, "group_name" is passed from the ecFflow script
then
  CONFIG_FILE=${CONFIG_FILE:-/lfs/h2/emc/lam/noscrub/${LOGNAME}/sref.v7_dell/parm/sref_para_config_cray}
  . $CONFIG_FILE
  export userid=$LOGNAME
  export DATA_IN=${DATA_IN:-/lfs/h2/emc/ptmp/$userid}

  export SENDCOM=${SENDCOM:-YES}
  export SENDDBN=${SENDDBN:-NO}
  export SENDECF=${SENDECF:-YES}
  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 COMIN=$COM_IN/${RUN}.${PDY}/${cyc}
  export COMOUT=$COM_OUT/${RUN}.${PDY}/$cyc
  export COMIN_PROF=$COMOUT/bufr
fi

export jlogfile=${jlogfile:-$DATA/jlogfile.${job}.$$}
# 
# obtain unique process id (pid) and make temp directories
#
export DATA_IN=${DATA_IN:-${DATAROOT}}

if [ ${RUN_ENVIR} != nco ] ; then
export DATA=$DATA_IN/sref_meanbufr_${CYC}_${envir}     #for developer test
else
export DATA=$DATA_IN/${jobid:?}                        #for NCO production
fi

rm -rf $DATA
mkdir -p $DATA
cd $DATA 

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

export CYC=$cyc
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}

#############################################
# COMIN and COMOUT
#############################################
if [ ${RUN_ENVIR} != nco ] ; then
export COMIN=${COMIN:-${COMROOT}/${NET}/${envir}/${RUN}.${PDY}/$cyc}
export COMOUT=${COMOUT:-${COMROOT}/${NET}/${envir}/${RUN}.${PDY}/$cyc}
export COMIN_PROF=${COMIN_PROF:-${COMROOT}/${NET}/${envir}/${RUN}.${PDY}/$cyc/bufr}
else
export COMIN=${COMIN:-$(compath.py ${NET}/${sref_ver})/${RUN}.${PDY}/$cyc}
export COMOUT=${COMOUT:-$(compath.py -o ${NET}/${sref_ver})/${RUN}.${PDY}/$cyc}
export COMIN_PROF=${COMIN_PROF:-$(compath.py ${NET}/${sref_ver})/${RUN}.${PDY}/$cyc/bufr}
fi

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

mkdir -p $COMOUT

env

###################################################
# Execute the Script
#
$HOMEsref/scripts/exsref_meanbufr.sh
###################################################
cat $pgmout

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

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

date