Home > functions > tool_box > load_NIFTI > convert_dicom > select_nii_ras_fname.m

select_nii_ras_fname

PURPOSE ^

Get RAS NIFTI file name by reading 'dcm2nii' logfile

SYNOPSIS ^

function [pname,fname,udir] = select_nii_ras_fname(logname, udir)

DESCRIPTION ^

 Get RAS NIFTI file name by reading 'dcm2nii' logfile
  and rename it by adding prefix 'RAS_'
  and delete other orientation NIFTI files
 --- Output files: Three set of NIFTI files are saved by 'dcm2nii'
  fname.hdr/img    : original image        : deleted
  o+fname.hdr/img  : RAS orientation image : renamed
  co+fname.hdr/img : cropped RAS image     : deleted
 ---
 [IN]
    logname : dcm2nii log filename
       udir : output directory
 [OUT]
      pname : RAS filename with path
      fname : Original NIfTI filename
 [NOTE]
    If dcm2nii does not reorient, only original file will be returned.

 Masa-aki Sato 2010-01-10
      rhayashi 2010-05-13 supported not reorienting case

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function    [pname,fname,udir] = select_nii_ras_fname(logname, udir)
0002 % Get RAS NIFTI file name by reading 'dcm2nii' logfile
0003 %  and rename it by adding prefix 'RAS_'
0004 %  and delete other orientation NIFTI files
0005 % --- Output files: Three set of NIFTI files are saved by 'dcm2nii'
0006 %  fname.hdr/img    : original image        : deleted
0007 %  o+fname.hdr/img  : RAS orientation image : renamed
0008 %  co+fname.hdr/img : cropped RAS image     : deleted
0009 % ---
0010 % [IN]
0011 %    logname : dcm2nii log filename
0012 %       udir : output directory
0013 % [OUT]
0014 %      pname : RAS filename with path
0015 %      fname : Original NIfTI filename
0016 % [NOTE]
0017 %    If dcm2nii does not reorient, only original file will be returned.
0018 %
0019 % Masa-aki Sato 2010-01-10
0020 %      rhayashi 2010-05-13 supported not reorienting case
0021 %
0022 % Copyright (C) 2011, ATR All Rights Reserved.
0023 % License : New BSD License(see VBMEG_LICENSE.txt)
0024 
0025 ext1 = '.hdr';
0026 ext2 = '.img';
0027 
0028 pname = ''; 
0029 fname = '';
0030 reorient = false;
0031 cropped  = false;
0032 
0033 fid = fopen(logname,'rt');    % text mode all platforms
0034 while(1)
0035     line = fgetl(fid);
0036     % EOF check
0037     if ~ischar(line), break; end
0038     % Reorienting check
0039     ix = strmatch('Reorienting as', line);
0040     if ~isempty(ix), reorient = true; end
0041     % cropped check
0042     ix = strmatch('Cropping', line);
0043     if ~isempty(ix), cropped = true; end
0044 
0045     % Get Output filename : '->*.img' (ex. '->RH.img')
0046     if ~isempty(strfind(line, '->')) && ~isempty(strfind(line, '.img'))
0047         ix = strfind(line, '->');
0048         pname = line(ix(end)+2:end);
0049     end
0050 end
0051 fclose(fid);
0052 if isempty(pname)
0053     warning('Cannot get subject name from dicom file.'); 
0054 end
0055 
0056 % Get .hdr file list
0057 [fpath, flist] = vb_get_ext_fname(udir, ext1);
0058 Nfile = length(flist);
0059 
0060 % Base file name(= Remove '.img', ex.'RH')
0061 fn0 = pname(1:end-4);
0062 
0063 for n=1:Nfile
0064     switch    flist{n}, 
0065     case    fn0
0066         if reorient 
0067             % Delete original orientation
0068             fprintf('Delete %s\n',[udir filesep fn0 ext1]);
0069             delete([udir filesep fn0 ext1]);
0070             delete([udir filesep fn0 ext2]);
0071         else
0072             % Return base filename
0073             pname = '';
0074             fname = [fn0 ext1];
0075         end
0076     case    ['co' fn0]
0077         if cropped
0078             % Delete cropped image
0079             fprintf('Delete %s\n',[udir filesep 'co' fn0 ext1]);
0080             delete([udir filesep 'co' fn0 ext1]);
0081             delete([udir filesep 'co' fn0 ext2]);
0082         end
0083     case    ['o' fn0]
0084         if reorient
0085             % Rename RAS original orientation
0086             fprintf('Rename %s\n',[udir filesep 'o' fn0 ext1]);
0087             movefile([udir filesep 'o' fn0 ext1], [udir filesep 'RAS_' fn0 ext1], 'f');
0088             movefile([udir filesep 'o' fn0 ext2], [udir filesep 'RAS_' fn0 ext2], 'f');
0089             % Return RAS filename with path
0090               pname = [udir filesep 'RAS_' fn0 ext1];
0091             % Return base filename
0092             fname = [fn0 ext1];
0093         end
0094     end
0095 end

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005