#!/bin/bash

# set -o verbose

# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------

help () {
   echo ""
   echo "Call: $0 sc-id datefile t0 t1 sys rate [calfile]"
   echo ""
   echo "sc-id     SC number (1-4)"
   echo "datefile  file containing dates in format yyyymmdd"
   echo "t0,t1     begin and end times in format yyyy-mm-ddThh:mm:ss.mssZ"
   echo "          or in t70 format"
   echo "sys       scs or gse"
   echo "rate      sr or hr (spin res. / high res.)"
   echo "calfile   optional calibration file specification"
   echo ""
   exit 1
}

# ----------------------------------------------------------------------------

if [ "$1" = "" ]; then 
    help
fi
sc=$1

if [ "$2" = "" ]; then
    help
fi
datefile=$2

if [ "$3" = "" ]; then
    help
fi
ct0=$3

if [ "$4" = "" ]; then
    help
fi
ct1=$4

if [ "$5" = "" ]; then
    help
fi
sys=$5

if [ "$6" = "" ]; then
    help
fi
rate=$6

if [ "$sys" != "scs" ] ; then
    if [ "$sys" != "gse" ] ; then
        help
    fi      
fi

if [ "$rate" != "hr" ] ; then
    if [ "$rate" != "sr" ] ; then
	help
    fi
fi

GDC_RDDA_ROOT="/windows/d/data/"
# GDC_RDDA_BCK1="/home/eg/usbhdd300/"
GDC_RDDA_BCK="/mnt/Ndata/CAA/" 
#GDC_RDDA_BCK="/mnt/Ndata/"
#####################  Don't change anything below this line ###
FGMROOT="/home/eg/cluster_fgm/"
export FGMPATH="$FGMROOT/cal/"
export SATTPATH="$FGMROOT/aux/"
exepath="$FGMROOT/bin/"
inp="$GDC_RDDA_BCK/rdda/"
inp1="$GDC_RDDA_BCK/rdda_bck/"
FGM_OUT="$(echo ~)/temporary.fgm"

if [ "$7" == "none" ]; then
   if [ "$rate" != hr ]; then
      echo "must use high rate data with calfile=none"
      exit 1
   fi
   echo "skipping calibration and coordinate transformation"
elif [ "$7" == "" ]; then
   echo "using default calfile"
   fgm_cal_cmd="fgmcal -i"
else
   echo "using special calfile $7"
   fgm_cal_cmd="fgmcal -i -c $7"
fi

datelist=$(cat $datefile)
for curdate in $datelist ; do
    echo "current date from list : $curdate"    
    #date=`echo $EDI_PISO_PRODUCTION_DATE | cut -c3-8`
    date=$(echo $curdate | cut -c3-8)
    echo "current date : $date"
    curinfiles=`find $inp -name *$date*fn*$sc -a ! -size 0 -o -name *$date*fb*$sc -a ! -size 0 -follow`
    if [ "$curinfiles" = "" ]; then
        curinfiles=`find $inp1 -name *$date*fn*$sc -a ! -size 0 -o -name *$date*fb*$sc -a ! -size 0 -follow`
    fi
    echo "files for $date : $curinfiles"
    infiles="$infiles $curinfiles"
done
    
if [ "$infiles" = "" ]; then 
    echo "error: no FGM data found for dates $datelist"
    exit 1
fi

echo "listing the files I found:"
for i in $infiles ; do
    echo "--> $i"
done
echo ""

 if [ "$7" == "none" ] ; then # no cal file to be used --> FS system, high res data (implicitly)
    echo "processing without calibration and coordinate transformation"
    $exepath/ddsmrg $infiles|$exepath/ddscut -b $ct0 -e $ct1|
    $exepath/fgmtel | $exepath/fgmvec -t1 -r -o $FGM_OUT
 else
    if [ "$rate" = "sr" ] ; then
       $exepath/ddsmrg $infiles|$exepath/ddscut -b $ct0 -e $ct1|
       $exepath/fgmtel | $exepath/$fgm_cal_cmd |
       $exepath/fgmhrt -s $sys |$exepath/fgmav -p 26.3671875| 
       $exepath/fgmvec -t1 -r -i -o $FGM_OUT
       \rm cal.log       
    else # high rate data
       $exepath/ddsmrg $infiles|$exepath/ddscut -b $ct0 -e $ct1|
       $exepath/fgmtel | $exepath/$fgm_cal_cmd |
       $exepath/fgmhrt -s $sys | 
       $exepath/fgmvec -t1 -r -o $FGM_OUT
       \rm cal.log
    fi   
 fi
 echo "data written to $FGM_OUT"
 echo ""
 exit 0

# --------------------------------------------------------------------------
