#!/bin/tcsh -f
#
# samseg2recon - create and populate a subjects dir in a way that
# recon-all can be run on it.

set VERSION = 'samseg2recon dev';

if(-e $FREESURFER_HOME/sources.csh) then
  source $FREESURFER_HOME/sources.csh
endif

set samsegdir = ();
set subject = ();
set input = ();
set DoUchar = 0; # recon-all apparently does not need it
set KeepECS = 1; # recon-all apparently handles extracereb seg ok
set DoCC = 1;    # Corpus callosum
set DoFill = ()
set DoNormalization2 = 0;
set HaveT2w = 0;
set HaveFLAIR = 0;
set tmpdir = ()
set LF = ()
set LongTP = ();
set DoLongBase = 0;
set LongUseMC = 1;
set cleanup = 1
# set this to 1 until can make all commands respect updates
set ForceUpdate = 1; 

set inputargs = ($argv);
set PrintHelp = 0;
if($#argv == 0) goto usage_exit;
set n = `echo $argv | grep -e -help | wc -l` 
if($n != 0) then
  set PrintHelp = 1;
  goto usage_exit;
endif
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:

set StartTime = `date`;
set tSecStart = `date '+%s'`;
set year  = `date +%Y`
set month = `date +%m`
set day   = `date +%d`
set hour   = `date +%H`
set min    = `date +%M`

mkdir -p $SUBJECTS_DIR/$subject/scripts 
mkdir -p $SUBJECTS_DIR/$subject/mri/transforms
mkdir -p $SUBJECTS_DIR/$subject/mri/tmp

if($#tmpdir == 0) then
  if(-dw /scratch)   set tmpdir = /scratch/tmpdir.samseg2recon.$$
  if(! -dw /scratch) set tmpdir = $SUBJECTS_DIR/$subject/mri/tmp/tmpdir.samseg2recon.$$
endif
mkdir -p $tmpdir

# Set up log file
if($#LF == 0) set LF = $SUBJECTS_DIR/$subject/scripts/samseg2recon.log
if($LF != /dev/null) rm -f $LF
echo "Log file for samseg2recon" >> $LF
date  | tee -a $LF
echo "" | tee -a $LF
echo "setenv SUBJECTS_DIR $SUBJECTS_DIR" | tee -a $LF
echo "cd `pwd`"  | tee -a $LF
echo $0 $inputargs | tee -a $LF
echo "" | tee -a $LF
cat $FREESURFER_HOME/build-stamp.txt | tee -a $LF
echo $VERSION | tee -a $LF
uname -a  | tee -a $LF
if($?PBS_JOBID) then
  echo "pbsjob $PBS_JOBID"  >> $LF
endif

#========================================================

# If input volume is not isotropic, then there will be downstream failures.
# Need to resolve the failures or conform-to-min from the start.

set mdir = $SUBJECTS_DIR/$subject/mri

# Copy the first input. As it is set up now, the input to samseg
# as been the orig.mgz (ie, it has been conformed). Eventually will
# probably want to have arbitrary inputs and will have to conform it.
set cmd = (mri_convert $input $mdir/orig.mgz)
echo $cmd | tee -a $LF
set ud = `UpdateNeeded $mdir/orig.mgz $input`
if($ud || $ForceUpdate) then
  $cmd | tee -a $LF
  if($status) exit 1;
else
  echo "$mdir/orig.mgz does not need updating"  | tee -a $LF
endif

# create a link from the orig.mgz to rawavg.mgz, needed for pctsurfcon
pushd $mdir
set cmd = (ln -sf orig.mgz rawavg.mgz)
echo $cmd | tee -a $LF
$cmd | tee -a $LF
if($status) goto error_exit;
popd

#set segvol = `find "$samsegdir" -iname "*crispSegmentation.nii" -print`
set segvol = $samsegdir/seg.mgz

echo $segvol
ls $segvol

set ud = `UpdateNeeded $mdir/aseg.auto_noCCseg.mgz $segvol`
if($ud || $ForceUpdate) then
  if(! $KeepECS) then
    # Remove extra cerebral stuff from samseg seg
    # --uchar has no effect here because --replaceonly copies type of input (float)
    set cmd = (mri_binarize  --i $segvol \
      --replaceonly 258 0 --replaceonly 259 0 --replaceonly 165 0 \
      --o $mdir/aseg.auto_noCCseg.mgz)
    echo $cmd | tee -a $LF
    fs_time $cmd | tee -a $LF
    if($status) goto error_exit;
    # The mri_binarize output will be float, so change to int or uchar (don't do this)
    # set cmd = (mri_convert $mdir/aseg.auto_noCCseg.mgz $mdir/aseg.auto_noCCseg.mgz)
    # if($DoUchar) then 
    #   set cmd = ($cmd  -odt uchar --no_scale 1)
    # else
    #   set cmd = ($cmd  -odt int --no_scale 1)
    # endif
  else
    echo "Keeping Extracerebral segs" | tee -a $LF
    # odt must be int because extracerebral seg ids are > 255
    set cmd = (mri_convert $segvol $mdir/aseg.auto_noCCseg.mgz -odt int --no_scale 1)
    echo $cmd | tee -a $LF
    fs_time $cmd | tee -a $LF
    if($status) goto error_exit;
  endif
else
  echo "$mdir/aseg.auto_noCCseg.mgz does not need updating" | tee -a $LF
endif

# Link the samseg talairach to the talairach.lta
pushd $mdir/transforms
set cmd = (ln -sf $samsegtaldir/samseg.talairach.lta talairach.lta)
echo $cmd | tee -a $LF
echo cd `pwd` | tee -a $LF
$cmd | tee -a $LF
if($status) goto error_exit;
# Create a talairach.xfm (needed for mris_anatomical stats) 
set cmd = (lta_convert --inlta talairach.lta --outmni talairach.xfm)
echo $cmd | tee -a $LF
$cmd | tee -a $LF
if($status) goto error_exit;
popd

# Link the samseg template.m3z to the talairach.m3z
if(-e $samsegtaldir/template.m3z) then
  pushd $mdir/transforms
  # This is not the same space as the talairach.m3z
  set cmd = (ln -sf $samsegtaldir/template.m3z talairach.m3z)
  echo $cmd | tee -a $LF
  echo cd `pwd` | tee -a $LF
  $cmd | tee -a $LF
endif

# Get basename for bias corrected data. At some point samseg
# should write out stereotypical file names
set vbase = `fname2stem $input`
set vbase = `basename $vbase`
set bc = $samsegdir/mode01_bias_corrected.mgz

# Need to rescale bias corrected such that WM is 110
# Get mask of WM, eroded
set wm = $tmpdir/wm.mask.mgh
set cmd = (mri_binarize --i $segvol --match 2 41 --erode 2 --o $wm)
echo $cmd | tee -a $LF
set ud = `UpdateNeeded $wm $segvol`
if($ud || $ForceUpdate) then
  fs_time $cmd | tee -a $LF
  if($status) goto error_exit;
else
  echo "$wm does not need updating" | tee -a $LF
endif
# Compute mean in the eroded WM mask
set wmmeanfile = $tmpdir/wm.mask.mean.dat
set cmd = (mri_segstats --id 1 --i $bc --seg $wm --avgwf $wmmeanfile)
echo $cmd | tee -a $LF
set ud = `UpdateNeeded $wmmeanfile $bc $wm`
if($ud || $ForceUpdate) then
  fs_time $cmd | tee -a $LF
  if($status) goto error_exit;
else
  echo "$wmmeanfile does not need updating" | tee -a $LF
endif
set wmmean = `cat $wmmeanfile`
set scale = `echo 110/$wmmean | bc -l`
echo T1w wmmean $wmmean | tee -a $LF
echo T1w scale $scale | tee -a $LF
# Now rescale the BC so that WM is 110, save as nu
set nu = $mdir/nu.mgz
set cmd = (mri_concat $bc --mul $scale --o $nu)
echo $cmd | tee -a $LF
set ud = `UpdateNeeded $nu $bc $wmmeanfile`
if($ud || $ForceUpdate) then
  fs_time $cmd | tee -a $LF
  if($status) goto error_exit;
else
  echo "$nu does not need updating" | tee -a $LF
endif

# Remove extracerebral stuff
# Could just use aseg created above
set mask = $tmpdir/mask.mgh
set cmd = (mri_binarize --i $segvol --match 0 165 258 259 --inv --o $mask)
echo $cmd | tee -a $LF
set ud = `UpdateNeeded $mask $segvol`
if($ud || $ForceUpdate) then
  fs_time $cmd | tee -a $LF
  if($status) goto error_exit;
else
  echo "$mask does not need updating" | tee -a $LF
endif

# Create the norm by just masking nu with the aseg
set cmd = (mri_mask $nu $mask $mdir/norm.mgz)
echo $cmd | tee -a $LF
set ud = `UpdateNeeded $mdir/norm.mgz $nu $mask`
if($ud || $ForceUpdate) then
  fs_time $cmd | tee -a $LF
  if($status) goto error_exit;
else
  echo "$mdir/norm.mgz does not need updating" | tee -a $LF
endif
# link norm to brainmask
pushd $mdir
set cmd = (ln -sf norm.mgz brainmask.mgz)
echo $cmd | tee -a $LF
$cmd | tee -a $LF
if($status) goto error_exit;
popd

echo $modenamelist | tee -a $LF
@ nthmode = 0
foreach mode ($modenamelist)
  @ nthmode = $nthmode + 1
  set nthmodestr = `printf %02d $nthmode`
  echo "$mode `date` ------------- "   | tee -a $LF
  if($mode == t1w) continue
  set modebase = $mode
  if($mode == t2w)   set modebase = T2
  if($mode == flair) set modebase = FLAIR
  set cmd = (mri_convert $coregdir/$mode/$mode.mgz $mdir/$modebase.prenorm.mgz)
  echo $cmd | tee -a $LF
  fs_time $cmd | tee -a $LF
  if($status) goto error_exit;
  # Compute mean $modebase in the eroded WM mask
  set bc = $samsegdir/mode$nthmodestr"_bias_corrected".mgz
  set wmmeanfile = $tmpdir/wm.mask.mean.$modebase.dat
  set cmd = (mri_segstats --id 1 --i $bc --seg $wm --avgwf $wmmeanfile)
  echo $cmd | tee -a $LF
  fs_time $cmd | tee -a $LF
  if($status) goto error_exit;
  set wmmean = `cat $wmmeanfile`
  set scale = `echo 110/$wmmean | bc -l`
  echo $modebase wmmean $wmmean | tee -a $LF
  echo $modebase scale $scale | tee -a $LF
  # Now rescale the BC so that WM is 110, 
  set cmd = (mri_concat $bc --mul $scale --o $mdir/$modebase.norm.mgz)
  echo $cmd | tee -a $LF
  fs_time $cmd | tee -a $LF
  if($status) goto error_exit;
  # Now mask 
  set cmd = (mri_mask $mdir/$modebase.norm.mgz $mask $mdir/$modebase.mgz)
  echo $cmd | tee -a $LF
  fs_time $cmd | tee -a $LF
  if($status) goto error_exit;
end

# T2/FLAIR
# orig/t2fraw.mgz
# Below is created with -T2pial  and  -FLAIRpial
#   transforms/t2fraw.lta (for long)
#   t2f.prenorm.mgz
#   t2f.norm.mgz
#   T2.mgz/FLAIR.mgz - masked by brain.finalsurfs.mgz
#   - this is used by mri_make_surfaces
#   if T2.mgz/FLAIR.mgz exist, then prenorm and norm are not created
#    but masking is still done 
#   T2/FL.mgz does not have to be resampled if the header is changed
#     - but takes some more effort to apply the samseg BC to the
#       raw input
#     - for now, it should be possible to create a {T2,FLAIR}.norm.mgz
#       and {T2,FLAIR}.mgz files in the same space as the t1w. When
#       recon-all is run with the above flags, the T2.mgz file will be
#       created from the T2.norm.mgz by masking

# Note: crispSegmentation.nii is type float.  Might have to change
# seg to uchar or int or else things might break downstream. Or
# downstream might not break. 


# Create aseg.auto.mgz This needs to be run separately, outside of
# recon-all, because in recon-all it is done as part of ca_label
pushd $mdir
if($DoCC) then
  set tmpfile = $tmpdir/isconformed
  mri_info --o $tmpfile --conformed-to-min aseg.auto_noCCseg.mgz > /dev/null
  set isconf = `cat $tmpfile | head -n 1`
  echo IsConformed $isconf | tee -a $LF
  if($isconf == yes) then
    set cmd = (mri_cc -aseg aseg.auto_noCCseg.mgz -o aseg.auto.mgz -lta transforms/cc_up.lta $subject)
    date | tee -a $LF
    echo $cmd | tee -a $LF
    fs_time $cmd | tee -a $LF
    if($status) goto error_exit;
    date | tee -a $LF
  else
    # If it is not conformed, have to go through some gymnastics because mri_cc
    # needs things to be conformed
    # Conform the norm and aseg
    set cmd = (mri_convert norm.mgz norm.conf.mgz --conform_min)
    echo $cmd | tee -a $LF
    fs_time $cmd | tee -a $LF
    if($status) goto error_exit;
    set cmd = (mri_label2vol --seg aseg.auto_noCCseg.mgz --temp norm.conf.mgz --regheader --o aseg.auto_noCCseg.conf.mgz)
    echo $cmd | tee -a $LF
    fs_time $cmd | tee -a $LF
    if($status) goto error_exit;
    # Now run mri_cc. Not sure if cc_up.lta will be valid (or needed)
    set cmd = (mri_cc -norm norm.conf.mgz -aseg aseg.auto_noCCseg.conf.mgz \
      -o aseg.auto.conf.mgz -lta transforms/cc_up.lta $subject)
    echo $cmd | tee -a $LF
    fs_time $cmd | tee -a $LF
    if($status) goto error_exit;
    # Now map it back to the non-conformed space
    set cmd = (mri_label2vol --seg aseg.auto.conf.mgz --temp norm.mgz --regheader --o aseg.auto.noconf.mgz)
    echo $cmd | tee -a $LF
    fs_time $cmd | tee -a $LF
    if($status) goto error_exit;
    # Merge the CC labels back into the original aseg. Do not use the non-conf volume above because
    # moving into and out of 1mm space may create a problem
    foreach segid (251 252 253 254 255)
      set cmd = (mri_binarize --i aseg.auto.noconf.mgz --match $segid --o $tmpdir/ccbin.mgh)
      echo $cmd | tee -a $LF
      fs_time $cmd | tee -a $LF
      if($status) goto error_exit;
      if($segid == 251) then
        set src = aseg.auto_noCCseg.mgz
      else
        set src = aseg.auto.mgz
      endif
      set cmd = (mergeseg --src $src --merge $tmpdir/ccbin.mgh  --o aseg.auto.mgz --segid $segid)
      set cmd = ($cmd --tmpdir $tmpdir/tmp.mergeseg)
      if($cleanup) set cmd = ($cmd --cleanup)
      echo $cmd | tee -a $LF
      fs_time $cmd | tee -a $LF
      if($status) goto error_exit;
    end
  endif
else
  echo "Not segmenting CC" | tee -a $LF
  # will the cc_up.lta be needed at some point?
  set cmd = (cp aseg.auto_noCCseg.mgz aseg.auto.mgz)
  date | tee -a $LF
  echo $cmd | tee -a $LF
  fs_time $cmd | tee -a $LF
  if($status) goto error_exit;
  date | tee -a $LF
endif
popd

if($DoNormalization2) then
  # Mimic normalization2 by just linking norm.mgz to brain.mgz In
  # recon-all it is created by mri_normalize. This is important because
  # brain.finalsurfs.mgz is created from brain.mgz, and the surfaces
  # are built from brain.finalsurfs.mgz.
  pushd $mdir
  rm -f brain.mgz
  ln -fs norm.mgz brain.mgz
  popd
endif

if($DoFill) then
  # Use the samseg aseg.auto.mgz to create the filled.mgz
  # Should be able to not run pretess in recon-all (except can't turn it off)
  pushd $mdir
  set cmd = (seg2filled --seg aseg.auto.mgz --norm norm.mgz --o filled.mgz)
  set cmd = ($cmd --tmpdir $SUBJECTS_DIR/$subject/tmp/tmp.mergeseg) # might fill /scratch
  if($cleanup) set cmd = ($cmd --cleanup)
  date | tee -a $LF
  echo $cmd | tee -a $LF
  fs_time $cmd | tee -a $LF
  if($status) goto error_exit;
  date | tee -a $LF
  # If creating the filled.mgz directly, then there is no need to run
  # mri_segments, etc, which creates wm.mgz. However, wm.mgz is used
  # by the topology fixer, so need something there. So just mask the
  # norm.mgz with the filled.mgz. Might be something better to do.
  set cmd = (mri_mask norm.mgz filled.mgz wm.mgz)
  date | tee -a $LF
  echo $cmd | tee -a $LF
  fs_time $cmd | tee -a $LF
  if($status) goto error_exit;
  date | tee -a $LF
  popd
endif

# Cleanup
if($cleanup) rm -rf $tmpdir

# Done
echo " " |& tee -a $LF
set tSecEnd = `date '+%s'`;
@ tSecRun = $tSecEnd - $tSecStart;
set tRunHours = `echo $tSecRun/3600|bc -l`
set tRunHours = `printf %5.2f $tRunHours`
echo "Started at $StartTime " |& tee -a $LF
echo "Ended   at `date`" |& tee -a $LF
echo "Samseg2recon-Run-Time-Sec $tSecRun" |& tee -a $LF
echo "Samseg2recon-Run-Time-Hours $tRunHours" |& tee -a $LF
echo " " |& tee -a $LF
echo "samseg2recon Done" |& tee -a $LF
exit 0

###############################################

############--------------##################
error_exit:
echo "ERROR: $cmd"
exit 1;
###############################################

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

  set flag = $argv[1]; shift;
  
  switch($flag)

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

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

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

    case "--long":
      if($#argv < 1) goto arg1err;
      set LongTP = $argv[1]; shift;
      # Check for and remove leading 0 in LongTP, otherwise printf may think it is hex
      set a = `echo $LongTP | cut -c1`
      if($a == 0) set LongTP = `echo $LongTP | cut -c2-`
      # Do it a second time, just in case, there's another leading 0
      set a = `echo $LongTP | cut -c1`
      if($a == 0) set LongTP = `echo $LongTP | cut -c2-`
      set LongTPStr = `printf %03d $LongTP`
      breaksw

    case "--base":
      set DoLongBase = 1;
      breaksw

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

    case "--nolog":
    case "--no-log":
      set LF = /dev/null
      breaksw

    case "--fill":
      set DoFill = 1;
      breaksw
    case "--no-fill":
      set DoFill = 0;
      breaksw

    case "--normalization2":
      set DoNormalization2 = 1;
      breaksw
    case "--nonormalization2":
      set DoNormalization2 = 0;
      breaksw

    case "--dice":
      if($#argv < 1) goto arg1err;
      set diceseg = $argv[1]; shift;
      set computedice = 1;
      breaksw

    case "--uchar":
      set DoUchar = 1;
      breaksw
    case "--no-uchar":
      set DoUchar = 0;
      breaksw

    case "--keep-exc":
      # keep extraceregral segmentations
      set KeepECS = 1;
      breaksw
    case "--no-keep-exc":
      # keep extraceregral segmentations
      set KeepECS = 0;
      breaksw

    case "--no-cc":
      set DoCC = 0;
      breaksw

    case "--cc":
      set DoCC = 1;
      breaksw

    case "--no-force-update":
      set ForceUpdate = 0;
      breaksw
    case "--force-update":
      set ForceUpdate = 1;
      breaksw

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

    case "--nocleanup":
      set cleanup = 0;
      breaksw

    case "--cleanup":
      set cleanup = 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($#subject == 0) then
  echo "ERROR: must supply subject"
  exit 1;
endif
if($#samsegdir == 0) set samsegdir = $SUBJECTS_DIR/$subject/mri/samseg
if(! -e $samsegdir) then
  echo "ERROR: cannot find $samsegdir"
  exit 1;
endif
# Get full path to samsegdir (good for links)
pushd $samsegdir > /dev/null
set samsegdir = `pwd`
popd  > /dev/null

if(! $#LongTP && ! $DoLongBase) then
  set coregdir = $SUBJECTS_DIR/$subject/coreg
  if(! -e $coregdir) then
    set coregdir = $samsegdir/coreg
    if(! -e $coregdir) then
      echo "ERROR: cannot find coreg dir"
      exit 1;
    endif
  endif
  set input = $coregdir/t1w/t1w.mgz
  set modenamelist = (`cat $coregdir/modenames.txt`)
  set ok = 0;
  foreach mode ($modenamelist)
    if($mode == t1w) set ok = 1;
    if($mode == t2w)   set HaveT2w = 1;
    if($mode == flair) set HaveFLAIR = 1;
  end
  if(! $ok) then
    echo "ERROR: cannot find t1w in mode list $modenamelist"
    exit 1;
  endif
  set samsegtaldir  = $samsegdir
else
  set modenamelist = (t1w)
  set samsegbasedir = $samsegdir/base
  set samsegtaldir  = $samsegbasedir
  if($DoLongBase) then
    set input = $samsegdir/mc/mctemplate.mgz
    set samsegdir = $samsegbasedir
  else
    set samseglongdir = $samsegdir/tp$LongTPStr
    if($LongUseMC) then
      set input = $samsegdir/mc/mc.$LongTPStr.mgz
    else
      set input = $samsegdir/mc/input_$LongTPStr.mgz
    endif
    set samsegdir = $samseglongdir
  endif
endif

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

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

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

############--------------##################
usage_exit:
  echo ""
  echo "samseg2recon"
  echo "  --s subject"
  echo "  --samseg samsegdir : ouput dir when samseg or samseg-long was run"
  echo "      if not supplied, then assumes subject/mri/samseg"
  echo "  --no-cc : do not do corpus callosum segmentation"
  echo "  --fill : use samseg to create the filled.mgz used for tessellation (--no-fill)"
  echo "  --normalization2 : seg brain.mgz to norm.mgz "
  echo "  --uchar : convert to uchar"
  echo "  --no-keep-exc : do not keep extracerebral segmentations"
  echo "  --long tpNumber : process TP, will find folder called tpXXX in samsegdir"
  echo "  --base : process base, will find folder called base in samsegdir"
  echo ""

  if(! $PrintHelp) exit 1;
  echo $VERSION
  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

Create and populate a subjects dir in a way that recon-all can be run
on it. This is a work in progress as it assumes that the input to samseg
is T1 and has been conformed.

For longitudinal analysis
Run samseg-long to create the samseglongdir; this will have a base
folder and time point folders called tp001, tp002, etc

Create a base subject by transfering all the base samseg analysis to the
recon-all base subject. The name of the base subject can be anything:
   samseg2recon --base --s subject_base  --samseg samseglongdir
Now run recon-all on the base
   recon-all -s subject_base -autorecon2-samseg -autorecon3

Create a subject for each time point by transfering all the samseg analysis 
from that TP to the subject for that TP. The subject name can be anything
   samseg2recon --long 2 --s long.tp002  --samseg samseglongdir
Then run
   recon-all -long-samseg subject_base long.tp002 -autorecon2-samseg -autorecon3


