Home > SurfStat > SurfStatWriteVol1.m

SurfStatWriteVol1

PURPOSE ^

Writes volumetric image data in MINC, ANALYZE, NIFTI or AFNI format.

SYNOPSIS ^

function SurfStatWriteVol( d, Z, T );

DESCRIPTION ^

Writes volumetric image data in MINC, ANALYZE, NIFTI or AFNI format. 
 
 Usage: fmris_write_image( d [, Z, T] ). 

 d.file_name = file name with extension .mnc, .img, .nii or .brik as above
 Z           = vector of slices.
 T           = vector of times. 
 If Z and T are omitted, writes the entire volume.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function SurfStatWriteVol( d, Z, T );
0002 
0003 %Writes volumetric image data in MINC, ANALYZE, NIFTI or AFNI format.
0004 %
0005 % Usage: fmris_write_image( d [, Z, T] ).
0006 %
0007 % d.file_name = file name with extension .mnc, .img, .nii or .brik as above
0008 % Z           = vector of slices.
0009 % T           = vector of times.
0010 % If Z and T are omitted, writes the entire volume.
0011 
0012 file = deblank(d.file_name);
0013 
0014 [base,ext]=fileparts2(file);
0015 d.file_name=[base ext];
0016    
0017 switch lower(ext)
0018 case '.mnc'
0019    if nargin == 3
0020       if length(T)<=160
0021          fmris_write_minc(d,Z,T);
0022       else
0023          fn=deblank(d.file_name);
0024          d.file_name=[fn(1:(length(fn)-3)) 'nii'];
0025          fmris_write_nifti(d,Z,T);
0026       end
0027    else
0028       if d.dim(4)<=160
0029          fmris_write_minc(d);
0030       else
0031          fn=deblank(d.file_name);
0032          d.file_name=[fn(1:(length(fn)-3)) 'nii'];
0033          fmris_write_nifti(d);
0034       end
0035    end
0036 case '.img'
0037    if nargin == 3
0038       fmris_write_analyze(d,Z,T);
0039    else
0040       fmris_write_analyze(d);
0041    end
0042 case '.brik'
0043    if nargin == 3
0044       fmris_write_afni(d,Z,T);
0045    else
0046       fmris_write_afni(d);
0047    end
0048 case '.nii'
0049    if nargin == 3
0050       fmris_write_nifti(d,Z,T);
0051    else
0052       fmris_write_nifti(d);
0053    end
0054 otherwise
0055    ['Unknown file extension']
0056 end
0057 
0058 return
0059 end
0060 
0061 %%
0062 
0063 function [base,ext]=fileparts2(string)
0064 if isstr(string)
0065     [path,name,ext]=fileparts(deblank(string));
0066 else
0067     [path,name,ext]=fileparts(deblank(string.file_name));
0068 end
0069 if strcmp(ext,'.gz')
0070    [path2,name,ext]=fileparts(name);
0071 end   
0072 if isempty(path)
0073    base=name;
0074 else
0075    base=[path '/' name];
0076 end
0077 
0078 return
0079 end
0080 
0081 
0082 %%
0083 
0084 function    [d]=fmris_write_afni(d,Z,T);
0085 
0086 existinfo=0;
0087 if ~isfield(d,'dim') & isfield(d,'parent_file')
0088    [path,name,ext]=fileparts(deblank(d.parent_file));
0089    [err, Infoparent] = BrikInfo([path '/' name '.HEAD']);
0090    d.dim=[Infoparent.DATASET_DIMENSIONS(1:3) Infoparent.DATASET_RANK(2)];
0091    existinfo=1;
0092 end
0093 
0094 if nargin==1 
0095    Z=1:d.dim(3);
0096    T=1:d.dim(4);
0097 end
0098 
0099 Opt.Prefix=d.file_name(1:(length(d.file_name)-5));
0100 Opt.Slices=Z;
0101 Opt.Frames=T;
0102 Opt.NoCheck=1;
0103 
0104 if Z(1)==1 & T(1)==1
0105    if ~existinfo
0106       [path,name,ext]=fileparts(deblank(d.parent_file));
0107       [err, Infoparent] = BrikInfo([path '/' name '.HEAD']);
0108    end
0109    [path,name,ext]=fileparts(deblank(d.file_name));
0110    Info.LABEL_1=name;
0111    Info.DATASET_NAME=['./' name];
0112    if isfield(d,'origin')
0113       Info.ORIGIN=d.origin;
0114    else
0115       Info.ORIGIN=Infoparent.ORIGIN;
0116    end
0117    if isfield(d,'vox')
0118       Info.DELTA=d.vox;
0119    else
0120       Info.DELTA=Infoparent.DELTA;
0121    end
0122    Info.SCENE_DATA=Infoparent.SCENE_DATA;
0123    Info.ORIENT_SPECIFIC=Infoparent.ORIENT_SPECIFIC;
0124    Info.TYPESTRING=Infoparent.TYPESTRING;
0125    Opt.NoCheck=0;
0126 end
0127 
0128 Info.DATASET_DIMENSIONS=[d.dim(1:3) 0 0];
0129 Info.DATASET_RANK=[3 d.dim(4) 0 0 0 0 0 0];
0130 Info.BRICK_TYPES=repmat(3,1,d.dim(4));
0131 Info.TypeName='float';
0132 Info.TypeBytes=4;
0133 Info.BYTEORDER_STRING='MSB_FIRST';
0134 Info.MachineFormat='ieee-be';
0135 
0136 if isfield(d,'df')
0137    if ~isempty(d.df)
0138       Info.WORSLEY_DF=d.df;
0139    end
0140 end
0141 
0142 if isfield(d,'nconj')
0143    if ~isempty(d.nconj)
0144       Info.WORSLEY_NCONJ=d.nconj;
0145    end
0146 end
0147 
0148 if isfield(d,'fwhm')
0149    if ~isempty(d.fwhm)
0150       Info.WORSLEY_FWHM=d.fwhm;
0151    end
0152 end
0153 
0154 [err, ErrMessage, Info] = WriteBrik(d.data, Info, Opt);
0155 
0156 return
0157 end
0158 
0159 
0160 %%
0161 
0162 function [d]=fmris_write_analyze(d,Z,T);
0163 
0164 % Write Analyze format Image Volumes
0165 %
0166 % Usage
0167 %
0168 % rpm_write_analyze(d,Z,T);
0169 %
0170 % Writes image data and attributes from the structure d.
0171 % The following fields are required from d.
0172 %
0173 % d.file_path: '/kop1/data/fdg/'
0174 % d.file_name: 'n03309_3d_dy2_CS-Cal_K1'
0175 % d.data: [128x128x31 double]
0176 % d.vox: [2.0900 2.0900 3.4200]
0177 % d.vox_units: 'mm'
0178 % d.vox_offset: 0
0179 % d.calib_units: 'min^-1'
0180 % d.origin: [0 0 0];
0181 % d.descrip: 'Parametric Image - K1 (1)'
0182 %
0183 % All other information is generated automatically.
0184 %
0185 % (c) Roger Gunn & John Aston
0186 
0187 if isfield(d,'parent_file')
0188    d2=SurfStatReadVol(d.parent_file,0,0);
0189    d.vox=d2.vox;
0190    d.vox_units=d2.vox_units;
0191    d.calib_units='';
0192    d.origin=d2.origin;
0193    d.vox_offset=d2.vox_offset;
0194 end
0195 if ~isfield(d,'descrip')
0196    d.descrip='';
0197 end
0198 file=d.file_name;
0199 
0200 d.origin=-d.origin./d.vox(1:3);
0201 
0202 if length(size(d.data))<5&size(d.data,1)>1&size(d.data,2)>1&length(d.origin)<4
0203     
0204     d.calib = [1 1];
0205     d.precision='float';  
0206     
0207     % Write Header
0208     if nargin==1
0209         [d]=Write_Analyze_Hdr(d);
0210     elseif nargin==3
0211         if (T(1)==1&Z(1)==1)
0212             [d]=Write_Analyze_Hdr(d);
0213         else
0214             d3 = fmris_read_image(file,0,0);
0215             d.hdr = d3.hdr;
0216         end      
0217     end
0218     
0219     if ~isstruct(d);
0220         return
0221     end
0222     % Write Image
0223     if nargin==1
0224         if ~isempty(d)
0225             fid = fopen(file,'w','n');
0226             if fid > -1
0227                 for t=1:d.hdr.dim(5)
0228                     for z=1:d.hdr.dim(4)
0229                         fwrite(fid,d.data(:,:,z,t)/d.hdr.funused1,d.precision);
0230                     end
0231                 end
0232                 fclose (fid);
0233             else
0234                 errordlg('Cannot open file for writing  ','Write Error');d=[];return;
0235             end
0236         end
0237     elseif nargin==3    
0238         if T(1)~=1|Z(1)~=1
0239             if ~exist(file,'file')
0240                 errordlg('Please write Plane 1 Frame 1 first','Write Error');return;
0241             end
0242         else
0243             fid=fopen(file,'w','n');fclose(fid);
0244         end
0245         fid = fopen(file,'r+','n');
0246         if fid > -1
0247             if T(1)==1&Z(1)==1
0248                 plane=zeros(d.dim(1:2));
0249                 for t=1:d.hdr.dim(5)
0250                     for z=1:d.hdr.dim(4)
0251                         fwrite(fid,plane,d.precision);
0252                     end
0253                 end      
0254             end
0255             
0256             for t=1:length(T)
0257                 for z=1:length(Z)
0258                     fseek(fid,4*d.dim(1)*d.dim(2)*((T(t)-1)*d.dim(3)+Z(z)-1),'bof');
0259                     if length(Z)~=1;
0260                         fwrite(fid,d.data(:,:,z,t),'float');
0261                     else
0262                         fwrite(fid,d.data(:,:,t),'float');
0263                     end
0264                 end
0265             end
0266             
0267             fclose (fid);
0268             
0269         else
0270             errordlg('Cannot open file for writing  ','Write Error');d=[];return;
0271             
0272         end
0273     end
0274     
0275 else
0276     errordlg('Incompatible data structure: Check dimension and Origin  ','Write Error'); 
0277 end
0278 
0279 return;
0280 end
0281 
0282 %%
0283 
0284 function [d]=Write_Analyze_Hdr(d);
0285 
0286 % Write Analyze Header from the structure d
0287 % Adapted from John Ashburners spm_hwrite.m
0288 
0289 d.file_name_hdr=[d.file_name(1:(length(d.file_name)-3)) 'hdr'];
0290 file=d.file_name_hdr;
0291 
0292 fid               = fopen(file,'w','n');
0293 if fid > -1
0294     d.hdr.data_type             = ['dsr      ' 0];
0295     d.hdr.db_name              = ['                 ' 0];
0296     if isfield(d,'dim')
0297         d.hdr.dim                = [4 1 1 1 1 0 0 0];
0298         d.hdr.dim(2:(1+length(d.dim(find(d.dim)))))= d.dim(find(d.dim));
0299     else
0300         d.hdr.dim                = [4 1 1 1 1 0 0 0];
0301         d.hdr.dim(2:(1+length(size(d.data))))   = size(d.data);
0302     end   
0303     
0304     d.hdr.pixdim             = [4 0 0 0 0 0 0 0];
0305     d.hdr.pixdim(2:(1+length(d.vox))) = d.vox;
0306     d.hdr.vox_units            = [0 0 0 0];
0307     d.hdr.vox_units(1:min([3 length(d.vox_units)])) = d.vox_units(1:min([3 length(d.vox_units)]));
0308     d.hdr.vox_offset         = d.vox_offset;
0309     d.hdr.calmin                = d.calib(1);
0310     d.hdr.calmax                = d.calib(2);
0311     switch d.precision
0312     case 'uint1'  % 1  bit
0313         d.hdr.datatype         = 1;
0314         d.hdr.bitpix             = 1;
0315         d.hdr.glmin            = 0;
0316         d.hdr.glmax             = 1;
0317         d.hdr.funused1        = 1;   
0318     case 'uint8'  % 8  bit
0319         % d.hdr.datatype         = 2;
0320         % d.hdr.bitpix             = 8;
0321         % d.hdr.glmin             = 0;
0322         % d.hdr.glmax             = 255;
0323         % d.hdr.funused1    = abs(d.hdr.calmin)/255;
0324         errordlg('You should write a float image','8 Bit Write Error');d=[];return;
0325     case 'int16'  % 16 bit
0326         d.hdr.datatype         = 4;
0327         d.hdr.bitpix          = 16;
0328         if abs(d.hdr.calmin)>abs(d.hdr.calmax)
0329             d.hdr.funused1      = abs(d.hdr.calmin)/(2^15-1);
0330         else
0331             d.hdr.funused1    = abs(d.hdr.calmax)/(2^15-1);
0332         end
0333         d.hdr.glmin             = round(d.hdr.funused1*d.hdr.calmin);
0334         d.hdr.glmax             = round(d.hdr.funused1*d.hdr.calmin);
0335     case 'int32'  % 32 bit
0336         d.hdr.datatype         = 8;
0337         d.hdr.bitpix          = 32;
0338         if abs(d.hdr.calmin)>abs(d.hdr.calmax)
0339             d.hdr.funused1      = abs(d.hdr.calmin)/(2^31-1);
0340         else
0341             d.hdr.funused1    = abs(d.hdr.calmax)/(2^31-1);
0342         end
0343         d.hdr.glmin             = round(d.hdr.funused1*d.hdr.calmin);
0344         d.hdr.glmax             = round(d.hdr.funused1*d.hdr.calmin);
0345     case 'float'  % float  (32 bit)
0346         d.hdr.datatype         = 16;
0347         d.hdr.bitpix              = 32;
0348         d.hdr.glmin             = 0;
0349         d.hdr.glmax             = 0;
0350         d.hdr.funused1         = 1;
0351     case 'double' % double (64 bit)
0352         d.hdr.datatype         = 64;
0353         d.hdr.bitpix          = 64;
0354         d.hdr.glmin             = 0;
0355         d.hdr.glmax             = 0;
0356         d.hdr.funused1         = 1;
0357     otherwise
0358         errordlg('Unrecognised precision (d.type)','Write Error');d=[];return;
0359     end
0360     d.hdr.descrip             = zeros(1,80);d.hdr.descrip(1:min([length(d.descrip) 79]))=d.descrip(1:min([length(d.descrip) 79]));
0361     d.hdr.aux_file            = ['none                   ' 0];
0362     d.hdr.origin              = [0 0 0 0 0];d.hdr.origin(1:length(d.origin))=d.origin;
0363     
0364     
0365     % write (struct) header_key
0366     %---------------------------------------------------------------------------
0367     fseek(fid,0,'bof');
0368     
0369     fwrite(fid,348,                    'int32');
0370     fwrite(fid,d.hdr.data_type,    'char' );
0371     fwrite(fid,d.hdr.db_name,        'char' );
0372     fwrite(fid,0,                    'int32');
0373     fwrite(fid,0,                    'int16');
0374     fwrite(fid,'r',                    'char' );
0375     fwrite(fid,'0',                    'char' );
0376     
0377     
0378     % write (struct) image_dimension
0379     %---------------------------------------------------------------------------
0380     fseek(fid,40,'bof');
0381     
0382     fwrite(fid,d.hdr.dim,            'int16');
0383     fwrite(fid,d.hdr.vox_units,    'char' );
0384     fwrite(fid,zeros(1,8),            'char' );
0385     fwrite(fid,0,                    'int16');
0386     fwrite(fid,d.hdr.datatype,    'int16');
0387     fwrite(fid,d.hdr.bitpix,        'int16');
0388     fwrite(fid,0,                    'int16');
0389     fwrite(fid,d.hdr.pixdim,        'float');
0390     fwrite(fid,d.hdr.vox_offset,    'float');
0391     fwrite(fid,d.hdr.funused1,    'float');
0392     fwrite(fid,0,                    'float');
0393     fwrite(fid,0,                    'float');
0394     fwrite(fid,d.hdr.calmax,        'float');
0395     fwrite(fid,d.hdr.calmin,        'float');
0396     fwrite(fid,0,                    'int32');
0397     fwrite(fid,0,                    'int32');
0398     fwrite(fid,d.hdr.glmax,        'int32');
0399     fwrite(fid,d.hdr.glmin,        'int32');
0400     
0401     % write (struct) data_history
0402     %---------------------------------------------------------------------------
0403     fwrite(fid,d.hdr.descrip,        'char');
0404     fwrite(fid,d.hdr.aux_file,       'char');
0405     fwrite(fid,0,                   'char');
0406     fwrite(fid,d.hdr.origin,     'uint16');
0407     
0408     fwrite(fid,zeros(1,10),         'char');
0409     
0410     if isfield(d,'df')
0411        fwrite(fid,sprintf('%10g',d.df(1)),'char');
0412        if length(d.df)>1
0413           fwrite(fid,sprintf('%10g',d.df(2)),'char');
0414        else
0415           fwrite(fid,zeros(1,10),'char');
0416        end
0417     else
0418        fwrite(fid,zeros(1,20),'char');
0419     end
0420     
0421     if isfield(d,'fwhm')
0422        fwrite(fid,sprintf('%10g',d.fwhm(1)),'char');
0423        if length(d.fwhm)>1
0424           fwrite(fid,sprintf('%10g',d.fwhm(2)),'char');
0425        else
0426           fwrite(fid,zeros(1,10),'char');
0427        end
0428     else
0429        fwrite(fid,zeros(1,20),'char');
0430     end
0431     
0432     if isfield(d,'nconj')
0433        fwrite(fid,sprintf('%3g',d.nconj),'char');
0434     else
0435        fwrite(fid,zeros(1,3),'char');
0436     end
0437     
0438     fwrite(fid,zeros(1,32),'char');
0439     
0440     s   = ftell(fid);
0441     fclose(fid);
0442 else
0443     errordlg('Cannot open file for writing  ','Write Error');d=[];return;
0444 end
0445 
0446 return;
0447 end
0448 
0449 
0450 %%
0451 
0452 function    [d]=fmris_write_minc(d,Z,T);
0453 
0454 if ~(d.file_name(1)=='/' | d.file_name(2)==':')
0455    if d.file_name(1:2)=='./'
0456       d.file_name=[pwd d.file_name(3:length(file))];
0457    else
0458       d.file_name=[pwd '/' d.file_name];
0459    end
0460 end
0461 
0462 fid=fopen(d.parent_file);
0463 d.parent_file=fopen(fid);
0464 fclose(fid);
0465 
0466 if ~isfield(d,'dim') 
0467    d.dim     = fliplr(miinquire(d.parent_file,'imagesize')');
0468    d.dim     = d.dim + (d.dim == 0);
0469 end
0470 
0471 if nargin==1
0472    Z=1:d.dim(3);
0473    T=1:d.dim(4);
0474 end
0475 
0476 if isfield(d,'precision')
0477    precision=d.precision;
0478    if isempty(precision)
0479        precision='float';
0480    end
0481 else
0482    precision='float';
0483 end
0484 
0485 if T(1)==1 & Z(1)==1
0486    dim=d.dim;
0487    if dim(4)==1
0488       dim(4)=0;
0489    end
0490    if exist(d.file_name,'file')
0491       delete(d.file_name);
0492    end
0493    newh=newimage(d.file_name,fliplr(dim),d.parent_file,precision);
0494    if isfield(d,'df')
0495       miwriteatt(d.file_name,'df','df',d.df);
0496    end
0497    if isfield(d,'nconj')
0498       miwriteatt(d.file_name,'nconj','nconj',d.nconj);
0499    end
0500    if isfield(d,'fwhm')
0501       miwriteatt(d.file_name,'fwhm','fwhm',d.fwhm);
0502    end
0503 else
0504    newh=openimage(d.file_name,'w');
0505 end
0506 
0507 d.data=squeeze(reshape(d.data,d.dim(1)*d.dim(2),length(Z),length(T)));
0508 
0509 if d.dim(4)<=1
0510    putimages(newh,d.data,Z);
0511 elseif length(T)==1|length(Z)==1
0512    putimages(newh,d.data,Z,T);
0513 else
0514    for i=1:length(T)
0515       putimages(newh,squeeze(d.data(:,:,i)),Z,T(i));
0516    end
0517 end
0518 
0519 closeimage(newh);
0520 
0521 return
0522 end
0523 
0524 
0525 %%
0526 
0527 function [d]=fmris_write_nifti(d,Z,T)
0528 
0529 if nargin<2
0530    Z=1;
0531    T=1;
0532 end
0533 
0534 if Z(1)==1 & T(1)==1
0535    fidout=fopen(d.file_name,'w');
0536    isniftiparent=0;
0537    if isfield(d,'parent_file')
0538       if exist(d.parent_file)
0539          pf=deblank(d.parent_file);
0540          ext=pf((length(pf)-2):length(pf));
0541          isniftiparent=(ext=='nii');
0542       end
0543    end
0544    if isniftiparent
0545       machineformats='nlbdgcas';
0546       for i=1:length(machineformats)
0547          fid=fopen(d.parent_file,'r',machineformats(i));
0548          sizeof_hdr=fread(fid,1,'int');
0549          if sizeof_hdr==348;
0550             %Must have found a formt that works, so break and continue using this format
0551             break
0552          else
0553             %Need to close if file is not native format
0554             %else if the file format is 's', then 7 stranded files are left orphaned and never closed.
0555             fclose(fid);
0556          end
0557       end
0558       data_type=fread(fid,10,'char');
0559       db_name=fread(fid,18,'char');
0560       extents=fread(fid,1,'int');
0561       session_error=fread(fid,1,'short');
0562       regular=char(fread(fid,1,'char')');
0563       dim_info=char(fread(fid,1,'char')');
0564       dim=fread(fid,8,'short');
0565       intent_p =fread(fid,3,'float');
0566       intent_code =fread(fid,1,'short');
0567       datatype=fread(fid,1,'short');
0568       bitpix=fread(fid,1,'short');
0569       slice_start=fread(fid,1,'short');
0570       pixdim=fread(fid,8,'float');
0571       vox_offset=fread(fid,1,'float');
0572       scl_slope=fread(fid,1,'float');
0573       scl_inter=fread(fid,1,'float');
0574       slice_end=fread(fid,1,'short');
0575       slice_code =char(fread(fid,1,'char')');
0576       xyzt_units =char(fread(fid,1,'char')');
0577       cal_max=fread(fid,1,'float');
0578       cal_min=fread(fid,1,'float');
0579       slice_duration=fread(fid,1,'float');
0580       toffset=fread(fid,1,'float');
0581       glmax=fread(fid,1,'int');
0582       glmin=fread(fid,1,'int');
0583       descrip=char(fread(fid,80,'char')');
0584       aux_file=char(fread(fid,24,'char')');
0585       qform_code =fread(fid,1,'short');
0586       sform_code =fread(fid,1,'short');
0587       quatern_b =fread(fid,1,'float');
0588       quatern_c =fread(fid,1,'float');
0589       quatern_d =fread(fid,1,'float');
0590       qoffset_x =fread(fid,1,'float');
0591       qoffset_y =fread(fid,1,'float');
0592       qoffset_z =fread(fid,1,'float');
0593       srow_x =fread(fid,4,'float');
0594       srow_y =fread(fid,4,'float');
0595       srow_z =fread(fid,4,'float');
0596       intent_name=char(fread(fid,16,'char')');
0597       magic =fread(fid,4,'char');
0598       fclose(fid);
0599    else
0600       data_type='          ';
0601       db_name='                  ';
0602       extents=0;
0603       session_error=0;
0604       regular='r';
0605       dim_info=' ';
0606       slice_start=0;
0607       pixdim=ones(1,8);
0608       slice_code =' ';
0609       xyzt_units =' ';
0610       cal_max=25500;
0611       cal_min=3;
0612       slice_duration=0;
0613       toffset=0;
0614       aux_file='                        ';
0615       qform_code =1;
0616       sform_code =0;
0617       quatern_b =0;
0618       quatern_c =1;
0619       quatern_d =0;
0620       qoffset_x =d.origin(1);
0621       qoffset_y =d.origin(2);
0622       qoffset_z =d.origin(3);
0623       srow_x =[0 0 0 0];
0624       srow_y =[0 0 0 0];
0625       srow_z =[0 0 0 0];
0626       intent_name='                ';
0627    end
0628    
0629    sizeof_hdr=348;
0630    datatype=16;
0631    bitpix=32;
0632    vox_offset=352;
0633    scl_slope =0;
0634    scl_inter =0;
0635    slice_end=0;
0636    glmax=0;
0637    glmin=0;
0638    descrip=['FMRISTAT' repmat(' ',1,72)];
0639    magic =[double('n+1') 0]';
0640    
0641    dim=ones(1,8);      
0642    dim(1)=max(find(d.dim>1));
0643    dim((1:dim(1))+1)=d.dim(1:dim(1));
0644    if isfield(d,'vox')
0645       pixdim=ones(1,8);
0646       pixdim(1)=-1;
0647       pixdim((1:dim(1))+1)=d.vox(1:dim(1));
0648    end
0649    
0650    intent_p=zeros(1,2);
0651    if isfield(d,'df') 
0652       intent_p(1:length(d.df))=d.df; 
0653       intent_code=length(d.df)+2;
0654    else
0655       intent_code=0;
0656    end
0657    
0658    intent_q=zeros(1,2);
0659    if isfield(d,'fwhm'); 
0660       intent_q(1:length(d.fwhm))=d.fwhm; 
0661    end;
0662    intent_q=round(intent_q/100*2^16);
0663    intent_q=(intent_q.*(intent_q>=0)-2^16).*(intent_q<2^16)+2^16;
0664 
0665    descrip=['FMRISTAT' repmat(' ',1,72)];
0666    
0667    fwrite(fidout,sizeof_hdr,'int');
0668    fwrite(fidout,data_type,'char');
0669    fwrite(fidout,db_name,'char');
0670    fwrite(fidout,extents,'int');
0671    fwrite(fidout,session_error,'short');
0672    fwrite(fidout,regular,'char');
0673    fwrite(fidout,dim_info,'char');
0674    fwrite(fidout,dim,'short');
0675    fwrite(fidout,intent_p,'float');
0676    fwrite(fidout,intent_q,'short');
0677    fwrite(fidout,intent_code,'short');
0678    fwrite(fidout,datatype,'short');
0679    fwrite(fidout,bitpix,'short');
0680    fwrite(fidout,slice_start,'short');
0681    fwrite(fidout,pixdim,'float');
0682    fwrite(fidout,vox_offset,'float');
0683    fwrite(fidout,scl_slope ,'float');
0684    fwrite(fidout,scl_inter ,'float');
0685    fwrite(fidout,slice_end,'short');
0686    fwrite(fidout,slice_code,'char');
0687    fwrite(fidout,xyzt_units,'char');
0688    fwrite(fidout,cal_max,'float');
0689    fwrite(fidout,cal_min,'float');
0690    fwrite(fidout,slice_duration,'float');
0691    fwrite(fidout,toffset,'float');
0692    fwrite(fidout,glmax,'int');
0693    fwrite(fidout,glmin,'int');
0694    fwrite(fidout,descrip,'char');
0695    fwrite(fidout,aux_file,'char');
0696    fwrite(fidout,qform_code,'short');
0697    fwrite(fidout,sform_code,'short');
0698    fwrite(fidout,quatern_b,'float');
0699    fwrite(fidout,quatern_c,'float');
0700    fwrite(fidout,quatern_d,'float');
0701    fwrite(fidout,qoffset_x,'float');
0702    fwrite(fidout,qoffset_y,'float');
0703    fwrite(fidout,qoffset_z,'float');
0704    fwrite(fidout,srow_x,'float');
0705    fwrite(fidout,srow_y,'float');
0706    fwrite(fidout,srow_z,'float');
0707    fwrite(fidout,intent_name,'char');
0708    fwrite(fidout,magic,'char');
0709    
0710    fwrite(fidout,0,'float');
0711 else
0712    fidout=fopen(d.file_name,'r+');
0713 end
0714 
0715 if nargin<2
0716    Z=1:d.dim(3);
0717    T=1:d.dim(4);
0718 end
0719 
0720 vox_offset=352;
0721 if ~isfield(d,'precision') | isempty(d.precision); d.precision='float32'; end;
0722 if ~isfield(d,'byte') | isempty(d.byte); d.byte=4; end;
0723 
0724 if Z(1)==1 & T(1)==1 & ~(all(Z==1:d.dim(3)) & all(T==1:d.dim(4)))
0725    for t=1:d.dim(4)
0726       fwrite(fidout,zeros(1,prod(d.dim(1:3))),d.precision);
0727    end
0728 end
0729 
0730 if all(Z>0)&all(Z<=d.dim(3))&all(T>0)&all(T<=d.dim(4))
0731    for t=1:length(T)
0732       for z=1:length(Z)
0733          position=d.byte*((T(t)-1)*prod(d.dim(1:3))+(Z(z)-1)*prod(d.dim(1:2)))+vox_offset;
0734          status=fseek(fidout,position,'bof');
0735          if length(size(d.data))==4
0736             fwrite(fidout,d.data(:,:,z,t),d.precision);
0737          elseif length(T)==1 
0738             fwrite(fidout,d.data(:,:,z),d.precision);
0739          elseif length(Z)==1  
0740             fwrite(fidout,d.data(:,:,t),d.precision);
0741          else
0742             'Slices and/or frames do not match data'
0743          end
0744       end
0745    end
0746 else
0747    'Slices and/or frames out of range:'
0748    Z
0749    T
0750 end
0751 
0752 fclose(fidout);
0753 
0754 return;
0755 end
0756

Generated on Fri 26-Sep-2008 14:05:29 by m2html © 2003