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

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

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

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

# first parameter is a path to the graph xml
graphXmlPath="/home/glu/SNAP_OLCI/AMK69/subset_c2rcc_pixex.xml"

# second parameter is a path to a parameter file
parameterFilePath="/home/glu/SNAP_OLCI/AMK69/resample_20m.properties"

# use third parameter for path to source products
sourceDirectory="/home/glu/SNAP_OLCI/AMK69/l1/"

# use fourth parameter for path to target products
targetDirectory="/home/glu/SNAP_OLCI/AMK69/subsets_test/"

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

   
############################################
# 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 elements to loop over to directories. Remove it, if you want to use files.
for F in $(ls -1d "${sourceDirectory}"S3*); do
  sourceFile="$(realpath "$F")"
  targetFile="${targetDirectory}/${targetFilePrefix}_$(removeExtension "$(basename ${F})").dim"
  ${gptPath} ${graphXmlPath} -e -p ${parameterFilePath} -t ${targetFile} ${sourceFile}
done
