#! /bin/csh -f

#
# bfileconvert
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/09 22:41:16 $
#    $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 VERSION = '$Id: bfileconvert,v 1.5 2007/01/09 22:41:16 nicks Exp $'
set cmdargs = ($argv);

set invol   = ();
set outvol  = ();
set mfile   = ();
set monly   = 0;
set oddplanes = 0;
set rescaleshort = 0;

## print usage ###
if($#argv == 0) then
  goto usage_exit;
  exit 1;
endif

## print version ##
set n = `echo $argv | grep version | wc -l` 
if($n != 0) then
  echo $VERSION
  exit 0;
endif

echo " "
echo " "

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

if($#mfile == 0) then
  set mfile = /tmp/run_bfileconvert_$$.m
else
  set monly = 1;
endif

if($#outvol != 0) then
  set outdir = `dirname $outvol`;
  mkdir -p $outdir
  pushd $outdir > /dev/null
  set outdir = `pwd`;
  popd  > /dev/null
endif

echo "% Temporary file to run bfileconvert %" > $mfile
if(! $monly) then
   echo "global QuitOnError;"                    >> $mfile
   echo "QuitOnError = 1;"                       >> $mfile
   echo "if (exist('fast_bfileconvert')==0)"     >> $mfile
   echo "  qoe('ERROR: matlab path incorrect');" >> $mfile  
   echo "end;"                                   >> $mfile  
endif

echo -n "fast_bfileconvert(" >> $mfile
set c = "";
foreach arg ($cmdargs)
  echo -n "$c'$arg'"         >> $mfile
  set c = ",";
end
echo ");" >> $mfile

if(! $monly) then
  cat $mfile | matlab -display iconic
  rm $mfile
  # bhdr now handled in fast_bfileconvert
  #set ibhdr = $invol.bhdr
  #set obhdr = $outvol.bhdr
  #if(-e $ibhdr && ! -e $obhdr) cp $ibhdr $obhdr
endif

echo " "
echo " "

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    case "-oddplanes":
      set oddplanes = 1;
      breaksw

    case "-rescale-short":
      set rescaleshort = 1;
      breaksw

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

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

    case "-ln2log10":
      set ln2log10 = 1;
      breaksw

    case "-echo":
      set echo = 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:

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

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

############--------------##################
usage_exit:
  echo "USAGE: bfileconvert"
  echo "Options:";
  echo "   -i input volume"
  echo "   -o output volume"
  echo "   -oext ext : set output extension (default input ext)"
  echo "   -oend endianness : set output endianness (default input )"
  echo " "
  echo "   -fs firstslice (0-based)"
  echo "   -ns nslices"
  echo "   -fr firstrow (0-based)"
  echo "   -nr nrows"
  echo "   -fc firstcol (0-based)"
  echo "   -nc ncols"
  echo "   -fp firstplane (0-based)"
  echo "   -np nplanes"
  echo "   -oddplanes : only copy odd planes"
  echo "   -rescale-short : rescaale intensity to fit in bshort"
  echo " "
  echo "   -umask umask  : set unix file permission mask"
  echo "   -version      : print version and exit"
exit 1;

