#! /bin/tcsh -f # # dcmdir-info-mgh # # # # Copyright © 2021 The General Hospital Corporation (Boston, MA) "MGH" # # Terms and conditions for use, reproduction, distribution and contribution # are found in the 'FreeSurfer Software License Agreement' contained # in the file 'LICENSE' found in the FreeSurfer distribution, and here: # # https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense # # Reporting: freesurfer@nmr.mgh.harvard.edu # # set VERSION = 'dcmdir-info-mgh dev'; 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 source $FREESURFER_HOME/sources.csh 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 " 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.