#!/bin/bash

jobsDone=`ls | grep trackEq-.*nC.done | wc -l`
if [ $jobsDone -lt 1 ] ; then
   echo "No jobs done"
   exit 1
fi

procList=""
w1List=""
for sem in trackEq-*nC.done ; do 
    root=${sem/.done/}
    w1List="$w1List $root.w1"
    # Average the last 500 turns
    sddsprocess $root.w1 -pipe=out -clip=0,500,invert \
    -process=Charge,ave,%sAve -process=[CS][xytd]*,ave,%sAve -process=[CS][xytd]*,stand,%sStDev \
    | sddscollapse -pipe=in $root.proc
    procList="$procList $root.proc"
done
sddsplot -convert=col,St,ps,s,1e12 -convert=col,Sdelta,%,,1e2 \
    -column=Pass,'(Sdelta,St)' -layout=1,2 -join=x -group=names -sep=names $w1List -legend=file,edit=Z-%/.w1// \
    -graph=line,vary

sddscombine $procList -merge -overwrite trackEq.proc
\rm $procList
sddsplot -graph=error \
    -yexclude=col,Step* \
    -convert=col,[SC][xy][AS]*,mm,m,1e3 \
    -convert=col,[SC][xy]p[AS]*,mr,,1e3 \
    -convert=col,[SC]t*,ps,s,1e12 \
    -convert=col,[SC]delta*,%,,1e2 \
    -column=ChargeAve,C[xytd]*Ave,y1edit=%/Ave/StDev/ \
    -column=ChargeAve,S[xytd]*Ave,y1edit=%/Ave/StDev/ \
    -sep trackEq.proc
