#! /bin/csh -f

#SBATCH --partition=singlepe
#SBATCH --time 72:00:00
#SBATCH --job-name=out_combine
#SBATCH --gid=romo
#SBATCH --output=/work/ROMO/users/hou/400.1/16s1_PSAT_v7.10/2.postproc/3.out_combine/outcombine.%j.rlog

#> The following commands output information from the SLURM
#> scheduler to the log files for traceability.
   if ( $SLURM_JOB_ID ) then
      echo
      echo Job ID is $SLURM_JOB_ID
      echo Host is $SLURM_SUBMIT_HOST
      echo Nodefile is $SLURM_JOB_NODELIST
      #> Switch to the working directory. By default,
      #>   SLURM launches processes from your home directory.
      echo Working directory is $SLURM_SUBMIT_DIR
      cd $SLURM_SUBMIT_DIR
   endif

#> Configure the system environment and set up the module capability
   limit stacksize unlimited

set jobID  = ${SLURM_JOB_ID}
set jobDIR = ${SLURM_SUBMIT_DIR}

unset verbose
unalias rm
unalias cp

#> Set the working directories and executable

set UTIL     = out_combine
set BASE     = /work/ROMO/users/hou/400.1/16s1_PSAT_v7.10/2.postproc
set DATADIR  = $BASE/outp
set WORKDIR  = $BASE/3.$UTIL

set SDATE    = 2016001
set EDATE    = 2016365

set DESC1    = allmonth.LST.Y_24.16s1_PSAT_v7.10
set DESC2    = camx_v710.GDIT-R6-BART.CAMxRun1.16s1_PSAT_v710.12km.${SDATE}-${EDATE}

set PSATAV1  = 
set PSATAV2  = ${DESC1}.avrg.grd02.sites.txt
set PSATSA1  = 
set PSATSA2  = ${DESC1}.sa.grd02.sites.txt
set OUTTXT   = ${DESC2}.data.template.txt

# check if input files exist

if (! (-e $DATADIR/$PSATAV2 ) ) then
   echo ' $DATADIR/$PSATAV2 must exist '
   exit
endif

if (! (-e $DATADIR/$PSATSA2 ) ) then
   echo ' $DATADIR/$PSATSA2 must exist '
   exit
endif

#if (! (-e $DATADIR) ) mkdir -p $DATADIR

# Remove any existing output files

if (-e $DATADIR/$OUTTXT) rm $DATADIR/$OUTTXT

setenv AVRGFILE2 $DATADIR/$PSATAV2
setenv SAFILE2   $DATADIR/$PSATSA2
setenv OUTFILE   $DATADIR/$OUTTXT

cd $WORKDIR
echo $SDATE

${UTIL}.exe < UI_${UTIL}

echo; date; echo

set TODAY = `date +%F | /usr/bin/perl -p -i -e "s/\-//g"`
\mv -vf ${jobDIR}/outcombine.${jobID}.rlog ${jobDIR}/outcombine.${TODAY}.rlog

echo

exit()

