#!/bin/bash
# enable next line for debugging purpose
# set -x 

############################################
# User Configuration
############################################

# adapt this path to your needs
export PATH=~/home/shadi/project_93/coherence:$PATH
gptPath="/home/shadi/esa_snap/bin/gpt"

############################################
# Command line handling
############################################

# first parameter is a path to the graph xml
graphXmlPath="/home/shadi/project_93/graphs/Filter_high_coh_pixels.xml"

# second parameter is a path to a parameter file
parameterFilePath="$2"

# use third parameter for path to source products
sourceDirectory="/home/shadi/project_93/ifg"

# use fourth parameter for path to target products
targetDirectory="/home/shadi/project_93/coherence"

# the fifth parameter is a file prefix for the target product name, typically indicating the type of processing
targetFilePrefix="coh_filt_0.97"

   
############################################
# Helper functions
############################################
removeExtension() {
    file="$1"
    echo "$(echo "$file" | sed -r 's/\.[^\.]*$//')"
}


############################################
# Main processing
############################################

# Create the target directory
mkdir -p "/home/shadi/project_93/coherence/target_directory"

# the d option limits the elemeents to loop over to directories. Remove it, if you want to use files.
for F in $(ls -1d "/home/shadi/project_93/ifg"/S2*.SAFE); do
  sourceFile="$(realpath "$F")"
  targetFile="/home/shadi/project_93/coherence/coh_filt_0.97_$(removeExtension "$(basename ${F})").dim"
  /home/shadi/esa_snap/bin/gpt /home/shadi/project_93/graphs/Filter_high_coh_pixels.xml -e -p ${parameterFilePath} -t ${targetFile} ${sourceFile}
done
