#!/bin/bash

[ $IRSPP_HOME ] || { echo "Please define environment variable IRSPP_HOME"; exit 1; }

[ -f $IRSPP_HOME/bin/irspp_env.sh ] || { echo "Error: cannot find $IRSPP_HOME/bin/irspp_env.sh"; exit 1; }

. $IRSPP_HOME/bin/irspp_env.sh

TOPDIR=$(cd ..; pwd)

#input and output files

mkdir -p $TOPDIR/output
infile_pc=$TOPDIR/input/W_XX-EUMETSAT-Darmstadt,SND+SAT,MTS1+IRS-1B-PC--Q4--CHK-BODY--DIS-NC4E_C_EUMT_20200604143345_IDPFS_DEV_20160315125300_20160315125310_N__C_0052_0049.nc
namelistfile=irs_main.nl
outfile_bufr=$TOPDIR/output/irs.bufr

append=No    #Set to Yes if you want reconstructed radiances in netCDF

#Note: to avoid making changes to the input dataset we create a copy in the output directory

if [ $append = Yes ]; then
  inoutfile_pc=$TOPDIR/output/$(basename $infile_pc)
  cp -p $infile_pc $inoutfile_pc
  chmod 644 $inoutfile_pc
fi

#Run the executable

if [ $append = Yes ]; then
  irs_main.exe -io $inoutfile_pc -n $namelistfile -o $outfile_bufr
else
  irs_main.exe -i $infile_pc -n $namelistfile -o $outfile_bufr
fi

status=$?

[ $status = 0 ] && echo "$0 finished" || echo "irs_main.exe failed with status $status"
