#!/bin/bash
set -e

# E2E test for simputfile
# Run simputfile with XSpec spectral model and light curve as input,
# and compare output with a reference file.

# NOTE: running this script with the option -r runs it in reference mode,
# generating new reference data

# Reference simulation done with
# * Tool: simputfile
# * SIMPUT 2.6.3, commit afa158a

### Setup
. ./data/scripts/setup/setup_e2e.sh

# Simulation parameters
base=simputfile_e2e
base_ref=ref_simputfile_e2e
refdata_dir=./data/refdata/
###

# get mode in which we're running
refmode=0
while getopts ":r" arg
do
  case "$arg" in
    r)
      refmode=1
  esac
done

if [ $refmode == 1 ]
then
  echo "Running in reference mode"
  base=${base_ref}
else
  echo "Running in test mode"
fi

mkdir -p ${output_dir}

### Run sumputfile
simputfile \
  Simput=${output_dir}/${base}.fits \
  RA=40.2 Dec=12.8 \
  XSPECFile=data/input/example_spectrum.xcm \
  LCFile=data/input/example_lightcurve.dat \
  MJDREF=50800.0 \
  Emin=0.5 Emax=10.0 \
  srcFlux=2.3e-12 \
  clobber=yes


### Compare output with reference file
if [ $refmode == 0 ]
then
  ftdiff \
      infile1=${output_dir}/${base}.fits \
      infile2=${refdata_dir}/${base_ref}.fits \
      cmpdata=yes caldsum=yes cmpnum=no

  # Cleanup
  rm -rf ${output_dir}
fi


printf "\n%s executed successfully\n" "${0}"
exit 0
