#!/bin/bash # ren-dcmcnv.sh # Version 1.2.1 # 09-Oct-2017 K.Nemoto # Usage: ren-dcncnv.sh [-d] # -d: option not to delete temporary files generated by dcm2nii # DICOM to NIFTI converter with rename functions # This script converts DICOM files into NIFTI mormat using dcm2nii. # Not only converting DICOM images, it renmaes nifti files # based on header information. # # All you need to prepare is make directories with subjects ID # e.g. If you prepare a directory "subj01", the following files # will be the following; # Volume files: V_subj01.nii # fMRI files: F_subj01.nii # DTI files: D_subj01.nii # # Prerequisites: You need to install dcm2nii (included in MRIcron) # and FSL beforehand and set paths to these. # Acknowledgement: This work was funded by ImPACT Program of Council # for Science, Technology and Innovation (Cabinet Office, Government # of Japan). # Debug option getopts "d" debug if [ $debug == "d" ]; then echo "debug option is enabled." echo " " else echo "debug option is disabled. (Default)" echo " " fi #Generate a log cnvdate=$(date +%Y%m%d-%H%M%S) touch ${cnvdate}_dcmcnv.log log=${cnvdate}_dcmcnv.log exec &> >(tee -a "$log") #Check dcm2nii path echo "Check if the path for dcm2nii is set." dcm2nii_path=$(which dcm2nii) if [ "$dcm2nii_path" = "" ]; then echo "Error: Please set path for dcm2nii!" exit 1 else echo "Path for dcm2nii is $dcm2nii_path" echo " " fi #Check FSL path echo "Check if the path for FSL is set." fsl_path=$(which fsl) if [ "$fsl_path" = "" ]; then echo "Error: Please set path for FSL!" exit 1 else echo "Path for FSL is $fsl_path" echo " " fi #Set parent directory modir=$(pwd) #Prepare 'DICOM' directory if [ ! -e $modir/DICOM ]; then mkdir -p $modir/DICOM fi #Prepare 'nifti' directory if [ ! -e $modir/nifti ]; then mkdir -p $modir/nifti fi #Move all directories except for DICOM and nifti to DICOM directory find . -maxdepth 1 \( -name DICOM -o -name nifti \) -prune -o \ -type d -print | grep / | sed -e 's@./@@' -e 's/ /\n/' | \ while read line; do mv $line DICOM; done #cd to DICOM directory cd $modir/DICOM for dir in $(ls -F | grep / | sed 's@/@@') do echo "Begin conversion of $dir" echo "$(date +%F_%T)" #dcm2nii echo "dcm2nii for $dir" dcm2nii -g n -r n -x n $dir echo " " #cd to $dir cd $dir #(for some scanners only) #Delete WIPDKI wipdki=$(find . -maxdepth 1 -name '*WIPDKI*') if [ ! -z "$wipdki" ]; then echo "remove WIPDKI sequence" echo " " rm *WIPDKI* fi #Acquire dimension of images for f in *.nii do dim1=$(fslinfo $f | grep ^dim1 | awk '{ print $2 }') dim2=$(fslinfo $f | grep ^dim2 | awk '{ print $2 }') dim3=$(fslinfo $f | grep ^dim3 | awk '{ print $2 }') dim4=$(fslinfo $f | grep ^dim4 | awk '{ print $2 }') te=$(fslhd $f | grep ^descrip |\ awk '{ print $2 }' | awk -F';' '{ print $1 }'|\ sed 's/TE=//' | sed 's/\.[0-9]*$//') pe=$(fslhd $f | grep ^descrip |\ awk '{ print $2 }' | awk -F';' '{ print $3 }'|\ sed 's/phaseDir=//') echo "========== Rename the file based on header info ==========" echo "Dimensions of $f is $dim1, $dim2, $dim3, and $dim4" echo "TE=${te}; phaseDir=$pe" #Decide if a nifti file is 3DT1, fMRI, or DTI. #Rules are as follows; #3DT1: dim2>=240, dim3>100, and TE<6 #fMRI: dim4>=130 and 20=8 and 50