#! /bin/tcsh -f

#
# cp-dicom
#
# REPLACE_WITH_ONE_LINE_SHORT_DESCRIPTION
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/06 00:01:13 $
#    $Revision: 1.4 $
#
# 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: cp-dicom,v 1.4 2007/01/06 00:01:13 nicks Exp $';

set inputargs = ($argv);
set dcmdir = ();
set outdir = ();
set PrintHelp = 0;

if($#argv == 0) goto usage_exit;
set n = `echo $argv | egrep -e -help | wc -l`
if($n != 0) then
  set PrintHelp = 1;
  goto usage_exit;
endif
set n = `echo $argv | egrep -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 flist = (`ls $dcmdir`);
echo "Found $#flist"
@ nthfile = 1;
foreach f ($flist)
  set ff = $dcmdir/$f
  set series = `mri_probedicom --i $ff --t 20 11`;
  if(! $status) then
    set protocol = `mri_probedicom --i $ff --t 18 1030`;
    if(! $status) then
      set protocol = `echo $protocol | sed 's/*//g' | sed 's/ //g'`
      set seriesdir = $outdir/$protocol-$series
    else
      set seriesdir = $outdir/$series
    endif
    echo "$nthfile/$#flist   $series   $ff"
    mkdir -p $seriesdir
    cp $ff $seriesdir
  endif
  @ nthfile = $nthfile + 1;
end

echo "done"
exit 0;

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

  set flag = $argv[1]; shift;

  switch($flag)

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

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

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

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

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

    case "-umask":
      if ( $#argv < 1) goto arg1err;
      umask $1; shift;
      breaksw

    default:
      echo ERROR: Flag $flag unrecognized.
      echo $cmdline
      exit 1
      breaksw
  endsw
end

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

############--------------##################
check_params:

if($#dcmdir == 0) then
  echo "ERROR: no dicom dir specified"
  exit 1;
endif

if($#outdir == 0) then
  echo "ERROR: no out dir specified"
  exit 1;
endif

mkdir -p $outdir
if($status) then
  echo "ERROR: creating $outdir"
  exit 1;
endif

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

############--------------##################
usage_exit:
  echo ""
  echo "USAGE: cp-dicom"
  echo ""
  echo "  -d dcmdir";
  echo "  -o outdir";
  echo ""
  echo "  -debug          : print out lots of info"
  echo "  -version        : print version of this script and exit"
  echo "  -help           : yea, like anybody's going to read this"
  echo ""

  if(! $PrintHelp) exit 1;

  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

Copies dicom files into separate directories for each series. The
series number is extracted from the dicom header, so the dicom file
names do not have to conformm to any standard.

Example:

  cp-dicom -d ./dicomdir -o /space/data/mydicom

All the dicom files should be in ./dicomdir.  The results will be in
/space/data/mydicom/protocol-S, where "protocol" is the protocol name
and S is the series number. There will be a separate directory for
each series. The dicom file names will not be changed. If the protocol
cannot be found in the dicom header, then only the series number is
used for the directory name.

The series number is based on DICOM tag 20 11.
The protocol is based on DICOM tag 18 1030.

Note: any spaces or asterices (*) are removed from the protocol name.


