#! /bin/tcsh -f

#
# spmmat2register
#
# REPLACE_WITH_ONE_LINE_SHORT_DESCRIPTION
#
# Original Author: REPLACE_WITH_FULL_NAME_OF_CREATING_AUTHOR
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/06 00:01:15 $
#    $Revision: 1.12 $
#
# 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
#


echo ""
echo "ERROR: spmmat2register is obsolete. Use tkregister2."
echo "Run tkregister2 with --help for extensive documentation."
echo "In particular, look at the section called USING WITH FSL and SPM"
echo ""
exit 1;

set SPMMAT2REGISTER_VER = '$Id: spmmat2register,v 1.12 2007/01/06 00:01:15 nicks Exp $';

set OutStream = /dev/null;
set funcvol   = ();
set structvol = ();
set regfile = "register.dat"
set subject = ();
set monly = 0;
set Intensity = 0.3;

if($#argv == 0) goto usage_exit;

goto parse_args;
parse_args_return:

goto check_args;
check_args_return:

set structimgfile = $structvol.img
if(! -e $structimgfile ) then
  echo "ERROR: cannot find $structimgfile "
  exit 1;
endif

set funcimgfile = $funcvol.img
if(! -e $funcimgfile ) then
  echo "ERROR: cannot find $funcimgfile "
  exit 1;
endif

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

if($monly) then
  set TARGET = " tee  $mfile "
else
  set TARGET = "matlab -display iconic "
endif

rm -f $regfile

#-----------------------------------------------------------------#
$TARGET > $OutStream <<EOF

  StructVol = '$structvol.img';
  FuncVol   = '$funcvol.img';

  % Load header of functional volume %
  [DIM VOX SCALE TYPE OFFSET ORIGIN DESCRIP] = spm_hread(FuncVol);
  fdim = DIM;
  fvox = abs(VOX);

  %% MGH-style Quantization matrix for functional volume  %
  Qfmgh = [-fvox(1)      0       0     fvox(1)*(fdim(1)-1)/2; ...
              0          0    fvox(3) -fvox(3)*(fdim(3)-1)/2; ...
              0      -fvox(2)    0     fvox(2)*(fdim(2)-1)/2; ...
              0          0       0       1];

  %% MGH-style Quantization matrix for structural volume  %
  Qcmgh = [-1     0     0   127.5; ...
            0     0     1  -127.5
            0    -1     0   127.5
            0     0     0     1];

  %% SPM-style Quantization matrix for functional volume  %
  Qfspm = spm_get_space(FuncVol);

  %% SPM-style Quantization matrix for structural volume  %
  Qcspm = spm_get_space(StructVol);

  %% This is a matrix to account for the fact that SPM starts
  %% counting indices at 1 where as MGH starts at 0.
  H = eye(4);
  H(:,4) = 1;

  Mreg = Qfmgh*inv(H)*inv(Qfspm)*Qcspm*H*inv(Qcmgh);

  SubjName = '$subject';
  fid = fopen('$regfile','w');
  if(fid == -1)
    fprintf(1,'ERROR: could not open $regfile\n');
    error('');
    quit;
  end
  fprintf(fid,'%s\n',SubjName);
  fprintf(fid,'%8.4f\n',fvox(1));
  fprintf(fid,'%8.4f\n',fvox(3));
  fprintf(fid,'%8.4f\n',$Intensity);
  fprintf(fid,'%8.4f %8.4f %8.4f %8.4f\n',Mreg'); %'
  fclose(fid);

  return;
EOF
#-----------------------------------------------------------------#

if(! $monly) then
  if(! -e $regfile ) then
    echo "ERROR: $regfile not created"
    echo "  Rerun with -debug flag"
    exit 1;
  endif
endif


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

parse_args:

set cmdline = "$argv";
while( $#argv != 0 )

  set flag = $argv[1]; shift;

  switch($flag)

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

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

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

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

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

    case "-debug":
      set verbose = 1;
      set OutStream = /dev/stdout;
      breaksw

    case "-verbose":
      set OutStream = /dev/stdout;
      breaksw

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

    case "-umask":
      if ( $#argv == 0) goto arg1err;
      #echo "INFO: Setting umask to $argv[1]"
      set newumask = $argv[1]; shift;
      umask $newumask
      breaksw

    default:
      echo ERROR: Flag $flag unrecognized.
      echo $cmdline
      goto usage_exit;
      breaksw
  endsw

end
goto parse_args_return;

#----------------------------------------------------------------#
check_args:

  if($#funcvol == 0) then
    echo "ERROR: must specify a functional volume";
    exit 1;
  endif

  if($#structvol == 0) then
    echo "ERROR: must specify a structural volume";
    exit 1;
  endif

  if($#subject == 0) then
    echo "ERROR: must specify a subject name";
    exit 1;
  endif

goto check_args_return;

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

#----------------------------------------------------------------#
usage_exit:
  echo "\nUSAGE: \n spmmat2register "
  echo "   -svol      structural volume     (required)"
  echo "   -fvol      functional volume     (required)"
  echo "   -subject   subject name          (required)"
  echo "   -o         output register file  (register.dat)"
  echo "   -intensity intensity value for register ($Intensity)"
#  echo "   -coordef   coordinate definition of structs ($CoorDef)"
  echo "   -umask     newumask"
  echo "   -monly     mfile"
  echo "   -debug     print out lots of info"
  echo "\n $SPMMAT2REGISTER_VER \n"
exit 1;
