0001 function convert_format(sinfo, inpath, outpath, scan_offset)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 nsub = length(sinfo);
0041
0042 if nargin < 2
0043 inpath = './';
0044 outpath = './';
0045 elseif nargin < 3
0046 outpath = './';
0047 end
0048
0049 if nargin < 4
0050 scan_offset = 0;
0051 end
0052
0053 if isempty(outpath)
0054 outpath = './';
0055 end
0056
0057 if isempty(inpath)
0058 inpath = './';
0059 end
0060
0061 for isub = 1:nsub
0062
0063 s = sinfo(isub);
0064 subj_root = s.id;
0065
0066
0067 outroot = fullfile(outpath, subj_root);
0068
0069 if ~exist(outroot,'dir')
0070 disp(sprintf('Creating directory: %s', outroot))
0071 unix(['mkdir ' outroot]);
0072 end
0073
0074 num_epi = 0;
0075 for iexam = 1:s.nexams
0076
0077 exam_root = sprintf('%05d', s.exam_nums(iexam));
0078 datapath = fullfile(inpath, subj_root, exam_root);
0079
0080 series=s.series_mappings{iexam};
0081 nseries= size(series,1);
0082
0083 for mapping_idx = 1:nseries
0084 indir = fullfile(datapath, char(series(mapping_idx,1)));
0085
0086
0087 if ~exist(indir)
0088 fprintf('Could not locate input directory: %s (skipping ...)\n', indir);
0089 else
0090 switch char(series(mapping_idx,2))
0091 case {'epi','epi1','epi2','epi3','epi4','epi_12','epi_34'}
0092 epidir = fullfile(outroot, char(series(mapping_idx,2)));
0093
0094
0095 if ~exist(epidir,'dir')
0096 disp(sprintf('Creating directory: %s', epidir))
0097 unix(['mkdir ' epidir]);
0098 end
0099
0100
0101
0102
0103 if size(series,2) == 3
0104 nruns = length(series{mapping_idx,3});
0105 multi_epi_series = 1;
0106 else
0107 nruns = length(s.cond_order);
0108 multi_epi_series = 0;
0109 end
0110
0111 for irun = 1:nruns
0112 num_epi = num_epi+1;
0113
0114 outdir = fullfile(epidir, sprintf('run%d', num_epi));
0115
0116
0117 if ~exist(outdir,'dir')
0118 disp(sprintf('Creating directory: %s', outdir))
0119 unix(['mkdir ' outdir]);
0120 end
0121
0122 if multi_epi_series
0123
0124 nvol = s.nvol(num_epi);
0125 else
0126 nvol = s.nvol(num_epi);
0127 end
0128
0129 if multi_epi_series
0130
0131
0132 [dummy, vol_idx] = intersect(1:length(s.cond_order), series{mapping_idx,3}(1:irun));
0133 run_offset = sum(s.nvol(vol_idx)) - s.nvol(vol_idx(irun));
0134 else
0135 run_offset = sum(s.nvol(1:num_epi))-s.nvol(num_epi);
0136 end
0137
0138 start_num = run_offset+2+scan_offset;
0139
0140
0141
0142 outstub = fullfile(outdir, sprintf('%s_%s_r%d',subj_root, char(series(mapping_idx,1)), num_epi));
0143 status = GE_convertADW(indir,outstub, start_num, ...
0144 nvol-scan_offset);
0145 if status
0146 disp(sprintf('Error in converting run %d, series %s, exam %s', irun, char(series(mapping_idx,1)), exam_root))
0147
0148
0149
0150
0151
0152
0153 unix(['rmdir ' outdir]);
0154 num_epi = num_epi - 1;
0155 break
0156 end
0157 end
0158 otherwise
0159 outdir = fullfile(outroot, char(series(mapping_idx,2)));
0160
0161 outstub = fullfile(outdir, [subj_root '_' char(series(mapping_idx,1))]);
0162 disp(sprintf('Source: %s; Destination: %s', indir, outdir))
0163
0164
0165 if ~exist(outdir,'dir')
0166 disp(sprintf('Creating directory: %s', outdir))
0167 unix(['mkdir ' outdir]);
0168 end
0169 switch char(series(mapping_idx,2))
0170 case {'hires_T2_PD'}
0171 GE_convertT2PD(indir,outstub);
0172 otherwise
0173 GE_convertVolume(indir,1,outstub);
0174 end
0175 end
0176 end
0177 end
0178 end
0179 end