#!/bin/bash

# Postprocess data to perform slice analysis using the first data file to
# define which slices the particles occupy.
# Note that particles may drift relative to one another in time without
# changing slice membership.

nrow0=`sdds2stream -rows=bare full457MeV-0001.wc`
echo $nrow0 rows in starting beam

for wc in full457MeV-????.wc ; do 
    if [ ! -e ${wc/.wc/.slice} ] ; then
        echo Processing $wc
        sddsxref $wc full457MeV-0001.wc -pipe=out -equate=particleID -take=t -rename=col,t=t0 \
            | sddssort -pipe -column=t0 \
            | sddsprocess -pipe -define=column,Slice,"i_row 10 * $nrow0 / int",type=short \
            | sddsbreak -pipe -change=Slice \
            | sddsanalyzebeam -pipe \
            | sddsprocess -pipe=in -define=column,Slice,i_row,type=short ${wc/.wc/.slice}
    else
        echo "${wc/.wc/.slice} already exists---not reprocessing"
    fi
done

sddsplot -column=Slice,enx full457MeV-????.slice -graph=sym,vary=subtype -order=spect
