Home > fmri > fsl > fsl_swapdims.m

fsl_swapdims

PURPOSE ^

status = fsl_swapdims(infname,swaplist,outfname)

SYNOPSIS ^

function status = fsl_swapdims(infname,swaplist,outfname,verbose)

DESCRIPTION ^

 status = fsl_swapdims(infname,swaplist,outfname)

 Swaps dimensions in file infname according to the list given in swaplist. 
 The function uses fslswapdim which is part of the FSL distribution.

 Example: 
 fsl_swapdims(orig_hires,{'z -x y','-x y z'}, new_hires)

 This would call fslswapdim twice, the first time placing 1) the z-dimension of
 the original image in to the x-dimension of a temporary image, 2) a flipped
 version of the x-dimension of the original image into the y-dimension of a
 temporary image, and 3) the y-dimension of the original image into the
 z-dimension of the temporary image.  The second swap specified by, '-x y z',
 would create a final image given by outfname.  If outfname is not specified,
 the original image is replaced!
 
 02/16/06 Petr Janata
 03/23/08 Fred Barrett - added additional handling of nii.gz files
 generated by avwswapdim, when executing avwchfiletype
 08/17/08 FB - seeded the random number generator within this function
 2009.03.11 FB - changed from avw* to fsl* utilities, with move to fsl v4.1

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function status = fsl_swapdims(infname,swaplist,outfname,verbose)
0002 % status = fsl_swapdims(infname,swaplist,outfname)
0003 %
0004 % Swaps dimensions in file infname according to the list given in swaplist.
0005 % The function uses fslswapdim which is part of the FSL distribution.
0006 %
0007 % Example:
0008 % fsl_swapdims(orig_hires,{'z -x y','-x y z'}, new_hires)
0009 %
0010 % This would call fslswapdim twice, the first time placing 1) the z-dimension of
0011 % the original image in to the x-dimension of a temporary image, 2) a flipped
0012 % version of the x-dimension of the original image into the y-dimension of a
0013 % temporary image, and 3) the y-dimension of the original image into the
0014 % z-dimension of the temporary image.  The second swap specified by, '-x y z',
0015 % would create a final image given by outfname.  If outfname is not specified,
0016 % the original image is replaced!
0017 %
0018 % 02/16/06 Petr Janata
0019 % 03/23/08 Fred Barrett - added additional handling of nii.gz files
0020 % generated by avwswapdim, when executing avwchfiletype
0021 % 08/17/08 FB - seeded the random number generator within this function
0022 % 2009.03.11 FB - changed from avw* to fsl* utilities, with move to fsl v4.1
0023 
0024 status = 0;
0025 
0026 if nargin < 3
0027   fprintf('Will replace original image!!\n');
0028   outfname = infname;
0029 end
0030 
0031 if nargin < 4
0032   verbose = 0;;
0033 end
0034 
0035 nswap = length(swaplist);
0036 
0037 % seed random number generator
0038 rand('state',sum(100*clock));
0039 
0040 % Execute the series of swaps
0041 tmp{1} = infname;
0042 for iswap = 1:nswap
0043   tmp{iswap+1} = sprintf('/tmp/tmp_%06d', fix(rand*999999));
0044   fsl_str = sprintf('fslswapdim %s %s %s', tmp{iswap}, swaplist{iswap}, tmp{iswap+1});
0045   if verbose, fprintf('%s\n', fsl_str); end
0046   status = unix(fsl_str);   
0047   if status, return, end
0048 
0049   % Generate the full name of the temporary output file
0050   unix_str = sprintf('ls %s.*', tmp{iswap+1});
0051   if verbose, fprintf('%s\n', unix_str); end
0052   [status, tmpfname] = unix(unix_str);
0053   tmp{iswap+1} = deblank(tmpfname);  % remove any newline characters or spaces
0054 
0055 end
0056 
0057 % Move the last file to the desired destination and clean up any intermediate
0058 % files
0059 
0060 % Check to see if the file type of intermediate file and destination match
0061 [destpath,deststub,destfmt] = fileparts(outfname);
0062 [tmppath,tmpstub,tmpfmt] = fileparts(tmp{end});
0063 
0064 % Convert if necessary
0065 if isempty(strmatch(destfmt,tmpfmt,'exact'))
0066   switch destfmt
0067     case '.nii'
0068       dest_type = 'NIFTI';
0069     case {'.img','.hdr'}
0070       dest_type = 'NIFTI_PAIR';
0071   end
0072 
0073   fsl_str = sprintf('fslchfiletype %s %s', dest_type, tmp{end});
0074   if verbose, fprintf('%s\n', fsl_str); end
0075   status = unix(fsl_str);
0076   if status, return, end
0077   
0078   if (strmatch('.gz',tmpfmt,'exact'))
0079       [tmppath0,tmpstub,tmpfmt0] = fileparts(tmpstub);
0080   end
0081   
0082   tmp{end} = fullfile(tmppath, [tmpstub destfmt]);
0083 end
0084 
0085 % Move the temporary output file to the destination output file
0086 unix_str = sprintf('mv -f %s %s', tmp{end},outfname);
0087 if verbose, fprintf('%s\n', unix_str); end
0088 status = unix(unix_str);
0089 if status, error, end
0090  
0091 % If we are dealing with an image pair, make sure we move the .hdr also
0092 if any(strmatch(destfmt,{'.img'},'exact'))
0093   unix_str = sprintf('mv -f %s %s', fullfile(tmppath, [tmpstub '.hdr']), fullfile(destpath,[deststub '.hdr']));
0094   if verbose, fprintf('%s\n', unix_str); end
0095   status = unix(unix_str);
0096   if status, error, end
0097 end
0098 
0099 % Delete the intermediate files, but not the original file!
0100 for iswap = 1:nswap-1
0101   unix_str = sprintf('rm %s', tmp{iswap+1});
0102   if verbose, fprintf('%s\n', unix_str); end
0103   status = unix(unix_str);
0104   if status, return, end
0105 end
0106 
0107 % Now we have to delete what might be erroneous orientation information
0108 fsl_str = sprintf('fslorient -deleteorient %s', outfname);
0109 if verbose, fprintf('%s\n', fsl_str); end
0110 status = unix(fsl_str);   
0111 if status, return, end

Generated on Sat 24-Aug-2019 04:00:39 by m2html © 2003