#!/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 HOMEbufr_dump is ${HOMEbufr_dump:?"###FATAL ERROR \"HOMEbufr_dump\" is not set"} 
echo
set -x

################################
# Print out version information
################################
set +x
echo "####################################################################"
echo
echo "OBSPROC version is ${obsproc_ver:-not set}"
echo "BUFR_DUMP version used by this job, for dumplist, is" \
       "${bufr_dump_ver:-not set}"
echo
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
echo "####################################################################"
echo
      module list
echo
echo "####################################################################"
echo
set -x

######################
# Specify NET and RUN
######################
export obsNET=${obsNET:-obsproc}
export NET=${NET:-gfs}
echo $job | grep gfs
err_gfs=$?
echo $job | grep gdas
err_gdas=$?
if [ $err_gfs = 0 ]; then
  export RUN=${RUN:-gfs}   # GFS
elif [ $err_gdas = 0 ]; then
  export RUN=${RUN:-gdas}  # GDAS
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
###############################
jobid=${jobid:-${job}.$(hostname -s).$$}
export DATA=${DATA:-$DATAROOT/${jobid}}
rm -rf $DATA
mkdir -p $DATA
cd $DATA
export pgmout=OUTPUT.$$

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

###############################################################################
# SENDCOM  - Copy files to $COMOUT directory
# SENDECF  - Flag Events on ECFLOW
# SENDDBN  - Alert output file to TOC
# SENDSDM  - Send files to SDM (via email and/or printer in bufr_datacount.sh)
###############################################################################
export SENDCOM=${SENDCOM:-YES}
export SENDECF=${SENDECF:-YES}
export SENDDBN=${SENDDBN:-YES}
export SENDSDM=${SENDSDM:-YES}
[[ "$envir" != prod ]] && export SENDSDM=NO  # parallel , not send email to SDM

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

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

export SCRIPTSobsproc=${SCRIPTSobsproc:-$HOMEobsproc/scripts}
export EXECobsproc=${EXECobsproc:-$HOMEobsproc/exec}
export USHobsproc=${USHobsproc:-$HOMEobsproc/ush}

# ----------------------------------------------------------------------
# Paths for bufr_dumplist items
# Default variables are pre-defined when the bufr_dump module is loaded.
# ----------------------------------------------------------------------

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

#########################################################################
# 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:-""}}
export COMPONENT=${COMPONENT:-atmos}

if [[ "$RUN_ENVIR" == nco ]]; then
  export COMIN=${COMIN:-$(compath.py ${envir}/${obsNET}/${obsproc_ver}/${RUN}.${PDY}/${cyc}/${COMPONENT})}
  export COMOUT=${COMOUT:-$(compath.py -o ${obsNET}/${obsproc_ver}/${RUN}.${PDY}/${cyc}/${COMPONENT})}
  mkdir -m 775 -p $COMOUT
# The following are used by $PROCESS_REMOREST_dm2 when it is YES
# --------------------------------------------------------------
  export COMINm2=${COMINm2:-$(compath.py ${envir}/${obsNET}/${obsproc_ver}/${RUN}.${PDYm2}/${cyc}/${COMPONENT})}
  export COMOUTm2=${COMOUTm2:-$(compath.py -o ${obsNET}/${obsproc_ver}/${RUN}.${PDYm2}/${cyc}/${COMPONENT})}
else
  export COMIN=${COMIN:-${COMIN_ROOT:?}/${obsNET}/${obsproc_ver}/${RUN}.${PDY}/${cyc}/${COMPONENT}}

# 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}/${cyc}/${COMPONENT}}
  mkdir -m 755 -p $COMOUT
# The following are used by $PROCESS_REMOREST_dm2 when it is YES
# --------------------------------------------------------------
  export COMINm2=${COMINm2:-${COMIN_ROOT:?}/${obsNET}/${obsproc_ver}/${RUN}.${PDYm2}/${cyc}/${COMPONENT}}
  export COMOUTm2=${COMOUTm2:-${COMOUT_ROOT}/${obsNET}/${obsproc_ver}/${RUN}.${PDYm2}/${cyc}/${COMPONENT}}
  mkdir -m 755 -p $COMOUTm2
fi

# comin and comin_m1 are paths to dump status files for bufr_avgdata.sh
export comin=${comin:-$(compath.py ${envir}/${obsNET}/${obsproc_ver}/${RUN}.${PDY})/${cyc}/${COMPONENT}}
export comin_m1=${comin_m1:-$(compath.py ${envir}/${obsNET}/${obsproc_ver}/${RUN}.${PDYm1})/${cyc}/${COMPONENT}}

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

# directory path to data count/alert processing executed in bufr_datacount.sh
#  and data count averaging executed in model script and in bufr_avgdata.sh
# ---------------------------------------------------------------------------
export HOMEarch=${HOMEarch:-$(compath.py ${envir}/$obsNET/${obsproc_ver})/sdm_rtdm/${NET}} 

#  .. AVGDarch_IN is directory path containing 30-day avg files (read by
#     bufr_datacount.sh)
#     ------------------------------------------------------------------
#  .. AVGDarch_OUT is directory path for avg tables output by model script
#     --------------------------------------------------------------------
if [[ "$RUN_ENVIR" == nco ]]; then
  export AVGDarch_IN=$HOMEarch/avgdata
  export AVGDarch_OUT=$HOMEarch/avgdata
  if [ ! -d $AVGDarch_OUT ]; then
    mkdir -p $AVGDarch_OUT
  fi
else
  [ -n "$HOMEarch" ] && HOMEarch_avgdata=$HOMEarch/avgdata
  export AVGDarch_IN=\
${AVGDarch_IN:-${HOMEarch_avgdata:-${COMIN_ROOT:?}/${obsNET}/${obsproc_ver}/sdm_rtdm/${RUN}/avgdata}}
  export AVGDarch_OUT=${AVGDarch_OUT:-${COMOUT_ROOT:?}/${obsNET}/${obsproc_ver}/sdm_rtdm/${RUN}/avgdata}
  mkdir -p $AVGDarch_OUT
fi

#  .. OBCNTarch is directory path to write new status files to & read all status
#     files from {can WRITE to this location only if COPY=YES (the default)}
#     --------------------------------------------------------------------------
export OBCNTarch=${OBCNTarch:-$HOMEarch/obcount_30day}

#  .. ALERTL is directory for alert log files output by bufr_datacount.sh
#     -------------------------------------------------------------------
if [[ "$RUN_ENVIR" == nco ]]; then
  if [[ ${envir} == prod ]]; then
    export ALERTL=${ALERTL:-${COMROOT:?}/logs/alertlog}
  else
    export ALERTL=${ALERTL:-${COMROOT:?}/logs/alertlog}
  fi
  mkdir -p $ALERTL
else # developer
  export ALERTL=${ALERTL:-${COMOUT_ROOT:?}/logs/alertlog}
  mkdir -p $ALERTL
fi

########################################################
# SETUP GDAS OR GFS DATA DUMP POST PROCESSING VARIABLES
########################################################

#########################################################
# Specify variables specific to this execution of script
#########################################################
export PROCESS_DATACOUNTS=${PROCESS_DATACOUNTS:-YES} # normally runs
export PROCESS_REMOREST=${PROCESS_REMOREST:-YES}     # normally runs
export PROCESS_UNBLKBUFR=${PROCESS_UNBLKBUFR:-NO}    # normally does not run
export PROCESS_LISTERS=${PROCESS_LISTERS:-YES}       # normally runs

# Data count average tables are generated in the 18Z run only
if [ "$cyc" = '18' ]; then
   export PROCESS_AVGTABLES=${PROCESS_AVGTABLES:-YES}
else
   export PROCESS_AVGTABLES=${PROCESS_AVGTABLES:-NO}
fi

env

[ $RUN = gdas ] && export NET=gdas
#####################
# Execute the script
#####################
$SCRIPTSobsproc/exdump_post.sh
eval err_${RUN}_dump_post=$?
eval [[ \$err_${RUN}_dump_post -ne 0 ]] && $DATA/err_exit

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

> $COMOUT/obsproc_version_for_${cycle}_${RUN}_dump_post_run
[ -n "$obsproc_ver" ]  &&  \
 echo "OBSPROC version is $obsproc_ver" >> \
 $COMOUT/obsproc_version_for_${cycle}_${RUN}_dump_post_run

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

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

exit
