#!/bin/csh -f
#
## HEADER ######################################################################
#
#  This script sets removes the NetCDF output files for each SMOKE program
#  because the I/O API cannot overwrite files when they are different.
#
################################################################################

set status = 0

## Abort if no files are to be deleted
#
if ( $?AUTO_DELETE ) then

   if ( $AUTO_DELETE == N ) then
      exit( 0 )
   endif
else
   echo "NOTE: AUTO_DELETE script setting is not set."
   echo "      Default is N to not delete SMOKE NCF intermediate and output files"
   exit( 0 )
endif

### Ensure new controller variables are set
if ( $?RUN_PART1 ) then
else
   setenv RUN_PART1 N 
endif
if ( $?RUN_PART1B ) then
else
   setenv RUN_PART1B N 
endif
if ( $?RUN_PART2 ) then
else
   setenv RUN_PART2 N 
endif
if ( $?RUN_PART3 ) then
else
   setenv RUN_PART3 N 
endif
if ( $?RUN_PART4 ) then
else
   setenv RUN_PART4 N 
endif

## For Smkinven and Grwinven output files
#
set import_aveinv = Y
if ( $?IMPORT_AVEINV_YN ) then 
   if ( $IMPORT_AVEINV_YN == N ) then
       set import_aveinv = N
   endif
endif

set inmap = ' '
if ( $RUN_SMKINVEN == 'Y' && $RUN_PART1 == 'Y' && $import_aveinv != N ) then
   if ( $SMK_SOURCE == 'A' ) then
      set inmap = $AREA
   endif
   if ( $SMK_SOURCE == 'M' ) then
      set inmap = $MOBL
   endif
   if ( $SMK_SOURCE == 'P' ) then
      set inmap = $PNTS
      if ( $DAY_SPECIFIC_YN == Y ) then
          /bin/rm -f $PDAY 
      endif
      if ( $HOUR_SPECIFIC_YN == Y ) then
          /bin/rm -f $PHOUR $PHOURPRO
      endif
   endif
endif

if ( $RUN_GRWINVEN == 'Y' && $RUN_PART1B == 'Y' && ( $SMK_FUTURE_YN == 'Y' || $SMK_CONTROL_YN == 'Y' )) then
   if ( $SMK_SOURCE == 'A' ) then
      set inmap = ( $AREA_O )
   endif
   if ( $SMK_SOURCE == 'M' ) then
      set inmap = ( $MOBL_O )
   endif
   if ( $SMK_SOURCE == 'P' ) then
      set inmap = ( $PNTS_O )
   endif
endif

if ( ( $RUN_SMKINVEN == Y && $RUN_PART1  == 'Y' && $import_aveinv != N ) || \
     ( $RUN_GRWINVEN == Y && $RUN_PART1B == 'Y' && ( $SMK_FUTURE_YN == 'Y' || $SMK_CONTROL_YN == 'Y' ) ) ) then
   foreach f ( $inmap )
   if ( $?f ) then
     set dataflag = n
     if ( -e $f ) then       
       set cnt = 0
       set ncnt = `cat $f | wc -l`
       while ( $cnt < $ncnt )
          @ cnt = $cnt + 1
          set line = ( `head -$cnt $f | tail -1` )
          if ( $line[1] == '/IOAPI/' ) then
             /bin/rm -f $INTERMED/$line[2]
          endif
          if ( $line[1] == '/DATMAP/' ) then
             set dataflag = y
             end
	  endif
          if ( $line[1] == '/END/' ) then
             set dataflag = n
          endif
          if ( $dataflag == y ) then
             if ( -e $INTERMED/$line[2] ) then
                /bin/rm -f $INTERMED/$line[2]
             endif
          endif
       end   
       if ( $status == 0 ) then
          /bin/rm $f
       endif
     endif
   endif     # if $f is set
   end
endif

if ( $RUN_GRDMAT == Y && $RUN_PART1 == 'Y' ) then
   if ( $SMK_SOURCE == A ) then
      /bin/rm -f $AGMAT
   endif
   if ( $SMK_SOURCE == M ) then
      /bin/rm -f $MGMAT
      /bin/rm -f $MUMAT
   endif
   if ( $SMK_SOURCE == P ) then
      /bin/rm -f $PGMAT
   endif
endif

if ( $RUN_SPCMAT == Y && $RUN_PART1 == 'Y' ) then
   if ( $SMK_SOURCE == A ) then
      set file = $ASMAT_S
      set len = `echo $file | wc -c`
      @ len = $len - 4
      set allfiles = `echo $file | cut -c1-$len`
      /bin/rm -rf $allfiles*      
      
      set file = $ASMAT_L
      set len = `echo $file | wc -c`
      @ len = $len - 4
      set allfiles = `echo $file | cut -c1-$len`
      /bin/rm -rf $allfiles*      
   endif
   if ( $SMK_SOURCE == M ) then
      set file = $MSMAT_S
      set len = `echo $file | wc -c`
      @ len = $len - 4
      set allfiles = `echo $file | cut -c1-$len`
      /bin/rm -rf $allfiles*      
      
      set file = $MSMAT_L
      set len = `echo $file | wc -c`
      @ len = $len - 4
      set allfiles = `echo $file | cut -c1-$len`
      /bin/rm -rf $allfiles*      
   endif
   if ( $SMK_SOURCE == P ) then
      set file = $PSMAT_S
      set len = `echo $file | wc -c`
      @ len = $len - 4
      set allfiles = `echo $file | cut -c1-$len`
      /bin/rm -rf $allfiles*      
 
      set file = $PSMAT_L
      set len = `echo $file | wc -c`
      @ len = $len - 4
      set allfiles = `echo $file | cut -c1-$len`
      /bin/rm -rf $allfiles*      
   endif
endif

if ( $RUN_TEMPORAL == Y && $RUN_PART2 == 'Y' ) then
   if ( $SMK_SOURCE == A ) then
      set roottmp = $ATMPNAME
   endif
   if ( $SMK_SOURCE == M ) then
      set roottmp = $MTMPNAME
   endif
   if ( $SMK_SOURCE == P ) then
      set roottmp = $PTMPNAME
   endif

   # Set array of temporal file names to use in smk_rmfiles.scr script
   set w = 0  # Initialize warning counter
   set nline = 0
   if ( -e $SMK_RUN_DATES_1 ) then
      set nline = `cat $SMK_RUN_DATES_1 | wc -l`  # Get number of lines in PROCDATES file
      set n = 0  # Initialize line counter
      while ( $n < $nline )    # Loop through number of lines in PROCDATES file
         @ n = $n + 1
         set tmpdate = `head -$n $SMK_RUN_DATES_1 | tail -1 | cut -d" " -f1`  # Extract YYYYMMDD

    	 if ( -e ${roottmp}${tmpdate}.ncf ) then
            /bin/rm -rf ${roottmp}${tmpdate}.ncf
         else
            @ w = $w + 1
         endif
      end
   else
      echo "SCRIPT WARNING: Could not delete Temporal output files because"
      echo "                SMK_RUN_DATES_1 file did not exist. File name:"
      echo "                $SMK_RUN_DATES_1"
   endif

   # Write warning that some of the files did not exist
   if ( $w == $nline && $w > 0 ) then
      echo "SCRIPT WARNING: All $w Temporal outputs not found for deletion"

   else
      if ( $w > 0 ) then
	 echo "SCRIPT WARNING: $w Temporal outputs not found for deletion"
      endif

   endif

endif

if ( $RUN_CNTLMAT == Y && $RUN_PART1B == 'Y' ) then
   if ( $SMK_SOURCE == A ) then
      /bin/rm -f $APMAT
      /bin/rm -f $ACMAT
      /bin/rm -f $ARMAT_S
      /bin/rm -f $ARMAT_L
   endif
   if ( $SMK_SOURCE == M ) then
      /bin/rm -f $MPMAT
      /bin/rm -f $MCMAT
      /bin/rm -f $MRMAT_S
      /bin/rm -f $MRMAT_L
   endif
   if ( $SMK_SOURCE == P ) then
      /bin/rm -f $PPMAT
      /bin/rm -f $PCMAT
      /bin/rm -f $PRMAT_S
      /bin/rm -f $PRMAT_L
   endif
endif

# C. Allen, 8 Aug 2014: SMK_EMISPATH no longer used
#if ( $RUN_EMISFAC == Y && $RUN_PART1 == 'Y' ) then
#   if ( $SMK_SOURCE == M ) then
#      /bin/rm -f $SMK_EMISPATH/*
#   endif
#endif

# C. Allen, 8 Aug 2014: SMK_METPATH no longer used
#if ( $RUN_PREMOBL == Y && $RUN_PART1 == 'Y' ) then
#   if ( $SMK_SOURCE == M ) then
#      /bin/rm -f $SMK_METPATH/*
#   endif
#endif

if ( $RUN_MBSETUP == Y && $RUN_PART1 == 'Y' ) then
   /bin/rm -f $DAILYGROUP  
   /bin/rm -f $WEEKLYGROUP 
   /bin/rm -f $MONTHLYGROUP
   /bin/rm -f $EPISODEGROUP
endif

if ( $RUN_RAWBIO == Y && $RUN_PART1 == 'Y' ) then
    /bin/rm -f $BGRD
    /bin/rm -f $BGRDW
endif

if ( $RUN_NORMBEIS3 == Y && $RUN_PART1 == 'Y' ) then
    /bin/rm -f $B3GRD
endif

if ( $RUN_TMPBIO == Y && $RUN_PART2 == 'Y' ) then
    /bin/rm -f $BGTS_S
    /bin/rm -f $BGTS_L
endif

if ( $RUN_TMPBEIS3 == Y && $RUN_PART2 == 'Y' ) then
    /bin/rm -f $B3GTS_L 
    /bin/rm -f $B3GTS_S 
    /bin/rm -f $B3GTS_L_O
    /bin/rm -f $B3GTS_S_O
endif

if ( $RUN_ELEVPOINT == Y && $RUN_PART3 == 'Y' ) then
    /bin/rm -f $STACK_GROUPS
endif

if ( $RUN_LAYPOINT == Y && $RUN_PART4 == 'Y' ) then
    /bin/rm -f $PLAY
    /bin/rm -f $PLAY_EX
endif

## For Smkmerge output NCF files
#
if ( ($RUN_SMKMERGE == 'Y' || $RUN_MOVESMRG == 'Y') && $RUN_PART4 == 'Y' ) then

   if ( $MRG_GRDOUT_YN == 'Y' ) then

      if ( $MRG_TEMPORAL_YN == 'Y' && $MRG_SPCMAT_YN == 'Y' ) then

         if ( $MRG_AREA == 'Y' || $SMK_SOURCE == 'A' ) then
	    /bin/rm -f $AOUT
         endif 

         if ( $MRG_MOBILE == 'Y' || $SMK_SOURCE == 'M' ) then
	    /bin/rm -f $MOUT
         endif 

         if ( $MRG_POINT == 'Y' || $SMK_SOURCE == 'P' ) then
	    /bin/rm -f $POUT
         endif 

         if ( $MRG_MULTI == 'Y' || $SMK_SOURCE == 'E' ) then
	    /bin/rm -f $EOUT  
         endif

      endif

   endif
#
## Elevated sources
#
   if ( $MRG_POINT == 'Y' && $SMK_PING_METHOD == 1 && $RUN_PART4 == 'Y' ) then
      /bin/rm -f $PING  
   endif

endif
