#!/bin/sh

#Charlotte Rae 2012

#Put me in your /home/bin/, and remember to make me executable with chmod a+rwx brain_mask.

#Extract the first b0 image from the data - usually the first volume - using fslroi, and call it nodif.nii.gz.  FSL takes "0" to be "1".

#Then, use bet to extract the brain from the nodif image, and call it nodif_brain.nii.gz.  Add the -m option to bet to also get a nodif_brain_mask out of the process.

#The nodif_brain_mask is used in subsequent dtifit.

#A good threshold for betting seems to be 0.1, espescially if most of the brains are quite large.

subjs="110159 110309"
#Put your subject folders in here

#Write your main directory here
cd /imaging/cr01/folder/

for zzVARroi in ${subjs} ; do
echo "Extracting b0 image for ${zzVARroi}"
fslroi ${zzVARroi}/${zzVARroi}.nii.gz ${zzVARroi}/nodif.nii.gz 0 1
done

for zzVARbet in ${subjs} ; do
echo "Betting nodif image for ${zzVARbet}"
cd /imaging/cr01/folder/${zzVARbet}/
bet nodif.nii.gz nodif_brain.nii.gz -m -f 0.1
done

#To make sure that the nodif_brain_mask (specified by option -m) is in the right subject's directory, the script cd's to each subject's individual directory before performing each bet.

#Check the betting after script has finished running.  Is it binary and brain-shaped?

echo "End"
