#! /bin/tcsh -f

#
# talairach
#
# CVS Revision Info:
#    $Author: greve $
#    $Date: 2007/03/15 23:23:11 $
#    $Revision: 1.6 $
#
# Copyright (C) 2002-2007,
# The General Hospital Corporation (Boston, MA).
# All rights reserved.
#
# Distribution, usage and copying of this software is covered under the
# terms found in the License Agreement file named 'COPYING' found in the
# FreeSurfer source code root directory, and duplicated here:
# https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferOpenSourceLicense
#
# General inquiries: freesurfer@nmr.mgh.harvard.edu
# Bug reports: analysis-bugs@nmr.mgh.harvard.edu
#

#
set VERSION = '$Id: talairach,v 1.6 2007/03/15 23:23:11 greve Exp $'
setenv LANG C # needed by MNI perl module


set InVol = ();
set XFM = ();
set Protocol = icbm

set LF = ();
set debug = 0;
set PrintHelp = 0;

set cmdargs = ($argv);

if($#argv == 0)  goto usage_exit;
set n = `echo $argv | egrep -e --help | wc -l`
if($n != 0) then
  set PrintHelp = 1;
  goto usage_exit;
  exit 1;
endif
set n = `echo $argv | egrep -e --version | wc -l`
if($n != 0) then
  echo $VERSION
  ## gather version info used by this script
  mri_convert --version
  set prog = `which mritotal`
  strings $prog | grep mritotal.in
  exit 0;
endif

# Parse the command-line arguments
goto parse_args;
parse_args_return:

# Check the command-line arguments
goto check_params;
check_params_return:

set OutDir = `dirname $XFM`;
mkdir -p $OutDir;

if($#LF == 0) set LF = $OutDir/talairach.log
if(-e $LF) mv $LF $LF.bak

pwd  | tee -a $LF
which talairach | tee -a $LF
echo $cmdargs | tee -a $LF
echo $VERSION | tee -a $LF
uname -a | tee -a $LF
date | tee -a $LF

set tmpdir = $OutDir/tmp.talairach.$$
mkdir -p $tmpdir
echo "tmpdir is $tmpdir" | tee -a $LF

# Convert input to minc
set cmd = (mri_convert $InVol $tmpdir/src.mnc)
pwd |& tee -a $LF
echo $cmd |& tee -a $LF
$cmd |& tee -a $LF
if($status) then
  echo "ERROR: converting to minc" |& tee -a $LF
  exit 1;
endif

echo "--------------------------------------------"
set cmd = (mritotal -verbose -debug -clobber \
	    -modeldir ${MINC_BIN_DIR}/../share/mni_autoreg \
	    -protocol $Protocol \
	    $tmpdir/src.mnc $XFM)
echo $cmd |& tee -a
$cmd >> $LF
set st = $status
rm -r $tmpdir
if($st) then
  echo "ERROR: mritotal failed, see $LF"
  exit 1;
endif

echo " " |& tee -a $LF
echo " " |& tee -a $LF
date | tee -a $LF
echo "talairach done" | tee -a $LF

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


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

  set flag = $argv[1]; shift;

  switch($flag)

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

    case "--protocol-default":
      set Protocol = default;
      # Refers to the MNI default protocol
      # Does not use default by default, uses icbm.
      breaksw

    case "--protocol-icbm":
      set Protocol = icbm;
      # Does not use default by default, uses icbm.
      breaksw

    case "--xfm":
    case "--x":
      if ( $#argv == 0) goto arg1err;
      set XFM = $argv[1]; shift;
      breaksw

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

    case "--debug":
      set verbose = 1;
      set echo = 1; # turns on terminal echoing
      set debug = 1;
      breaksw

    default:
      echo "ERROR: flag $flag not recognized"
      exit 1;
      breaksw
  endsw

end

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

############--------------##################
check_params:
  if($#InVol == 0) then
    echo "ERROR: must specify an input volume"
    exit 1;
  endif
  if(! -e $InVol) then
    echo "ERROR: input volume $InVol does not exist"
    exit 1;
  endif
  if($#XFM == 0) then
    echo "ERROR: must specify an output transform file"
    exit 1;
  endif
goto check_params_return;
############--------------##################

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

############--------------##################
usage_exit:
  echo ""
  echo "USAGE: mri_nu_correct.mni"
  echo ""
  echo " --i   invol  : input volume"
  echo " --xfm outxfm : output xfm file"
  echo ""
  echo "Optional flags and arguments:"
  echo ""
  echo " --log      : log file. default is outdir/talarach.log"
  echo " --help     : print help and exit"
  echo " --debug    : turn on debugging"
  echo " --version  : print version and exit"
  echo ""

  if(! $PrintHelp) exit 1;

  echo $VERSION

  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

Front-end for MINCs mritotal. Computes the talairach transform for
that maps the input volume to the MNI305. Note: does not add
the xfm to the header of the input file. See mri_add_xform_to_header.
When called by recon-all, the xfm is added to the header after
the transform is computed.

mritotal is documented here:
www.bic.mni.mcgill.ca/users/louis/MNI_AUTOREG_home/readme/node1.html

minctracc is documented here:
www.bic.mni.mcgill.ca/users/louis/tracc/tracc_doc/section3_1.html
