Home > vbmeg > external > bioelectromagnetism > avw_hdr_write.m

avw_hdr_write

PURPOSE ^

AVW_HDR_WRITE - Write Analyze header file (*.hdr)

SYNOPSIS ^

function avw_hdr_write(avw, fileprefix, machine)

DESCRIPTION ^

 AVW_HDR_WRITE - Write Analyze header file (*.hdr)
 
 avw_hdr_write(avw, fileprefix, machine)
 
 eg, avw_hdr_write(avw,'test');
 
 avw        - a struct with .hdr field, which itself is a struct,
              containing all fields of an Analyze header.
              For details, see avw_hdr_read.m
 
 fileprefix - a string, the filename without the .hdr extension.
              If empty, may use avw.fileprefix
 
 machine    - a string, see machineformat in fread for details.
              The default here is 'ieee-le'.
 
 See also, AVW_HDR_READ AVW_HDR_MAKE
           AVW_IMG_READ AVW_IMG_WRITE

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function avw_hdr_write(avw, fileprefix, machine)
0002 
0003 % AVW_HDR_WRITE - Write Analyze header file (*.hdr)
0004 %
0005 % avw_hdr_write(avw, fileprefix, machine)
0006 %
0007 % eg, avw_hdr_write(avw,'test');
0008 %
0009 % avw        - a struct with .hdr field, which itself is a struct,
0010 %              containing all fields of an Analyze header.
0011 %              For details, see avw_hdr_read.m
0012 %
0013 % fileprefix - a string, the filename without the .hdr extension.
0014 %              If empty, may use avw.fileprefix
0015 %
0016 % machine    - a string, see machineformat in fread for details.
0017 %              The default here is 'ieee-le'.
0018 %
0019 % See also, AVW_HDR_READ AVW_HDR_MAKE
0020 %           AVW_IMG_READ AVW_IMG_WRITE
0021 %
0022 
0023 % $Revision: 1332 $ $Date:: 2011-02-24 15:57:20 +0900#$
0024 
0025 % Licence:  GNU GPL, no express or implied warranties
0026 % History:  05/2002, Darren.Weber@flinders.edu.au
0027 %           02/2003, Bennett.Landman@ieee.org
0028 %                    - more specific data history var sizes
0029 %                    - 02/2003 confirmed, Darren
0030 %
0031 %                    The Analyze format and c code below is copyright
0032 %                    (c) Copyright, 1986-1995
0033 %                    Biomedical Imaging Resource, Mayo Foundation
0034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0035 
0036 version = '[$Revision: 1332 $]';
0037 fprintf('AVW_HDR_WRITE [v%s]\n',version(12:16));  tic;
0038 
0039 %----------------------------------------------------------------------------
0040 % Check inputs
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 %    fprintf('AVW_HDR_WRITE: Removing .hdr extension from ''%s''\n',fileprefix);
0079     fileprefix = strrep(fileprefix,'.hdr','');
0080 end
0081 
0082 
0083 
0084 %----------------------------------------------------------------------------
0085 % MAIN
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     % check the file size is 348 bytes
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     % Original header structures - ANALYZE 7.5
0129     % struct header_key                      /* header key      */
0130     %       {                                /* off + size      */
0131     %       int sizeof_hdr                   /*  0 +  4         */
0132     %       char data_type[10];              /*  4 + 10         */
0133     %       char db_name[18];                /* 14 + 18         */
0134     %       int extents;                     /* 32 +  4         */
0135     %       short int session_error;         /* 36 +  2         */
0136     %       char regular;                    /* 38 +  1         */
0137     %       char hkey_un0;                   /* 39 +  1         */
0138     %       };                               /* total=40 bytes  */
0139     
0140     fseek(fid,0,'bof');
0141     
0142     fwrite(fid, hk.sizeof_hdr(1),   'int32');  % must be 348!
0143     
0144     data_type = sprintf('%-10s',hk.data_type); % ensure it is 10 chars
0145     fwrite(fid, hk.data_type(1:10), 'uchar');
0146     
0147     db_name   = sprintf('%-18s',hk.db_name);   % ensure it is 18 chars
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);     % ensure it is 1 char
0154     fwrite(fid, regular(1),         'uchar');  % might be uint8
0155     
0156     %hkey_un0  = sprintf('%1s',hk.hkey_un0);    % ensure it is 1 char
0157     %fwrite(fid, hkey_un0(1),        'uchar');
0158     fwrite(fid, hk.hkey_un0(1),     'uint8');
0159     
0160     %    >Would you set hkey_un0 as char or uint8?
0161     %   Really doesn't make any difference.  As far as anyone here can remember,
0162     %   this was just to pad to an even byte boundary for that structure.  I guess
0163     %   I'd suggest setting it to a uint8 value of 0 (i.e, truly zero-valued) so
0164     %   that it doesn't look like anything important!
0165     %   Denny <hanson.dennis2@mayo.edu>
0166     
0167 return
0168 
0169 %----------------------------------------------------------------------------
0170 
0171 function image_dimension(fid,dime)
0172     
0173     %struct image_dimension
0174     %       {                                /* off + size      */
0175     %       short int dim[8];                /* 0 + 16          */
0176     %       char vox_units[4];               /* 16 + 4          */
0177     %       char cal_units[8];               /* 20 + 8          */
0178     %       short int unused1;               /* 28 + 2          */
0179     %       short int datatype;              /* 30 + 2          */
0180     %       short int bitpix;                /* 32 + 2          */
0181     %       short int dim_un0;               /* 34 + 2          */
0182     %       float pixdim[8];                 /* 36 + 32         */
0183     %            /*
0184     %                pixdim[] specifies the voxel dimensions:
0185     %                pixdim[1] - voxel width
0186     %                pixdim[2] - voxel height
0187     %                pixdim[3] - interslice distance
0188     %                    ..etc
0189     %            */
0190     %       float vox_offset;                /* 68 + 4          */
0191     %       float roi_scale;                 /* 72 + 4          */
0192     %       float funused1;                  /* 76 + 4          */
0193     %       float funused2;                  /* 80 + 4          */
0194     %       float cal_max;                   /* 84 + 4          */
0195     %       float cal_min;                   /* 88 + 4          */
0196     %       int compressed;                  /* 92 + 4          */
0197     %       int verified;                    /* 96 + 4          */
0198     %       int glmax;                       /* 100 + 4         */
0199     %       int glmin;                       /* 104 + 4         */
0200     %       };                               /* total=108 bytes */
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     % Ensure compatibility with SPM (according to MRIcro)
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     % Original header structures - ANALYZE 7.5
0232     %struct data_history
0233     %       {                                /* off + size      */
0234     %       char descrip[80];                /* 0 + 80          */
0235     %       char aux_file[24];               /* 80 + 24         */
0236     %       char orient;                     /* 104 + 1         */
0237     %       char originator[10];             /* 105 + 10        */
0238     %       char generated[10];              /* 115 + 10        */
0239     %       char scannum[10];                /* 125 + 10        */
0240     %       char patient_id[10];             /* 135 + 10        */
0241     %       char exp_date[10];               /* 145 + 10        */
0242     %       char exp_time[10];               /* 155 + 10        */
0243     %       char hist_un0[3];                /* 165 + 3         */
0244     %       int views                        /* 168 + 4         */
0245     %       int vols_added;                  /* 172 + 4         */
0246     %       int start_field;                 /* 176 + 4         */
0247     %       int field_skip;                  /* 180 + 4         */
0248     %       int omax;                        /* 184 + 4         */
0249     %       int omin;                        /* 188 + 4         */
0250     %       int smax;                        /* 192 + 4         */
0251     %       int smin;                        /* 196 + 4         */
0252     %       };                               /* total=200 bytes */
0253     
0254     descrip     = sprintf('%-80s', hist.descrip);       % 80 chars
0255     aux_file    = sprintf('%-24s', hist.aux_file);      % 24 chars
0256 
0257     % Modified by M. Sato
0258     %originator  = sprintf('%-10s', hist.originator);    % 10 chars
0259     
0260     generated   = sprintf('%-10s', hist.generated);     % 10 chars
0261     scannum     = sprintf('%-10s', hist.scannum);       % 10 chars
0262     patient_id  = sprintf('%-10s', hist.patient_id);    % 10 chars
0263     exp_date    = sprintf('%-10s', hist.exp_date);      % 10 chars
0264     exp_time    = sprintf('%-10s', hist.exp_time);      % 10 chars
0265     hist_un0    = sprintf( '%-3s', hist.hist_un0);      %  3 chars
0266     
0267     % ---
0268     % The following should not be necessary, but I actually
0269     % found one instance where it was, so this totally anal
0270     % retentive approach became necessary, despite the
0271     % apparently elegant solution above to ensuring that variables
0272     % are the right length.
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 %    if length(originator) < 10,
0285 %      paddingN = 10-length(originator);
0286 %      padding = char(repmat(double(' '),1,paddingN));
0287 %      originator = [originator, padding];
0288 %    end
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     % -- if you thought that was anal, try this;
0321     % -- lets check for unusual ASCII char values!
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 %    if find(double(originator)>128),
0332 %      indexStrangeChar = find(double(originator)>128);
0333 %      originator(indexStrangeChar) = ' ';
0334 %    end
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     % --- finally, we write the fields
0362     
0363     fwrite(fid, descrip(1:80),    'uchar');
0364     fwrite(fid, aux_file(1:24),   'uchar');
0365     
0366     
0367     %orient      = sprintf(  '%1s', hist.orient);        %  1 char
0368     %fwrite(fid, orient(1),        'uchar');
0369     fwrite(fid, hist.orient(1),   'uint8');     % see note below on char
0370     
0371     % Modified by M. Sato
0372     fwrite(fid, hist.originator(1:5),  'int16'); % SPM origin field
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 % Note on using char:
0395 % The 'char orient' field in the header is intended to
0396 % hold simply an 8-bit unsigned integer value, not the ASCII representation
0397 % of the character for that value.  A single 'char' byte is often used to
0398 % represent an integer value in Analyze if the known value range doesn't
0399 % go beyond 0-255 - saves a byte over a short int, which may not mean
0400 % much in today's computing environments, but given that this format
0401 % has been around since the early 1980's, saving bytes here and there on
0402 % older systems was important!  In this case, 'char' simply provides the
0403 % byte of storage - not an indicator of the format for what is stored in
0404 % this byte.  Generally speaking, anytime a single 'char' is used, it is
0405 % probably meant to hold an 8-bit integer value, whereas if this has
0406 % been dimensioned as an array, then it is intended to hold an ASCII
0407 % character string, even if that was only a single character.
0408 % Denny  <hanson.dennis2@mayo.edu>
0409 
0410 % See other notes in avw_hdr_read

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005