#! /bin/tcsh -f

#
# make_cortex_label
#
# Creates a label for cortex from Christophe's parcellation (eveything but 
# Medial_wall). The label name is ?h.cortex.label. As of freesurfer version 4,
# all recons should have a label of this name (created using a different
# process). If this label name exists, then the execution is halted.
# Also copies ?h.Medial_wall.label to label dir.
#
# Original Author: Douglas Greve
# 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
#
#

set VERSION = '$Id: make_cortex_label,v 1.4 2011/03/02 20:16:39 nicks Exp $';
set inputargs = ($argv);

set subject   = ();
set hemilist  = (lh rh)

set parc = aparc;
set NonCtxLabelList = (unknown corpuscallosum);
                               
set CtxLabel = cortex;

#set parc = aparc.a2006s;
#set NonCtxLabelList = (Medial_wall);

set n = `echo $argv | grep -e -version | wc -l`
if($n != 0) then
  echo $VERSION
  exit 0;
endif
if($#argv == 0) goto usage_exit;
goto parse_args;
parse_args_return:
goto check_params;
check_params_return:

# Loop thru the hemis
foreach hemi ($hemilist)
  set annot = $sdir/label/$hemi.$parc.annot
  if(! -e $annot) then
    echo "ERROR: cannto find $annot"
    exit 1;
  endif

  # Create tmpdir
  set tmpdir = $sdir/make_cortex_label.tmp.$$
  rm -rf $tmpdir
  mkdir -p $tmpdir

  # Unload all labels into tmpdir
  set cmd = (mri_annotation2label --subject $subject --hemi $hemi \
    --outdir $tmpdir --annotation $parc)
  pwd
  echo $cmd 
  $cmd
  if($status) exit 1;

  # Delete medial wall label
  foreach lbl ($NonCtxLabelList) 
    rm -f $tmpdir/$hemi.$lbl.label
  end

  # Create the cortex label with all the remaining labels
  set ctxlabel = $sdir/label/$hemi.$CtxLabel.label
  set cmd = (mri_mergelabels -o $ctxlabel -d $tmpdir)
  echo $cmd 
  $cmd
  if($status) exit 1;

  # Cleanup
  rm -rf $tmpdir

end

date
echo "make_cortex_label done"

exit 0;

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

  set flag = $argv[1]; shift;

  switch($flag)

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

    case "--h"
      if($#argv < 1) goto arg1err;
      set hemilist = $argv[1]; shift;
      breaksw

    case "--lh"
      set hemilist = (lh);
      breaksw

    case "--rh"
      set hemilist = (rh);
      breaksw

    case "--a2009s"
      set parc = aparc.a2009s;
      set NonCtxLabelList = (Medial_wall);
      breaksw

    case "--a2005s"
      set parc = aparc.a2005s;
      set NonCtxLabelList = (Medial_wall);
      breaksw

    case "--o"
      if($#argv < 1) goto arg2err;
      set CtxLabel = $argv[1]; shift;
      breaksw

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

    default:
      echo "ERROR: $flag not regocnized"
      exit 1;
      breaksw
  endsw

end

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

############--------------##################
check_params:
  if($#subject == 0) then
    echo "ERROR: need subject"
    exit 1;
  endif
  set sdir = $SUBJECTS_DIR/$subject
  if(! -e $sdir) then
    echo "ERROR: cannto find $sdir"
    exit 1;
  endif
goto check_params_return;
############--------------##################

############--------------##################
arg1err:
  echo "ERROR: flag $flag requires one argument"
  exit 1
############--------------##################
############--------------##################
usage_exit:
  echo ""
  echo "USAGE: make_cortex_label"
  echo ""
  echo "   --s subject"
  echo "   --h hemi (default is both)"
  echo "   --a2009s : use aparc.a2009 insteda of aparc"
  echo "   --o outname : output will be ?h.outname.label (def is $CtxLabel)"
  echo ""
exit 1;
