#!/bin/sh

if [ $# -lt 1 ];then
    echo "Usage: post_xfibres <directory> [thresh]"
    echo ""
    exit
fi



dirname=$1
if [ $# -gt 1 ];then
    thresh=$2;
else
    thresh=0.05;
fi

cd $dirname
numfib=`${FSLDIR}/bin/imglob *f*samples* |wc -w`

echo "$numfib fibres"
fib=1;
${FSLDIR}/bin/fslroi `${FSLDIR}/bin/imglob *f1samples*` numfibs 0 1
${FSLDIR}/bin/fslmaths numfibs -mul 0 numfibs
while [ $fib -le $numfib ];do
    echo $fib
    ${FSLDIR}/bin/fslmaths `${FSLDIR}/bin/imglob *f${fib}samples*` -Tmean mean_f${fib}samples
    ${FSLDIR}/bin/fslmaths mean_f${fib}samples -thr $thresh -bin ${$}bin${fib}
    ${FSLDIR}/bin/fslmaths numfibs -add ${$}bin${fib} numfibs
    ${FSLDIR}/bin/make_dyadic_vectors `${FSLDIR}/bin/imglob *th${fib}samples*` `${FSLDIR}/bin/imglob *ph${fib}samples*` dyads${fib}
    fib=`echo "$fib +1"|bc`;
done
rm ${$}bin*

