#! /bin/tcsh -f

#
# bbregister
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: greve $
#    $Date: 2013/03/25 18:04:53 $
#    $Revision: 1.49.2.3 $
#
# Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH"
#
# Terms and conditions for use, reproduction, distribution and contribution
# are found in the 'FreeSurfer Software License Agreement' contained
# in the file 'LICENSE' found in the FreeSurfer distribution, and here:
#
# https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense
#
# Reporting: freesurfer@nmr.mgh.harvard.edu
#
#

#
set VERSION = '$Id: bbregister,v 1.49.2.3 2013/03/25 18:04:53 greve Exp $';
set inputargs = ($argv);

set subject = ();
set movvol = ();
set intvol = (); # intermediate volume
set frame = ();
set midframe = 0;
set outreg = ();
set outfsl = ();
set outlta = ();
set InitFSL = 0;
set InitSPM = 0;
set SPMUseNII = 0;
set InitHeader = 0;
set InitReg = ();
set InitRegOut = ();
set VSM = ();
set Brute1Max = 4;
set Brute1Delta = 4;
set SubSamp1 = 100;
set DoAbs = 0;
set SaveCurReg = 1;
set featdir = ();
set InitCostFile = ();

set LHOnly = 0;
set RHOnly = 0;
set Slope1 = 0.5;
set Slope2 = 0.5;
set Offset2 = 0;
set Contrast = ()
set TolF = 1e-8;
set Tol1D = 1e-3;
set nPowellMax = 36;
set DOF = 6;
set FSLDOF = 6;
set FSLSwapTrans = 0;
set DoPass1 = 1;

set WMProjAbs = 2;
set GMProjFrac = 0.5;
set GMProjAbs = ();

set nSubSamp = 1;
set Interp = trilinear
set UseEPIMask = 0;
set UseCortexLabel = 1;

set RMSFile = ();
set templateout = ();
set OutVol = ();
set fsvol  = brainmask;
set surfcost = ();
set surfcon  = ();
set surfname = "white";
set MaskLabel = ();

set RandInitMax = ();

set debug = 0;
set tmpdir = ();
set cleanup = 1;
set PrintHelp = 0;
set nolog = 0;

if($#argv == 0) goto usage_exit;
set n = `echo $argv | egrep -e -version | wc -l`
if($n != 0) then
  echo $VERSION
  exit 0;
endif

goto parse_args;
parse_args_return:
goto check_params;
check_params_return:

set StartTime = `date`;
set tSecStart = `date '+%s'`;

set outdir = `dirname $outreg`;
if($#tmpdir == 0) set tmpdir = $outdir/tmp.bbregister.$$
mkdir -p $tmpdir

if(! $nolog) then
  set LF = $outreg.log
  if(-e $LF) mv $LF $LF.old
else
  set LF = /dev/null
endif

echo "Log file is $LF"

echo "Logfile for bbregister" >> $LF
date |& tee -a $LF
echo ""|& tee -a $LF
echo "setenv SUBJECTS_DIR $SUBJECTS_DIR" |& tee -a $LF
echo "cd `pwd`" |& tee -a $LF
echo $0 $inputargs |& tee -a $LF
echo ""|& tee -a $LF
echo $VERSION |& tee -a $LF
uname -a |& tee -a $LF
echo "FREESURFER_HOME $FREESURFER_HOME" |& tee -a $LF

# Create template
if($#templateout) then
  set template = $templateout
else
  set template = $tmpdir/template.nii
endif
set cmd = (mri_convert $movvol $template)
if($#frame != 0) set cmd = ($cmd --frame $frame)
if($midframe) set cmd = ($cmd --mid-frame)
echo $cmd | tee -a $LF
$cmd |& tee -a $LF
if($status) exit 1;

if($#InitReg == 0) then
  set regvol = $template
  set InitReg = $tmpdir/reg.init.dat
  if($#intvol) then
    set regvol = $intvol
    set InitReg = $tmpdir/reg.intermediate.dat
  endif
  if($InitFSL) then
    set cmd = (fslregister --s $subject --mov $regvol --reg $InitReg \
      --niters 1 --maxangle 90 --nobetmov --tmp $tmpdir/fslregister \
      --dof $FSLDOF --fsvol $fsvol.mgz)
    if($FSLSwapTrans) set cmd = ($cmd --allow-swap --trans)
    if($nolog) set cmd = ($cmd --nolog)
    echo $cmd | tee -a $LF
    $cmd |& tee -a $LF
    if($status) exit 1;
  endif
  if($InitSPM) then
    set cmd = (spmregister --s $subject --mov $regvol --reg $InitReg \
      --tmp $tmpdir/spmregister)
    if($nolog) set cmd = ($cmd --nolog)
    if($SPMUseNII) set cmd = ($cmd --nii)
    echo $cmd | tee -a $LF
    $cmd |& tee -a $LF
    if($status) exit 1;
  endif
  if($InitHeader) then
    set cmd = (tkregister2_cmdl --s $subject --mov $regvol \
      --regheader --reg $InitReg --noedit)
    echo $cmd | tee -a $LF
    $cmd |& tee -a $LF
    if($status) exit 1;
  endif
  if($#intvol) then
    # Intermediate volume
    set InitReg0 = $tmpdir/reg.init.dat
    set cmd = (tkregister2_cmdl --s $subject --mov $template \
      --int $intvol $InitReg  --noedit --reg $InitReg0)
    echo $cmd | tee -a $LF
    $cmd |& tee -a $LF
    if($status) exit 1;
    set InitReg = $InitReg0;
  endif
endif

if($#InitRegOut) cp $InitReg $InitRegOut

# Pass 1
set Pass1Reg = $tmpdir/bbr.pass1.dat
if($DoPass1) then
  set cmd = (mri_segreg --mov $template --init-reg $InitReg --out-reg $Pass1Reg\
    --subsamp-brute $SubSamp1 --subsamp $SubSamp1 --tol 1e-4 --tol1d 1e-3 \
    --brute -$Brute1Max $Brute1Max $Brute1Delta --surf $surfname);
  if($#GMProjFrac) set cmd = ($cmd --gm-proj-frac $GMProjFrac)
  if($#GMProjAbs)  set cmd = ($cmd --gm-proj-abs  $GMProjAbs)
  if("$Contrast" == "-1") set cmd = ($cmd --wm-gt-gm $Slope1);
  if("$Contrast" == "+1") set cmd = ($cmd --gm-gt-wm $Slope1);
  if($#MaskLabel)  set cmd = ($cmd --label $MaskLabel);
  if($LHOnly) set cmd = ($cmd --lh-only);
  if($RHOnly) set cmd = ($cmd --rh-only);
  if($UseEPIMask) set cmd = ($cmd --mask);
  if(! $UseCortexLabel) set cmd = ($cmd --no-cortex-label);
  if($#VSM) set cmd = ($cmd --mask --vsm $VSM);
  if($#RandInitMax) then
    # Only for testing
    set cmd = ($cmd --trans-rand $RandInitMax --rot-rand $RandInitMax);
  endif
  echo $cmd | tee -a $LF
  $cmd |& tee -a $LF
  if($status) exit 1;
else
  cp $InitReg $Pass1Reg
endif

# Pass 2
set MinCostFile = $outreg.mincost; # This will be the final cost
set ParamFile = $outreg.param; # This will be the final parameters
set cmd = (mri_segreg --mov $template --init-reg $Pass1Reg \
  --out-reg $outreg --brute -0.1 0.1 0.1 --interp $Interp \
  --wm-proj-abs $WMProjAbs --tol $TolF --tol1d $Tol1D\
  --c0 $Offset2 --mincost $MinCostFile --dof $DOF \
  --nmax $nPowellMax --param $ParamFile --surf $surfname);
if($SaveCurReg) set cmd = ($cmd --cur-reg $tmpdir/reg.curopt.dat)
if($#GMProjFrac) set cmd = ($cmd --gm-proj-frac $GMProjFrac)
if($#GMProjAbs)  set cmd = ($cmd --gm-proj-abs  $GMProjAbs)
if($#nSubSamp) set cmd = ($cmd --nsub $nSubSamp);
if($LHOnly) set cmd = ($cmd --lh-only);
if($RHOnly) set cmd = ($cmd --rh-only);
if($UseEPIMask) set cmd = ($cmd --mask);
if(! $UseCortexLabel) set cmd = ($cmd --no-cortex-label);
if($#VSM) set cmd = ($cmd --mask --vsm $VSM);
if($#OutVol) set cmd = ($cmd --o $OutVol);
if($#MaskLabel)  set cmd = ($cmd --label $MaskLabel);
if("$Contrast" == "-1") set cmd = ($cmd --wm-gt-gm $Slope2);
if("$Contrast" == "+1") set cmd = ($cmd --gm-gt-wm $Slope2);
if($#surfcost)  set cmd = ($cmd --surf-cost $surfcost);
if($#surfcon)   set cmd = ($cmd --surf-con  $surfcon);
if($DoAbs) set cmd = ($cmd --penalty-abs)
if($#InitCostFile) set cmd = ($cmd --initcost $InitCostFile);

echo $cmd | tee -a $LF
$cmd |& tee -a $LF
if($status) exit 1;

# Compute RMS wrt the initial reg (however it was created)
if($#RMSFile) then
  set rmsdat = ();
  if(! $RHOnly) then
    set lhrms = $tmpdir/rh.rms
    set cmd = (mri_surf2surf --reg $InitReg --reg-diff $outreg \
     --sval-xyz white --rms $lhrms --s $subject --hemi lh)
    echo $cmd | tee -a $LF
    $cmd |& tee -a $LF
    if($status) exit 1;
    set rmsdat = (`cat $lhrms`);
  endif
  if(! $LHOnly) then
    set rhrms = $tmpdir/rh.rms
    set cmd = (mri_surf2surf --reg $InitReg --reg-diff $outreg \
     --sval-xyz white --rms $rhrms --s $subject --hemi rh)
    echo $cmd | tee -a $LF
    $cmd |& tee -a $LF
    if($status) exit 1;
    set rmsdat = ($rmsdat `cat $rhrms`);
  endif
  echo $rmsdat > $RMSFile
  echo Final RMS $rmsdat | tee -a $LF
endif

if($#outfsl) then
  set cmd = (tkregister2_cmdl --mov $movvol --reg $outreg \
    --noedit --fslregout $outfsl);
  echo $cmd | tee -a $LF
  $cmd | tee -a $LF
  if($status) exit 1;
endif

if($#outlta) then
  set cmd = (tkregister2_cmdl --mov $movvol --reg $outreg \
    --noedit --ltaout $outlta);
  echo $cmd | tee -a $LF
  $cmd | tee -a $LF
  if($status) exit 1;
endif

if($#featdir) then
  cp $outreg $featdir/reg/freesurfer/register.dat
  set reg152 = $featdir/reg/freesurfer/anat2std.register.dat
  set cmd = (mni152reg --s $subject --o $reg152)
  echo $cmd | tee -a $LF
  $cmd | tee -a $LF
  if($status) exit 1;
endif

# Cleanup
if($cleanup) then
  echo "Cleaning up" |& tee -a $LF
  rm -r $tmpdir
endif

set tSecEnd = `date '+%s'`;
@ tSecRun = $tSecEnd - $tSecStart;

echo " " |& tee -a $LF
echo "Started at $StartTime " |& tee -a $LF
echo "Ended   at `date`" |& tee -a $LF
echo "BBR-Run-Time-Sec $tSecRun" |& tee -a $LF
echo " " |& tee -a $LF
echo "bbregister Done" |& tee -a $LF

echo "To check results, run:" |& tee -a $LF
if($#templateout) then
  echo "tkregister2 --mov $templateout --reg $outreg --surf" |& tee -a $LF
else
  echo "tkregister2 --mov $movvol --reg $outreg --surf" |& tee -a $LF
endif
echo " "


exit 0;
###############################################

############--------------##################
parse_args:
set cmdline = ($argv);
while( $#argv != 0 )

  set flag = $argv[1]; shift;

  switch($flag)

    case "-h"
    case "-u"
    case "-usage"
    case "--usage"
    case "-help"
    case "--help"
      set PrintHelp = 1;
      goto usage_exit;
      breaksw

    case "--s":
      if ( $#argv < 1) goto arg1err;
      set subject = $argv[1]; shift;
      breaksw

    case "--feat":
      if ( $#argv < 1) goto arg1err;
      set featdir = $argv[1]; shift;
      if(! -e $featdir) then
        echo "ERROR: cannot find $featdir"
        exit 1;
      endif
      set movvol = `stem2fname $featdir/example_func`
      if($status) then
        echo "$movvol"
        exit 1;
      endif
      mkdir -p $featdir/reg/freesurfer
      if($status) exit 1;
      set outreg = $featdir/reg/freesurfer/anat2exf.register.dat
      set InitFSL = 1;
      set InitSPM = 0;
      set InitHeader = 0;
      set InitReg = ();
      set Contrast = +1;
      breaksw

    case "--s-from-reg":
      if($#argv < 1) goto arg1err;
      set tmp = $argv[1]; shift;
      if(! -e $tmp) then
        echo "ERROR: cannot find $tmp"
        exit 1;
      endif
      set subject = `head -n 1 $tmp`
      breaksw

    case "--mov":
      if ( $#argv < 1) goto arg1err;
      set movvol = $argv[1]; shift;
      if(! -e $movvol) then
        echo "ERROR: cannot find $movvol"
        exit 1;
      endif
      breaksw

    case "--int":
      if ( $#argv < 1) goto arg1err;
      set intvol = $argv[1]; shift;
      if(! -e $intvol) then
        echo "ERROR: cannot find $intvol"
        exit 1;
      endif
      breaksw

    case "--vsm":
      if ( $#argv < 1) goto arg1err;
      set VSM = $argv[1]; shift;
      if(! -e $VSM) then
        echo "ERROR: cannot find $VSM"
        exit 1;
      endif
      breaksw

    case "--reg":
      if ( $#argv < 1) goto arg1err;
      set outreg = $argv[1]; shift;
      breaksw

    case "--fslmat":
      if ( $#argv < 1) goto arg1err;
      set outfsl = $argv[1]; shift;
      breaksw

    case "--lta":
      if ( $#argv < 1) goto arg1err;
      set outlta = $argv[1]; shift;
      breaksw

    case "--init-fsl":
      set InitFSL = 1;
      set InitSPM = 0;
      set InitHeader = 0;
      set InitReg = ();
      breaksw
 
    case "--fsl-swap-trans":
      set FSLSwapTrans = 1;
      breaksw

    case "--init-spm":
      set InitFSL = 0;
      set InitSPM = 1;
      set InitHeader = 0;
      set InitReg = ();
      breaksw
 
    case "--spm-nii":
      set SPMUseNII = 1;
      breaksw
 
    case "--regheader":
    case "--reg-header":
    case "--init-header":
      set InitFSL = 0;
      set InitSPM = 0;
      set InitHeader = 1;
      set InitReg = ();
      breaksw
 
    case "--init-reg":
      if ( $#argv < 1) goto arg1err;
      set InitReg = $argv[1]; shift;
      if(! -e $InitReg) then
        echo "ERROR: cannot find $InitReg"
        exit 1;
      endif
      set subject = `head -n 1 $InitReg`
      set InitFSL = 0;
      set InitSPM = 0;
      set InitHeader = 0;
      breaksw

    case "--init-reg-out":
      if( $#argv < 1) goto arg1err;
      set InitRegOut = $argv[1]; shift;
      breaksw

    case "--slope1":
      if ( $#argv < 1) goto arg1err;
      set Slope1 = $argv[1]; shift;
      breaksw

    case "--slope2":
      if ( $#argv < 1) goto arg1err;
      set Slope2 = $argv[1]; shift;
      breaksw

    case "--offset2":
      if ( $#argv < 1) goto arg1err;
      set Offset2 = $argv[1]; shift;
      breaksw

    case "--initcost":
      if ( $#argv < 1) goto arg1err;
      set InitCostFile = $argv[1]; shift;
      breaksw

    case "--abs":
      # Experimental
      set Contrast = +1;
      set DoAbs = 1;
      breaksw

    case "--bold":
    case "--dti":
    case "--T2":
    case "--t2":
      set Contrast = +1;
      breaksw

    case "--T1":
    case "--t1":
      set Contrast = -1;
      breaksw

    case "--s-from-reg":
      if ( $#argv < 1) goto arg1err;
      set tmpfile = $argv[1]; shift;
      set subject = `head -n 1 $tmpfile`;
      if($status) then
        echo "$subject"
        exit 1;
      endif
      breaksw

    case "--fsvol":
      if ( $#argv < 1) goto arg1err;
      set fsvol = $argv[1]; shift;
      breaksw

    case "--template-out":
      if ( $#argv < 1) goto arg1err;
      set templateout = $argv[1]; shift;
      breaksw

    case "--frame":
      if ( $#argv < 1) goto arg1err;
      set frame = $argv[1]; shift;
      breaksw

    case "--mid-frame":
      set midframe = 1;
      breaksw

    case "--lh-only":
      set LHOnly = 1;
      set RHOnly = 0;
      breaksw

    case "--rh-only":
      set LHOnly = 0;
      set RHOnly = 1;
      breaksw

    case "--o":
      if ( $#argv < 1) goto arg1err;
      set OutVol = $argv[1]; shift;
      breaksw

    case "--brute1max":
      if ( $#argv < 1) goto arg1err;
      set Brute1Max = $argv[1]; shift;
      breaksw

    case "--brute1delta":
      if ( $#argv < 1) goto arg1err;
      set Brute1Delta = $argv[1]; shift;
      breaksw

    case "--subsamp1":
      if ( $#argv < 1) goto arg1err;
      set SubSamp1 = $argv[1]; shift;
      breaksw

    case "--surf":
      if ( $#argv < 1) goto arg1err;
      set surfname = $argv[1]; shift;
      breaksw

    case "--surf-cost":
      if ( $#argv < 1) goto arg1err;
      set surfcost = $argv[1]; shift;
      breaksw

    case "--surf-con":
      if ( $#argv < 1) goto arg1err;
      set surfcon = $argv[1]; shift;
      breaksw

    case "--tol":
      if ( $#argv < 1) goto arg1err;
      set Tol = $argv[1]; shift;
      set TolF = $Tol
      # set Tol1D = $Tol; only set tolf!
      breaksw

    case "--tolf":
      if ( $#argv < 1) goto arg1err;
      set TolF = $argv[1]; shift;
      breaksw

    case "--tol1d":
      if ( $#argv < 1) goto arg1err;
      set Tol1D = $argv[1]; shift;
      breaksw

    case "--nmax":
      if ( $#argv < 1) goto arg1err;
      set nPowellMax = $argv[1]; shift;
      breaksw

    case "--gm-proj-frac":
      if($#argv < 1) goto arg1err;
      set GMProjFrac = $argv[1]; shift;
      set GMProjAbs = ();
      breaksw

    case "--gm-proj-abs":
      if($#argv < 1) goto arg1err;
      set GMProjAbs = $argv[1]; shift;
      set GMProjFrac = ();
      breaksw

    case "--wm-proj-abs":
      if($#argv < 1) goto arg1err;
      set WMProjAbs = $argv[1]; shift;
      breaksw

    case "--proj-abs":
      if($#argv < 1) goto arg1err;
      set ProjAbs = $argv[1]; shift;
      set WMProjAbs = $ProjAbs;
      set GMProjAbs = $ProjAbs;
      set GMProjFrac = ();
      breaksw

    case "--nsub":
    case "--subsamp":
      if($#argv < 1) goto arg1err;
      set nSubSamp = $argv[1]; shift;
      breaksw

    case "--6":
      # Default anyway
      set DOF = 6;
      breaksw

    case "--9":
      set DOF = 9;
      breaksw

    case "--12":
      set DOF = 12;
      breaksw

    case "--no-pass1":
      set DoPass1 = 0;
      breaksw

    case "--fsl-dof":
      if($#argv < 1) goto arg1err;
      set FSLDOF = $argv[1]; shift;
      breaksw

    case "--rms":
      if($#argv < 1) goto arg1err;
      set RMSFile = $argv[1]; shift;
      breaksw

    case "--rand-init":
      if($#argv < 1) goto arg1err;
      set RandInitMax = $argv[1]; shift;
      breaksw

    case "--nearest":
      set Interp = nearest
      breaksw

    case "--trilin":
    case "--trilinear":
      set Interp = trilinear
      breaksw

    case "--label":
      if($#argv < 1) goto arg1err;
      set MaskLabel = $argv[1]; shift;
      if(! -e $MaskLabel) then
        echo "ERROR: cannot find $MaskLabel"
        exit 1;
      endif
      breaksw

    case "--epi-mask":
      set UseEPIMask = 1;
      breaksw

    case "--no-cortex-label":
      set UseCortexLabel = 0;
      breaksw

    case "--no-save-cur-reg":
      set SaveCurReg = 0;
      breaksw

    case "--tmp":
    case "--tmpdir":
      if ( $#argv < 1) goto arg1err;
      set tmpdir = $argv[1]; shift;
      set cleanup = 0;
      breaksw

    case "--nocleanup":
    case "--no-cleanup":
      set cleanup = 0;
      breaksw

    case "--cleanup":
    case "--clean-up":
      set cleanup = 1;
      breaksw

    case "--debug":
      set verbose = 1;
      set echo = 1;
      breaksw

    case "--nolog":
      set nolog = 1;
      breaksw

    default:
      echo ERROR: Flag $flag unrecognized.
      echo $cmdline
      exit 1
      breaksw
  endsw

end

goto parse_args_return;
############--------------##################

############--------------##################
check_params:

  if($#subject == 0) then
    echo "ERROR: must spec a subject id"
    exit 1;
  endif
  if(! -e $SUBJECTS_DIR/$subject) then
    echo "ERROR: cannot find $subject in $SUBJECTS_DIR"
    exit 1;
  endif

  if($#movvol == 0) then
    echo "ERROR: must spec an movput vol"
    exit 1;
  endif

  if($#Contrast == 0) then
    echo "ERROR: you must specify a contrast."
    echo " use --bold, --dti, --t2, or --t1,"
    echo " which ever is most appropriate"
    exit 1;
  endif

  if($#outreg == 0) then
    if ($#outlta == 0) then
      echo "ERROR: must spec an output reg file (using either --reg or --lta)"
      exit 1;
    else
      set outreg = ${outlta:r}.dat
    endif
  endif

  if($#frame && $midframe) then
    echo "ERROR: cannot --frame AND --mid-frame"
    exit 1;
  endif

  if(! $InitFSL && ! $InitSPM && ! $InitHeader && ! $#InitReg) then
    echo "ERROR: must spec an init method"
    exit 1;
  endif

  if($?BBR_TEST_TOLERANCE) then
    # This allows a program at a much higher level to set the tolerances.
    # This is useful in the case where only the execution needs to be
    # tested (and the output itself is not important). Setting the tolerances
    # to be high makes BBR run faster.
    set TolF  = $BBR_TEST_TOLERANCE
    set Tol1D = $BBR_TEST_TOLERANCE
    echo ""
    echo "WARNING: using BBR_TEST_TOLERANCE $BBR_TEST_TOLERANCE"
    echo ""
    sleep 2
  endif

endif



goto check_params_return;
############--------------##################

############--------------##################
arg1err:
  echo "ERROR: flag $flag requires one argument"
  exit 1
############--------------##################

############--------------##################
arg2err:
  echo "ERROR: flag $flag requires two arguments"
  exit 1
############--------------##################

############--------------##################
usage_exit:

if($PrintHelp) then
  cat $0 | \
    awk 'BEGIN{prt=0}{if(prt) print $0; if($1 == "BEGINHELP") prt = 1 }'
else
  echo "Usage: bbregister --s <subj> --mov <volid> --reg <regfile> --init-<type> --<contrast>"
  echo "Help:  bbregister --help"
endif

exit 1;

#---- Everything below is printed out as part of help -----#
#-- During make install, the output of 'fsPrintHelp bbregister.help.xml' -- #
#-- is concatenated to this file --#
BEGINHELP
				Help

NAME
	bbregister

SYNOPSIS
	bbregister --s <subj> --mov <volid> --reg <regfile> \
		--init-<type> --<contrast>

DESCRIPTION
	This program performs within-subject, cross-modal registration using a
	boundary-based cost function. The registration is constrained to be 6 
	DOF (rigid). It is required that you have an anatomical scan of the 
	subject that has been analyzed in freesurfer.

POSITIONAL ARGUMENTS
	None

REQUIRED FLAGGED ARGUMENTS
	--s subject
		FreeSurfer subject name as found in $SUBJECTS_DIR.

	--mov volid
		"Moveable" volume. This is the template for the cross-modal 
		volume. Eg, for fMRI, it is the volume used for motion 
		correction.

	--reg register.dat
		Output FreeSurfer (tkregister-style) registration file (simple
		text).

	Initialization Arguments (one required)

	--init-fsl
		Initialize using FSL FLIRT (requires that FSL be installed).

	--init-spm
		Initialize using SPM spm_coreg (requires that SPM and matlab 
		be installed).

	--init-header
		Assume that the geometry information in the cross-modal and 
		anatomical are sufficient to get a close voxel-to-voxel 
		registration. This usually is only the case if they were 
		acquired in the same session.

	--init-reg initregfile
		Supply an initial registration matrix.

	Contrast Arguments (one required)

	--t1
		Assume t1 contrast, ie, White Matter brighter than Grey Matter

	--t2
		Assume t2 contrast, ie, Gray Matter brighter than White Matter

	--bold
		Same as --t2

	--dti
		Same as --t2

OPTIONAL FLAGGED ARGUMENTS
	--int intvol
		Supply a volume to use an an intermediate volume when 
		performing registration. This is useful for when the 
		cross-modal is volume is a partial field-of-view (FoV). If you
		acquire in the same session a whole-head FoV, then pass the 
		whole-head as the intermediate and the partial as the 
		moveable.

	--mid-frame
		reg to middle frame (not with --frame)

	--frame frameno
		reg to frameno (default 0=1st)

	--template-out template
		save template (good with --frame)

	--o outvol
		resample mov and save as outvol

	--s-from-reg reg
		get subject name from regfile

	--rms rmsfile
		RMS change in cortical surface position

	--fslmat flirt.mtx
		output an FSL FLIRT matrix

	--lta output.lta
		output an LTA registration matrix (This flag can be used along
		with or instead of --reg!) 

	--lh-only
		only use left hemi

	--rh-only
		only use right hemi

	--slope1 slope1
		cost slope for 1st stage (default is 0.5)

	--slope2  slope2
		cost slope for 2nd stage (default is 0.5)

	--offset2 offset2
		cost offset for 2nd stage (default is 0)

	--tol1d tol1d
		2nd stage 1D tolerance 

	--tol tol
		2nd stage loop tolerance (same as --tolf)

	--tolf  tolf
		2nd stage loop tolerance (same as --tol)

	Be careful making these more stringent as they can cause underflows 
	and NaNs.

	--nmax nPowellMax
		set max number of iterations (default 36)

	--rand-init randmax
		randomly change input to 1st stage reg

	--gm-proj-frac frac
		default is 0.5

	--gm-proj-abs abs
		use absolute instead of relative

	--wm-proj-abs  dist
		2nd stage, default is 2mm

	--proj-abs  dist
		use wm and gm proj abs in 2nd stage

	--subsamp nsub
		2nd stage vertex subsampling, default is 1

	--nearest
		2nd stage, use nearest neighbor interp (defalt is trilinear)

	--epi-mask
		mask out brain edge and B0 regions (1st and 2nd stages)

	--no-cortex-label
		Do not use ?h.cortex.label to mask. 

	--label labelfile
		Use label to mask. 

	--brute1max max
		pass 1 search -max to +max (default 4)

	--brute1delta delta
		pass 1 search -max to +max step delta (default 4)

	--subsamp1 nsubsamp
		pass 1 vertex subsampling (default 1)

	--no-pass1
		turn off pass 1

	--surf surfname
		change surface to surfname from ?h.white 

	--surf-cost basename
		saves final cost as basename.?h.mgh

	--surf-con  basename
		saves final contrast as basename.?h.mgh

	--init-reg-out outinitreg
		save initial reg

	--initcost initcostfile
		save initial cost

	--spm-nii
		Use NIFTI format as input to SPM when using --init-spm 
		(spmregister). Ordinarily, it uses ANALYZE images to be 
		compatible with older versions of SPM, but this has caused 
		some left-right reversals in SPM8.

	--feat featdir
		FSL FEAT directory. Sets mov to featdir/example_func, uses 
		--init-fsl, --bold, sets reg to featdir/reg/freesurfer/
		anat2exf.register.dat. This replaces reg-feat2anat.

	--tmp tmpdir
		temporary dir (implies --nocleanup)

	--nocleanup
		do not delete temporary files

	--version
		print version and exit

	--help
		print help and exit

EXAMPLE 1
	Intialize with FLIRT, view result with tkregister2:
	bbregister --s bert --mov func.nii --init-fsl --reg 
	register.dat
	tkregister2 --mov func.nii --reg register.dat --surf 

EXAMPLE 2
	Intialize with SPM, view result with tkregister2:
	bbregister --s bert --mov func.nii --init-spm --reg 
	register.dat
	tkregister2 --mov func.nii --reg register.dat --surf

EXAMPLE 3
	Register a partial FoV using whole FoV as intermdediate:
	bbregister --s bert --mov partial.nii --init-fsl --int whole.nii --reg
	register.partial.dat
	tkregister2 --mov partial.nii --reg register.partial.dat --surf

REPORTING
	Report bugs to <freesurfer@nmr.mgh.harvard.edu>


