#!/bin/csh -f

#SBATCH --partition=singlepe
#SBATCH --time 72:00:00
#SBATCH --job-name=model_select
#SBATCH --gid=romo
#SBATCH --output=/work/ROMO/users/hou/400.1/16s1_PSAT_v7.10/2.postproc/2.model_select/modsel.%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    = model_select
 set BASE    = /work/ROMO/users/hou/400.1/16s1_PSAT_v7.10/2.postproc
 set DATADIR = $BASE/outp
 set WORKDIR = $BASE/2.$UTIL

 set SDATE = 2016001
 set EDATE = 2016365
 
foreach file (avrg sa)
#foreach file (sa)
foreach grd  (02)

#set DESC     = camx_v630.EPA-R6-2016.TX-BART.CAMxRun4.36-12.PiG-PSAT.${SDATE}-${EDATE}.${file}.grd${grd}
#set DESC     = camx_v710.GDIT-R6-BART.CAMxRun1.12km.PSAT.${SDATE}-${EDATE}.${file}.grd${grd}
 set DESC     = allmonth.LST.Y_24.16s1_PSAT_v7.10.${file}.grd${grd}
 set MASKDATA = improve_maskdata_grd${grd}.2016_BART.csv
 set DAVG     = ${DESC}.ncf
 set OUTNC    = ${DESC}.sites.ncf
 set OUTTXT   = ${DESC}.sites.txt
 
 unsetenv INFILE
 unsetenv SITEFILE
 unsetenv OUTFILE
 unsetenv ASCIIFILE
 
 setenv INFILE    $DATADIR/$DAVG
 setenv SITEFILE  $WORKDIR/$MASKDATA
 setenv OUTFILE   $DATADIR/$OUTNC
 setenv ASCIIFILE $DATADIR/$OUTTXT
 
 # check if input files exist
 
 if (! (-e $INFILE ) ) then
    echo " $INFILE must exist "
    exit
 endif
 
 if (! (-e $SITEFILE ) ) then
    echo " $SITEFILE must exist "
    exit
 endif
 
 # Remove any existing output files
 
 if (-e $OUTFILE)   rm $OUTFILE
 if (-e $ASCIIFILE) rm $ASCIIFILE
  
 cd $WORKDIR
 
 ${UTIL}.exe < UI_${UTIL}_${file}_grd${grd}
 
# if (${grd} == 01) then
#     if (${file} == avrg) then
#       set AV1_sites  = $ASCIIFILE
#       echo "Text file $AV1_sites written and set to AV1 "
#     else 
#       set SA1_sites  = $ASCIIFILE
#       echo " Text file $SA1_sites written and set to SA1 "
#     endif
# else
#     if (${file} == avrg) then
#       set AV2_sites  = $ASCIIFILE
#       echo " Text file $AV2_sites written and set to AV2 "
#     else 
#       set SA2_sites  = $ASCIIFILE
#       echo " Text file $SA2_sites written and set to SA2 "
#     endif
# endif
     
end	# grd = 01 or 02
end	# file = avrg or sa

echo; date; echo

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

echo

exit()

