function batch_ASDR(fid_prefix,hpr,starthour,endhour)

fpath = ['C:\Users\USER\Documents\mingfaifong\' fid_prefix '\'];
spath = fpath;
load([fpath  fid_prefix '_nowaveform.mat']);

load('empty.SS','-mat');
SS.name = [fid_prefix '_nowaveform'];
SS.clean = cleanspk;
SS.time = spktimes;
SS.channel = channels;
SS.badchannel = [1 8 33 57 64];
SS.badunit = 0;
SS.Save(spath);
clear channels cleanspk spktimes

if nargin<2
    hpr = 24;
end
if nargin<3
    starthour = 0;
end
if nargin<4
    endhour = ceil(max(SS.time)/3600);
end

n_subplots = ceil((endhour-starthour)/hpr);
binsize_options_secs = [1,2,5,10,30,1*60,2*60,5*60,10*60,30*60,1*3600];
binsize_options = [1,2,5,10,30,1,2,5,10,30,1];
binsize_units = {'sec','sec','sec','sec','sec','min','min','min','min','min','hr'};

[evt_times evt_text] = get_events(fid_prefix);

SS.ASDR(1,0,0); %from 1 second bins
asdr_times = SS.asdr(:,1);
asdr_1sec = SS.asdr(:,2);
asdr_med_1min = medfilt1(SS.asdr(:,2),60);
asdr_med_1hr = medfilt1(SS.asdr(:,2),3600,5000);

%keyboard
for i_bin = 0:length(binsize_options)
%for i_bin = 1:1

    close all;
    figure('units','normalized','outerposition',[0 0 1 1])    
    %h_tight = tight_subplot(n_subplots,1,[.08 .04],[.15 .01],[.1 .01]);
    h_tight = tight_subplot(n_subplots,1,[.08 .04],[.15 .08],[.1 .01]);

    for i_subplot = 1:n_subplots
%        subplot(10,1,i_subplot);
        axes(h_tight(i_subplot));
 
        switch i_bin
            case 0
                %%%%%%%%--------medfilt ASDR--------%%%%%%%%%%
                binsize_label = '';
                y_upperlim = max(asdr_med_1min);
                save([fid_prefix '/' fid_prefix '_asdr_medfilt.mat']);
                plot(asdr_times,asdr_1sec,'Color',[.8 .8 .8],'LineWidth',0.25);
                hold on;
                plot(asdr_times,asdr_med_1min,'Color',[.4 .4 .4],'LineWidth',1);
                plot(asdr_times,asdr_med_1hr,'Color',[0 0 0],'LineWidth',2.5);
                fid_suffix = 'ASDR_medfilt';
                %%%%%%%%--------medfilt ASDR--------%%%%%%%%%%
            otherwise
                %%%%%%%%--------regular ASDR--------%%%%%%%%%%
                binsize = binsize_options_secs(i_bin);
                binsize_label = ['bin = ' num2str(binsize_options(i_bin)) binsize_units{i_bin}];
                SS.ASDR(binsize,1,0);
                hold on;
                if endhour*3600 < max(SS.asdr(:,1))
                    asdr_endind = find(SS.asdr(:,1)==endhour*3600);
                else
                    asdr_endind = size(SS.asdr,1);
                end
                asdr_sorted = sortrows(SS.asdr(1:asdr_endind,2));
                y_upperlim = asdr_sorted(round(length(asdr_sorted)*.9999));
                fid_suffix = ['ASDR_bin' binsize_label(7:end)];
                %%%%%%%%--------regular ASDR--------%%%%%%%%%%
        end
        
        axis([((i_subplot-1)*hpr+starthour)*3600 ((i_subplot*hpr)+starthour)*3600 0 y_upperlim]);
        set(gca,'FontSize',11,'TickDir','out','TickLength',[0.002 0.025],...
            'XTick',[((i_subplot-1)*hpr+starthour)*3600:3600:((i_subplot*hpr)+starthour)*3600],...
            'XTickLabel',[(i_subplot-1)*hpr+starthour:1:(i_subplot*hpr)+starthour]);
        set(gcf,'Clipping','off')
 
        evt_lim = xlim;
        evt_ind = find(evt_times>=evt_lim(1) & evt_times<evt_lim(2));
        if evt_ind
            for m = evt_ind(1):evt_ind(end)
                plot([evt_times(m) evt_times(m)],[0 y_upperlim],'--r');
                text(evt_times(m)+(hpr*.005)*3600,y_upperlim*.9,evt_text{m},'Color','r','FontSize',8);
            end
        end
        
        switch i_subplot
            case 1
                xlabel(''); ylabel('');
   %                text((hpr*.82+starthour)*3600,y_upperlim*.9,['bin size = ' binsize_label]);
                    text(((hpr-2.5)+starthour)*3600,y_upperlim*1.2,binsize_label);
            case round(n_subplots/2)
                 xlabel(''); ylabel('Array-Wide Spike Rate (Hz)','FontSize',14);
            case n_subplots
                if i_bin == 0
                    h_leg = legend('Spike Rate (1 sec bins)',...
                        'Median Spike Rate (avg over 1 min)',...
                        'Median Spike Rate (avg over 1 hr)',...
                        'Location','NorthEast');
                    set(h_leg,'FontSize',6);
                    %text(((hpr-3)+starthour)*3600,y_upperlim*1.2,['median-filtered']);
                end
                xlabel([10,'Time (hours)'],'FontSize',14);
                ylabel('');
            otherwise
                xlabel(''); ylabel('');
        end
    
    end

%    keyboard
    print('-depsc',[fid_prefix '/figures/' fid_prefix '_' fid_suffix '.eps']);
    print('-dpng',[fid_prefix '/figures/' fid_prefix '_' fid_suffix '.png']);
    saveas(gcf,[fid_prefix '/figures/' fid_prefix '_' fid_suffix '.fig'],'fig');
 end