#!/bin/bash

# #### 08/28/07 ###########################################################
# Daily Update Downscaling Vector Estimation Between RTMA and NCEP Analysis        
# #########################################################################

####################################################
# 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=naefs
export RUN=naefs

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 pgmerr=errfile
export outid="LL$job"
export jobid="${outid}.o${pid}"
export pgmout="OUTPUT.${pid}"

##############################################
# SENDECF  - Flag Events on ecFLOW
# 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 HOMErtma=${HOMErtma:-${PACKAGEROOT}/naefs.${naefs_ver}}
export EXECrtma=${EXECrtma:-$HOMErtma/exec}
export FIXrtma=${FIXrtma:-$HOMErtma/fix}
export USHrtma=${USHrtma:-$HOMErtma/ush}

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

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

##################################
# REGID  - Define the NDGD Region
#   ak    - Alaska
#   conus - CONUS
#   hi    - Hawaii
#   gu    - Guam
#   pr    - Puerto Rico
##################################
export regid=conus

export PDYm03=`$NDATE -3 $PDY$cyc | cut -c1-8`
export cycm03=`$NDATE -3 $PDY$cyc | cut -c9-10`

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

export COM_DV=${COM_DV:-$(compath.py $envir/$NET/${naefs_ver})}
export COM_RTMA=${COM_RTMA:-$(compath.py ${envir}/com/rtma/${rtma_ver})}
export COMINgefs=${COMINgefs:-$(compath.py ${envir}/com/gefs/${gefs_ver}/gefs.${PDY})/${cyc}/atmos/pgrb2ap5}

export COM_OUT=${COM_OUT:-$(compath.py -o $NET/${naefs_ver})}
export COMOUT=${COM_OUT}/${RUN}.${PDY}/${cyc}/ndgd_gb2
export COMOUT_GEFS=${COM_OUT}/gefs.${PDY}/${cyc}/ndgd_gb2

export COMOUTm03=${COM_OUT}/${RUN}.${PDYm03}/${cycm03}/ndgd_gb2
export COMOUTm03_GEFS=${COM_OUT}/gefs.${PDYm03}/${cycm03}/ndgd_gb2

mkdir -m 775 -p ${COMOUT}
mkdir -m 775 -p ${COMOUT_GEFS}
mkdir -m 775 -p ${COMOUTm03}
mkdir -m 775 -p ${COMOUTm03_GEFS}

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

env

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

$HOMErtma/scripts/exnaefs_dvrtma_bias.sh

cat $pgmout.*
msg="JOB COMPLETED NORMALLY"
postmsg "$jlogfile" "$msg"

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

date