#!/bin/bash
##### 05/05/99 #########################################
# SETUP CMC BIAS CORRECTED ENS PREP PROCESSING VARIABLES
# #######################################################

######################################################
# The following two variable could be defined in the
# loadleveler submission script (the sms script), if
# not they will take the default values which is set
# for the NCO running enviroment
#######################################################

set -xa

####################################
# Specify NET and RUN Name and model
####################################
export NET=${NET:-naefs}
export RUN=${RUN:-cmce}

export PS4='$SECONDS + '
date

#########################################################
# obtain unique process id (pid) and make temp directory
#########################################################
export pid=$$
export DATAROOT=${DATAROOT:-/lfs/f1/ops/${envir}/tmp}
export DATA=${DATA:-${DATAROOT:?}/${jobid}}

mkdir -p $DATA
cd $DATA

export cycle=t${cyc}z

##################
# File To Log Msgs
##################

export jlogfile=${jlogfile:-${DATA}/jlogfile.${job}.${pid}}

#####################################
# Determine Job Output Name on System
#####################################

export outid="LL$job"
export jobid="${outid}.o${pid}"
export pgmout="OUTPUT.${pid}"
export pgmerr=errfile

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

#########################
# Specify Execution Areas
#########################

export HOMEcmce=${HOMEcmce:-${PACKAGEROOT}/naefs.${naefs_ver}}
export EXECcmce=${EXECcmce:-$HOMEcmce/exec}
export USHcmce=${USHcmce:-$HOMEcmce/ush}
export FIXcmce=${FIXcmce:-$HOMEcmce/fix}

export ERRSCRIPT=err_chk
export LOGSCRIPT=startmsg
export REDOUT='1>>'
export REDERR='2>'

#########################################
# Run setpdy and initialize PDY variables
#########################################

setpdy.sh
. ./PDY

########################
# Define COM directories
########################

export DCOM_IN=${DCOM_IN:-${DCOMROOT}/$PDY/wgrbbul/cmcensbc_gb2}
export COMINbias=${COMINbias:-$(compath.py $envir/$NET/${naefs_ver})}

export COM_OUT=${COM_OUT:-$(compath.py -o $NET/${naefs_ver})}
export COMOUTBC_GB2=$COM_OUT/${RUN}.${PDY}/${cyc}/pgrb2ap5_bc
export COMOUTAN_GB2=$COM_OUT/${RUN}.${PDY}/${cyc}/pgrb2ap5_an
export COMOUTWT_GB2=$COM_OUT/${RUN}.${PDY}/${cyc}/pgrb2ap5_wt

mkdir -m 775 -p $COMOUTBC_GB2
mkdir -m 775 -p $COMOUTAN_GB2
mkdir -m 775 -p $COMOUTWT_GB2

msg="HAS BEGUN on `hostname`"
postmsg "$jlogfile" "$msg"

env

####################
# Execute the script
####################

$HOMEcmce/scripts/exnaefs_cmcensbc_prep.sh

msg="JOB COMPLETED NORMALLY"
postmsg "$jlogfile" "$msg"

########################################
# Remove the Temporary working directory
########################################
if [ $KEEPDATA != YES ]; then
  cd $DATAROOT
  rm -rf $DATA
fi

date