#!/bin/bash
# 
# Script for NRT VPW processing to run cleanup for L1b, the PS Projection step, or the Winds step. 
#
# For the L1b step it cleans up all the L1B files at the start of each cycle.  
#
# For the Projection step it cleans up all the Cloud granules no longer needed based on 
# the most recently generated composite image (sat dependent). 
#
# For Winds step it cleans up all composite images that have already been processed for winds. 
#     It cleans up the npp, j01, j02, and tandem repository composite images based on the 
#     most recently produced tandem winds dataset (for nps and sps).  
#

process=$1 # 'L1b' 'Projection' or 'Winds'
vsat=$2    # npp, j01, j02: Only used for projection path cleanup

# Set paths
VIIRS_Tandem_Image_Repository_Path=/data/smcd6/abailey/VIIRS/Temporary_Data/Tandem_maps/
VIIRS_Tandem_VPW_Output_Path=/data/smcd8/abailey/run_ASF_VIIRS_Tandem_Winds/Output/
VIIRS_Temporary_Data_Path=/data/smcd8/abailey/run_ASF_VIIRS_Tandem_Winds/Temporary_Data/
VIIRS_Intermediate_Data_Path=/data/smcd8/abailey/run_ASF_VIIRS_Tandem_Winds/Intermediate_Data/
VIIRS_vsat_l1b_path=/data/smcd8/abailey/run_ASF_VIIRS_Tandem_Winds/${vsat}/

echo "start processing time: "$(date)

if [ "$process" = "L1b" ]
then
   echo "Cleaning up ALL L1B Granules to process next cycle for $vsat..."
   rm ${VIIRS_vsat_l1b_path}*.h5

elif [ "$process" = "Projection" ] 
then
   case "$vsat" in
     "npp") noaa_sat="NPP" ;;
     "j01") noaa_sat="NOAA20" ;;
     "j02") noaa_sat="NOAA21" ;;
   esac

   # Pull Day/Time (yyyydddhhmm) from most recently created composite image (NP and SP)
   echo "Comp_Image_NP_Latest=$(ls -l "${VIIRS_Tandem_Image_Repository_Path}" | awk '{print $NF}' | grep "nps_${noaa_sat}" | tail -1 | awk -F_ '{print $8$9}')"
	 Comp_Image_NP_Latest=$(ls -l "${VIIRS_Tandem_Image_Repository_Path}" | awk '{print $NF}' | grep "nps_${noaa_sat}" | tail -1 | awk -F_ '{print $8$9}')
   echo "Comp_Image_SP_Latest=$(ls -l "${VIIRS_Tandem_Image_Repository_Path}" | awk '{print $NF}' | grep "sps_${noaa_sat}" | tail -1 | awk -F_ '{print $8$9}')"
         Comp_Image_SP_Latest=$(ls -l "${VIIRS_Tandem_Image_Repository_Path}" | awk '{print $NF}' | grep "sps_${noaa_sat}" | tail -1 | awk -F_ '{print $8$9}')

   echo " "
   echo "Removing Cloud granule files from the $VIIRS_Intermediate_Data_Path directory for $vsat."
   # Set based on most recently generated composite image with a buffer of minus 102 minutes
   Comp_Image_Latest=$(( Comp_Image_NP_Latest > Comp_Image_SP_Latest ? Comp_Image_NP_Latest : Comp_Image_SP_Latest ))

   # Convert the date string to seconds since the epoch.
   seconds_since_epoch=$(date -d "${Comp_Image_Latest:0:4}-01-01 +$((${Comp_Image_Latest:4:3}-1)) days ${Comp_Image_Latest:7:2}:${Comp_Image_Latest:9:2}" +"%s")

   # Subtract 102 minutes - this is done to allow for some overlap in the cloud files to 
   # ensure full composites are made with each cycle.
   new_seconds=$((seconds_since_epoch - 6120))

   # Convert the new epoch time back to the desired output format.
   Comp_Image_Latest_m102=$(date -d "@$new_seconds" +"%Y%j%H%M")

   echo "Comp_Image_Latest: $Comp_Image_Latest"
   echo "Comp_Image_Latest_m102: $Comp_Image_Latest_m102"

   echo "All cloud output granule Files older than: $Comp_Image_Latest_m102 will be removed."

   # Initialize a counter for the number of removed files.
   num=0

   echo ' '
   echo '========================================================================================== '
   echo ' '
   echo "Check granules in ${VIIRS_Intermediate_Data_Path}${vsat}:"
   echo ' '
   # L1B Band and Cloud Granule files
   L1B_Band_Files=($(ls -1 ${VIIRS_Intermediate_Data_Path}${vsat} | grep ${noaa_sat}_VIIRS | grep SAT_VIIRS))
   Cloud_Granule_Files=($(ls -1 ${VIIRS_Intermediate_Data_Path}${vsat} | grep ${noaa_sat}_VIIRS | grep CLOUD))

   # Get the number of files in the lists.
   L1B_Band_num=${#L1B_Band_Files[@]}
   Cloud_Granule_num=${#Cloud_Granule_Files[@]}

   # Loop through l1b band files and cleanup those no longer needed.
   for (( i = 0 ; i < L1B_Band_num ; i++ )); do
     # Pull out day/time from file
     file_date=$(echo ${L1B_Band_Files[$i]} | awk -F_ '{print $3}')
     file_hhmm=$(echo ${L1B_Band_Files[$i]} | awk -F_ '{print $4}')
     file_timecheck=$file_date$file_hhmm

     # Compare timestamps to most recent file time. Delete anything older than this.
     if (( $file_timecheck < $Comp_Image_Latest_m102 ))
     then
        echo "Comp_Image_Latest_m102=$Comp_Image_Latest_m102"
        echo "file_timecheck=$file_timecheck"
        # Delete files from directory, if they exist.
        if [[ -n "${L1B_Band_Files[$i]}" ]]; then
            echo "rm ${VIIRS_Temporary_Data_Path}${sat_prefix}_maps/${L1B_Band_Files[$i]}"
            rm "${VIIRS_Intermediate_Data_Path}${vsat}/${L1B_Band_Files[$i]}"
            ((num++))
        fi
      fi
   done

   echo ' '
   echo "$num L1b band granules removed from ${VIIRS_Intermediate_Data_Path}${vsat}"
   echo ' '
   echo '========================================================================================== '
   echo ' '

   # Reset counter for the number of removed files.
   num=0

   # Loop through cloud granule files and cleanup those no longer needed.
   for (( i = 0 ; i < Cloud_Granule_num ; i++ )); do
     # Pull out day/time from file
     file_date=$(echo ${Cloud_Granule_Files[$i]} | awk -F_ '{print $4}')
     file_hhmm=$(echo ${Cloud_Granule_Files[$i]} | awk -F_ '{print $5}')
     file_timecheck=$file_date$file_hhmm

     # Compare timestamps to most recent file time. Delete anything older than this.
     if (( $file_timecheck < $Comp_Image_Latest_m102 ))
     then
        echo "Comp_Image_Latest_m102=$Comp_Image_Latest_m102"
        echo "file_timecheck=$file_timecheck"
        # Delete files from directory, if they exist.
        if [[ -n "${Cloud_Granule_Files[$i]}" ]]; then
            echo "rm ${VIIRS_Temporary_Data_Path}${vsat}/${Cloud_Granule_Files[$i]}"
            rm "${VIIRS_Intermediate_Data_Path}${vsat}/${Cloud_Granule_Files[$i]}"
            ((num++))
        fi
      fi
   done

   echo ' '
   echo "$num Cloud granules removed from ${VIIRS_Intermediate_Data_Path}${vsat}"
   echo ' '
   echo '========================================================================================== '
   echo ' '

elif [ "$process" = "Winds" ] 
then
   # Pull Day/Time (yyyydddhhmm) from most recently created winds dataset (NP and SP)
   VPW_NP_Latest=$(find ${VIIRS_Tandem_VPW_Output_Path}/TANDEM_POLAR_3305x3305_NPS* -type f | sort -n | tail -1 | awk -F_ '{print $9$10}')
   VPW_SP_Latest=$(find ${VIIRS_Tandem_VPW_Output_Path}/TANDEM_POLAR_3305x3305_SPS* -type f | sort -n | tail -1 | awk -F_ '{print $9$10}')

   echo " " 
   echo "Removing composite image files from the $VIIRS_Temporary_Data_Path directories."
   echo "Files older than NP: $VPW_NP_Latest and SP: $VPW_SP_Latest will be removed."

   # Generate file lists from the image file directories and loop through the lists 
   # to delete any files no longer needed. 
   # Define the list of satellites to loop over.
   satellites=("npp" "j01" "j02" "Tandem")

   # Initialize a counter for the number of removed files.
   num=0

   echo ' '
   echo '========================================================================================== '
   echo ' '
    
   # Loop through each satellite prefix to handle the file lists.
   for sat_prefix in "${satellites[@]}"; do
       echo ' '
       echo "Check nps/sps triplet images in ${VIIRS_Temporary_Data_Path}${sat_prefix}_maps:"
       echo ' '
       # Triplet Images
       NP_Triplet_Image_Files=($(ls -1 ${VIIRS_Temporary_Data_Path}${sat_prefix}_maps | grep nps))
       SP_Triplet_Image_Files=($(ls -1 ${VIIRS_Temporary_Data_Path}${sat_prefix}_maps | grep sps))
    
       # Get the number of files in each list.
       NP_num=${#NP_Triplet_Image_Files[@]}
       SP_num=${#SP_Triplet_Image_Files[@]}
    
       # Loop through NP triplet image files and cleanup those no longer needed.
       for (( i = 0 ; i < NP_num ; i++ )); do
         # Pull out day/time from file
         file_date=$(echo ${NP_Triplet_Image_Files[$i]} | awk -F_ '{print $7}')
         file_hhmm=$(echo ${NP_Triplet_Image_Files[$i]} | awk -F_ '{print $8}')
         file_timecheck=$file_date$file_hhmm

         # Compare timestamps to most recent file time. Delete anything older than this.
         if (( $file_timecheck < $VPW_NP_Latest ))
         then
   #        echo "VPW_NP_Latest=$VPW_NP_Latest"
   #        echo "file_timecheck=$file_timecheck"
            # Delete files from directory, if they exist.
            if [[ -n "${NP_Triplet_Image_Files[$i]}" ]]; then
                echo "rm ${VIIRS_Temporary_Data_Path}${sat_prefix}_maps/${NP_Triplet_Image_Files[$i]}"
                rm "${VIIRS_Temporary_Data_Path}${sat_prefix}_maps/${NP_Triplet_Image_Files[$i]}"
                ((num++))
            fi
          fi
       done

       # Loop through SP triplet image files and cleanup those no longer needed.
       for (( i = 0 ; i < SP_num ; i++ )); do
         # Pull out day/time from file
         file_date=$(echo ${SP_Triplet_Image_Files[$i]} | awk -F_ '{print $7}')
         file_hhmm=$(echo ${SP_Triplet_Image_Files[$i]} | awk -F_ '{print $8}')
         file_timecheck=$file_date$file_hhmm

         # Compare timestamps to most recent file time. Delete anything older than this.
         if (( $file_timecheck < $VPW_SP_Latest ))
         then
   #        echo "VPW_SP_Latest=$VPW_SP_Latest"
   #        echo "file_timecheck=$file_timecheck"
            # Delete files from directory, if they exist.
            if [[ -n "${SP_Triplet_Image_Files[$i]}" ]]; then
                echo "rm ${VIIRS_Temporary_Data_Path}${sat_prefix}_maps/${SP_Triplet_Image_Files[$i]}"
                rm "${VIIRS_Temporary_Data_Path}${sat_prefix}_maps/${SP_Triplet_Image_Files[$i]}"
                ((num++))
            fi
         fi
       done

       echo ' '
       echo "$num nps/sps triplet images removed from ${VIIRS_Temporary_Data_Path}${sat_prefix}_maps"
       echo ' '
       echo '========================================================================================== '
       echo ' '
       # Reset counter for the number of removed files.
       num=0
   done

   echo ' '
   echo "Clean up older Winds Log directories and ASCII files - keep ~48 hours worth. "
   echo "${VIIRS_NRT_AMV_Path}clnmax_dir.bash -dir 40 ${VIIRS_NRT_AMV_Path}Output/ LOG_NPS_"
         ${VIIRS_NRT_AMV_Path}clnmax_dir.bash -dir 40 ${VIIRS_NRT_AMV_Path}Output/ LOG_NPS_
   echo "${VIIRS_NRT_AMV_Path}clnmax_dir.bash -dir 40 ${VIIRS_NRT_AMV_Path}Output/ LOG_SPS_"
         ${VIIRS_NRT_AMV_Path}clnmax_dir.bash -dir 40 ${VIIRS_NRT_AMV_Path}Output/ LOG_SPS_
   echo "${VIIRS_NRT_AMV_Path}clnmax_dir.bash -dir 40 ${VIIRS_NRT_AMV_Path}Output/ ASCII"
         ${VIIRS_NRT_AMV_Path}clnmax_dir.bash -dir 40 ${VIIRS_NRT_AMV_Path}Output/ ASCII

fi # Winds
echo "Done!"

