Dicom dir to analyze

From Wiki
Jump to navigation Jump to search
#!/bin/bash

usage () {
	echo
	echo "Usage:"
	echo "	`basename $0` <dicom dir>"
	echo "	This script searches through <dicom dir> for DICOM files"
	echo "	(e.g. 00001.dcm).  It creates a new directory called"
	echo "	<dicom dir>-analyze and places converted Analyze files in"
	echo "	corresponding locations there."
	echo
	exit 1
}

if [[ ! -x `which dicom2analyze` ]]; then
	echo "ERROR: can't find dicom2analyze binary."
    exit 1
fi

# check number of arguments
if (( $# != 1)); then
	usage
fi

# check dicom_dir
dicom_dir_input=$1
if [[ ! -d $dicom_dir_input ]]; then
	echo
	echo "	ERROR: $dicom_dir_input is not a valid directory"
	usage
fi

# move to parent directory
cd $dicom_dir_input/..
dicom_dir=`basename $dicom_dir_input`
analyze_dir="${dicom_dir}-analyze"

# create parallel directory structure
for dicom_subdir in `find $dicom_dir -name *.dcm -exec dirname {} \; | sort -u`
do
	newdir=${analyze_dir}${dicom_subdir#"$dicom_dir"}
	mkdir -p $newdir
	if (( $? != 0 )); then
		echo
		echo "ERROR: failed to create $newdir."
		echo "Do you have write permissions here?"
		usage
	fi
done

# create analyze files
for dicom_file in `find $dicom_dir -name *.dcm -print`
do
	analyze_prefix=${analyze_dir}${dicom_file#"$dicom_dir"}
	`dicom2analyze $dicom_file $analyze_prefix`
	if (( $? != 0 )); then
		echo
		echo "ERROR: failed to convert $dicom_file"
		echo "Please contact the system administrator."
		echo
		exit 1
	fi
done

exit 0