%   BAT.m

%   This software is protected by german copyright and international treaties.             
%   Copyright 2004 Markus Junghöfer & Peter Peyk. All Rights Reserved.                     
%                                                                                          
%   THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE        
%   NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,    
%   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE    
%   OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
%   TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT,       
%   INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE      
%   OR DOCUMENTATION. 


SubVec=1;%[2:3,5:9,11:21]
%SubVec=[11:21]

NSub=length(SubVec)
StatStr='Para';	%LoOff'%'HiOff'	%'LoHi72'	%'HiOff'	%'LoHi'	%Para
                                                                     
Dim=[79 95 68]
ProdDim=prod(Dim)
for SubInd=1:NSub
   ActSub=SubVec(SubInd);
   ActSubStr=int2str(ActSub);
   if ActSub<10
      ActSubStr=['0',ActSubStr];
   end
  	FolderName=['E:\fMRIData\fMRIDataGV\',ActSubStr,'\snraS',ActSubStr,StatStr,'\']
   ConFileMat=spm_get('Files',FolderName,'con_0009.img')
   NFiles=size(ConFileMat,1);
   for FileInd=1:NFiles
      FilePath=ConFileMat(FileInd,:);
      FlipFilePath=FilePath;
      FlipFilePath(length(FlipFilePath)-7)='m';
      FilePathHdr=[FilePath(1:length(FilePath)-3),'hdr'];
      FlipFilePathHdr=FilePathHdr;
      FlipFilePathHdr(length(FlipFilePathHdr)-7)='m';
		fid=fopen(FilePathHdr,'r','ieee-le');
      Hdr=fread(fid,inf,'int8');
      fclose(fid);
      fprintf(1,'Write header to file...\n\n');
      disp(FlipFilePathHdr)
     	fprintf(1,'\n')
      fid=fopen(FlipFilePathHdr,'w','ieee-le');
      fwrite(fid,Hdr,'int8');
      fclose(fid);
      fid=fopen(FilePath,'r','ieee-le');
      [DataMat,Count]=fread(fid,ProdDim,'float');
      fclose(fid);
		DataMat=reshape(DataMat,Dim);
      FlipMat=DataMat;
      for Ind=1:Dim(2)
         FlipMat(:,Ind,:)=(squeeze(DataMat(:,Ind,:))+flipud(squeeze(DataMat(:,Ind,:))))/2;
      end
      fprintf(1,'Write data to file...\n\n');
		disp(FlipFilePath)
		fprintf(1,'\n\n')
		fid=fopen(FlipFilePath,'w','ieee-le');
      fwrite(fid,FlipMat,'float');
      fclose(fid);
   end
end
