#! /bin/tcsh -f

#
# talairach2
#
# USAGE: talairach2 subjectid <mgzflag>
#  subjectid - name of the subject
#  mgzflag - if anything is present, then it will assume
#   that the nu volume is stored as nu.mgz in the mri
#   directory (ie, the nu dir does not exist and there
#   is no COR volume).
#
# originally mri/orig was used for this purpose
# The algorithm of mritotal is correlation based and thus
# it is better to use the intensity normalized volume, nu.
#
# Original Author: REPLACE_WITH_FULL_NAME_OF_CREATING_AUTHOR
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/06 00:01:15 $
#    $Revision: 1.13 $
#
# 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: talairach2,v 1.13 2007/01/06 00:01:15 nicks Exp $';

set MGZ = ();

# MNI perl module needs the following setting
setenv LANG C

# Look for version #
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

if($#argv < 1 || $#argv > 2) then
  echo "USAGE: talairach2 subjectid <mgzflag>"
  exit 1;
endif

set subject = $argv[1]; shift;
if($#argv != 0) set MGZ = ".mgz"

set base = $SUBJECTS_DIR/$subject/mri

if(! -e $base) then
  echo "ERROR cannot find $base"
  exit 1;
endif

# model=/homes/nmrnew/home/inverse/local/lib/model/average_305.mnc
# dir=`pwd`

mkdir -p $base/transforms
set transform = $base/transforms/talairach.xfm
pushd $base > /dev/null
echo "INFO: registering $subject"
date

echo "--------------------------------------------"
set cmd = (mri_convert nu$MGZ nu.mnc)
echo $cmd
$cmd
if($status) then
  echo "ERROR: mri_convert failed"
  exit 1;
endif

if(! -e nu.mnc) then
  echo "ERROR: cannot find nu.mnc"
  exit 1;
endif

echo "--------------------------------------------"
set cmd = (mritotal -verbose -debug -clobber \
	    -modeldir ${MINC_BIN_DIR}/../share/mni_autoreg \
	    -protocol icbm \
	    nu.mnc transforms/talairach.xfm)
echo $cmd
$cmd > ../scripts/mritotal.log
set st = $status
rm nu.mnc
if($st) then
  echo "ERROR: mritotal failed, see scripts/mritotal.log"
  exit 1;
endif

echo "--------------------------------------------"
set cmd = (mri_add_xform_to_header $transform $base/orig$MGZ $base/orig$MGZ)
echo $cmd
$cmd
if($status) then
  echo "ERROR: mri_add_xform_to_header $base/orig$MGZ failed"
  exit 1;
endif

echo "--------------------------------------------"
set cmd = (mri_add_xform_to_header $transform $base/nu$MGZ $base/nu$MGZ)
echo $cmd
$cmd
if($status) then
  echo "ERROR: mri_add_xform_to_header $base/nu$MGZ failed"
  exit 1;
endif
popd > /dev/null

date

exit 0
