Home > fmri > utils > old > GE2SPM.orig > GE_convert.m

GE_convert

PURPOSE ^

SYNOPSIS ^

function GE_convert(inDir,outStem)

DESCRIPTION ^

 GE_convert(inDir,outStem)

GE_convert 

 Converts a series of GE slices into Analyze format for SPM.
 inDir is the name of the directory containing the series, e.g. 003
 outStem is the stem used for naming the Analyze files

 Souheil J. Inati  
 Dartmouth College 2000

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function GE_convert(inDir,outStem)
0002 %
0003 % GE_convert(inDir,outStem)
0004 %
0005 %GE_convert
0006 %
0007 % Converts a series of GE slices into Analyze format for SPM.
0008 % inDir is the name of the directory containing the series, e.g. 003
0009 % outStem is the stem used for naming the Analyze files
0010 %
0011 % Souheil J. Inati
0012 % Dartmouth College 2000
0013 
0014 %
0015 % 04/18/00 PJ  Added check to make sure that imageVol is not empty when
0016 % returning from GE_readVolume
0017 %
0018 
0019 if (nargin < 2)
0020         error('Not enough input arguments.')
0021         return
0022 end
0023 
0024 % Create the name of the first file in inDir
0025 firstfile = fullfile(inDir,'I.001');
0026 
0027 % Generate the Analyze header and other info from the first file
0028 [header, orient, im_offset, adwcount] = GE_createSPMHeader(firstfile);
0029 volSize =  [header.dim(2) header.dim(3) header.dim(4)];
0030 % Reorient the header to SPM orientation
0031 header = GE_reorientHeader(header, orient); 
0032 
0033 % Loop over the volumes until there is no more looping to be done.
0034 
0035 % Loop over the runs
0036 % Loop over the passes (volumes)
0037 % Initialize the counters
0038 done = 0;
0039 runnum = 1;   % run number
0040 passnum = 0;  % pas number
0041 
0042 while ~done
0043 
0044   for i = 1:adwcount
0045     passnum = passnum + 1;
0046 
0047     % Create analyze file name
0048     series_str = sprintf('0%d',runnum);
0049     series_str = series_str(length(series_str)-1:length(series_str));
0050     vol_str = sprintf('000%d',i);
0051     vol_str = vol_str(length(vol_str)-3:length(vol_str));
0052     outName = [outStem sprintf('_s%s_i%s',series_str,vol_str)];
0053 
0054     % Read in the volume
0055     [imageVol, lastfile] = GE_readVolume(inDir, passnum, volSize, header.bitpix, im_offset);
0056 
0057     if isempty(imageVol)
0058       error('Failed to read in volume ...')
0059     end
0060     
0061     % Reorient the volume to SPM orientation
0062     imageVol = GE_reorientImage(imageVol, orient);
0063 
0064     % Write the SPM header file
0065     outFile = strcat(outName,'.hdr');
0066     status = GE_writeSPMHeader(outFile,header);
0067 
0068     % Write out the Analyze File
0069     outFile = [outName sprintf('.img')];
0070     [fid,message] = fopen(outFile,'w','l');
0071     if (fid == -1)
0072       fprintf('Cannot Open %s for writing.\n',outFile);
0073       error(message);
0074     end
0075 
0076     fwrite(fid,reshape(imageVol,1,prod(size(imageVol))),'int16');
0077     status = fclose(fid);
0078 
0079     % Done with vol
0080     fprintf('Wrote %s\n',outName);
0081 
0082     if runnum == 1 & adwcount > 1 & passnum == 1
0083        break
0084     end
0085 
0086   end % Run loop
0087 
0088   % Check for another run
0089   [path_stem, path_start] = fileparts(inDir);
0090   % Make the filename for the first file of the next run
0091   filep = ceil((passnum+1)*volSize(3)/999) - 1; 
0092   filen = (passnum+1)*volSize(3) - 999*filep;
0093   path_num = str2num(path_start) + 20*filep;
0094   path_now = sprintf('00%d',path_num);
0095   path_now = path_now(length(path_now)-2:length(path_now));
0096   path = fullfile(path_stem, path_now);
0097   stub = sprintf('00%d',filen);
0098   stub = stub(length(stub)-2:length(stub));
0099   nextFile = fullfile(path,['I.' stub]); % last file of next volume
0100   [fid,message] = fopen(nextFile,'r');
0101 
0102   if (fid == -1)
0103     done = 1;
0104   else
0105     fclose(fid);
0106     runnum = runnum + 1;
0107   end
0108 
0109 end
0110 
0111 % Done
0112 fprintf('\nConversion Finished. \n');
0113 
0114 return

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