The second script is similar to the first except it rotates the gradient vectors based on rotations for each gradient volume in the corresponding dataset. In other words, the 3dvolreg or 3dWarpDrive was used to align the whole DWI dataset.
#!/bin/tcsh
# @rotatevectors
#
# rotate vectors from tensor.1D based on volume registered dataset, r1_vr+orig
#
# use the matrix vector rotation fields from the header attributes
# if 3dvolreg was used, they will be named VOLREG_MATVEC_nnnnnn
# if 3dWarpDrive was used, they will be named WARPDRIVE_MATVEC_FOR_nnnnnn
if ("$1" == '' || "$1" == '-help' || "$1" == '-h') then
goto HELP
endif
if ("$#" < 2) then
echo "Not enough parameters"
goto END
endif
set dset = $1
set tensor1D = $2
set ngrads = `3dnvals $dset`
@ ngrads = $ngrads - 1
echo "Dataset: $dset"
echo "Gradient vectors: $tensor1D"
echo "Rotating vectors..."
rm -f rotatedgrads.1D
foreach grad ( `count -digits 6 1 $ngrads`)
# put the matrix vector on three rows to be compatible with Vecwarp
( cat_matvec "$dset"'::VOLREG_MATVEC_'${grad} > tempmatvec.1D ) > & /dev/null
# replace the translation vector with 0s
1dcat tempmatvec.1D'[0..2]' '1D:3@0' > tempmatvec0.1D
# if there are no attributes from 3dvolreg in header, try 3dWarpDrive rotations
set check = `wc tempmatvec.1D`
if ($check[1] == 0) then
( cat_matvec "$dset"'::WARPDRIVE_ROTMAT_FOR_'${grad} > tempmatvec0.1D ) > & /dev/null
set check = `wc tempmatvec0.1D`
if ($check[1] == 0) then
echo "No rotation matrix in header"
goto END
endif
endif
# get the original vector for the current gradient
# (no zero row, so subtract 1)
@ gradrow = $grad - 1
1dcat "$tensor1D{$gradrow}" > tempvector.1D
# rotate the vector for the current gradient
Vecwarp -matvec tempmatvec0.1D -backward -input tempvector.1D -force -output temprotgrad.1D > & /dev/null
# |& tee > /dev/null
# append the rotated gradients to a single file
cat temprotgrad.1D >> rotatedgrads.1D
rm temprotgrad.1D
rm tempmatvec*.1D
rm tempvector.1D
end
echo "Output gradient vector file is rotatedgrads.1D"
goto END
HELP:
echo "Usage: `basename $0` volreg_dset gradients.1D"
echo "Rotate DTI gradient vectors based on rotations from volume registration"
echo "Current script assumes 3dvolreg or 3dWarpDrive corrected dataset with"
echo "preference to 3dvolreg rotation parameters"
echo "Output file is rotatedgrads.1D"
echo ""
echo "Daniel Glen SSCC/NIMH/NIH"
echo ""
goto END
END: