#!/bin/sh # filename: viewercd.sh # # UTIL script: 22-Jun-2005 (eNlf v0.17) # # purpose : rename & stack files # # notes : specific for Philips Gemini PET/CT system # # # vars # PATH=/usr/bin:/usr/sbin:/usr/ucb/bin:/usr/local/bin:/opt/sfw/bin export PATH DCMDICTPATH=/usr/local/dcmtk/lib/dicom.dic export DCMDICTPATH dcmdump=/usr/local/dcmtk/bin/dcmdump spooldir="/home/patient/.viewercd" dicomdir="$spooldir/dicom" volumesdir="$spooldir/volumes" lockfile="$spooldir/.lock" wait=90 # number of 10 sec steps = 15 minutes burndicom=no # burn dicom files on CD: yes|no # # functions # clean_up() { /bin/rm -rf "$dicomdir" /bin/rm -rf "$volumesdir" /bin/rm -rf $spooldir/PETCT_* /bin/rm -f "$lockfile" } # # init # ulimit -n 600 # # go into spooldir cd "$spooldir" # # only one process if [ -r "$lockfile" ]; then exit 0 fi # # we`re running touch "$lockfile" # # insert blank media message -W 35 -D information -A "Insert blank media AFTER all image transfers are DONE. Waiting now for media to burn ViewerCD ..." & # # wait for blank media (max 15 minutes) count=0 while [ ! -n "`/opt/sfw/bin/sudo cdrw -M 2>&1 | grep 'Media is blank'`" ] && [ $count -lt $wait ] do sleep 10 count=`expr $count + 1` done; if [ "$count" -eq $wait ]; then clean_up message -W 35 -D error "No blank media detected. Burning ViewerCD cancelled." & exit 1 fi # # sorted dicom spool mkdir "$dicomdir" # # volumes dir mkdir "$volumesdir" # # get all study subdirs subdirs=`ls -d PETCT_*` if [ -z "$subdirs" ]; then /bin/rm -f "$lockfile" message -W 35 -D error "No spooled files found. Burning ViewerCD cancelled." & exit 0 fi # # give sign of life message -W 35 -D information -A "Processing ViewerCD files ..." & # # rename files & move from subdir + get distinct series ctprev="" ctseries="" ptprev="" ptseries="" for subdir in $subdirs do # # go into subdir cd "$subdir" # # CT files ctfiles=`ls CT*` if [ -n "$ctfiles" ]; then for ctfile in $ctfiles do # # get tags SeriesNumber 20,11 and InstanceNumber 20,13 ctnmbrs=`$dcmdump --load-short --search "0020,0011" --search "0020,0013" --search-first $ctfile | sed 's/.*\[\(.*\)\].*/\1/g' | tr '\012' ' '` # # get slice number (= 1ST entry) ctslice=`echo $ctnmbrs | awk '{ printf("%05d\n",$1) }'` # # get distinct serie (= 2ND entry) ctserie=`echo $ctnmbrs | awk '{ printf("%05d\n",$2) }'` if [ "x$ctprev" != "x$ctserie" ]; then ctprev=$ctserie ctseries="$ctseries $ctserie" fi # # rename and move /bin/mv -f "$ctfile" "$dicomdir/CT$ctserie$ctslice.dcm" #echo $ctfile $ctserie $ctslice done fi # # PT files # ptfiles=`ls PI*` if [ -n "$ptfiles" ]; then for ptfile in $ptfiles do # # get MediaStorageSOPClassUID 0002,003 ptuid=`$dcmdump --load-short --search-first --search "0002,0003" $ptfile | sed 's/.*\[\(.*\)\].*/\1/g'` # # get slicenr from filename ptslice=`echo $ptuid | awk -F. '{ printf("%05d\n",$11) }'` # # get distinct series ptserie=`echo $ptuid | awk -F. '{ printf("%05d\n",$10) }'` if [ "x$ptprev" != "x$ptserie" ]; then ptprev=$ptserie ptseries="$ptseries $ptserie" fi # rename and move /bin/mv -f "$ptfile" "$dicomdir/PT$ptserie$ptslice.dcm" #echo $ptfile $ptserie $ptslice done fi # # go into spooldir cd "$spooldir" # # clean up subdir /bin/rm -rf "$subdir" done # # stack files # # # go into volumesdir cd "$volumesdir" # # CT for ctserie in $ctseries do # # get first file firstfile=`/bin/ls -1 $dicomdir/CT$ctserie*.dcm | head -1` # # get ContrastBolusAgent ctcontrast=`$dcmdump --load-short --search "0018,10" --search-first $firstfile | sed 's/.*\[\(.*\)\].*/\1/g' | awk '{ print $1 }'` # # get PatientPosition ctpatposition=`$dcmdump --load-short --search "0018,5100" --search-first $firstfile | sed 's/.*\[\(.*\)\].*/\1/g' | awk '{ print $1 }'` # # get all slices, properly sorted if [ "x$ctpatposition" = "xHFS" ]; then # HFS = reverse slice order allslices=`/bin/ls $dicomdir/CT$ctserie*.dcm | sort -rn` else # FFS = keep slice order allslices=`/bin/ls $dicomdir/CT$ctserie*.dcm | sort -n` fi # # stack CT medcon -s -one -f $allslices -c intf -o UZG-CT-$ctserie-$ctcontrast -stack3d -noprefix done # # PT for ptserie in $ptseries do # # get first file firstfile=`/bin/ls $dicomdir/PT$ptserie*.dcm | head -1` # # get type of attenuation correction: NAC, AC, CTAC, CTAC_B #ptattn=`strings $firstfile | grep "Attenuation Cor" | sed 's/Attenuation Cor=\(.*\),.*/\1/g'` ptattn=`$dcmdump --load-short --search "7053,1003" --search-first $firstfile | sed 's/.*\[p[0-9]*s[0-9]*_.*_\(.*\)\.img\].*/\1/g' | /usr/xpg4/bin/tr '[:lower:]' '[:upper:]'` # # get all slices, properly sorted allslices=`/bin/ls $dicomdir/PT$ptserie*.dcm | sort -n` # # stack PT medcon -qc -b16 -s -one -f $allslices -c intf -o UZG-PT-$ptserie-$ptattn -stack3d -noprefix done # # go into spooldir cd "$spooldir" # # burn dicom on CD? if [ "x$burndicom" = "xno" ]; then /bin/rm -rf "$dicomdir" fi # # burn cd /usr/local/bin/make-cdrom -c -g "$spooldir" # # clean up clean_up # # leave exit 0