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

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

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

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

# first parameter is a path to the graph xml
graphXmlPath="/Applications/snap/bin"

# second parameter is a path to a parameter file
parameterFilePath="/Applications/snap/bin"

# use third parameter for path to source products
sourceDirectory="/Users/Binbin/Documents/trytry"

# use fourth parameter for path to target products
targetDirectory="/Users/Binbin/Documents/trytry"

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

   
############################################
# 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}"/S2*.SAFE); do
  sourceFile="$(realpath "$F")"
  targetFile="${targetDirectory}/${targetFilePrefix}_$(removeExtension "$(basename ${F})").nc"
  ${gptPath} ${graphXmlPath} -e -p ${parameterFilePath} -t ${targetFile} -f NetCDF4-CF ${sourceFile}
done
