#! /bin/tcsh -f

#
# mri-func2sph
#
# 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:14 $
#    $Revision: 1.5 $
#
# 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 FUNC2SPH_VER = '$Id: mri-func2sph,v 1.5 2007/01/06 00:01:14 nicks Exp $';

set icosize  = 10242;
set hemi     = ()
set fvitdir  = ();
set instem   = ();
set outstem  = ();
set niters   = 100;
set ErrorMsg = "";
set intype   = "auto";
set newumask = `umask`;

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

goto parse_args;
parse_args_return:

goto check_args;
check_args_return:

## Make sure that the input volume exists ##
set tmp = $instem"_000.hdr";
if(! -e $tmp) then
   echo "ERROR: cannot find $tmp"
   exit 1;
else

## Autodetect the input type ##
if("$intype" == "auto") then
  set datfile = $instem.dat;
  if(! -e $datfile) then
    set intype = "bvolume";
  else
    set n = `grep Version $datfile | wc -l`;
    if($n == 0) then
      set intype = "selavg";
    else
      set intype = "selxavg";
    endif
  endif
endif
echo "Detected input volume as type $intype";

## Make sure that the hole-filling vit exists ##
set icofill = $FREESURFER_HOME/lib/bem/ic$icosize.vit
if(! -e $icofill ) then
  echo "ERROR: cannot find $icofill"
  exit 1;
endif

## Make sure that the func-to-ico vit exists ##
set func2ico  = $fvitdir/$hemi.func-to-ic$icosize.vit
if(! -e $func2ico ) then
  echo "ERROR: cannot find $func2ico"
  exit 1;
endif

set outdir  = `dirname $outstem`;
if(! -d $outdir) mkdir $outdir


echo "------ ------ Hemisphere $hemi ------ ------ "
set tmpoutfile = $outstem-ic$icosize.tmp
set outfile    = $outstem"_000.bfloat"

  #-------------------------------------------------------------------#
  echo "------ Resampling"
  set cmd = "vss-resample -s $instem -f $intype -vit $func2ico -t $tmpoutfile";
  $cmd
  set rt = $status;
  if($rt) then
    echo "ERROR: vss-resample returned with status $rt"
    echo $cmd
    exit 1;
  endif

  #-------------------------------------------------------------------#
  if($niters > 0) then
    set tmpfillfile = $outstem-ic$icosize.tmpfill
    echo "------ Filling Holes "
    set cmd = "vss-fillholes $tmpoutfile $icofill $niters $tmpfillfile"
    $cmd
    set rt = $status;
    if($rt) then
      echo "ERROR: vss-fillholes returned with status $rt"
      echo $cmd
      exit 1;
    endif
  else
    set tmpfillfile = $tmpoutfile
  endif

  #-------------------------------------------------------------------#
  echo "------ Converting to BFile Format"
  if($icosize == 10242) then
    set nr = 18;
    set nc = 569;
  else
    set nr = $icosize;
    set nc = 1;
  endif
  set cmd = "vss-convert -vss $tmpoutfile -o $outstem -of $intype $nr $nc 1"
  $cmd
  set rt = $status;
  if($rt) then
    echo "ERROR: vss-convert returned with status $rt"
    echo $cmd
    exit 1;
  endif

  rm -f $tmpoutfile $tmpfillfile

  if($intype == selavg) then
    echo "  Copying selavg-related files"
    cp $instem.dat $outstem.dat
    cp $instem"_000.dof" $outstem"_000.dof"
  endif

  if($intype == selxavg) then
    echo "  Copying selxavg-related files"
    cp $instem.dat $outstem.dat
    #cp $instem"_hcov_000.bfloat" $outstem"_hcov_000.bfloat"
    #cp $instem"_hcov_000.hdr" $outstem"_hcov_000.hdr"
    if(1) then
      echo "INFO: resampling selxavg offset files"
      mri-func2sph -i $instem"-offset" -o $outstem"-offset" -hemi $hemi\
         -fvitdir $fvitdir -niters $niters -icosize $icosize  \
         -intype bvolume -umask $newumask
    endif
  endif


echo "mri-func2sph completed successfully"

exit 0;
#####################################
############--------------##################
parse_args:

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

  set flag = $argv[1]; shift;

  switch($flag)

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

    case "-hemi":
      if ( $#argv == 0) goto arg1err;
      set hemi = $argv[1]; shift;
      if("$hemi" != "lh" && "$hemi" != "rh") then
        echo "ERROR: -hemi must be either lh or rh ($hemi)"
        exit 1;
      endif
      breaksw

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

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

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

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

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

    case "-umask":
    case "-um":
      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($#instem == 0) then
    echo "ERROR: must specify an input stem";
    exit 1;
  endif

  if(! -d $InDir ) then
    echo "ERROR: cannot find $InDir"
    exit 1;
  endif

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

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

  if($#fvitdir == 0) then
    echo "ERROR: must specify an fvtidir stem";
    exit 1;
  endif

  if(! -d $fvitdir ) then
    echo "ERROR: cannot find $fvitdir"
    exit 1;
  endif

  if($#intype > 0) then
    if("$intype" != "auto" && "$intype" != "bvolume" && \
       "$intype" != "selavg" && "$intype" != "selxavg") then
      echo "ERROR: intype set to $intype"
      echo "  must be one of: auto, bvolume, selavg, selxavg"
      exit 1;
    endif
  endif

goto check_args_return;

#----------------------------------------------------------------#
usage_exit:
  echo "\nUSAGE: \n\n mri-func2sph "
  echo "   -i        instem             required"
  echo "   -o        outstem            required"
  echo "   -hemi     lh, rh hemisphere  required"
  echo "   -fvitdir  func vit dir       required"
  echo "   -niters   hole-filling iters $niters"
  echo "   -icosize  icosahedron size   $icosize"
  echo "   -intype   type of input      auto-detect"
  echo "   -umask    newumask"
  echo "\n $FUNC2SPH_VER \n"
exit 1;
