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

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

# adapt this path to your needs
export PATH=~progs/snap/bin:$PATH
gptPath="/Applications/snap/bin/gpt"

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

# first parameter is a path to the graph xml
graphXmlPath="/Users/amalia/Documents/ICMAN/DINOPHISYS/SENTINEL_3/gpt_olcic2rcc_graphsxml.xml"

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

# use third parameter for path to source products
sourceDirectory="/Users/amalia/Documents/ICMAN/DINOPHISYS/SENTINEL_3/2016/SNAP_subset_2016"

# use fourth parameter for path to target products
targetDirectory="/Users/amalia/Documents/ICMAN/DINOPHISYS/SENTINEL_3/2016/C2RCC"

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

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


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

# Create the target directory
mkdir -p "${targetDirectory}"

# the d option limits the elemeents to loop over to directories. Remove it, if you want to use files.
for F in $(ls -1d "${sourceDirectory}"/*.dim); do
  sourceFile="$(realpath "$F")"
  targetFile="${targetDirectory}/${targetFilePrefix}_$(removeExtension "$(basename ${F})").dim"
  ${gptPath} ${graphXmlPath} -e -p ${parameterFilePath} -t ${targetFile} ${sourceFile}
done
