function detrate = detspike_mar(data,D,name,rank,DCTthresh,spike,slope)

numdays = 3;
ld = length(data);
datarange = (max(data)-min(data))
height = datarange*2;
initpoint = 30;
n = 1;
fid = fopen([name '.spikedetection.mar' num2str(rank) '.slope.' num2str(slope)],'w');
outlist = [];
detrate = [];
%collecting outliers for the sensitivity plot
do1(1:ld,1:50) = 0;
do2(1:ld,1:50) = 0;
do3(1:ld,1:50) = 0;
heightind = 1;

pdlist = predictions(data,n,rank,[]);   %get the original predictions for all of the data (not including spikes)
while height  >= 0.00005
    fprintf('Considering height of %f\n',height);
    fprintf(fid,'Considering height of %f\n',height);
    fprintf(fid,'Period             Points in the model   Number of outliers\n');
    out1 = [];
day(1:3) = 0;
    for i = initpoint:ld-numdays
        data_spike = data;
        dsl=i+numdays;
        fprintf(fid,'%s-%s  ',num2str(D(1,1:8)),num2str(D(dsl,1:8)));
        data_spike(i:dsl) = data(i:dsl)+spike*height;
        [outliers,outdates,sigmas]=spikedetsystem_mar(fid,data_spike(1:dsl),i,pdlist,n,rank,D,[],DCTthresh);
        fprintf(fid,'%d                      %d\n',dsl,(size(outliers,1)));
        fprintf(fid,'Detected outliers: %s \n',num2str(outdates(:,1:8)'));
        fprintf(fid,'Sigmas: %d %f \n',sigmas');
        out1 = [out1;size(outliers,1)];
        if ~isempty(outliers)
            for j = 1:numdays            
                if  find(outliers<=i+j)   %including the previous day
                   day(j) = day(j) + 1;
                end;
            end;
          if find(outliers == i+1)
            do1(i+1,heightind) = 1;
          end;
          if find(outliers == i+2)
            do2(i+2,heightind) = 1;
          end;
          if find(outliers == i+3)
            do3(i+3,heightind) = 1;
          end;
      end; 
            
    end;
    heightind = heightind + 1; 
    day
    detrate = [detrate; height day/(ld-numdays-initpoint)];

    height = height - height/4;
    outlist = [outlist out1];
end;
fclose(fid);

do1 = do1(:,1:heightind-1);
do2 = do2(:,1:heightind-1);
do3 = do3(:,1:heightind-1);

%figure;
%plot(detrate(:,1),detrate(:,2:size(detrate,2)));

% %colormap('gray');
% 
% figure;
% subplot(4,1,1);
% plot(initpoint+1:ld,data(initpoint+1:ld),'black');
% xlim([initpoint+1 ld]);
% title([name ' Dataset. System MAR' int2str(rank) '. Spike slope ' num2str(slope)]);
% set(gca,'XTick',initpoint+1:60:ld);
% set(gca,'XTickLabel',num2str(D(initpoint+1:60:ld,1:8)));
% set(gca,'XGrid','on');
% 
% %colormap('gray');
% subplot(4,1,2);
% bar(do1,'stack');
% title('Detected on day 1');
% xlim([initpoint+1 ld]);
% ylim([0 heightind]);
% set(gca,'XTick',initpoint+1:60:ld);
% set(gca,'XTickLabel',num2str(D(initpoint+1:60:ld,1:8)));
% grid on
% 
% %colormap('gray');
% subplot(4,1,3);
% bar(do2,'stack');
% title('Detected on day 2');
% xlim([initpoint+1 ld]);
% ylim([0 heightind]);
% set(gca,'XTick',initpoint+1:60:ld);
% set(gca,'XTickLabel',num2str(D(initpoint+1:60:ld,1:8)));
% grid on
% 
% %colormap('gray');
% subplot(4,1,4);
% bar(do3,'stack');
% title('Detected on day 3');
% xlim([initpoint+1 ld]);
% ylim([0 heightind]);
% %colorbar('vert');
% set(gca,'XTick',initpoint+1:60:ld);
% set(gca,'XTickLabel',num2str(D(initpoint+1:60:ld,1:8)));
% grid on