#!/bin/csh -f
#
# 
# $Id: fsl_rigid_register,v 1.7 2003/08/21 23:28:03 ebeth Exp $
set VERSION = '$Id: fsl_rigid_register,v 1.7 2003/08/21 23:28:03 ebeth Exp $';

set refvol = ();
set invol = ();
set outvol = ();
set interp = trilinear;
set debug = 0;
set tmpdir = /tmp/fsl_rigid_register.$$;
set cleanup = 1;
set PrintHelp = 0;
set applyxfm = ();
set initxfm = ();
set initgeom = 1;
set dof = 6;
set maxangle = 90;
set bins = 256;
set cost = corratio;
set costlist = (mutualinfo corratio normcorr normmi leastsq);
set fslmatfile = ();
set regmatfile = ();
set xfmmatfile = ();

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

goto parse_args;
parse_args_return:
goto check_params;
check_params_return:

echo $VERSION
date

set StartTime = `date`;

# make the temporary directory
mkdir -p $tmpdir

# create the output directory
echo $outvol
set outdir = `dirname $outvol`;
mkdir -p $outdir

# Convert reference to analyze
set refvolbase = $tmpdir/refvol.fsl_rigid_register
set refvolimg  = $refvolbase.img
set cmd = (mri_convert $refvol $refvolimg)
echo "--------------------------------------"
pwd
echo $cmd
$cmd
if($status) exit 1;

# Convert input to analyze #
set involbase = $tmpdir/invol.fsl_rigid_register
set involimg  = $involbase.img
set cmd = (mri_convert $invol $involimg)
echo "--------------------------------------"
pwd
echo $cmd
$cmd
if($status) exit 1;

# Create an init mat based on geometry in the header #
if($initgeom) then
  set initxfm = $tmpdir/initxfm.fslmat
  set cmd = (tkregister2 --targ $refvol --mov $invol)
  set cmd = ($cmd --reg $tmpdir/tkregister.dat)
  set cmd = ($cmd --fslregout $initxfm)
  set cmd = ($cmd  --regheader --noedit --s doesnotmatter);
  echo "--------------------------------------"
  pwd
  echo $cmd
  $cmd
  if($status) exit 1;
  rm $tmpdir/tkregister.dat;
  echo "Init FSL Mat based on geometry -------------------"
  cat $initxfm
  echo "-------------------------------------------------"
endif


set outvolbase = $tmpdir/outvol.fsl_rigid_register
set outvolimg  = $outvolbase.img

# Run flirt
set cmd = (flirt -in $involbase.hdr)
set cmd = ($cmd -out $outvolbase.hdr)
set cmd = ($cmd -bins $bins -cost $cost )
set cmd = ($cmd -searchrx -$maxangle $maxangle)
set cmd = ($cmd -searchry -$maxangle $maxangle)
set cmd = ($cmd -searchrz -$maxangle $maxangle)
set cmd = ($cmd -interp $interp -dof $dof)
set cmd = ($cmd -ref $refvolbase.hdr)
if($#initxfm  != 0) set cmd = ($cmd -init $initxfm )
if($#applyxfm == 0) then
  set cmd = ($cmd  -omat $outvolbase.fslmat)
else
  set cmd = ($cmd -init $applyxfm -applyxfm)
endif

echo "--------------------------------------"
pwd
echo $cmd
$cmd
if($status) then
  echo "ERROR: running flirt"
  exit 1;
endif

# Copy analyze .mat from ref to output
cp $refvolbase.mat $outvolbase.mat

# Convert to the output format
set cmd = (mri_convert $outvolbase.img $outvol);
echo "--------------------------------------"
pwd
echo $cmd
$cmd
if($status) exit 1;

if($#applyxfm == 0) then
  # Copy registration matrix
  cp $outvolbase.fslmat $fslmatfile

  if($#regmatfile || $#xfmmatfile) then
    # Convert to register.dat or xfm #
    set tmpregdat = $tmpdir/reg.dat
    set cmd = (tkregister2 --targ $refvol --mov $invol)
    set cmd = ($cmd --reg $tmpregdat)
    set cmd = ($cmd --fslreg $fslmatfile)
    set cmd = ($cmd --noedit);
    echo "--------------------------------------"
    pwd
    echo $cmd
    $cmd
    if($status) exit 1;
    if($#regmatfile) cp $tmpregdat $regmatfile
    if($#xfmmatfile) then
      rm -f $xfmmatfile
      echo "MNI Transform File" >> $xfmmatfile
      echo "% This file was created by fsl_rigid_register" >> $xfmmatfile
      echo "Transform_Type = Linear;" >> $xfmmatfile
      echo "Linear_Transform =" >> $xfmmatfile
      cat $tmpregdat | tail -5 | head -n 2  >> $xfmmatfile
      set l3 = `cat $tmpregdat | tail -3 | head -n 1`
      echo "$l3;"   >> $xfmmatfile
    endif

  endif

endif

# Cleanup
if($cleanup) then
  echo "Cleaning up"
  rm $involbase.* $outvolbase.* $refvolbase.*
  if($initgeom) rm $initxfm
endif

echo " "
echo "Started at $StartTime "
echo "Ended   at `date`"
echo " "
echo "fsl_rigid_register Done"
echo " "

echo "To check results, run:"
echo "tkmedit -f $refvol -aux $outvol"
echo " "


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

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

  set flag = $argv[1]; shift;
  
  switch($flag)

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

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

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

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

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

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

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

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

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

    case "-cost":
      if ( $#argv < 1) goto arg1err;
      set cost = $argv[1]; shift;
      set err = 1;
      foreach c ($costlist);
        if($cost == $c) err = 0;
      end
      if($err) then 
        echo "ERROR: cost $cost is unrecognized"
        echo " I only know about $costlist)"
        exit 1;
      endif
      breaksw

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

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

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

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

    case "-noinitgeom":
      set initgeom = 0;
      breaksw

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

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

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

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

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

    case "-umask":
      if ( $#argv == 0) goto arg1err;
      umask $1; shift;
      breaksw

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

end

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

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

  if($#refvol == 0) then
    # Need ref even with -applyxfm to get mat file #
    echo "ERROR: must spec a ref vol"
    exit 1;
  endif

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

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

  if($#initxfm && $#applyxfm) then
    echo "ERROR: cannot spec initxfm and applyxfm"
    exit 1;
  endif

  if($initgeom && $#applyxfm) then
    echo "ERROR: cannot spec initgeom and applyxfm"
    exit 1;
  endif

  if($#initxfm && $initgeom) then
    echo "ERROR: cannot spec initxfm and initgeom"
    exit 1;
  endif

  if($#fslmatfile == 0) set fslmatfile = $outvol.fslmat

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:
  echo ""
  echo "USAGE: fsl_rigid_register"
  echo ""
  echo "Required Arguments:";
  echo "   -r refvol    : reference/target volume"
  echo "   -i inputvol  : input/moveable volume"
  echo "   -o outputvol : input resampled to reference"
  echo ""
  echo "Optional Arguments"
  echo "   -fslmat fsmatfile  : spec explicitly"
  echo "   -regmat regmatfile : get reg matrix as register.dat file"
  echo "   -xfmmat xfmmatfile :  get reg matrix as MNI xfm file"
  echo "   -noinitgeom : do not initialize matrix based on geometry"
  echo "   -initxfm  xfmfile : use this as an initial matrix"
  echo "   -applyxfm xfmfile : do not reg, just apply xfm to input"
  echo "   -maxangle maxangle : only search over +/- maxangle degrees"
  echo "   -interp method : <trilinear>, nearestneighbour, sinc"
  echo "   -dof dof : use dof instead of 6"
  echo "   -bins bins : number of bins to use (default $bins)"
  echo "   -cost cost : objective function (default $cost)"
  echo "      valid costs are $costlist"
  echo "   -tmp  tmpdir (default is $tmpdir)"
  echo "   -nocleanup  : do not delete temporary files"
  echo ""
  echo "   -version : print version and exit"
  echo "   -help    : print help and exit"
  echo ""

  if($PrintHelp) \
  cat $0 | awk 'BEGIN{prt=0}{if(prt) print $0; if($1 == "BEGINHELP") prt = 1 }'

exit 1;

#---- Everything below here is printed out as part of help -----#
BEGINHELP

This is a front-end for the FSL (www.fmrib.ox.ac.uk/fsl/index.html)
flirt program. Computes the registration matrix that transforms
inputvol to refvol and resamples inputvol to outputvol using that
matrix. The matrix is stored in outputvol.fslmat. The registration is
constrained to be rigid (ie, 6 dof). The input and ref volumes are
automatically converted to analyze, and the output volume is
automatically converted from analyze to the output format. Note: if
using COR as output, the COR directory must exist prior to running
this script. The FLIRT/FSL registration matrix will be stored in
outvol.fslmat (unless -fslmat).

By default, an init FSL registration matrix is computed from the
geometry information in the header ref and input headers. This is a
good idea because the anlyze format does not keep the direction cosine
info around.  

-fslmat fslmatfile

Store the FSL registration matrix in fslmatfile instead of outvol.fslmat

-regmat regmatfile

Convert the FSL matrix to register.dat format and save in regmatfile. You
will have to edit the subject name.

-xfmmat xfmmatfile

Convert the FSL matrix to an MNI xfm file. 

-noinitgeom

Do not compute init registration matrix from geometry.

-initxfm fslmatfile

Use matrix in fslmatfile as the initial registation matrix. If no
initialization is specified, the identity is assumed. Forces
-noinitgeom.

-maxangle maxangle

Search only +/- maxangle degrees around the initial starting
point. Default is 90.

-applyxfm 

When the -applyxfm flag is is used, the input is resampled to the
output using the FSL mat file supplied as the argument to the
-applyxfm.  The ref volume is still needed in order to get the proper
geometry for the output file. Forces -noinitgeom.

-dof dof

Non-rigid registration can be performed by changing the dof from 6.


BUGS: 

If using COR as output, the COR directory must exist prior to running
this script.

