#!/bin/bash
#==============================================================
#  Name: rfGetRawAdcs.sh
#  Abs:  Execute LCLS-II LLRF script to read raw RF ADCs
#        Intended to be run from CPU which has direct access
#          to LLRF FPGA network
#
#  Facility: LCLS-II
#
#  Auth: Sonya Hoobler, 27 May 2022
#  Rev:  dd-mmm-yyyy, First Lastname (USERNAME)
#
#
#--------------------------------------------------------------
#  Mod:
#   
#--------------------------------------------------------------
#
#  Usage:
#         rfGetRawAdcsCommon.sh <address> <mask> <nacq> <scriptdir> <outputdir>
#
#
#============================================================

function usage {
echo "
   Purpose: Dump PRC ADC data for one cavity or PRL signal
            Intended to be run from CPU which has direct access to LLRF FPGA network

   Usage:
          rfGetRawAdcsCommon.sh <address> <mask> <scriptdir> <outputdir>

          ADDRESS: PRC chassis address, full FEED leep address
                Example for LCLS chassis for cavities 1-4 using Kintex fiber
                  leep://prca:803
                Example for LCLS chassis for cavities 5, 6 using Spartan interface
                  leep://prcb
                
          MASK: Channel mask. Single bit for each channel in this order:                
                   PRL reverse      1
                   PRL forward      2
                   Cavity 1 (or 5)  4
                   Cavity 2 (or 6)  8
                   Cavity 3 (or 7)  16
                   Cavity 4 (or 8)  32

          SCRIPTDIR:   Path to LLRF hardware test scripts on CPU

          OUTPUTDIR:  Directory prefix where script writes ADC data. Script will create 
                      this directory with timestamp appended to its name
"
}

# Check for input arguments
NARGS=$#
if [ $NARGS -ne 4 ]
then
    echo "rfGetRawAdcsCommon.sh: 4 arguments expected, but received ${NARGS}."
	usage
    exit 0
fi

ADDRESS=`echo $1`
MASK=`echo $2`
SCRIPTDIR=`echo $3`
OUTPUTDIR=`echo $4`

CMD="cd $SCRIPTDIR"
echo $CMD
$CMD

NACQ=1
NPT=65536
echo "Read PRC ADC channel for $ADDRESS with mask $MASK, $NACQ iteration, and $NPT points"
CMD="python get_raw_adcs.py -a $ADDRESS -n $NPT -m $MASK -c $NACQ -D $OUTPUTDIR"
echo $CMD
$CMD

exit 0
