#!/usr/bin/env bash
########################################################################
#
#  JOB NAME:  JLMP_1HCNVLTG_MOSPREDS (ADOPTED FROM JLMP_CNVLTG_PREP)
#
#  HISTORY:   NOV 07 2016      Smplatsky       - created
#             DEC 29 2016      Huang           - deleted module load
#                                                statement as that was
#                                                done in the master
#             SEP 08 2021      SAMPLATSKY        MODIFIED FOR WCOSS2 UPDATES
#
########################################################################

########################################################################
# SET SHELL PROCESSING VARIABLES
########################################################################

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

if [ -z "$ELEM" ]; then  # cnv or ltg
  err_exit "The ELEM variable must be defined!"
fi

########################################################################
# OBTAIN UNIQUE PROCESS ID (PID) AND CREATE TEMP DIRECTORY
########################################################################

export DATA=${DATA:-${DATAROOT:?}/${jobid:?}}

mkdir -p $DATA
cd $DATA

export cycle=t${cyc}z
export HOUR=$cyc

########################################################################
# SPECIFY NET AND MODEL VARIABLES
########################################################################

export NET=${NET:-lmp}
export RUN=${RUN:-lmp}

########################################################################
# DETERMINE JOB OUTPUT NAME (USED IN $DATA WHILE JOB IS
# RUNNING)
########################################################################
export pgmout="OUTPUT.$$"

########################################################################
# SENDECF  - Flag Events on ECF
# SENDCOM  - Copy Files From TMPDIR to $com
# SENDDBN  - Send files to OSO
########################################################################
export SENDECF=${SENDECF:-YES}
export SENDCOM=${SENDCOM:-YES}
export SENDDBN=${SENDDBN:-YES}

########################################################################
# SPECIFY DIRECTORIES WHERE JOB-RELATED FILES RESIDE
########################################################################
export USHlmp=${USHlmp:-${HOMElmp}/ush}
export EXEClmp=${EXEClmp:-${HOMElmp}/exec}
export FIXlmp=${FIXlmp:-${HOMElmp}/fix}
export PARMlmp=${PARMlmp:-${HOMElmp}/parm}
export SCRlmp=${SCRlmp:-${HOMElmp}/scripts}
export EXEClamp_shared=${EXEClamp_shared:-${HOMElamp_shared}/exec}

########################################################################
# DEFINE COMIN/COMOUT DIRECTORIES, CREATE COMOUT IF
# DIRECTORY DOES NOT EXIST
########################################################################
export PDY=$PDY
setpdy.sh
. ./PDY

export COMOUT=${COMOUT:-$(compath.py -o ${NET}/${lmp_ver})/${RUN}.${PDY}}
mkdir -p ${COMOUT} 

env

########################################################################
# SET DATE AND TIME VARIABLES.  NOTE THE MOSPDY AND MOSDAT
# VARIABLES ARE NOT EXPORTED AS THEY ARE NOT NEEDED IN THE
# SCRIPT.
########################################################################
export DAT=${PDY}${HOUR}

case ${HOUR} in
  04 | 10 | 16 | 22 ) 
    export offset=4
    export offset2=10;;
  05 | 11 | 17 | 23 )
    export offset=5
    export offset2=11;;
  06 | 12 | 18 | 00 )
    export offset=6
    export offset2=12;;
  07 | 13 | 19 | 01 )
    export offset=7
    export offset2=13;;
  08 | 14 | 20 | 02 )
    export offset=8
    export offset2=14;;
  09 | 15 | 21 | 03 )
    export offset=9
    export offset2=15;;
esac

MOSDATcurr=`$NDATE -${offset} ${DAT}`
MOSDATprev=`$NDATE -${offset2} ${DAT}`

export MOSPDYcurr=`echo ${MOSDATcurr} | cut -c1-8`
export MOSPDYprev=`echo ${MOSDATprev} | cut -c1-8`

export MOSCYCcurr=`echo ${MOSDATcurr} | cut -c9-10`
export MOSCYCprev=`echo ${MOSDATprev} | cut -c9-10`

########################################################################
# SET UP DIRECTORY PATHS DEPENDENT ON DATE VARIABLES
########################################################################

COMINbase=${COMINbase:-$(compath.py ${envir}/com/${NET}/${lmp_ver})}
export COMINcurr=${COMINcurr:-${COMINbase}/${RUN}.${MOSPDYcurr}}
export COMINprev=${COMINprev:-${COMINbase}/${RUN}.${MOSPDYprev}}

msg="$job has begun on `hostname` at `date`"

########################################################################
# EXECUTE THE SCRIPT
########################################################################

${SCRlmp}/exlmp_1hcnvltg_mospreds.sh
export err=$?; err_chk

cat ${pgmout}

########################################################################
# WIPE OUT $DATA
########################################################################

cd $COMOUT

if [ "${KEEPDATA^^}" != "YES" ]; then
   rm -rf $DATA
fi

date