% function ActogramGUI(t,x,light,figurewindow,Tcycle)
%
% Makes a figure with the actogram, including light unless enter light=[]
% Script by Tanya Leise, Amherst College

function Actogram(t,activity,light,figurewindow,Tcycle)

activitycolor=0.4*[1 1 1];
darknesscolor=0.85*[1 1 1];

if strcmp(figurewindow,'new')
    figure('Position',[100 50 600 800]);
end

if ~isempty(light)
    light=1.0*(max(light,0)>0); % light=1 dark=0
else
    light=ones(size(t));
end

% dt=median(diff(t));
% tfull=(t(1):dt:t(end))';
% xfull=nan(size(tfull));
% ltfull=ones(size(tfull));
% for k=1:length(activity)
%    [mm,ind]=min(abs(tfull-t(k)));
%    if mm<=dt/2
%        xfull(ind)=activity(k);
%        ltfull(ind)=light(k);
%    end
% end
% t=tfull;activity=xfull;light=ltfull;

tnan=t(isnan(activity));
activity(isnan(activity))=0;

Ndays=floor((t(end)-t(1))/Tcycle+1);
day0=floor(t(1)/Tcycle);
dt=t(2)-t(1);

patch([0 mod(t(1),Tcycle) mod(t(1),Tcycle) 0],[day0 day0 day0+1 day0+1],'w','FaceColor','w','EdgeColor','none');hold on;
for day=1:Ndays
    range=find(t>=(day-1+day0)*Tcycle-2*dt & t<=(day+1+day0)*Tcycle+2*dt);
    
    if numel(range)>10
        tt=t(range);
        
        if ~isempty(light)
            lt=light(range);
            dl=diff(lt);
            lton=find(dl>0);
            nOn=length(lton);
            ltoff=find(dl<0);
            nOff=length(ltoff);
            if lt(1)>0 % starts lights on
                nOn=nOn+1;
                lton=[1;lton;numel(lt)];
                ltoff=[ltoff;numel(lt)];
                for k=1:nOn
                    patch([tt(ltoff(k)) tt(lton(k)) tt(lton(k)) tt(ltoff(k))]-(day+day0-1)*Tcycle,...
                        [day+day0-1 day+day0-1 day+day0 day+day0],'w','FaceColor','w','EdgeColor','none');hold on;
                end
                for k=1:nOff
                    patch([tt(ltoff(k)) tt(lton(k+1)) tt(lton(k+1)) tt(ltoff(k))]-(day+day0-1)*Tcycle,...
                        [day+day0-1 day+day0-1 day+day0 day+day0],'w','FaceColor',darknesscolor,'EdgeColor','none');hold on;
                end
            else % starts lights off
                nOff=nOff+1;
                lton=[lton;numel(lt)];
                ltoff=[1;ltoff;numel(lt)];
                for k=1:nOff
                    patch([tt(ltoff(k)) tt(lton(k)) tt(lton(k)) tt(ltoff(k))]-(day+day0-1)*Tcycle,...
                        [day+day0-1 day+day0-1 day+day0 day+day0],'w','FaceColor',darknesscolor,'EdgeColor','none');hold on;
                end
                for k=1:nOn
                    patch([tt(ltoff(k+1)) tt(lton(k)) tt(lton(k)) tt(ltoff(k+1))]-(day+day0-1)*Tcycle,...
                        [day+day0-1 day+day0-1 day+day0 day+day0],'w','FaceColor','w','EdgeColor','none');hold on;
                end
            end   
        end
        
        today=activity(range)/max(activity);
        today=day+day0-0.8*today;
        area(tt(today>0)-(day+day0-1)*Tcycle,today(today>0),'FaceColor',activitycolor,'BaseValue',day+day0,'EdgeColor','none');hold on;
        
        nanind=find(tnan>=(day-1+day0)*Tcycle & tnan<=(day+1+day0)*Tcycle);
        for k=1:length(nanind)
           patch([tnan(nanind(k)) tnan(nanind(k))+dt tnan(nanind(k))+dt tnan(nanind(k))]-(day+day0-1)*Tcycle,...
               [day+day0-0.8 day+day0-0.8 day+day0 day+day0],nancolor,'FaceColor',nancolor,'EdgeColor','none','FaceAlpha',0.25);hold on;
        end
    end
end
patch([2*Tcycle t(end)-(Ndays+day0-2)*Tcycle t(end)-(Ndays+day0-2)*Tcycle 2*Tcycle],[Ndays+day0-1 Ndays+day0-1 Ndays+day0-2 Ndays+day0-2],'w','FaceColor','w','EdgeColor','none');
patch([2*Tcycle t(end)-(Ndays+day0-1)*Tcycle t(end)-(Ndays+day0-1)*Tcycle 2*Tcycle],[Ndays+day0-1 Ndays+day0-1 Ndays+day0 Ndays+day0],'w','FaceColor','w','EdgeColor','none');

if Ndays>12
    skip=floor(Ndays/12);
    set(gca,'YDir','reverse','FontSize',14,'YTick',(0:skip:Ndays)+day0+.5,'YTickLabel',(0:skip:Ndays)+day0,'XTick',0:Tcycle/2:2*Tcycle);
else
    set(gca,'YDir','reverse','FontSize',14,'YTick',(0:Ndays)+day0+.5,'YTickLabel',(0:Ndays)+day0,'XTick',0:Tcycle/2:2*Tcycle);
end
xlabel('Time in hours (double-plotted) ');
ylabel('Day ');
axis([0 2*Tcycle day0 day0+Ndays]);