#!/bin/bash
# Wrapper for running AAPP NOAA Level 1 processing in Singularity container
CONTAINER=$DBVM_HOME/aapp_build/dbnet/singularity/aapp_8.2.simg

# Get arguments
input_file=$1
tle_file=$2
input_file=$(readlink -f $input_file)
tle_file=$(readlink -f $tle_file)

# Create temporary directory
start_dir=$PWD
temp_dir=$(mktemp -d -p $PWD)
cd $temp_dir

# Create writable AAPP data directories
mkdir orbelems
singularity run -e -c -B $PWD:/mnt $CONTAINER cp -r /opt/install/AAPP_8.2/AAPP/data .

# Run processing script in Singularity container
export SINGULARITY_BINDPATH="$PWD:/mnt,$PWD/data:/opt/install/AAPP_8.2/AAPP/data,$PWD/orbelems:/opt/install/AAPP_8.2/AAPP/orbelems,$input_file,$tle_file,$DBVM_HOME/scripts/run_noaa_level1.bash"
singularity run -e -c $CONTAINER $DBVM_HOME/scripts/run_noaa_level1.bash $input_file $tle_file

# Clean up
cd $start_dir
mv $temp_dir/output .
rm -rf $temp_dir
