#! /bin/csh -f # # spmimg2bvolume - convert an spm .img series of files into # a bvolume (ie, bshort or bfloat) # # Original Author: Doug Greve # CVS Revision Info: # $Author: nicks $ # $Date: 2007/01/09 22:41:18 $ # $Revision: 1.2 $ # # Copyright (C) 2002-2007, # The General Hospital Corporation (Boston, MA). # All rights reserved. # # Distribution, usage and copying of this software is covered under the # terms found in the License Agreement file named 'COPYING' found in the # FreeSurfer source code root directory, and duplicated here: # https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferOpenSourceLicense # # General inquiries: freesurfer@nmr.mgh.harvard.edu # Bug reports: analysis-bugs@nmr.mgh.harvard.edu # # set VERSION = '$Id: spmimg2bvolume,v 1.2 2007/01/09 22:41:18 nicks Exp $' set inputargs = ($argv); set PWDCMD = `getpwdcmd`; set spmstem = (); set npad = 3; set outstem = (); set outbext = (); set slicedim = 1; set swaprowcol = 0; set flip = 0; set reverse = 0; set firstslice = (); set nslices = (); set monly = 0; set MLF = (); set QuitOnError = 1; set firstframe = (); set nframes = (); set lastframe = (); set help = 0; set n = `echo $argv | grep version | wc -l` if($n != 0) then echo $VERSION exit 0; endif set n = `echo $argv | grep help | wc -l` if($n != 0) then set help = 1; goto usage_exit; exit 0; endif if($#argv == 0) then goto usage_exit; exit 1; endif goto parse_args; parse_args_return: goto check_params; check_params_return: set MATLAB = `getmatlab`; if($status) exit 1; if($#MLF == 0) set MLF = spmimg2bvolume_$$.m rm -f $MLF set okfile = $outdir/spmmat2bvolume.ok.$$ rm -f $okfile echo matlab file is $MLF rm -f $MLF; set spmpadfmt = %0$npad"d"; if($#firstframe == 0) set firstframe = 1; # Count the SPM Files for this stem # set NFRAMES = $firstframe; set spmfile = `printf "%s$spmpadfmt.img" $spmstem $NFRAMES `; if(! -e $spmfile) then echo "ERROR: cannot find $spmfile" exit 1; endif while(-e $spmfile) @ NFRAMES = $NFRAMES + 1; set spmfile = `printf "%s$spmpadfmt.img" $spmstem $NFRAMES `; #set spmfile = `printf "%s%03d.img" $spmstem $NFRAMES `; end @ NFRAMES = $NFRAMES - $firstframe; echo "INFO: Found $NFRAMES spm files with stem $spmstem" if($#nframes == 0) set nframes = $NFRAMES; @ lastframe = $firstframe + $nframes - 1; if($firstframe < 1) then echo "ERROR: First TP must be >= 1" exit 1; endif if($firstframe > $NFRAMES) then echo "ERROR: First TP must be <= NFRAMES ($NFRAMES)" exit 1; endif if($nframes < 1) then echo "ERROR: Number of TPs must be >= 1" exit 1; endif if($nframes > $NFRAMES) then echo "ERROR: Number of TPs must be <= NFRAMES ($NFRAMES)" exit 1; endif #if($lastframe > $NFRAMES) then # echo "ERROR: FirstFrame+NFrames-1 must be <= NFRAMES ($NFRAMES)" # echo "FirstFrame = $firstframe" # echo "nFrames = $NFRAMES" # echo "LastFrame = $lastframe" # exit 1; #endif #-------------------------------------------------------# tee $MLF < /dev/null set outdir = `$PWDCMD`; popd > /dev/null goto check_params_return; ############--------------################## ############--------------################## usage_exit: echo "" echo "USAGE: spmimg2bvolume" echo "" echo " -spm spmstem (eg, snaV for snaV001,snaV002,...)" echo " -o outstem" echo "" echo "Options:"; echo "" echo " -npad n : number of zero padding on spm file <3>" echo " -slicedim n : <1>, 2, or 3" echo " -reverse : reverse slice order" echo " -swap : swap rows and columns" echo " -flip : flip images up/down (after swap)" echo " -firstframe : first frame to convert <1>" echo " -nframes : number of frames to convert " echo " -obext extention : ,bshort,bfloat" echo "" echo " -help : print out a help message" echo " -umask umask : set unix file permission mask" echo " -version : print version and exit" echo "" echo "NOTE: requires that spm99 be in your matlab path" echo "" if($help) goto print_help; exit 1; ############--------------################## print_help: set startcode = "#StartHelp" set startcode = "$startcode#" set endcode = "#EndHelp" set endcode = "$endcode#" cat $0 | awk -v sc=$startcode -v ec=$endcode 'BEGIN{go=0}{if($0==ec) go=0; if(go) print $0;if($0==sc) go=1; }' echo "" exit 1; #StartHelp# This is program converts an spm volume into a bshort/bfloat volume. If the spm volume has multiple frames, the bvolume will have multiple frames (though this can be controlled with the -firstframe and -nframes options). The spm files are assumed to be of the form spmstemFFF.img, where FFF is a zero-padded frame number. For example, spmT_0017.img would be the volume of the 17th frame; the spmstem is spmT_ (include the underscore). npad - controls the width of the frame number field. It is 3 by default. For example, if the spm file is snaV001.img, then npad=3 because 001 has three digits. If the spm file is spmT_0012, then npad = 4. Note that the spm stem would be spmT_ (ie, include the underscore). slicedim - allows user to select which of the space dimensions will be used as the slice dimension. reverse - reverse the order of the slices swap - swap the rows and columns of each image. Essentially, this rotates each image by 90 degrees. flip - flip each image about the center row. This is done after swapping. firstframe - first frame to convert nframes - total number of frames to convert obext - extension (and precision) of the output bvolume. By default, the program looks at the type field in the spm header. If the type is 16 bit integer (type=4), the bshort is used. Otherwise, bfloat is used. The user can specify explicitly which precision through -obext. #EndHelp# ############--------------################## ############--------------################## arg1err: echo "ERROR: flag $flag requires one argument" exit 1 ############--------------##################