#!/bin/sh

#
# map_to_base
#
# maps an input image or surface to the base space 
#
# Original Author: Martin Reuter
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2011/03/02 20:16:39 $
#    $Revision: 1.4 $
#
# 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
#
#

VERSION='$Id: map_to_base,v 1.4 2011/03/02 20:16:39 nicks Exp $';

# WITHOUT FREESURFER DO NOTHING
if [ -z $FREESURFER_HOME ]
then
  echo "ERROR: environment variable FREESURFER_HOME not set"
  exit 1;
fi

# USAGE
if [ "$#" -le "3" ]
then
  echo
	echo "`basename $0` baseid tpid input rt [cross]"
	echo
	echo "  baseid:    id of base"
	echo "  tpid  :    id of tp (without .long.baseid)"
	echo "  input :    input image, e.g. norm.mgz, aseg.mgz, lh.white"
	echo "  rt    :    resample-type "
	echo "               'interpolate' for norm, T1, orig.."
	echo "               'nearest'     for aseg .."
	echo "               'surface'     for surfaces .."
	echo "  cross :    if '1' input from cross sectionals"
	echo "             default: use longitudinal directories"
	echo
	echo " Will map single image or surface from tp dir (cross or long)"
	echo " to the base space and place output in base dir."
	echo
	echo " Output will be in base/mri directory or base/surf"
	echo " Naming will be: tpid-long.input or tpid-cross.input"
	echo
	exit 1
fi

# SET PARAMETERS
base=$1
tp=$2
input=$3
rt=$4
ldir=mri
if [ "$rt" == "surface" ] ; then ldir=surf ; fi

long=1
tpid=$tp.long.$base
longtxt="-long"
if [ "$#" -ge "5" ] ; then long=0; tpid=$tp ; longtxt="-cross"; fi

# check input
infile=$SUBJECTS_DIR/$tpid/$ldir/$input
if [ ! -e $infile ]
then
  echo "ERROR: $infile does not exist ..."
	exit 1
fi
bdir=$SUBJECTS_DIR/$base
if [ ! -e $bdir ]
then
  echo "ERROR: $bdir does not exist ..."
	exit 1
fi
lta=$bdir/mri/transforms/${tp}_to_${base}.lta
if [ ! -e $lta ]
then
  echo "ERROR: $lta does not exist, make sure tp is part of this base ..."
	exit 1
fi

ofile=$bdir/$ldir/${tp}${longtxt}.$input

case "$rt" in
  "interpolate" )
    cmd="mri_convert -at $lta"
	  cmd="$cmd -rl $bdir/mri/$input"
	  cmd="$cmd $infile $ofile" ;;
	"nearest" )
    cmd="mri_convert -at $lta -rt $rt"
	  cmd="$cmd -rl $bdir/mri/$input"
	  cmd="$cmd $infile $ofile" ;;
	"surface" )
	  cmd="mris_transform $infile $lta $ofile";;
   * )
	  echo "ERROR: Reslice type: $rt not recognized"
		exit 1 ;;
esac

  echo $cmd
	eval $cmd
