#This script is specifically designed for Enhanced Dicom to NIFTI convert using dcm2nii for data from XNAT. Author: Maya Reiter
#Please refer to this page about what this script is for.
#!/bin/bash
scan=$1
zdim=$2
#files used:
oldHd=/tmp/header$$
newHd=/tmp/NewHeader$$
#get the old header:
fslhd -x ${scan} | grep -v "qform" | grep -v "qto" > $oldHd
cat $oldHd
#get the mistaken number from the old number:
mNum=`cat $oldHd | grep "dz" | cut -c8- | sed "s/'//g"`
#get the other mistaken lines:
msLine=`cat $oldHd | grep "sto_xyz_matrix = '" | cut -c20- | sed
"s/'//g" #| sed 's/ 0 0 0 1//g'`
msLine2=`cat $oldHd | grep "sto_ijk matrix = '" | cut -c20- | sed
"s/'//g" #| sed 's/ 0 0 0 1//g'`
read -a mArray <<<${msLine}
read -a mArray2 <<<${msLine2}
#change the mistakes:
count=0
for i in ${mArray[@]}
do
if [ $count -eq 2 ] || [ $count -eq 6 ] || [ $count -eq 10 ]
then
aNum=`echo ${i}`
mArray[$count]=`bc -l <<< $aNum/$mNum*$zdim` #bash sucks at deviding
fi
count=`expr ${count} + 1`
done
count=0
for i in ${mArray2[@]}
do
if [ $count -eq 2 ] || [ $count -eq 6 ] || [ $count -eq 10 ]
then
aNum=`echo ${i}`
mArray2[$count]=`bc -l <<< $aNum/$mNum*$zdim` #bash sucks at deviding
fi
count=`expr ${count} + 1`
done
#mistake lines:
stoLine=`cat $oldHd | grep "sto_xyz_matrix"`
stoLine2=`cat $oldHd | grep "sto_ijk matrix"`
qtoLine=`cat $oldHd | grep "sto_xyz_matrix"`
qtoLine2=`cat $oldHd | grep "sto_ijk matrix"`
#correct lines (to be corrected):
sMatrixVals="sto_xyz_matrix = '"`echo ${mArray[@]}`"'"
sMatrixVals2="sto_ijk_matrix = '"`echo ${mArray2[@]}`"'"
qMatrixVals="qto_xyz_matrix = '"`echo ${mArray[@]}`"'"
qMatrixVals2="qto_ijk_matrix = '"`echo ${mArray2[@]}`"'"
#rewrite bad header (replace dz and slope values):
dzLine=`cat $oldHd | grep "dz"`
ndzLine=" dz = '"${zdim}"'"
slpLine=`cat $oldHd | grep "scl_slope"`
nslpLine=" scl_slope = '1'"
cat $oldHd | sed "s/$dzLine/$ndzLine/g" > $newHd
cat $newHd | sed "s/$slpLine/$nslpLine/g" > ${newHd}$$
#works till here.
cat ${newHd}$$ | sed "s/$stoLine/$sMatrixVals/g" > ${newHd}
cat ${newHd} | sed "s/$stoLine2/$sMatrixVals2/g" > ${newHd}$$
cat ${newHd}$$ | sed "s/$qtoLine/$qMatrixVals/g" > ${newHd}
cat ${newHd} | sed "s/$qtoLine/$qMatrixVals2/g" > ${newHd}$$
#put the new header on the image:
fslcreatehd ${newHd}$$ ${scan}
#show what we put on the image
fslhd -x $scan
#clean up the mess we made in the process: rm $oldHd
rm ${newHd}*