#! /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: 2007/05/11 19:33:27 $
#    $Revision: 1.12 $
#
# 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.12 2007/05/11 19:33:27 greve Exp $';

if($#argv != 1 && $#argv != 2) then
  echo "dcmdir-info-mgh dicomdir"
  echo "dcmdir-info-mgh dicomdir unpackdir"
  echo "dcmdir-info-mgh -help"
  exit 1;
endif

if("$argv[1]" == "-help") then
  echo ""
  echo "dcmdir-info-mgh dicomdir <unpackdir>"
  echo ""
  echo $VERSION
  echo ""
  echo "Scans a DICOM directory and extracts information about each series."
  echo "The files in the directory must conform to the following naming"
  echo "convention:"
  echo ""
  echo "        NNNNNN-S-MMMMM.dcm"
  echo ""
  echo "where NNNNNN and MMMMM are any strings of any length as long as thay"
  echo "do not contain dashes (-). S is the series/run number. This is the"
  echo "naming convention of the data at MGH after being pushed from the scanner"
  echo "to the network archive. This function will not work on data burned to"
  echo "CD from the scanner.  Example: 953000-2-9.dcm"
  echo ""
  echo "This function is considerably faster than using unpacksdcmdir with"
  echo "the -scanonly option, but it does require that the files be named"
  echo "appropriately."
  echo ""
  echo "The output is a list of series/runs with the pulse sequence, "
  echo "protocol, and file name from the series. The Patient Name, Study Date,"
  echo "and Study Time are echo also printed out."
  echo ""
  echo "If an unpackdir is given, the the dicom files are converted to mgz "
  echo "format with the following name: sequencename_runR.mgz, where R is the "
  echo "run number. "
  echo ""
  exit 1;
endif


set dcmdir = $argv[1];
if(! -e $dcmdir) then
  echo "ERROR: cannot find $dcmdir"
  exit 1;
endif

if($#argv == 2) then
  set unpackdir = $argv[2];
  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
else
  set unpackdir = ();
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];
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)
  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'`;
  #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
