function van_bnpm_results

SCCSid = '1.5';
SPMid  = spm('SFnBanner',mfilename,SCCSid);
%-----------------------------------------%
      
%========== set parameters via GUI ==============================% 
%clear all;
H = spm('FnUIsetup','BnPM Results',0);
% Loading the BPM structure
if exist('spm_get')
    BnPMstr_fname  = spm_get(1,'*','Select the BnPMstr.mat file ...', pwd);
else
    BnPMstr_fname  = spm_select(1, 'mat', 'Select the BnPMstr.mat file ...', [], pwd, '.*');
end
[path,name,ext] = fileparts(BnPMstr_fname);
load(BnPMstr_fname);  
% Uncorrected p value or Corrected p value
ptype = spm_input('P value', '+1', 'Uncorrected|Corrected',[],1);
if strcmp(ptype,'Corrected')
    psign = spm_input('T-sign', '+1', '+|-',[],1);
end

close(H)


cd(BnPMstr.result_dir);

tol = 1e-4;

nPiCond = BnPMstr.nPiCond;
npicon = ceil((nPiCond-1)/BnPMstr.njob);
nRows = BnPMstr.dim(1);
nCols = BnPMstr.dim(2);
nSlices = BnPMstr.dim(3);
nPtmpwhole = zeros(nRows,nCols,nSlices);
MaxTwhole = zeros(nPiCond,2);
Vt   = spm_vol(BnPMstr.Tmap0);
T0 = spm_read_vols(Vt);
Vmask = spm_vol(BnPMstr.mask);
brain_mask = spm_read_vols(Vmask);
MaxTwhole(1,1) = max(T0(:));
MaxTwhole(1,2) = -min(T0(:));

for job = 1:BnPMstr.njob
    rfolder = [BnPMstr.result_dir,'Result_Job',num2str(job)];
    ptmpname = fullfile(rfolder,'nPtmp');
    [fid, message] = fopen(ptmpname, 'r', 'b');
    if fid == -1
        error(message);
    end
    [nPtmp, count] = fread(fid, 'double');
    if fid ~= -1
        fclose(fid);
    end
    nPtmp = reshape(nPtmp,nRows,nCols,nSlices);
    nPtmpwhole = nPtmpwhole + nPtmp;
    
    maxtname = fullfile(rfolder,'MaxT');
    [fid, message] = fopen(maxtname, 'r', 'b');
    if fid == -1
        error(message);
    end
    [MaxT, count] = fread(fid, 'double');
    if fid ~= -1
        fclose(fid);
    end
    startperm = 2+npicon*(job-1);
    endperm = min((startperm+npicon-1),nPiCond);    
    MaxT = reshape(MaxT,npicon,2);
    MaxTwhole(startperm:endperm,:) = MaxT;
end

MaxT_pos = MaxTwhole(:,1);
MaxT_neg = MaxTwhole(:,2);

if strcmp(ptype,'Uncorrected')
    punc = nPtmpwhole/nPiCond;
else
    Pc_pos = zeros(size(T0));
    Pc_neg = zeros(nRows,nCols,nSlices);
    if strcmp(psign,'+')
        for t = MaxT_pos'
        %-FEW-corrected p is proportion of randomisation greater or
        % equal to statistic.
        %-Use a > b -tol rather than a >= b to avoid comparing
        % two reals for equality.
        Pc_pos = Pc_pos + (t > T0 -tol);
        end
        Pc_pos = reshape(Pc_pos,nRows,nCols,nSlices);
        Pc_pos = Pc_pos/nPiCond;
    else
        for t = MaxT_neg'
        %-FEW-corrected p is proportion of randomisation greater or
        % equal to statistic.
        %-Use a > b -tol rather than a >= b to avoid comparing
        % two reals for equality.
        Pc_neg = Pc_neg + (t > -T0 -tol);
        end 
        Pc_neg = reshape(Pc_neg,nRows,nCols,nSlices);
        Pc_neg = Pc_neg/nPiCond;
    end
end

statmap = zeros(size(T0));
if strcmp(BnPMstr.Stat,'T')
    if strcmp(ptype,'Uncorrected')
       statmap = Compute_Nonp_Tmap(punc,BnPMstr.dof,brain_mask,statmap,nRows,nCols);
    else
        if strcmp(psign,'+')
            statmap = Compute_Nonp_Tmap(Pc_pos,BnPMstr.dof,brain_mask,statmap,nRows,nCols);
        else
            statmap = Compute_Nonp_Tmap(Pc_neg,BnPMstr.dof,brain_mask,statmap,nRows,nCols);  
        end
    end
else
    if strcmp(ptype,'Uncorrected')
       statmap = Compute_Nonp_Fmap(punc,BnPMstr.dof,brain_mask,statmap,nRows,nCols);
    else
        if strcmp(psign,'+')
            statmap = Compute_Nonp_Fmap(Pc_pos,BnPMstr.dof,brain_mask,statmap,nRows,nCols);
        else
            statmap = Compute_Nonp_Fmap(Pc_neg,BnPMstr.dof,brain_mask,statmap,nRows,nCols);  
        end
    end
end

system(sprintf('mv Tmap1.hdr Tmap0.hdr'));
system(sprintf('mv Tmap1.img Tmap0.img'));
file_name = sprintf('%s',BnPMstr.fname);
Vtemp = spm_vol(file_name);
Vtemp = wfu_bpm_hdr_struct(Vtemp);

Vtemp.fname = fullfile(pwd,'Tmap1.img');   
spm_write_vol(Vtemp, statmap) ;
load('BPM.mat');
wfu_insert_map(BPM);
cd ..
end

