#!/bin/bash
#
# This script filters out any composites that are near duplicates in the final output directories 
# (satellite-specific) and then soft links those composite images to the VPW Tandem Image Triplet 
# Repository. It also archives VIIRS composite imagery to a local STAR archive for
# the months of Jan, Apr, Jul, Oct for later research. 

# NOTE: The processing of the composites can lead to duplicate (or nearly duplicate) entries
#       due to having duplicate granule files pulled from SCDR or the processing of near
#       duplicate granules. Granules are filtered at different points in this process, but
#       duplicate composites still sometime get created.

# Set paths
VIIRS_NRT_AMV_Path=/data/smcd8/abailey/run_ASF_VIIRS_Tandem_Winds/
VIIRS_NRT_Temporary_Data_Path=/data/smcd6/abailey/VIIRS/Temporary_Data/
VIIRS_Composite_Images_Archive_Path=/data/smcd8/VIIRS_Composite_Imagery_Archive/
VIIRS_Tandem_Maps=/data/smcd8/abailey/run_ASF_VIIRS_Tandem_Winds/Temporary_Data/Tandem_maps/

# Read the user entered VIIRS satellite being processed
vsat=$1

echo ' ' 
echo 'First check composite images for any near duplicates (it happens)...'
echo "${VIIRS_NRT_AMV_Path}VIIRS_NRT_Remove_Duplicate_Composites.bash $vsat"
      ${VIIRS_NRT_AMV_Path}VIIRS_NRT_Remove_Duplicate_Composites.bash $vsat

# For NRT VPW processing, link the pole-crossing images created from the projection step
# to a common directory of images for use in tandem winds processing. Images from all three LEO
# sats are placed (soft-linked) to one location and renamed to NOAA20 for the framework to process
# tham as tandem winds.
echo ' ' 
echo "Link any new composite images created to the VPW Tandem Triplet Image Repository for use by Winds program"
echo Pull file list from most recently created remapped wind files for $vsat...NP and SP
echo ' '

case "$vsat" in
  "npp") Remap_Dir="../${vsat}_maps/*ps_NPP*" ;;
  "j01") Remap_Dir="../${vsat}_maps/*ps_NOAA20*" ;;
  "j02") Remap_Dir=""../${vsat}_maps/*ps_NOAA21* ;;
esac

cd $VIIRS_Tandem_Maps

# loop through list of remapped files and link to common directory
for file in $Remap_Dir;
do
   # Link files (renamed as noaa20) to common repository for image triplets
   pole=$(echo $file | awk -F/ '{print $3}' | cut -c1-3)
   timestamp=$(echo $file | awk -F_ '{print $8"_"$9"_"$10}')
   linked_as_n20_file="${pole}_NOAA20_VIIRS_750M_Granule_3305x3305_${timestamp}"

   echo ' '
   echo "ln -sf $file $linked_as_n20_file"
         ln -sf $file $linked_as_n20_file

done # Done linking files

skip=no
if [[ "$skip" = "no" ]]
then

# We want to archive composite VIIRS imagery to a local STAR repository for research, but only
# for a period of one month from each season (i.e., Jan, Apr, Jul, Oct).
Composite_Dir=${VIIRS_NRT_Temporary_Data_Path}${vsat}_maps/*

# Loop through list of remapped files and copy to archive directory for the months
# of Jan, Apr, Jul, Oct
for file in $Composite_Dir;
do
   img_file=$(echo $file | awk -F/ '{print $8}')
   image_yyyyddd=$(echo $file | awk -F_ '{print $9}')
   calmonth=$(date -d "${image_yyyyddd:0:4}-01-01 ${image_yyyyddd:4:3} days -1 day" '+%m')

   # remove leading zero
   mm_nozero=$(echo $calmonth | sed 's/^0*//')

   if [[ "$mm_nozero" = 1 || "$mm_nozero" = 4 || "$mm_nozero" = 7 || "$mm_nozero" = 10 ]]
   then
      echo ' '
      echo Archive composite image for month ${calmonth}...
      echo "cp ${VIIRS_NRT_Temporary_Data_Path}${vsat}_maps/$img_file ${VIIRS_Composite_Images_Archive_Path}${vsat}/$calmonth"
            cp ${VIIRS_NRT_Temporary_Data_Path}${vsat}_maps/$img_file ${VIIRS_Composite_Images_Archive_Path}${vsat}/$calmonth

      # Run a second check on the VPW Tandem Image Triplet Repository for any duplicates. They
      # still seem to be occassionally showing up there even with the above filtering on the
      # individual composite directories before linking. Still trying to figure out why...in
      # the meantime run this.
      echo ' '
      echo 'First check composite images for any near duplicates (it happens)...'
      echo "${VIIRS_NRT_AMV_Path}VIIRS_Remove_Duplicate_Composites_In_Archive.bash"
            ${VIIRS_NRT_AMV_Path}VIIRS_Remove_Duplicate_Composites_In_Archive.bash
   fi
done
fi
