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

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

# adapt this path to your needs
export PATH=/home/nico/esa-snap/bin:$PATH
gptPath="/home/nico/esa-snap/bin/gpt"

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

# first parameter is a path to the graph xml
graphXmlPath="/home/nico/esa-snap/gpt_processing_files/atm.xml"

# second parameter is a path to a parameter file
parameterFilePath="/home/nico/esa-snap/gpt_processing_files/atm.properties"

# use third parameter for path to source products
sourceDirectory="/media/nico/Archivio_dati/Magellano/pf/Sentinel2_caltavuturo/resampl-mask"

# use fourth parameter for path to target products
targetDirectory="/media/nico/Archivio_dati/Magellano/pf/Sentinel2_caltavuturo/atm"

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


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