#!/bin/bash
#PBS -N obsproc_%JTYP%_prep_%PDY%_%CC%_%DESC%
#PBS -j oe 
#PBS -S /bin/bash
#PBS -q dev
#PBS -A OBSPROC-DEV
#PBS -l walltime=00:15:00
#PBS -l place=vscatter,select=1:ncpus=1:mem=40gb:prepost=true
#PBS -l debug=true

#####################################################################
# Submit notes:
# Run from [ps]tmp when running manually
# When running from cron, output is written to /u/$USER. Bottom of trigger mv's to stmp
# For specific PDY:
# > jtyp=rap|rap_e|rap_p cyc=00 PDY=20170126 desc=somethingfun /u/Shelley.Melchior/bin/cycqsub /path/to/triggers/jrap_prep.wc2.pbs
# For latest/current PDY:
# > jtyp=rap cyc=00 /u/Shelley.Melchior/bin/cycqsub /path/to/triggers/jrap_prep.wc2.pbs
# cycqsub location: /u/Shelley.Melchior/bin
#####################################################################

set -xu

export envir=prod
export cyc=%CC%
JTYP=%JTYP%
type=%DESC%
DESC=%DESC%
if [ $JTYP = "rap_e" ]; then
  export job=$(echo $JTYP | cut -c1-3)_prep_erly_$cyc
elif [ $JTYP = "rap_p" ]; then
  export job=$(echo $JTYP | cut -c1-3)_prep_pcyc_$cyc
else
export job=${JTYP}_prep_$cyc
fi
export jobid=$job.$PBS_JOBID
export PDY=%PDY%

userROOT=/lfs/h2/emc/obsproc/noscrub/$USER

export obsproc_ver=v1.0
export obsproc_ver_pckg=v1.0.0
export prepobs_ver=1.0.0
export obsNET=obsproc
PACKAGEROOTpara=/lfs/h1/ops/para/packages
#export HOMEobsproc=${PACKAGEROOTpara}/obsproc.${obsproc_ver_pckg}   # NCO para
#export HOMEobsproc=${PACKAGEROOT}/obsproc.${obsproc_ver_pckg}       # NCO prod
export HOMEobsproc=${userROOT}/install/obsproc-rmprep           # local

VERSION_FILE=${HOMEobsproc}/versions/run.ver
if [ -f $VERSION_FILE ]; then
  . $VERSION_FILE
else
  echo Need version info...  Exiting...
  exit 7
fi

#Load the modules
module load grib_util/${grib_util_ver}
module load netcdf/${netcdf_ver}
module load intel/${intel_ver}
module load craype/${craype_ver}
module load cray-mpich/${cray_mpich_ver}
module load cray-pals/${cray_pals_ver}
module load cfp/${cfp_ver}
# use local bufr_dump installation
#module unload bufr_dump
#module use ${userROOT}/install/bufr_dump/modulefiles
module load bufr_dump/${bufr_dump_ver}
# use local prepobs installation
#module unload prepobs
#module use ${userROOT}/install/prepobs/modulefiles
module load prepobs/${prepobs_ver}

#Check if they exist
if [[ $(echo $LOADEDMODULES | egrep -c "(^|:)grib_util/") -eq 0 ]]; then echo "grib_util is not loaded!"; fi
if [[ $(echo $LOADEDMODULES | egrep -c "(^|:)netcdf/") -eq 0 ]]; then echo "netcdf is not loaded!"; fi
if [[ $(echo $LOADEDMODULES | egrep -c "(^|:)bufr_dump/") -eq 0 ]]; then echo "bufr_dump is not loaded!"; fi
if [[ $(echo $LOADEDMODULES | egrep -c "(^|:)prepobs/") -eq 0 ]]; then echo "prepobs is not loaded!"; fi
if [[ $(echo $LOADEDMODULES | egrep -c "(^|:)intel/") -eq 0 ]]; then echo "intel is not loaded!"; fi
if [[ $(echo $LOADEDMODULES | egrep -c "(^|:)craype/") -eq 0 ]]; then echo "craype is not loaded!"; fi
if [[ $(echo $LOADEDMODULES | egrep -c "(^|:)cray-mpich/") -eq 0 ]]; then echo "cray-mpich is not loaded!"; fi
if [[ $(echo $LOADEDMODULES | egrep -c "(^|:)cray-pals/") -eq 0 ]]; then echo "cray-pals is not loaded!"; fi
if [[ $(echo $LOADEDMODULES | egrep -c "(^|:)cfp/") -eq 0 ]]; then echo "cfp is not loaded!"; fi


export SENDECF=NO  # developer
export SENDSDM=NO  # developer
#export SENDDBN=YES; export DBNROOT="echo test " # developer


export DATAROOT=/lfs/h2/emc/stmp/$USER
export jlogfile=/lfs/h2/emc/stmp/$USER/${JTYP}.${PDY}.jlogfile

export COMIN_ROOT=/lfs/h2/emc/stmp/$USER/CRON/${DESC}/com
#export COMINgdas=/lfs/h1/ops/canned/com/gfs/${gfs_ver}/gdas.${PDY}/${cyc}/atmos
#export COMINgfs=/lfs/h1/ops/canned/com/gfs/${gfs_ver}/gfs.${PDY}/${cyc}/atmos
#export GETGES_COM=/lfs/h1/ops/canned/com/gfs/${gfs_ver}/
#export GESROOT=/lfs/h1/ops/canned/com/gfs/${gfs_ver}/
export COMOUT_ROOT=/lfs/h2/emc/stmp/$USER/CRON/${DESC}/com/

export KEEPDATA=YES
export DEBUG_LEVEL=3
export LOUD=ON

#export POE=NO        # use these settings
#export BACK=NO       # when want to force
#export NSPLIT=1      # serial run

$HOMEobsproc/jobs/JOBSPROC_RAP_PREP
err=$?

# When run from cron, the stdout is written to /u/$USER
# mv to stmp
pbsjobid=$(echo $PBS_JOBID | cut -d'.' -f1)
cronlogfile=/u/$USER/$PBS_JOBNAME.o$pbsjobid
outputdir=/lfs/h2/emc/stmp/${USER}/CRON/${DESC}/output
if [ -f "$cronlogfile" ]; then
  mkdir -p $outputdir
  mv $cronlogfile ${outputdir}/$PBS_JOBNAME.o$pbsjobid
fi

# If you wish to only run the prep job, un-comment the exit line below
#exit

# Kick off prep_post job
if [ $err -eq 0 ]; then
  echo "submit jrap_prep_post:"
  jtyp=$JTYP PDY=$PDY desc=$DESC bash -l /u/$USER/bin/cycqsub \
  /lfs/h2/emc/obsproc/noscrub/Shelley.Melchior/triggers/jrap_prep_post.wc2.pbs
fi

exit