#!/bin/bash
# JMODS
#  job script to trigger MODBUFR file processing
#
# 26 Feb 2019 JWhiting cp'd fr /nwprod/jobs/JMODS of 17 Jun 2014
#              config'd for phase 3 (versioned utils, etc)
#               rm'd RUN_ENVIR var; added KEEPDATA var
#               rm'd refs to SENDDBN
# 23 Nov 2021 Praveen Kumar modified for WCOSS2
#----

echo : echo "JMODS: welcome to j-job script ($(date -u))" ; echo

set -xa                                                                         

###################################################################
# Set DEBUG LEVEL - modifies info in execution trace prompt string
###################################################################
export DEBUG_LEVEL=${DEBUG_LEVEL=1}
case $DEBUG_LEVEL in
  1) export PS4='+ ${SECONDS}s + ' ;;       # add seconds (this is typical case)
  0) export PS4='+ ';;                      # don't include seconds (useful if
                                            #  want to diff output from 2 runs)
  2) export PS4='+ ${SECONDS}s:L$LINENO + ';;         # add script line #
  3) export PS4='+ ${SECONDS}s:${0##*/}:L$LINENO + ';;# add script name & line #
  4) export PS4='+ ${0##*/}:L$LINENO + ';;  # script name & line (no seconds)
  *) echo "Unrecognized DEBUG_LEVEL.  Stay with current PS4 setting";;
esac

########################################
# Check that required variables are set
########################################
set +x
echo
echo cyc is ${cyc:?"###FATAL ERROR \"cyc\" is not set"}
echo envir is ${envir:?"###FATAL ERROR \"envir\" is not set"}
echo job is ${job:?"###FATAL ERROR \"job\" is not set"}
echo DATAROOT is ${DATAROOT:?"###FATAL ERROR \"DATAROOT\" is not set"}
echo "DCOMROOT is ${DCOMROOT:?"###FATAL ERROR 'DCOMROOT' is not set"}"
echo
set -x

################################
# Print out version information
################################
set +x
echo
echo "####################################################################"
echo
echo "OBSPROC version used by this job is ${obsproc_ver:-not set}"
echo "BUFR_DUMP version used by this job is ${bufr_dump_ver:-not set}"
echo "grib_util module version used by this job is ${grib_util_ver:-not set}"
if [[ $(echo $LOADEDMODULES | egrep -c "(^|:)bufr_dump/") -eq 0 ]];
     then echo "bufr_dump is required but not loaded! Exiting...";
     exit;
fi
echo
echo "####################################################################"
echo
set -x

#####################################################################
# List modules loaded for this job
#####################################################################
set +x
echo
echo "####################################################################"
echo
      module list
echo
echo "####################################################################"
echo
set -x

# ####  5/26/2004 #############################
# SETUP MODSBUFR DUMP PROCESSING VARIABLES
# #############################################

##########################
# Specify NET and RUN Name
##########################
export obsNET=${obsNET:-obsproc}
export NET=${NET:-mods}
export RUN=${RUN:-mods}

set +x
echo
echo obsNET is ${obsNET:?"###FATAL ERROR \"obsNET\" is not set"}
echo NET is ${NET:?"###FATAL ERROR \"NET\" is not set"}
echo RUN is ${RUN:?"###FATAL ERROR \"RUN\" is not set"}
echo
set -x


###################################################
# obtain unique process id (pid) and make temp directory
###################################################
export pid=$$
#export DATA=${DATA:-/tmpnwprd1/$job.$pid}
jobid=${jobid:-${job}.$(hostname -s).$$}
export DATA=${DATA:-$DATAROOT/${jobid}}
mkdir -p $DATA
cd $DATA

export cycle=t${cyc}z

####################################
# Determine Job Output Name on System
####################################
export outid="LL$job"
[ $envir != prod ]  && export outid="LL${job}_${envir}"
export jobid="${outid}.o${pid}"
export pgmout="OUTPUT.${pid}"

######################################
# Set job log file for postmsg output
######################################
jlogfile=${jlogfile:-${DATA}/jlogfile}


#################################################
# SENDCOM      - Copy files to $COMOUT directory
# SENDECF      - Flag Events on ECFLOW
#################################################
export SENDCOM=${SENDCOM:-YES}
export SENDECF=${SENDECF:-YES}


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

# -------------------------------
# Paths for obsproc items
# -------------------------------
export HOMEobsproc=${HOMEobsproc:\
-${PACKAGEROOT:?}/obsproc/${obsproc_ver:?\
"###FATAL ERROR \"obsproc_ver\" is not set"}}

# ---------------------------------------------------------
# Paths for dump and bufr_dumplist items
# Variables are pre-defined when bufr_dump module is loaded
# ---------------------------------------------------------

export USHbufr_dump=${USHbufr_dump:-$HOMEbufr_dump/ush}
export FIXbufr_dump=${FIXbufr_dump:-$HOMEbufr_dump/fix}

# NOTE: path to dump executables defaults to $HOMEbufr_dump/exec in dumpjb

# directory path to data dump script executed in model script
# -----------------------------------------------------------
export ushscript_dump=${ushscript_dump:-$USHbufr_dump}

export EXECmods=${EXECmods:-$HOMEobsproc/exec}
export USHmods=${USHmods:-$HOMEobsproc/ush}
export BTABdir=${BTABDIR:-$HOMEobsproc/fix}

export DUMP=${DUMP:-$DUMPJB}
#########################################################################
# Add some prod utilities to working directory (replaces obsolete setup.sh)
#########################################################################
echo "step ############# break ##############################" > ./break
cp $UTILROOT/ush/err_chk   .; chmod +x err_chk
cp $UTILROOT/ush/err_exit  .; chmod +x err_exit
cp $UTILROOT/ush/prep_step .; chmod +x prep_step
cp $UTILROOT/ush/postmsg   .; chmod +x postmsg
cp $UTILROOT/ush/setpdy.sh .; chmod +x setpdy.sh

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

export YYYY=`echo $PDYm9 | cut -c1-4` #mods doing $PDYm9  data

##################################################
# Define COMOUT directory  (no COMIN for this job)
##################################################

export COMOUT=${COMOUT:-$(compath.py -o ${obsNET}/$obsproc_ver/${NET}.${PDY})}
export MODSDIR=${MODSDIR:-$(compath.py -o ${obsNET}/$obsproc_ver)/mods.$YYYY}
mkdir -m 775 -p $COMOUT

##############################################
# Define location of MODS tanks
##############################################
# -- need to set DCOMROOT subdir (dfix) per system

export DCOMROOT=${DCOMROOT:-/lfs/h1/ops/${envir}/dcom}
echo $DCOMROOT
#export MODSDIR=${MODSDIR:-${DCOMROOT}/mods}
#export MODSDIR=${MODSDIR:-${COM_OUT}/mods.$YYYY}


env ; echo "JMODS: end of env listing" ; echo 
 set -x

###################################################
# Execute the script
###################################################
$HOMEobsproc/scripts/exmods.sh 
err_exmods=$?
[[ $err_exmods -ne 0 ]] && $DATA/err_exit


# -- write out job & version info to COMOUT files

echo "$SITE `hostname`  --  `date -u`" > $COMOUT/where_${cycle}_${RUN}_ran

> $COMOUT/obsproc_version_for_${cycle}_${RUN}_run
[ -n "$obsproc_ver" ]  &&  \
 echo "OBSPROC version is $obsproc_ver" >> \
 $COMOUT/obsproc_version_for_${cycle}_${RUN}_run
[ -n "$bufr_dump_ver" ]  &&  \
 echo "BUFR_DUMP version used by this job is $bufr_dump_ver" >> \
 $COMOUT/obsproc_version_for_${cycle}_${RUN}_run


if [ "$KEEPDATA" != YES ]; then
   cd $DATAROOT
   rm -rf $DATA
fi
date -u

exit $err_exmods

