#!/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 "Current OBSPROC version is ${obsproc_ver:-not set}"
echo "GFS version used by this job is ${gfs_ver:-not set}"
echo "####################################################################"
echo
set -x

#####################################################################
# List modules loaded for this job
#####################################################################
set +x
echo
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
# SENDWEB      - Post content to WWW server (GDAS only)
########################################################
export SENDCOM=${SENDCOM:-YES}
export SENDECF=${SENDECF:-YES}
export SENDDBN=${SENDDBN:-YES}
[ $RUN = gdas ] && SENDWEB=${SENDWEB:-YES}

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

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


export SCRIPTSprepobs=${SCRIPTSprepobs:-$HOMEprepobs/scripts}
export USHobsproc=${USHobsproc:-$HOMEobsproc/ush}
export FIXprepobs=${FIXprepobs:-$HOMEprepobs/fix}
export EXECobsproc=${EXECobsproc:-$HOMEobsproc/exec}

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

# The following are used by $PROCESS_TIMETWINS in the GDAS when it is YES
# -----------------------------------------------------------------------
export COMINgdas=${COMINgdas:-$(compath.py ${envir}/${NET}/${gfs_ver})/${RUN}.}
export COMINGFS=${COMINGFS:-$(compath.py ${envir}/${obsNET}/${obsproc_ver}/${NET}.${PDY}/${cyc}/${COMPONENT})}
#export COMIN1=${COMIN1:-$(compath.py ${envir}/${NET}/${gfs_ver})/${RUN}.}
export COMIN1=${COMIN1:-$(compath.py ${envir}/${obsNET}/${obsproc_ver})/${RUN}.} #twinwind file now write in obsproc package

#################################################################
# SETUP GDAS AND GFS POST-ANALYSIS PREPBUFR PROCESSING VARIABLES
#################################################################

#########################################################
# Specify variables specific to this execution of script
#########################################################
export PROCESS_REMOREST=${PROCESS_REMOREST:-YES}  # normally runs
export PROCESS_UNBLKBUFR=${PROCESS_UNBLKBUFR:-NO} # normally does not run
if [ $RUN = gfs ]; then
  export PROCESS_TIMETWINS=NO          # never runs in GFS
  export PROCESS_ALL_REPORT_COUNTS=NO  # never runs in GFS
  export PROCESS_MASTER_SHIP_STNLST=NO # never runs in GFS
elif [ $RUN = gdas ]; then
  export PROCESS_TIMETWINS=${PROCESS_TIMETWINS:-YES} # normally runs in GDAS

# $PROCESS_ALL_REPORT_COUNTS and $PROCESS_MASTER_SHIP_STNLST both default to
#  YES only in 18z GDAS runs and are otherwise hardwired to NO
# --------------------------------------------------------------------------
  if [ $cyc != 18 ]; then  # only done in 18z GDAS runs
    export PROCESS_ALL_REPORT_COUNTS=NO
    export PROCESS_MASTER_SHIP_STNLST=NO
  else
    export PROCESS_ALL_REPORT_COUNTS=${PROCESS_ALL_REPORT_COUNTS:-YES}
    export PROCESS_MASTER_SHIP_STNLST=${PROCESS_MASTER_SHIP_STNLST:-YES}
  fi
  if [ $PROCESS_ALL_REPORT_COUNTS == YES ]; then
    export REBUILD_MAINPAGE=${REBUILD_MAINPAGE:-NO}
    MM=`echo $PDY | cut -c5-6`
    MONS=(null JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC)
    MON=${MONS[10#$MM]}   # get month string from array MONS.
    YYYYMM=`echo $PDY  | cut -c1-6`
    if [[ "$RUN_ENVIR" == nco ]]; then 
      export DATCOMIN=$(compath.py ${envir}/${obsNET}/${obsproc_ver}/${RUN}/gdascounts)
      export SATCOMIN=$(compath.py ${envir}/${obsNET}/${obsproc_ver}/${RUN}/gdascounts)
      export ARCHCOMIN=$(compath.py ${envir}/${obsNET}/${obsproc_ver})
      export DATCOM1_IN=$(compath.py ${envir}/${obsNET}/${obsproc_ver}/${RUN}/gdascounts)
      export DATCOMOUT_dir=$(compath.py -o ${obsNET}/${obsproc_ver}/${RUN}/gdascounts/data_counts.$YYYYMM)
      export SATCOMOUT_dir=$(compath.py -o ${obsNET}/${obsproc_ver}/${RUN}/gdascounts/satcounts.$YYYYMM)
      export ARCHCOMOUT=$(compath.py -o ${obsNET}/${obsproc_ver})  # used in gdascounts_archive_pyear.sh
      export DATCOM1_OUT=$(compath.py ${envir}/${obsNET}/${obsproc_ver}/${RUN}/gdascounts)
      mkdir -m 775 -p $DATCOMOUT_dir $SATCOMOUT_dir $ARCHCOMOUT $DATCOM1_OUT
    else
      export DATCOMIN=${DATCOMIN:-${COMIN_ROOT:?}/${obsNET}/${obsproc_ver}/${RUN}/gdascounts}
      export SATCOMIN=${SATCOMIN:-${COMIN_ROOT:?}/${obsNET}/${obsproc_ver}/${RUN}/gdascounts}
      export ARCHCOMIN=${ARCHCOMIN:-${COMIN_ROOT:?}/${obsNET}/${obsproc_ver}}
      export DATCOM1_IN=${DATCOM1_IN:-${COMIN_ROOT:?}/${obsNET}/${obsproc_ver}/${RUN}/gdascounts}
      export DATCOMOUT_dir=${DATCOMOUT_dir:-${COMOUT_ROOT:?}/${obsNET}/${obsproc_ver}/${RUN}/gdascounts/data_counts.$YYYYMM}
      export SATCOMOUT_dir=${SATCOMOUT_dir:-${COMOUT_ROOT:?}/${obsNET}/${obsproc_ver}/${RUN}/gdascounts/satcounts.$YYYYMM}
      export ARCHCOMOUT=${ARCHCOMOUT:-${COMOUT_ROOT:?}/${obsNET}/${obsproc_ver}} #proposed location
      export DATCOM1_OUT=${DATCOM1_OUT:-${COMOUT_ROOT:?}/${obsNET}/${obsproc_ver}/${RUN}/gdascounts}
      mkdir -m 775 -p $DATCOMOUT_dir $SATCOMOUT_dir $ARCHCOMOUT $DATCOM1_OUT
    fi
  fi 
  if [ $PROCESS_MASTER_SHIP_STNLST == YES ]; then
    export DCOMROOT=${DCOMROOT:-/lfs/h1/ops/prod/dcom}
    export SHPNAMDIR_DCOM=${SHPNAMDIR:-${DCOMROOT}/ndbc}
    if [[ "$RUN_ENVIR" == nco ]]; then
      export VOSarch_IN=${COMROOT:?}/arch/VOS
      export VOSarch_OUT=${COMROOT:?}/arch/VOS
      mkdir -m 755 $VOSarch_OUT
    else
      export VOSarch_IN=${VOSarch_IN:-${COMROOT:?}/arch/VOS}
      export VOSarch_OUT=${VOSarch_OUT:-${COMOUT_ROOT:?}/arch/VOS}
      mkdir -p -m 755 $VOSarch_OUT
    fi
  fi 
fi 

env

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

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

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

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

exit
