#! /bin/csh -f

#
# rawfunc2tal
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/09 22:41:18 $
#    $Revision: 1.4 $
#
# 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: rawfunc2tal,v 1.4 2007/01/09 22:41:18 nicks Exp $'
set FASTVER = `cat $FMRI_ANALYSIS_DIR/docs/version`;

#------ Defaults --------#
set instem   = ();
set outstem  = ();
set voxsize  = ();
set dim      = ();
set fov      = ();
set reg      = ();
set talxfm   = ();

set inputargs = ($argv);

set n = `echo $argv | grep -e -help | wc -l` 
if($#argv == 0 || $n != 0)  goto usage_exit;

set n = `echo $argv | grep -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:

echo $VERSION
pwd
echo $0         
echo $inputargs 
uname -a        
date

set outdir = `dirname $outstem`;
mkdir -p $outdir

set inbext = `getbext $instem`;
set infile = $instem"_000."$inbext
set outfile = $outstem"_000.bfloat"

set cmd = (mri_vol2vol --in $infile)
set cmd = ( $cmd --out $outfile );
set cmd = ( $cmd --temp $SUBJECTS_DIR/talairach/mri/orig COR)
set cmd = ( $cmd --voxres $voxsize);
set cmd = ( $cmd --xfm $reg --fstal);
if($#dim) set cmd = ( $cmd --voxdim $dim);

echo "---------------------------------------------" 
pwd        
echo $cmd  
echo "---------------------------------------------" 

$cmd 
if($status) then
  echo "ERROR: mri_vol2vol failed"
  exit 1;
endif

date
echo "rawfunc2tal: Done"

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 instem = $argv[1]; shift;
      breaksw

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

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

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

    case "-voxsize":
      if( $#argv < 3) then
        echo "ERROR: $flag requires 3 arguments"
        exit 1;
      endif
      set voxsize = ($argv[1] $argv[2] $argv[3]);
      shift; shift; shift;
      breaksw

    case "-fov":
      if( $#argv < 3) then
        echo "ERROR: $flag requires 3 arguments"
        exit 1;
      endif
      set fov = ($argv[1] $argv[2] $argv[3]);
      shift; shift; shift;
      breaksw

    case "-dim":
      if( $#argv < 3) then
        echo "ERROR: $flag requires 3 arguments"
        exit 1;
      endif
      set dim = ($argv[1] $argv[2] $argv[3]);
      shift; shift; shift;
      breaksw

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

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

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

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

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

end

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

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

  if($#instem == 0) then
    echo "ERROR: need to specify an input stem"
    exit 1;
  endif

  if($#outstem == 0) then
    echo "ERROR: need to specify an output stem"
    exit 1;
  endif

  if($#reg == 0) then
    echo "ERROR: need to specify a registration file"
    exit 1;
  endif

  if(! -e $reg ) then
    echo "ERROR: registration file $reg does not exist"
    exit 1;
  endif

  if($#voxsize == 0) then
    echo "ERROR: need to specify output voxel sizes"
    exit 1;
  endif

  if($#dim == 0) then
    echo "ERROR: need to specify output dimension"
    exit 1;
  endif

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

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

############--------------##################
usage_exit:
  echo ""
  echo "USAGE: rawfunc2tal"
  echo ""
  echo "Required Arguments"
  echo " -i instem  : input stem"
  echo " -o outstem : output stem"
  echo " -r regfile : register.dat file"
  echo " -voxsize colsize rowsize slicesize : size in mm"
  echo " -dim     ncols  nrows  nslice  : dimension of the output vol"
  #echo " -fov     colfov  rowfov  slicefov  : field of view in mm"
  echo ""
  echo "Other Arguments"
  echo " -t talxfm      : talairach.xfm file"
  echo " -umask umask   : set unix file permission mask"
  echo " -version       : print version and exit"
  echo " -help          : get more info"

  set n = `echo $argv | grep -e -help | wc -l` 
  if($n != 0)  goto help_exit;

  echo ""
exit 1;

############--------------##################
help_exit:
echo "\
\
This program will resample a raw data set into talairach space (it can \
actually transform it into any arbitrary space -- see below). The \
regfile holds the transform from an anatomical space to the data \
space.  The talxfm file is the transform from the same anatomical \
space to talairach space. The output will be centered at the origin of \
talairach space. The FOV in the col (x) direction will be colfov; the \
FOV in the row (z) direction will be rowfov; and the FOV in the slice \
(y) direction will be slicefov.  etc. Each dimension will be divided \
into voxels according to the -voxsize flag. The data are resampled \
using nearest-neighbor. If the talairach transform file is not \
specified, the the subject name is obtained from the registration file \
and SUBJECTS_DIR/subjectname/mri/transforms/talairach.xfm is used. \
Note the data set can be resampled to any space if a .xfm file that \
maps the anatomical space to the target space is available. This \
program will also produce an output registration file called outstem.reg.\
\
Notes:\
\
There are no restrictions on the voxel size or FOV, but users should\
check that the resulting volume encompasses as much of the original\
volume as desired.\
\
column = x, row = z, slice = y\
\
Examples:\
\
1. This will resample the volume f in the current directory to   \
   talairach space with isotropic 5 mm voxels +/- 95 mm around  \
   the center of talairach space in the x and z directions and \
   +/- 110 mm in the y direction. The resulting volume will have \
   38 columns and rows, and 44 rows. If the talairach.xfm file \
   had not been specified, it would have been obtained from the \
   FreeSurfer recon directory of the subject listed in the \
   registration file. \
\
  rawfunc2tal -i f -o tal/f           \
     -r ../register.dat                \
     -t ../../3danat/002/talairach.xfm \
     -voxsize 5 5 5 -fov 190 190 220   \
\
"

exit;
