#!/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 "####################################################################"
echo
set -x

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
#################################################
export SENDCOM=${SENDCOM:-YES}
export SENDECF=${SENDECF:-YES}
export SENDDBN=${SENDDBN:-YES}

##########################
# 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 FIXprepobs=${FIXprepobs:-$HOMEprepobs/fix}
export USHprepobs=${USHprepobs:-$HOMEprepobs/ush}

# directory path to data prep script executed in model script
# -----------------------------------------------------------
export ushscript_prep=${ushscript_prep:-$USHprepobs}

# ---------------------------------------------------------
# Paths for prep items
# ---------------------------------------------------------

# directory paths to overall PREP processing executed in
#  prepobs_makeprepbufr.sh and its various child scripts
#  (other than prepobs_syndata.sh, see below)
# ------------------------------------------------------
export EXECPREP=${EXECPREP:-${HOMEprepobs}/exec}
export FIXPREP=${FIXPREP:-${HOMEprepobs}/fix}
export PARMPREP=${PARMPREP:-${HOMEobsproc}/parm}

# directory paths to SYNDATA processing executed in prepobs_syndata.sh
# --------------------------------------------------------------------
export EXECSYND=${EXECSYND:-${HOMEprepobs}/exec}
export FIXSYND=${FIXSYND:-${HOMEprepobs}/fix}
export PARMSYND=${PARMSYND:-${HOMEobsproc}/parm}

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

# DICTPREP is directory path to metar.tbl dictionary used in
#  prepobs_prepacqc.sh if PREPOBS_PREPACPF is executed
# ----------------------------------------------------------
export DICTPREP=${DICTPREP:-${OPSROOT}/decoders/decod_shared/dictionaries_${dictionaries_ver}}   

# HOMEgfs is directory path to prepobs_errtable.global (PRVT), used in
# prepobs_makeprepbufr.sh and its child scripts prepobs_syndata.sh and
# prepobsp_prevents.sh.
# ---------------------------------------------------------------------------
#export HOMEgfs=${HOMEgfs:-${PACKAGEROOT:?}/gfs.${gfs_ver:?"###FATAL ERROR \"gfs_ver\" is not set"}}
export HOMEgfs=${HOMEgfs:-$(compath.py gfs/${gfs_ver})}

#########################################################################
# 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 COMINtcvital=${COMINtcvital:-$(compath.py ${envir}/${NET}/${gfs_ver}/${RUN}.${PDY})/${cyc}/${COMPONENT}}
  export COMOUT=${COMOUT:-$(compath.py -o ${obsNET}/${obsproc_ver}/${RUN}.${PDY}/${cyc}/${COMPONENT})}
  mkdir -m 775 -p $COMOUT
else
  export COMIN=${COMIN:-${COMIN_ROOT:?}/${obsNET}/${obsproc_ver}/${RUN}.${PDY}/${cyc}/${COMPONENT}}
  export COMINtcvital=${COMINtcvital:-$(compath.py ${envir}/${NET}/${gfs_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
fi

export NEMSIO_IN=${NEMSIO_IN:-.false.}        # switch for nemsio vs sigio guess
export NETCDF_IN=${NETCDF_IN:-.true.}         # switch for netcdf history guess
                                              # NOTE: only NEMSIO_IN or NETCDF_IN can be .true.

export envir_getges=${envir_getges:-${envir:-prod}}
export GETGES_COM=${GETGES_COM:-$COMROOT}     # Root location for GFS/GDAS COM data
export GETGES_NWG=${GETGES_NWG:-$COMROOT/nwges} 
export COMINgdas=${COMINgdas:-$(compath.py ${envir_getges}/gfs/${gfs_ver})/gdas.$PDY/${cyc}/${COMPONENT}}
export COMINgfs=${COMINgfs:-$(compath.py ${envir_getges}/gfs/${gfs_ver})/gfs.$PDY/${cyc}/${COMPONENT}}



###################################################
# SETUP GDAS AND GFS PREPBUFR PROCESSING VARIABLES
###################################################

#########################################################
# Specify variables specific to this execution of script
#########################################################

#  Set POE *or* BACK to YES in order to run this job in a parallel environment

export POE=${POE:-YES}
##export BACK=YES  # set BACK to YES for an alternate method of running this
                   #  job in a parallel environment (in this case POE must be
                   #  NO)

#  NSPLIT indicates the number of parts into which the PREPDATA
#         processing will will be split in order to run in parallel
#         for computational efficiency (either under poe tasks when
#         POE=YES or in background threads when BACK=YES)
#   (Applies only when either POE=YES or BACK=YES, and PREPDATA=YES)
#   Consider the size of the backgrounds fields if using threads. 

#IG export NSPLIT=${NSPLIT:-4}
export NSPLIT=${NSPLIT:-12} # ${NSPLIT:-8} #16 adds time to MERGE step :(

#  BUFRLIST indicates the list of BUFR data dump file names to process

export BUFRLIST=${BUFRLIST:-"uprair proflr aircar aircft satwnd adpupa adpsfc \
 sfcshp vadwnd wdsatr ascatw rassda gpsipw"}

export errPREPDATA_limit=4 # allow program PREPOBS_PREPDATA to continue at all
                           # center hour (cycle) times if foreground exit
                           # status is 4 (i.e., when either either "ADPUPA" or
                           # "ADPSFC" dump is empty)

# VARIABLES THAT CONTROL PROCESSING OF INDIVIDUAL PROGRAMS
# --------------------------------------------------------

#  -- These can individually be switched to NO without affecting the
#      success of other programs and the success of the complete job
#      (all should normally be set to YES unless otherwise specified;
#       if job fails in one of these programs, set offending program
#       to NO and resubmit to "work-around" problem)

export SYNDATA=${SYNDATA:-YES}   # Execute program SYNDAT_SYNDATA   if YES

export PREPACQC=${PREPACQC:-YES} # Execute PREPACQC script processing if YES
   export PROCESS_ACQC=${PROCESS_ACQC:-YES} # Execute program PREPOBS_PREPACQC
                                            #  if YES
   export PROCESS_ACPF=${PROCESS_ACPF:-NO}  # Execute program PREPOBS_PREPACPF
                                            #  if YES

export PROFCQC=${PROFCQC:-YES}   # Execute program PREPOBS_PROFCQC  if YES

export CQCVAD=${CQCVAD:-YES}     # Execute program PREPOBS_CQCVAD   if YES

export CQCBUFR=${CQCBUFR:-YES}   # Execute program PREPOBS_CQCBUFR  if YES

export OIQCBUFR=${OIQCBUFR:-NO} # Execute program PREPOBS_OIQCBUFR if YES


#  -- If any of these are switched to NO it will affect the success of
#      other programs and the success of the complete job
#      (these are set-up only for CHECKOUT runs - they should always
#       be set to YES for operational runs)

export PREPDATA=${PREPDATA:-YES}  
                      # Execute programs PREPOBS_MPCOPYBUFR, PREPOBS_PREPDATA,
                      # PREPOBS_LISTHEADERS and PREPOBS_MONOPREPBUFR if YES

export GETGUESS=${GETGUESS:-YES}
                      # Encode first guess (background) values interpolated to
                      # observation locations in the PREPBUFR file for use by
                      # the q.c. programs.  This guess is always from a global
                      # sigmai (legacy) or nemsio or netcdf history guess file 
                      # valid at the center PREPBUFR processing date/time.

export DO_QC=${DO_QC:-YES}
                      # IF NO, programs PREPOBS_PREPACQC, PREPOBS_PREPACPF,
                      # PREPOBS_PROFCQC, PREPOBS_CQCVAD, PREPOBS_CQCBUFR and
                      # PREPOBS_OIQCBUFR will NEVER execute regardless of
                      # switches above -
                      # should be set to NO only as a last resort!!!

export PROCESS_UNBLKBUFR=${PROCESS_UNBLKBUFR:-NO}
                      # IF YES, create prepbufr.unblok

env

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

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

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

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

exit
