%   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:10,12:21]
%SubVec=[11:21]
SubVec=[3 4 5 6 7 9 10 11 12 13 14 15 16 17 18 20 21 22 23 24 25]
ConVec=[22 44 66]
NSub=length(SubVec)
                                                                     
NCon=length(ConVec)

%StatStr='HiOff'%'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
    for ConInd=1:NCon
        ActCon=ConVec(ConInd);
        ActConStr=int2str(ActCon);
        if ActCon<10
            ActConStr=['000',ActConStr];
        elseif ActCon>9 & ActCon<100
            ActConStr=['00',ActConStr];
        elseif ActCon>99 & ActCon<1000
            ActConStr=['0',ActConStr];
        end
        FolderName=['D:\ValAro\',ActSubStr,'\snraS',ActSubStr,'Base0Smooth08']
        %FolderName=['K:\fMRIData\fMRIDataGV\',ActSubStr,'\Smooth4\snraS',ActSubStr,StatStr,'\']
        %ConFileMat=spm_get('Files',FolderName,'mean.img')
        %ConFileMat=spm_get('Files',FolderName,'con_0009.img') %HighAll Subject 1
        ['con_',ActConStr,'.img']
        ConFileMat=spm_get('Files',FolderName,['con_',ActConStr,'.img'])
        NFiles=size(ConFileMat,1);
        for FileInd=1:NFiles
            FilePath=ConFileMat(FileInd,:);
            FlipFilePath=FilePath;
            FlipFilePath(length(FlipFilePath)-7)='f';
            FilePathHdr=[FilePath(1:length(FilePath)-3),'hdr'];
            FlipFilePathHdr=FilePathHdr;
            FlipFilePathHdr(length(FlipFilePathHdr)-7)='f';
            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');
            %[DataMat,Count]=fread(fid,ProdDim,'int16');
            fclose(fid);
            DataMat=reshape(DataMat,Dim);
            FlipMat=DataMat;
            for Ind=1:Dim(2)
                FlipMat(:,Ind,:)=flipud(squeeze(DataMat(:,Ind,:)));
            end
            %FlipMat=FlipMat+5.*rand(size(FlipMat));
            fprintf(1,'Write data to file...\n\n');
            disp(FlipFilePath)
            fprintf(1,'\n\n')
            fid=fopen(FlipFilePath,'w','ieee-le');
            %fwrite(fid,FlipMat,'int16'); 
            %       a=find(~isnan(FlipMat));
            %       FlipMat(a)=FlipMat(a)./FlipMat(a);
            fwrite(fid,FlipMat,'float'); 
            %fwrite(fid,DataMat+5.*rand(size(FlipMat)),'int16');
            fclose(fid);
        end
    end
end
