#! /bin/tcsh -f

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

# wfilemask
#
set VERSION = '$Id: wfilemask,v 1.5 2007/01/06 00:01:15 nicks Exp $';
set inputargs = ($argv);

set LabelFile = ();
set WFile     = ();
set WFileOut  = ();
set PrintHelp = 0;
set monly = 0;

if($#argv == 0)  goto usage_exit;
set n = `echo $argv | egrep -e -version | wc -l`
if($n != 0) then
  echo $VERSION
  exit 0;
endif
set n = `echo $argv | egrep -e -help | wc -l`
if($n != 0) then
  set PrintHelp = 1;
  goto usage_exit;
endif

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

if(! $monly) set MLF = /tmp/wfilemask-$$.m
echo MLF is $MLF
rm -f $MLF

#--------------------------------------------------------------#
tee $MLF > /dev/null <<EOF
tic;

addpath $FREESURFER_HOME/matlab
wfile = '$WFile';
labelfile = '$LabelFile';
wfile_out = '$WFileOut';

label = read_label([],labelfile);
if(isempty(label))
  fprintf('ERROR: could not load %s\n',labelfile);
  return;
end

[w v] = read_wfile(wfile);
if(isempty(w))
  fprintf('ERROR: could not load %s\n',wfile);
  return;
end
nv = length(v);
%fast_write_wfile('~/sg1/tmp/w0-lh.w',w,v);

ind = label(:,1);
nlabel = length(ind);

nmax = max(max(v+1),max(ind+1));
w2 = zeros(nmax,1);
w2(v+1) = w;
w2(ind+1) = 0;
v2 = [0:nmax-1]';
indnz = find(w2 ~= 0);
w2 = w2(indnz);
v2 = v2(indnz);

write_wfile(wfile_out,w2,v2);

fprintf('matlab: wfilemask done (t=%g s)\n',toc);

EOF
#--------------------------------------------------------------#

echo "-----------------------------------------------------"

set StartTime = `date`;
if(! $monly) then
  cat $MLF | matlab -nojvm -nosplash -display iconic
  rm -f $MLF
endif

echo ""
echo ""
echo "Started at $StartTime"
echo "Ended   at `date`"
echo "wfilemask completed"
echo " "
echo " "

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

############--------------##################
parse_args:
set cmdline = ($argv);
while( $#argv != 0 )

  set flag = $argv[1]; shift;

  switch($flag)

    case "-l":
      if ( $#argv == 0) goto arg1err;
      set LabelFile = $argv[1]; shift;
      if(! -e $LabelFile) then
        echo "ERROR: cannot find $LabelFile"
        exit 1;
      endif
      breaksw

    case "-w":
      if ( $#argv == 0) goto arg1err;
      set WFile = $argv[1]; shift;
      if(! -e $WFile) then
        echo "ERROR: cannot find $WFile"
        exit 1;
      endif
      breaksw

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

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

    case "-umask":
      if ( $#argv == 0) goto arg1err;
      umask $argv[1]; shift;
      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:

  set errs = 0;

  if($#LabelFile == 0) then
    echo "ERROR: no label file specified"
    exit 1;
  endif

  if($#WFile == 0) then
    echo "ERROR: no input w file specified"
    exit 1;
  endif

  if($#WFileOut == 0) then
    echo "ERROR: no output w file specified"
    exit 1;
  endif

  set WFileOutDir = `dirname $WFileOut`;
  mkdir -p $WFileOutDir
  if($status) then
    echo "ERROR: making directory $WFileOutDir"
    exit 1;
  endif

  if($#FREESURFER_HOME == 0) then
    echo "ERROR: FREESURFER_HOME environment variable not set"
    exit 1;
  endif

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

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

############--------------##################
usage_exit:
  echo " "
  echo "USAGE: wfilemask"
  echo ""
  echo "   -w wfile : input w file"
  echo "   -l label file : label to use as a mask "
  echo "   -o wfileout : output w file"
  echo " "
  echo "   -help"
  echo "   -umask umask   : set unix file permission mask"
  echo "   -version       : print version and exit"
  echo " "

  if(! $PrintHelp ) exit 1;

  echo " "
  echo "$VERSION "
  echo " "

  cat $0 | awk 'BEGIN{prt=0}{if(prt) print $0; if($1 == "BEGINHELP") prt = 1 }'
exit 1;

#---- Everything below here is printed out as part of help -----#
BEGINHELP

wfilemask

This program allows the user to zero-out regions of a surface value
file (also known as a .w file) using a label. The user specifies an
input w (with -w) file and a label (with -l). The vertices
corresponding to the label are set to zero, and the result is saved in
a new w file (specified with -o). NOTE: this script requires matlab.

