#!/bin/tcsh -f
alias MATH 'set \!:1 = `echo "\!:3-$" | bc -l`'

if ( ! $?GEOSUTIL ) then
      echo " "
      echo Environment variable GEOSUTIL must be defined before use!
      echo Set GEOSUTIL to the directory path of the GEOS_util module
      echo " "
      exit 1
endif

@ nmax = $#argv

# -------------------
if( $nmax == 0 ) then
Usage:
      echo " "
      echo "Usage:"
      echo "-----"
      echo "quickstat -rc       STATS_RC"
      echo "         [-exps     EXPS]"
      echo "         [-level    LEVEL]"
      echo "         [-fields   FIELDS]"
      echo "         [-plots    PLOTS]"
      echo "         [-output   OUTPUT]"
      echo "         [-gradsx   GRADSX]"
      echo "         [-batch          ]"
      echo "         [-partition PARTITION]"
      echo "         [-model   MODEL]"
      echo "         [-qos       QOS]"
      echo "         [-debug        ]"
      echo " "
      echo "where:    STATS_RC: is the full path (including filename) of the stats resource file to use         "
      echo "              EXPS: Experiment IDs to plot (e.g., 0 1 3 4 )  (Default: ALL experiments in STATS_RC) "
      echo "             LEVEL: Pressure Level to plot (e.g., 850 )      (Default: ALL Levels)                  "
      echo "            FIELDS: Fields to plot (e.g., p u v )            (Default: p u v t q h)                 "
      echo "             PLOTS: Plot Types (corcmp, sys, syscmp)         (Default: ALL)                         "
      echo "            OUTPUT: Output directory to contain plots        (Default: STATS_RC directory)          "
      echo "            GRADSX: GrADS executable to use for processing   (Default: grads)                       "
      echo "         PARTITION: Partition to use                         (Default: compute)                     "
      echo "             MODEL: Processor model to use                   (Default: hasw)                        "
      echo "            GRADSX: GrADS executable to use for processing   (Default: grads)                       "
      echo " "
      echo "Note:    Plots will be located in experiment sub-directories under OUTPUT                           "
      echo "         Usage of  -batch option will send plots to BATCH                                           "
      echo " "
      exit 1
endif


# Set Variables
# -------------
set QUEUE     = partition
set PARTITION = compute
set MODEL     = hasw
set debug     = FALSE
set batch     = NULL
set statsrc   = NULL
set output    = NULL
set gradsx    = grads
set exps      = ALL
set level     = ALL
set plots     = `echo corcmp pltsys syscmp`
set fields    = `echo p u v t q h`
set rms       = NULL

set lcd = `pwd`

# Parse command line
# ------------------

@       n  = 1
while( $n <= $nmax )
       if( "$argv[$n]" == "-rc" ) then
                    @ n = $n + 1
             set statsrc  = $argv[$n]
       endif
       if( "$argv[$n]" =~ \-lev* ) then
                    @ n = $n + 1
             set  level = $argv[$n]
       endif
       if( "$argv[$n]" =~ \-rms* ) then
                    @ n = $n + 1
             set    rms = $argv[$n]
       endif
       if( "$argv[$n]" =~ \-b* ) then
             set batch  = 'BATCH'
       endif
       if( "$argv[$n]" == "-partition" ) then
                    @ n = $n + 1
             set PARTITION = $argv[$n]
       endif
       if( "$argv[$n]" == "-model" ) then
                    @ n = $n + 1
             set MODEL = $argv[$n]
       endif
       if( "$argv[$n]" == "-qos" ) then
                    @ n = $n + 1
             set PARTITION = $argv[$n]
             set QUEUE     = 'qos'
       endif
       if( "$argv[$n]" == "-gradsx"   ) then
                    @ n = $n + 1
             set gradsx = $argv[$n]
       endif
       if( "$argv[$n]" == "-debug"   ) then
             set  debug = 'TRUE'
       endif
       if( "$argv[$n]" == "-output"   ) then
                    @ n = $n + 1
             set output = $argv[$n]
       endif
       if( "$argv[$n]" =~ \-exp*  ) then
                    @ n = $n + 1
                                      set exps = ""
                                      set next   = $argv[$n]
                                      set bit    = `echo $next | cut -c1-1`
                                      while( "$bit" != "-" )
                                         set exps = `echo $exps $next`
                                          @   n  = $n + 1
                                         if( $n <= $nmax ) then
                                             set next = $argv[$n]
                                             set bit  = `echo $next | cut -c1-1`
                                         else
                                             set bit  = "-"
                                         endif
                                      end
                                             @ n = $n - 1
       endif
       if( "$argv[$n]" =~ \-plot* ) then
                    @ n = $n + 1
                                      set plots = ""
                                      set   next  = $argv[$n]
                                      if( "$next" == "sys" ) set next = pltsys
                                      set bit    = `echo $next | cut -c1-1`
                                      while( "$bit" != "-" )
                                         set plots = `echo $plots $next`
                                          @   n  = $n + 1
                                         if( $n <= $nmax ) then
                                             set   next  = $argv[$n]
                                             if( "$next" == "sys" ) set next = pltsys
                                             set bit  = `echo $next | cut -c1-1`
                                         else
                                             set bit  = "-"
                                         endif
                                      end
                                             @ n = $n - 1
       endif
       if( "$argv[$n]" =~ \-field*  ) then
                    @ n = $n + 1
                                      set fields = ""
                                      set next   = $argv[$n]
                                      set bit    = `echo $next | cut -c1-1`
                                      while( "$bit" != "-" )
                                         set fields = `echo $fields $next`
                                          @   n  = $n + 1
                                         if( $n <= $nmax ) then
                                             set next = $argv[$n]
                                             set bit  = `echo $next | cut -c1-1`
                                         else
                                             set bit  = "-"
                                         endif
                                      end
                                             @ n = $n - 1
       endif
     @ n = $n + 1
end

if( $statsrc  == NULL | -d $statsrc ) then
     echo ' '
     echo '\!\! You must enter the full path (including filename) of the stats.rc to use \!\!'
     goto Usage
     exit
endif

set rcfile = `basename $statsrc`
setenv GRADSX $gradsx 


# Determine ROOT directory containing stats.rc
# --------------------------------------------
@ n = 1
set root = `echo $statsrc | cut -d / -f$n`
while( .$root == . )
@ n = $n + 1
set root = `echo $statsrc | cut -d / -f$n`
end

set ROOTDIR = '' 
while( $root != $rcfile )
set ROOTDIR = `echo ${ROOTDIR}/${root}`
@ n = $n + 1 
set root = `echo $statsrc | cut -d / -f$n`
end


# Set SOURCE directory containing Plots
# -------------------------------------
if( $output == NULL ) then
    setenv SOURCE $ROOTDIR
else
    if(! -e $output ) mkdir -p $output
    setenv SOURCE $output
    if(-e $output/$rcfile ) /bin/rm -f $output/$rcfile
    /bin/ln -sf $statsrc $output
endif



# Determine Experiments to plot
# -----------------------------
if( $exps[1] == ALL ) then
 set allexps = `grep "EXP.*:" $statsrc | grep -v \# | grep -v \*`
 @   numexps = $#allexps / 2
 set    exps = ''
 @   n = 1
 while ($n <= $numexps )
   @ loc = $n * 2 - 1
   set len1 = `echo $allexps[$loc] | awk '{print length($0)}'`
   @   len2 = $len1 - 1
   set exp  = `echo $allexps[$loc] | cut -c4-$len2`
   set exps = `echo $exps $exp`
   @      n = $n + 1
 end
endif


# Configure QUICKSTAT to Computing Platform
# -----------------------------------------
cd $SOURCE

if(    -e LOCKFILE ) /bin/echo "Waiting for LOCKFILE ... "
if(    -e LOCKFILE ) set LOCK = `stat -c %Z LOCKFILE`
while( -e LOCKFILE )
          set    EPOCH = `date +'%s'`
          @    LOCKAGE = $EPOCH - $LOCK
          if( $LOCKAGE > 120 ) /bin/rm -f LOCKFILE
end
                                 lockfile LOCKFILE
$GEOSUTIL/plots/configure
source $SOURCE/.quickplotrc
     /bin/rm -f LOCKFILE

echo " "
echo "        ARCH = " `uname`
echo "        HOST = " `hostname`
echo "       GRADS = " $GRADS
echo "    GEOSUTIL = " $GEOSUTIL
echo "      OUTPUT = " $SOURCE
echo "    STATS_RC = " $statsrc
echo "        EXPS = " $exps
echo "       LEVEL = " $level
echo "      FIELDS = " $fields
echo "       PLOTS = " $plots
echo " "
 
# Check for BATCH Job
# -------------------
if( $batch == 'BATCH' ) then

# Set Default GEOS Build Directories
# ----------------------------------
set  GEOSSRC = @CMAKE_INSTALL_PREFIX@
set  GEOSBIN = @CMAKE_INSTALL_PREFIX@/bin
set  GEOSAPP = @CMAKE_INSTALL_PREFIX@/bin


# Create QUICKSTAT commands
# -------------------------
set NPLOTS  = $#plots
set NEXPS   = $#exps
set NFIELDS = $#fields

set  FIELDS_PER_TASK_SYS    = 1
set  FIELDS_PER_TASK_SYSCMP = 1

echo "             NPLOTS     = " $NPLOTS
echo "             NEXPS      = " $NEXPS
echo "             NFIELDS    = " $NFIELDS
echo " FIELDS_PER_TASK_SYS    = " $FIELDS_PER_TASK_SYS
echo " FIELDS_PER_TASK_SYSCMP = " $FIELDS_PER_TASK_SYSCMP
echo " "

  @ SUBTASKS_SYS = 0
  @ n = 0
  foreach FIELD ($fields)
  @ n = $n + 1
    if( $n == $FIELDS_PER_TASK_SYS ) then
        @ SUBTASKS_SYS = $SUBTASKS_SYS + 1
        @ n = 0
    endif
  end
if( $n != 0 ) then
    @ SUBTASKS_SYS = $SUBTASKS_SYS + 1
endif

  @ SUBTASKS_SYSCMP = 0
  @ n = 0
  foreach FIELD ($fields)
  @ n = $n + 1
    if( $n == $FIELDS_PER_TASK_SYSCMP ) then
        @ SUBTASKS_SYSCMP = $SUBTASKS_SYSCMP + 1
        @ n = 0
    endif
  end
if( $n != 0 ) then
    @ SUBTASKS_SYSCMP = $SUBTASKS_SYSCMP + 1
endif

set quickstat_corcmp = ''
set quickstat_pltsys = ''
set quickstat_syscmp = ''

@ NTASKS = 0
foreach plot ($plots)
   if( $plot == 'corcmp' ) set quickstat_corcmp = `echo "./quickstat -rc $statsrc -plots corcmp "`
   if( $plot == 'pltsys' ) set quickstat_pltsys = `echo "./quickstat -rc $statsrc -plots sys    "`
   if( $plot == 'syscmp' ) then
       set quickstat_syscmp = `echo "./quickstat -rc $statsrc -plots syscmp "`
       set syscmpexps = ''
       foreach exp ($exps)
       if( $exp != 0 ) set syscmpexps = `echo $syscmpexps $exp`
       end
   endif
end
echo " "

/bin/rm -f $SOURCE/gcm_quickstat.tmpl
touch      $SOURCE/gcm_quickstat.tmpl
cat        $GEOSAPP/gcm_quickstat.j >> $SOURCE/gcm_quickstat.tmpl

@ IDX = 1

# Create CORCMP Commands
# ----------------------
if( $#quickstat_corcmp > 1 ) then
      foreach FIELD ($fields)
      echo 'if( $ID == '$IDX' ) '$quickstat_corcmp -field $FIELD              >> $SOURCE/gcm_quickstat.tmpl
              @ IDX =   $IDX + 1
      end
      echo " "                                                                >> $SOURCE/gcm_quickstat.tmpl
endif

# Create PLTSYS Commands
# ----------------------
if( $#quickstat_pltsys > 1 ) then
      foreach EXP  ($exps)
      foreach FIELD ($fields)
         if( $FIELD == 'p' ) then
             echo 'if( $ID == '$IDX' ) '$quickstat_pltsys" -field $FIELD -exp $EXP" -level 1000        >> $SOURCE/gcm_quickstat.tmpl
                     @ IDX =   $IDX + 1
         else
             if( $level == 'ALL' ) then
                 echo 'if( $ID == '$IDX' ) '$quickstat_pltsys" -field $FIELD -exp $EXP" -level HORIZ   >> $SOURCE/gcm_quickstat.tmpl
                         @ IDX  =  $IDX  + 1
                 echo 'if( $ID == '$IDX' ) '$quickstat_pltsys" -field $FIELD -exp $EXP" -level ZONAL   >> $SOURCE/gcm_quickstat.tmpl
                         @ IDX  =  $IDX  + 1
             else
                 echo 'if( $ID == '$IDX' ) '$quickstat_pltsys" -field $FIELD -exp $EXP" -level $level  >> $SOURCE/gcm_quickstat.tmpl
                         @ IDX  =  $IDX  + 1
             endif
         endif
      end
      echo " "                                                                                  >> $SOURCE/gcm_quickstat.tmpl
      end
endif
 
# Create SYSCMP Commands
# ----------------------
if( $#quickstat_syscmp > 1 ) then
      foreach EXP  ($syscmpexps)
      foreach FIELD ($fields)
         if( $FIELD == 'p' ) then
             echo 'if( $ID == '$IDX' ) '$quickstat_syscmp" -field $FIELD -exp $EXP"  -level 1000   >> $SOURCE/gcm_quickstat.tmpl
                     @ IDX =   $IDX + 1
         else
             if( $level == 'ALL' ) then
                 echo 'if( $ID == '$IDX' ) '$quickstat_syscmp" -field $FIELD -exp $EXP"  -level HORIZ  >> $SOURCE/gcm_quickstat.tmpl
                         @ IDX  =  $IDX  + 1
                 echo 'if( $ID == '$IDX' ) '$quickstat_syscmp" -field $FIELD -exp $EXP"  -level ZONAL  >> $SOURCE/gcm_quickstat.tmpl
                         @ IDX  =  $IDX  + 1
             else
                 echo 'if( $ID == '$IDX' ) '$quickstat_syscmp" -field $FIELD -exp $EXP"  -level $level >> $SOURCE/gcm_quickstat.tmpl
                         @ IDX  =  $IDX  + 1
             endif
         endif
      end
      end
      echo " "                                                                    >> $SOURCE/gcm_quickstat.tmpl
endif

@ NTASKS =   $IDX - 1
@ NODES  = ( $NTASKS - 1 ) / 28 + 1

/bin/rm -f $SOURCE/gcm_quickstat.j
/bin/rm -f $SOURCE/sedfile
touch      $SOURCE/sedfile
cat >      $SOURCE/sedfile << EOF
s?@GEOSBIN?$GEOSBIN?g
s?@GEOSSRC?$GEOSSRC?g
s?@NTASKS?$NTASKS?g
s?@NODES?$NODES?g
s?@PARTITION?$PARTITION?g
s?@MODEL?$MODEL?g
s?@QUEUE?$QUEUE?g
EOF
sed -f $SOURCE/sedfile $SOURCE/gcm_quickstat.tmpl > $SOURCE/gcm_quickstat.j

echo "Please Submit: $SOURCE/gcm_quickstat.j  for processing"
echo " "
exit
endif


# Run Grads-Based Quickstat Utility for Anomaly Correlation Comparison
# --------------------------------------------------------------------
if( "$plots" =~ *corcmp* ) then
     set       tmpdir = ""

     foreach field ($fields)
        if( $rms == 'NULL' ) then
             set rmstypes = `echo 0 1 2 3 4`
        else
             set rmstypes = $rms
        endif
        foreach rmstype ($rmstypes)
           set       pltdir = $SOURCE/scratch_corcmp$rmstype/$field.`date +%Y%m%d_%H%M%S`
           mkdir -p $pltdir
           cd       $pltdir ; set tmpdir = `echo $tmpdir $pltdir`

           if( $debug == 'TRUE' ) then
               $GRADS -l    -c "run $GEOSUTIL/plots/fcst/pltcorcmp -source $SOURCE -rc $statsrc -fields $field -rms $rmstype"
           else
               $GRADS -l -b -c "run $GEOSUTIL/plots/fcst/pltcorcmp -source $SOURCE -rc $statsrc -fields $field -rms $rmstype" &
           endif
           sleep 5

        end
     end

     foreach exp ($exps)
     if( $exp != 0 ) then

        set DESC = `cat $statsrc | grep DESC:      | grep -v \# | grep -v \* | cut -d: -f2-`
        set EXP0 = `cat $statsrc | grep EXP0:      | grep -v \# | grep -v \* | cut -d: -f2-`
        set DSC0 = `cat $statsrc | grep DSC0:      | grep -v \# | grep -v \* | cut -d: -f2-`
        set EXP1 = `cat $statsrc | grep EXP${exp}: | grep -v \# | grep -v \* | cut -d: -f2-`
        set DSC1 = `cat $statsrc | grep DSC${exp}: | grep -v \# | grep -v \* | cut -d: -f2-`

        foreach field ($fields)
        if( $field != p ) then
           set       pltdir = $SOURCE/scratch_corcmpz/$field.`date +%Y%m%d_%H%M%S`
           mkdir -p $pltdir
           cd       $pltdir ; set tmpdir = `echo $tmpdir $pltdir`

           /bin/rm -f          $pltdir/corcmp_$exp.rc
           touch               $pltdir/corcmp_$exp.rc
           echo DESC: $DESC >> $pltdir/corcmp_$exp.rc
           echo EXP0: $EXP0 >> $pltdir/corcmp_$exp.rc
           echo DSC0: $DSC0 >> $pltdir/corcmp_$exp.rc
           echo EXP1: $EXP1 >> $pltdir/corcmp_$exp.rc
           echo DSC1: $DSC1 >> $pltdir/corcmp_$exp.rc

           if( $debug == 'TRUE' ) then
               $GRADS -l    -c "run $GEOSUTIL/plots/fcst/pltcorcmpz -source $SOURCE -rc $pltdir/corcmp_$exp.rc -fields $field"
           else
               $GRADS -l -b -c "run $GEOSUTIL/plots/fcst/pltcorcmpz -source $SOURCE -rc $pltdir/corcmp_$exp.rc -fields $field" &
           endif
           sleep 5

        endif
        end

        foreach field ($fields)
        if( $field != p ) then
           if( $rms == 'NULL' ) then
                set rmstypes = `echo 0 1 2 3 4`
           else
                set rmstypes = $rms
           endif
           foreach rmstype ($rmstypes)

           set       pltdir = $SOURCE/scratch_rmscmp${rmstype}z/$field.`date +%Y%m%d_%H%M%S`
           mkdir -p $pltdir
           cd       $pltdir ; set tmpdir = `echo $tmpdir $pltdir`

           /bin/rm -f          $pltdir/corcmp_$exp.rc
           touch               $pltdir/corcmp_$exp.rc
           echo DESC: $DESC >> $pltdir/corcmp_$exp.rc
           echo EXP0: $EXP0 >> $pltdir/corcmp_$exp.rc
           echo DSC0: $DSC0 >> $pltdir/corcmp_$exp.rc
           echo EXP1: $EXP1 >> $pltdir/corcmp_$exp.rc
           echo DSC1: $DSC1 >> $pltdir/corcmp_$exp.rc
           
           if( $debug == 'TRUE' ) then
               $GRADS -l    -c "run $GEOSUTIL/plots/fcst/pltrmscmpz -source $SOURCE -rc $pltdir/corcmp_$exp.rc -fields $field -rms $rmstype"
           else
               $GRADS -l -b -c "run $GEOSUTIL/plots/fcst/pltrmscmpz -source $SOURCE -rc $pltdir/corcmp_$exp.rc -fields $field -rms $rmstype" &
           endif
           sleep 5

           end
        endif
        end

     endif
     end
     wait
     cd $SOURCE
    $GEOSUTIL/plots/grads_util/stats_montage $statsrc
    $GEOSUTIL/plots/grads_util/stats_rms_montage
    /bin/rm -r $tmpdir
endif


# Run Grads-Based Quickstat Utility for Systematic Error
# ------------------------------------------------------
if( "$plots" =~ *pltsys* ) then
     set tmpdir = ""
     foreach exp   ($exps)
     foreach field ($fields)
        if( $field == p ) then
            if( $level == 'ALL' | $level == 1000 ) then
                set       pltdir = $SOURCE/scratch_pltsys/EXP${exp}_${field}_1000.`date +%Y%m%d_%H%M%S`
                mkdir -p $pltdir
                cd       $pltdir ; set tmpdir = `echo $tmpdir $pltdir`
                if( $debug == 'TRUE' ) then
                    $GRADS -l    -c "run pltsys.gs -exp $exp -field $field -rc $statsrc -level 1000"
                else
                    $GRADS -l -b -c "run pltsys.gs -exp $exp -field $field -rc $statsrc -level 1000" &
                endif
                sleep 5
            endif
        else
                                      set levels =  $level
            if( "$level" == "HORIZ" ) set levels = `echo 1000 975 950 925 900 850 800 750 700 600 500 400 300 250 200 150 100 70 50 30 10 7 5 3 1`
            if( "$level" == "ZONAL" ) set levels = `echo ZONAL`

            foreach level  ( $levels )
               set       pltdir = $SOURCE/scratch_pltsys/EXP${exp}_${field}_${level}.`date +%Y%m%d_%H%M%S`
               mkdir -p $pltdir
               cd       $pltdir ; set tmpdir = `echo $tmpdir $pltdir`
               if( $debug == 'TRUE' ) then
                   $GRADS -l    -c "run pltsys.gs -exp $exp -field $field -rc $statsrc -level $level"
               else
                   $GRADS -l -b -c "run pltsys.gs -exp $exp -field $field -rc $statsrc -level $level" &
               endif
               sleep 5
            end
        endif
     end
     end
     wait
     foreach exp   ($exps)
        set DESC = `cat $statsrc | grep DSC$exp | grep -v \# | grep -v \* | grep : | cut -d: -f2`
        set sysfiles = `/bin/ls -1 $SOURCE/$DESC/*fma*.nc3`
        set SEASON = `echo $sysfiles[1] | cut -d. -f3`
        if( $#sysfiles == 56 ) then
            if(    -e LOCKFILE ) /bin/echo "Waiting for LOCKFILE ... "
            if(    -e LOCKFILE ) set LOCK = `stat -c %Z LOCKFILE`
            while( -e LOCKFILE )
                      set    EPOCH = `date +'%s'`
                      @    LOCKAGE = $EPOCH - $LOCK
                      if( $LOCKAGE > 120 ) /bin/rm -f LOCKFILE
            end
            lockfile LOCKFILE
            $GEOSUTIL/plots/grads_util/make_syserr $statsrc
            /bin/rm -f LOCKFILE
        endif
     end
    /bin/rm -r $tmpdir
endif


# Run Grads-Based Quickstat Utility for Systematic Error Comparison
# -----------------------------------------------------------------
if( "$plots" =~ *syscmp* ) then
     set tmpdir = ""
     foreach exp   ($exps)
     if( $exp != 0 ) then
         foreach field ($fields)
           if( $field == p ) then
               if( $level == 'ALL' | $level == 1000 ) then
                   set       pltdir = $SOURCE/scratch_syscmp/EXP${exp}_${field}_1000.`date +%Y%m%d_%H%M%S`
                   mkdir -p $pltdir
                   cd       $pltdir ; set tmpdir = `echo $tmpdir $pltdir`
                   if( $debug == 'TRUE' ) then
                       $GRADS -l    -c "run $GEOSUTIL/plots/fcst/setup_statd -exps 0 $exp -fields $field -rc $statsrc -levels 1000 "
                   else
                       $GRADS -l -b -c "run $GEOSUTIL/plots/fcst/setup_statd -exps 0 $exp -fields $field -rc $statsrc -levels 1000 " &
                   endif
                   sleep 5
               endif
           else
                                         set levels =  $level
               if( "$level" == "HORIZ" ) set levels = `echo 1000 975 950 925 900 850 800 750 700 600 500 400 300 250 200 150 100 70 50 30 10 7 5 3 1`
               if( "$level" == "ZONAL" ) set levels = `echo ZONAL`

               foreach level  ( $levels )
               set       pltdir = $SOURCE/scratch_syscmp/EXP${exp}_${field}_${level}.`date +%Y%m%d_%H%M%S`
               mkdir -p $pltdir
               cd       $pltdir ; set tmpdir = `echo $tmpdir $pltdir`
               if( $debug == 'TRUE' ) then
                   $GRADS -l    -c "run $GEOSUTIL/plots/fcst/setup_statd -exps 0 $exp -fields $field -rc $statsrc -levels $level "
               else
                   $GRADS -l -b -c "run $GEOSUTIL/plots/fcst/setup_statd -exps 0 $exp -fields $field -rc $statsrc -levels $level " &
               endif
               sleep 5
               end
           endif
         end
     endif
     end
     wait
    /bin/rm -r $tmpdir
endif


# Sleep for 30 seconds to ensure all plots are finished
# -----------------------------------------------------
sleep 30


# Reset Environment
# -----------------
cd $SOURCE

exit
