#!/bin/bash
set -xa
date -u

###################################################################
# 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
set -x

###############################
# Print out version information
###############################
set +x
echo
echo "####################################################################"
echo
echo "OBSPROC version is ${obsproc_ver:-not set}"
echo "BUFR_DUMP version used by this job, for dump and dumplist, 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
#   On luna/surge may need to initialize the Modules package to find
#   the module command.  Will do so for either system.
#####################################################################
set +x
echo
echo "####################################################################"
echo
      module list
echo
echo "####################################################################"
echo
set -x

###########################
# Specify NET and RUN Name
###########################
export obsNET=${obsNET:-obsproc}
export NET=${NET:-rap}
echo $job | grep pcyc
err_p=$?
echo $job | grep erly
err_e=$?
#echo $job | grep ehrrr
#err_eh=$?
if [ $err_p = 0 ]; then
  export RUN=${RUN:-rap_p}  # Partial cycle
elif [ $err_e = 0 ]; then
  export RUN=${RUN:-rap_e}  # Early cycle
#elif [ $err_eh = 0 ]; then   # 10/2019; disabled for HRRRv4
#  export RUN=${RUN:-rap_eh}  # Early HRRR cycle
else
  export RUN=${RUN:-rap}    # Full cycle
fi

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

##############################
# Specify cycle and time-mark
##############################
export cycle=t${cyc}z
export tmmark=tm00

###############################
# Create new working directory
###############################
host=$(hostname -s)
export DATA=${DATA:-$DATAROOT/${jobid}.${host}}
rm -rf $DATA
mkdir -p $DATA
cd $DATA
if [[ "$RUN_ENVIR" != nco ]]; then
  TMPDIR=$DATAROOT  # In the event TMPDIR is reset to a temporary LSF
                    #  directory when batch jobs are submitted, set it to
                    #  $DATAROOT so ensure it remains under the user's control
                    #  (normally needed only if child script dumpjb executed)
fi

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

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

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

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

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

export SCRIPTSobsproc=${SCRIPTSobsproc:\
-$HOMEobsproc/scripts}

# ----------------------------------------------------
# Paths for dump and bufr_dumplist items
# variables predefined 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}

# ---------------------------------------------------------
# Paths for combined obsproc and dump items
# ---------------------------------------------------------

# directory paths to SSMI dump re-processing executed in bufr_dump_obs
# --------------------------------------------------------------------
export EXECPREP=${EXECPREP:-$HOMEbufr_dump/exec}
export PARMPREP=${PARMPREP:-$HOMEobsproc/parm}
export FIXPREP=${FIXPREP:-$HOMEbufr_dump/fix} # also path to expanded NAM
                                              #  domain mask file

# directory paths to ERS, QuikSCAT & ASCAT dump re-processing executed in
#  bufr_dump_obs
# -----------------------------------------------------------------------
export EXECWAVE=${EXECWAVE:-$HOMEbufr_dump/exec}
export PARMWAVE=${PARMWAVE:-$HOMEobsproc/parm}
export FIXWAVE=${FIXWAVE:-$HOMEbufr_dump/fix}

# directory paths to SSMI, TRMM & WindSAT dump re-processing executed in
#  bufr_dump_obs
# ----------------------------------------------------------------------
export EXECbufr=${EXECbufr:-$HOMEbufr_dump/exec}
export PARMbufr=${PARMbufr:-$HOMEobsproc/parm}
export FIXbufr=${FIXbufr:-$HOMEbufr_dump/fix}

# ---------------------------
# Paths for non-obsproc items
# ---------------------------

# TANK is root directory path to observational database used in dumpjb
# --------------------------------------------------------------------
export DCOMROOT=${DCOMROOT:-/lfs/h1/ops/prod/dcom}
export TANK=${TANK:-${DCOMROOT}}

# directory path to imssnow grib files used in model script
# ---------------------------------------------------------
export TANK_GRIBFLDS=${TANK_GRIBFLDS:-${DCOMROOT}}

#########################################################################
# Add some prod utilities to working directory
#########################################################################
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
. PDY

#########################
# Define COM directories
#########################
export COMIN_ROOT=${COMIN_ROOT:-${COMROOT:-""}}

if [[ "$RUN_ENVIR" == nco ]]; then
  export COMIN=${COMIN:-$(compath.py ${envir}/${obsNET}/${obsproc_ver}/${RUN}.${PDY})}
  export COMOUT=${COMOUT:-$(compath.py -o ${obsNET}/${obsproc_ver}/${RUN}.${PDY})}
  mkdir -m 775 -p $COMOUT
else
  export COMIN=${COMIN:-${COMIN_ROOT:?}/${obsNET}/${obsproc_ver}/${RUN}.${PDY}}

# COMOUT_ROOT for developers defaults to unique $DATA/com directory as root to
#  prevent job from writing into a non-unique directory that might already have
#  output in it from a previous checkout run for the same cycle
# -----------------------------------------------------------------------------
  export COMOUT_ROOT=${COMOUT_ROOT:-${DATA}/com}
  export COMOUT=${COMOUT:-${COMOUT_ROOT:?}/${obsNET}/${obsproc_ver}/${RUN}.${PDY}}
  mkdir -m 755 -p $COMOUT
fi

########################################################################
# SETUP RAP (full cycle), RAP_P (partial cycle), RAP_E (early cycle) or
#  RAP_EH (early HRRR cycle) DATA DUMP PROCESSING VARIABLES
#  10/2019 rap_eh disabled for HRRRv4
########################################################################

#########################################################
# Specify variables specific to this execution of script
#########################################################
if [ $RUN = rap_p ]; then
#if [ $RUN = rap_p -o $RUN = rap_eh ]; then         # 10/2019; rap_eh disabled for HRRRv4
   export PROCESS_GRIBFLDS=${PROCESS_GRIBFLDS:-NO}  # defaults to NO in
                                                    # partial cycle jobs
else
   export PROCESS_GRIBFLDS=${PROCESS_GRIBFLDS:-YES} # defaults to YES in
                                                    # full and early cycle jobs
fi
export PROCESS_DUMP=${PROCESS_DUMP:-YES}
export prepssmi=NO # "spssmi" reprocessed dump no longer generated
                   # since SSM/I instrument on F-13 has failed (11/2009)
export KEEP_NEARDUP_ACFT=${KEEP_NEARDUP_ACFT:-NO} # toss near-dupl. acft rpts

# LALO is set if doing geographic dumping
#  Expanded NAM domain: 0.5 degree mask nam_expdomain_halfdeg_imask.gbl
export LALO=${LALO:-"F$FIXPREP/nam_expdomain_halfdeg_imask.gbl"}

# POE must be set to on if running poe
###export POE=on

# -- or -- BACK must be set to on if running background shells
export BACK=on # "on" is default

# 10/2019; rap_eh disabled for HRRRv4
#if [ $RUN = rap_eh ]; then
#
# Early HRRR cycle jobs will dump only data in DUMP_group6 ("nexrad")
# -------------------------------------------------------------------
#   export num_DUMP_group=7  # test to make sure job & model scripts are in sync
#   export DUMP_group1=NO
#   export DUMP_group2=NO
#   export DUMP_group3=NO
#   export DUMP_group4=NO
#   export DUMP_group5=NO
#   export DUMP_group6=YES
#   export DUMP_group7=NO
#
#fi

env

#####################
# Execute the script
#####################
$SCRIPTSobsproc/exrap_dump.sh
err_rap_dump=$?
eval err_${RUN}_dump=${err_rap_dump}
[[ $err_rap_dump -ne 0 ]] && $DATA/err_exit

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

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

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

exit
