#! /bin/csh

#echo usage, if no arguments given
if ($#argv == 0) then
  echo " "
  echo "FreeSurfer Normalization/Brain Extract/Segmentation Script"
  echo " "
  echo "process_anatomy.csh -sub <subject dir> -sp <subject prefix>"
  echo " "
  echo "For example:"
  echo " "
  echo "process_anatomy.csh -sub ptsdpet-c01 -sp c01 \\"
  echo " "
  echo "Note that -sub is given relative to the freesurfer"
  echo "subjects directory (eg, /usr/local/freesurfer/subjects/."
  echo " "
  echo " "
  exit 1
endif


#init
set sub=""
set s=""

#collect arguments
while ($#argv != 0)
  switch ($1)
      case "-sub":
        shift
        set surfersub=$1
        shift
       breaksw
      case "-sp":
        shift
        set s=$1
        shift
       breaksw
      default:
        echo "Unknown option '$1'"
        exit 1
       breaksw
    endsw
end

# Check the inputs
if ( ("$surfersub" == "") || ("$s" == "") ) then
  echo " "
  echo "Please supply all arguments:"
  echo " "
  echo "process_anatomy.csh -sub <subject dir> -sp <subject prefix>"
  echo " "
  echo "For example:"
  echo " "
  echo "process_anatomy.csh -sub ptsdpet-c01 -sp c01 \\"
  echo " "
  exit 0
endif


# FREESURFER setup
setenv FREESURFER_HOME /usr/local/freesurfer
setenv NO_MINC
setenv NO_FSFAST

source $FREESURFER_HOME/FreeSurferDefs.csh
source $FREESURFER_HOME/FreeSurferEnv.csh
cd $CSURF_DIR

set sub="${SUBJECTS_DIR}/$surfersub"

echo "\n\n*********************************************\n\n"
echo "\nProcessing Subject $sub\n"

echo "\n    ********* Intensity Normalisation *********\n"
#usage: mri_normalize [options] <input volume> <output volume>
#    -n <# of 3d normalization iterations>, default=5
#    -g <max intensity/mm gradient>, default=0.6
#    -s <sigma>, intensity interval pct for 3d, default=0.15
rm  $sub/mri/T1/cor-???
mri_normalize $sub/mri/orig $sub/mri/T1

echo "\n    ********* Skull Stripping *********\n"
rm  $sub/mri/brain/cor-???
mri_strip_skull `basename $sub` 1
mri_strip_skull `basename $sub` 2
mri_strip_skull `basename $sub` 3
mri_strip_skull `basename $sub` 4
#mri_watershed  $sub/mri/T1 $sub/mri/brain
    
echo "\n    ********* Segment White Matter *********\n"

rm  $sub/mri/wm/cor-???
mri_segment    $sub/mri/brain $sub/mri/wm

exit
