#! /bin/tcsh -f

#
# dcmdir-info-mgh
#
# REPLACE_WITH_ONE_LINE_SHORT_DESCRIPTION
#
# Original Author: REPLACE_WITH_FULL_NAME_OF_CREATING_AUTHOR
# CVS Revision Info:
#    $Author: greve $
#    $Date: 2008/08/26 22:11:27 $
#    $Revision: 1.14 $
#
# 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: dcmdir-info-mgh,v 1.14 2008/08/26 22:11:27 greve Exp $';

set dcmdir = ();
set unpackdir = ();
set UsePre = 1;

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(! -e $dcmdir) then
  echo "ERROR: cannot find $dcmdir"
  exit 1;
endif

if($#unpackdir) then
  mkdir -p $unpackdir
  set LF = $unpackdir/dcmdir-info-mgh.log
  if(-e $LF) mv $LF $LF.bak
  pwd  | tee -a $LF
  echo $argv | tee -a $LF
  date | tee -a $LF
  pushd $unpackdir > /dev/null
  set unpackdir = `pwd`;
  popd > /dev/null
endif

pushd $dcmdir > /dev/null

# Get a list of the series numbers
# find -- hopefully only get files that end in .dcm (cannot do ls *.dcm)
# sed  -- replace dashes with spaces
# awk  -- print the 2nd to last field, should be series number
# sort -- sort series numbers numerically
# uniq -- keep only unique series numbers
#set serlist = (`find $dcmdir -name '*.dcm' | sed 's/-/ /g' | awk '{a=NF-1;print $a}' | sort -n | uniq`);
set serlist = (`find . -name '*.dcm' | sed 's/-/ /g' | awk '{a=NF-1;print $a}' | sort -n | uniq`);
if($#serlist == 0) then
  pwd
  echo "ERROR: finding data"
  exit 1;
endif

set series = $serlist[1];
if($UsePre)   set flist = (`ls *-$series-*.dcm`)
if(! $UsePre) set flist = (`ls $series-*.dcm`)
set dcm0 = $flist[1];
set Patient = `mri_probedicom --i $dcm0 | grep PatientName`;
set Date    = `mri_probedicom --i $dcm0 | grep StudyDate`;
set Time    = `mri_probedicom --i $dcm0 | grep StudyTime`;
echo $Patient
echo $Date
echo $Time

foreach series ($serlist)
  if($UsePre)   set flist = (`ls *-$series-*.dcm`)
  if(! $UsePre) set flist = (`ls $series-*.dcm`)
  set dcm = $flist[1];
  #echo $dcm0

  set PulSeq  = `mri_probedicom --i $dcm | grep PulseSequence | awk '{print $2}' | sed 's/ //g'| sed 's/*//g'| sed 's/?//g'`;
#  set PulSeq  = `mri_probedicom --i $dcm | grep PulseSequence | awk '{print $2}'`;
#  set PusSeq  = `echo "$PulSeq" | sed 's/ //g'| sed 's/*//g'| sed 's/?//g'`;

  #set Protocol = `mri_probedicom --i $dcm | grep ProtocolName | awk '{print $3}'`;
  set Protocol = `mri_probedicom --i $dcm --t 18 1030| sed 's/ //g'`;
  echo "$series $PulSeq $Protocol `basename $dcm`"

  if($#unpackdir != 0) then
    set outfile = $unpackdir/$Protocol"_run"$series.mgz
    set cmd = (mri_convert $dcm $outfile)
    echo "#@#-------------------------------------------- " |& tee -a $LF
    echo $cmd |& tee -a $LF
    $cmd |& tee -a $LF
    if($status) exit 1;
  endif

end

exit 0
###############################################

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

  set flag = $argv[1]; shift;

  switch($flag)

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

    case "--nopre":
    case "-nopre":
      set UsePre = 0;
      breaksw

    default:
      if($#dcmdir == 0) then
        set dcmdir = $flag;
        echo "dcmdir $dcmdir"
        breaksw;
      endif
      if($#unpackdir == 0) then
        set unpackdir = $flag;
        echo "unpackdir $unpackdir"
        breaksw;
      endif
      echo ERROR: Flag $flag unrecognized.
      echo $cmdline
      exit 1
  endsw

end

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

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

  if($#dcmdir == 0) then
    echo "ERROR: must spec a dcmdir"
    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 "USAGE: dcmdir-info-mgh dicomdir <unpackdir>"
  echo ""
  echo "   --version : print version and exit"
  echo "   --help    : print help and exit"
  echo "   --nopre   : do not assume NNNNNN-"
  echo ""

  if($PrintHelp) \
  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

Scans a DICOM directory and extracts information about each series.
The files in the directory must conform to the following naming
convention:

        NNNNNN-S-MMMMM.dcm

where NNNNNN and MMMMM are any strings of any length as long as thay
do not contain dashes (-). S is the series/run number. This is the
naming convention of the data at MGH after being pushed from the scanner
to the network archive. This function will not work on data burned to
CD from the scanner.  Example: 953000-2-9.dcm

This function is considerably faster than using unpacksdcmdir with
the -scanonly option, but it does require that the files be named
appropriately.

The output is a list of series/runs with the pulse sequence, 
protocol, and file name from the series. The Patient Name, Study Date,
and Study Time are echo also printed out.

If an unpackdir is given, the the dicom files are converted to mgz 
format with the following name: sequencename_runR.mgz, where R is the 
run number. 
