0001 function avw_hdr_write(avw, fileprefix, machine)
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 version = '[$Revision: 1332 $]';
0037 fprintf('AVW_HDR_WRITE [v%s]\n',version(12:16)); tic;
0038
0039
0040
0041
0042 if ~exist('avw','var'),
0043 fprintf('...no input avw - calling avw_hdr_make\n');
0044 avw = avw_hdr_make;
0045 elseif isempty(avw),
0046 fprintf('...empty input avw - calling avw_hdr_make\n');
0047 avw = avw_hdr_make;
0048 elseif ~isfield(avw,'hdr'),
0049 fprintf('...empty input avw.hdr - calling avw_hdr_make\n');
0050 avw = avw_hdr_make;
0051 end
0052 if ~isequal(avw.hdr.hk.sizeof_hdr,348),
0053 msg = sprintf('...avw.hdr.hk.sizeof_hdr must be 348!\n');
0054 error(msg);
0055 end
0056
0057 quit = 0;
0058 if ~exist('fileprefix','var'),
0059 if isfield(avw,'fileprefix'),
0060 if ~isempty(avw.fileprefix),
0061 fileprefix = avw.fileprefix;
0062 else,
0063 quit = 1;
0064 end
0065 else
0066 quit = 1;
0067 end
0068 if quit,
0069 fprintf('...no input fileprefix - see help avw_hdr_write\n\n');
0070 helpwin avw_hdr_write;
0071 return;
0072 end
0073 end
0074
0075 if ~exist('machine','var'), machine = 'ieee-le'; end
0076
0077 if findstr('.hdr',fileprefix),
0078
0079 fileprefix = strrep(fileprefix,'.hdr','');
0080 end
0081
0082
0083
0084
0085
0086
0087 tic;
0088
0089 fid = fopen(sprintf('%s.hdr',fileprefix),'w',machine);
0090 if fid < 0,
0091 msg = sprintf('Cannot write to file %s.hdr\n',fileprefix);
0092 error(msg);
0093 else
0094 fprintf('...writing %s Analyze header.\n',machine);
0095 write_header(fid,avw);
0096 end
0097
0098 t=toc; fprintf('...done (%5.2f sec).\n\n',t);
0099
0100 return
0101
0102
0103
0104
0105
0106
0107
0108 function write_header(fid,avw)
0109
0110 header_key(fid,avw.hdr.hk);
0111 image_dimension(fid,avw.hdr.dime);
0112 data_history(fid,avw.hdr.hist);
0113
0114
0115 fbytes = ftell(fid);
0116 fclose(fid);
0117 if ~isequal(fbytes,348),
0118 msg = sprintf('...file size is not 348 bytes!\n');
0119 warning(msg);
0120 end
0121
0122 return
0123
0124
0125
0126 function header_key(fid,hk)
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140 fseek(fid,0,'bof');
0141
0142 fwrite(fid, hk.sizeof_hdr(1), 'int32');
0143
0144 data_type = sprintf('%-10s',hk.data_type);
0145 fwrite(fid, hk.data_type(1:10), 'uchar');
0146
0147 db_name = sprintf('%-18s',hk.db_name);
0148 fwrite(fid, db_name(1:18), 'uchar');
0149
0150 fwrite(fid, hk.extents(1), 'int32');
0151 fwrite(fid, hk.session_error(1),'int16');
0152
0153 regular = sprintf('%1s',hk.regular);
0154 fwrite(fid, regular(1), 'uchar');
0155
0156
0157
0158 fwrite(fid, hk.hkey_un0(1), 'uint8');
0159
0160
0161
0162
0163
0164
0165
0166
0167 return
0168
0169
0170
0171 function image_dimension(fid,dime)
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202 fwrite(fid, dime.dim(1:8), 'int16');
0203 fwrite(fid, dime.vox_units(1:4),'uchar');
0204 fwrite(fid, dime.cal_units(1:8),'uchar');
0205 fwrite(fid, dime.unused1(1), 'int16');
0206 fwrite(fid, dime.datatype(1), 'int16');
0207 fwrite(fid, dime.bitpix(1), 'int16');
0208 fwrite(fid, dime.dim_un0(1), 'int16');
0209 fwrite(fid, dime.pixdim(1:8), 'float32');
0210 fwrite(fid, dime.vox_offset(1), 'float32');
0211
0212
0213 if dime.roi_scale == 0, dime.roi_scale = 0.00392157; end
0214 fwrite(fid, dime.roi_scale(1), 'float32');
0215
0216 fwrite(fid, dime.funused1(1), 'float32');
0217 fwrite(fid, dime.funused2(1), 'float32');
0218 fwrite(fid, dime.cal_max(1), 'float32');
0219 fwrite(fid, dime.cal_min(1), 'float32');
0220 fwrite(fid, dime.compressed(1), 'int32');
0221 fwrite(fid, dime.verified(1), 'int32');
0222 fwrite(fid, dime.glmax(1), 'int32');
0223 fwrite(fid, dime.glmin(1), 'int32');
0224
0225 return
0226
0227
0228
0229 function data_history(fid,hist)
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254 descrip = sprintf('%-80s', hist.descrip);
0255 aux_file = sprintf('%-24s', hist.aux_file);
0256
0257
0258
0259
0260 generated = sprintf('%-10s', hist.generated);
0261 scannum = sprintf('%-10s', hist.scannum);
0262 patient_id = sprintf('%-10s', hist.patient_id);
0263 exp_date = sprintf('%-10s', hist.exp_date);
0264 exp_time = sprintf('%-10s', hist.exp_time);
0265 hist_un0 = sprintf( '%-3s', hist.hist_un0);
0266
0267
0268
0269
0270
0271
0272
0273
0274 if length(descrip) < 80,
0275 paddingN = 80-length(descrip);
0276 padding = char(repmat(double(' '),1,paddingN));
0277 descrip = [descrip,padding];
0278 end
0279 if length(aux_file) < 24,
0280 paddingN = 24-length(aux_file);
0281 padding = char(repmat(double(' '),1,paddingN));
0282 aux_file = [aux_file,padding];
0283 end
0284
0285
0286
0287
0288
0289 if length(generated) < 10,
0290 paddingN = 10-length(generated);
0291 padding = char(repmat(double(' '),1,paddingN));
0292 generated = [generated, padding];
0293 end
0294 if length(scannum) < 10,
0295 paddingN = 10-length(scannum);
0296 padding = char(repmat(double(' '),1,paddingN));
0297 scannum = [scannum, padding];
0298 end
0299 if length(patient_id) < 10,
0300 paddingN = 10-length(patient_id);
0301 padding = char(repmat(double(' '),1,paddingN));
0302 patient_id = [patient_id, padding];
0303 end
0304 if length(exp_date) < 10,
0305 paddingN = 10-length(exp_date);
0306 padding = char(repmat(double(' '),1,paddingN));
0307 exp_date = [exp_date, padding];
0308 end
0309 if length(exp_time) < 10,
0310 paddingN = 10-length(exp_time);
0311 padding = char(repmat(double(' '),1,paddingN));
0312 exp_time = [exp_time, padding];
0313 end
0314 if length(hist_un0) < 10,
0315 paddingN = 10-length(hist_un0);
0316 padding = char(repmat(double(' '),1,paddingN));
0317 hist_un0 = [hist_un0, padding];
0318 end
0319
0320
0321
0322
0323 if find(double(descrip)>128),
0324 indexStrangeChar = find(double(descrip)>128);
0325 descrip(indexStrangeChar) = ' ';
0326 end
0327 if find(double(aux_file)>128),
0328 indexStrangeChar = find(double(aux_file)>128);
0329 aux_file(indexStrangeChar) = ' ';
0330 end
0331
0332
0333
0334
0335 if find(double(generated)>128),
0336 indexStrangeChar = find(double(generated)>128);
0337 generated(indexStrangeChar) = ' ';
0338 end
0339 if find(double(scannum)>128),
0340 indexStrangeChar = find(double(scannum)>128);
0341 scannum(indexStrangeChar) = ' ';
0342 end
0343 if find(double(patient_id)>128),
0344 indexStrangeChar = find(double(patient_id)>128);
0345 patient_id(indexStrangeChar) = ' ';
0346 end
0347 if find(double(exp_date)>128),
0348 indexStrangeChar = find(double(exp_date)>128);
0349 exp_date(indexStrangeChar) = ' ';
0350 end
0351 if find(double(exp_time)>128),
0352 indexStrangeChar = find(double(exp_time)>128);
0353 exp_time(indexStrangeChar) = ' ';
0354 end
0355 if find(double(hist_un0)>128),
0356 indexStrangeChar = find(double(hist_un0)>128);
0357 hist_un0(indexStrangeChar) = ' ';
0358 end
0359
0360
0361
0362
0363 fwrite(fid, descrip(1:80), 'uchar');
0364 fwrite(fid, aux_file(1:24), 'uchar');
0365
0366
0367
0368
0369 fwrite(fid, hist.orient(1), 'uint8');
0370
0371
0372 fwrite(fid, hist.originator(1:5), 'int16');
0373
0374 fwrite(fid, generated(1:10), 'uchar');
0375 fwrite(fid, scannum(1:10), 'uchar');
0376 fwrite(fid, patient_id(1:10), 'uchar');
0377 fwrite(fid, exp_date(1:10), 'uchar');
0378 fwrite(fid, exp_time(1:10), 'uchar');
0379 fwrite(fid, hist_un0(1:3), 'uchar');
0380
0381 fwrite(fid, hist.views(1), 'int32');
0382 fwrite(fid, hist.vols_added(1), 'int32');
0383 fwrite(fid, hist.start_field(1),'int32');
0384 fwrite(fid, hist.field_skip(1), 'int32');
0385 fwrite(fid, hist.omax(1), 'int32');
0386 fwrite(fid, hist.omin(1), 'int32');
0387 fwrite(fid, hist.smax(1), 'int32');
0388 fwrite(fid, hist.smin(1), 'int32');
0389
0390 return
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410