%this is a program to tag the spikes that occurred near landmarks and those
%that were outside of landmarks 
clear all
close all
exp_date=input('please type in the experiment date : ','s');
load(strcat(exp_date,'_Stage1_output.mat'));
load(strcat(exp_date,'_Stage2_output.mat'));
load(strcat(exp_date,'_Stage4_output.mat'));

y=0:80:1600;
x=0:80:1200;
resy=1600/80;
resx=1200/80;
ang=0:0.01:2*pi;
Dout=75;
Din=75;
alpha=30;
fs=500;%sampling rate for the position of the fish (interpolated)
for tn=1:length(filename)

    if filename{tn}(11)=='1' || filename{tn}(11)=='3' || filename{tn}(11)=='4' || filename{tn}(11)=='5'
        load(strcat('landmark_coordinates_',filename{tn}(1:8),'_t3.mat'));
    
    %plot the landmarks
    figure(1)
    plot(tank_circle(1,:),(1200-tank_circle(2,:)),'b','LineWidth',0.5)
    hold on
    tank_circle2(1,:)=tank_circle(1,:)-Dout*cos(ang);
    tank_circle2(2,:)=tank_circle(2,:)-Dout*sin(ang);

    plot(hc(:,1),(1200-hc(:,2)))
    plot(stp(:,1),(1200-stp(:,2)),'b','LineWidth',0.5)
    plot(ltp(:,1),(1200-ltp(:,2)),'b','LineWidth',0.5)
    plot(sqp(:,1),(1200-sqp(:,2)),'b','LineWidth',0.5)
    plot(sc(1,:),(1200-sc(2,:)),'b','LineWidth',0.5)
    plot(lc(1,:),(1200-lc(2,:)),'b','LineWidth',0.5)
    plot(filter(:,1),(1200-filter(:,2)),'b','LineWidth',0.5)
    %Dout cm area around home
    hc2=hc
    plot(hc2(:,1),(1200-hc2(:,2)))   
    %Dout cm area around small triangle
    stp2(1,1)=stp(1,1)-(Dout*sind(alpha));
    stp2(1,2)=stp(1,2)+(Dout*cosd(alpha));
    
    stp2(3,1)=stp(3,1)+Dout;
    stp2(3,2)=stp(3,2);
    
    stp2(2,1)=stp(2,1)-(Dout*sind(alpha));
    stp2(2,2)=stp(2,2)-(Dout*cosd(alpha));
    
    stp2(4,:)=stp2(1,:);
    
    plot(stp2(:,1),(1200-stp2(:,2)),'r')
    stp2grid=polygrid(stp2(:,1),1200-stp2(:,2),(1/80));
    
    %same around the large triangle
    
    ltp2(1,1)=ltp(1,1)-(Dout*sind(alpha));
    ltp2(1,2)=ltp(1,2)+(Dout*cosd(alpha));
    
    ltp2(3,1)=ltp(3,1)+Dout;
    ltp2(3,2)=ltp(3,2);
    
    ltp2(2,1)=ltp(2,1)-(Dout*sind(alpha));
    ltp2(2,2)=ltp(2,2)-(Dout*cosd(alpha));
    
    ltp2(4,:)=ltp2(1,:);
    
    plot(ltp2(:,1),(1200-ltp2(:,2)),'r')
    ltp2grid=polygrid(ltp2(:,1),1200-ltp2(:,2),(1/80));
    % plot(ltp2grid(:,1),ltp2grid(:,2),'*')
    
    %Dout cm area around the small square
    sqp2(1,:)=sqp(1,:)-Dout;
    sqp2(2,2)=sqp(2,2)-Dout;
    sqp2(2,1)=sqp(2,1)+Dout;
    sqp2(3,1)=sqp2(2,1);
    sqp2(3,2)=sqp(3,2)+Dout;
    sqp2(4,1)=sqp2(1,1);
    sqp2(4,2)=sqp2(3,2);
    sqp2(5,:)=sqp2(1,:);
    plot(sqp2(:,1),(1200-sqp2(:,2)),'r')
    sqp2grid=polygrid(sqp2(:,1),1200-sqp2(:,2),(1/80));
    %plot(sqp2grid(:,1),sqp2grid(:,2),'*')
    
    %Dout cm radius around the circles
    sc2(1,:)=sc(1,:)+Dout*cos(ang);
    sc2(2,:)=sc(2,:)+Dout*sin(ang);
    plot(sc2(1,:),1200-sc2(2,:),'r')
    sc2grid=polygrid(sc2(1,:),1200-sc2(2,:),(1/80));
    %plot(sc2grid(:,1),sc2grid(:,2),'*')
    
    lc2(1,:)=lc(1,:)+Dout*cos(ang);
    lc2(2,:)=lc(2,:)+Dout*sin(ang);
    plot(lc2(1,:),1200-lc2(2,:),'r')
    lc2grid=polygrid(lc2(1,:),1200-lc2(2,:),(1/80));
    %plot(lc2grid(:,1),lc2grid(:,2),'*')
    
    %Dout cm radius around the filter staying within the tank
    filter2(1,1)=filter(1,1)-Dout;
    filter2(1,2)=filter(1,2)-Dout;
    filter2(2,1)=filter(2,1)+Dout;
    filter2(2,2)=filter(2,2)-Dout;
    filter2(3,1)=filter(3,1)+Dout;
    filter2(3,2)=filter(3,2);
    filter2(4,1)=filter(4,1)-Dout;
    filter2(4,2)=filter(4,2);
    filter2(5,:)=filter2(1,:);
    
    plot(filter2(:,1),(1200-filter2(:,2)),'r')
    filter2grid=polygrid(filter2(:,1),1200-filter2(:,2),(1/80));
   else
        load(strcat('landmark_coordinates_',filename{tn},'.mat'));
        %plot the landmarks
        figure(1)
        plot(tank_circle(1,:),(1200-tank_circle(2,:)),'k')
        hold on
        %plot(hc(:,1),(1200-hc(:,2)))
        plot(stp(:,1),(1200-stp(:,2)),'k')
        plot(ltp(:,1),(1200-ltp(:,2)),'k')
        plot(sqp(:,1),(1200-sqp(:,2)),'k')
        plot(sc(1,:),(1200-sc(2,:)),'k')
        plot(lc(1,:),(1200-lc(2,:)),'k')
        plot(filter(:,1),(1200-filter(:,2)),'k')

        %Dout cm area around small triangle
        stp2(1,1)=stp(1,1)-(Dout*sind(alpha));
        stp2(1,2)=stp(1,2)-(Dout*cosd(alpha));

        stp2(3,1)=stp(3,1);
        stp2(3,2)=stp(3,2)+Dout;

        stp2(2,1)=stp(2,1)+(Dout*sind(alpha));
        stp2(2,2)=stp(2,2)-(Dout*cosd(alpha));

        stp2(4,:)=stp2(1,:);

        plot(stp2(:,1),(1200-stp2(:,2)),'r')
        stp2grid=polygrid(stp2(:,1),1200-stp2(:,2),(1/80));

        %same around the large triangle

        ltp2(1,1)=ltp(1,1)-(Dout*sind(alpha));
        ltp2(1,2)=ltp(1,2)+(Dout*cosd(alpha));

        ltp2(3,1)=ltp(3,1)+Dout;
        ltp2(3,2)=ltp(3,2);

        ltp2(2,1)=ltp(2,1)-(Dout*sind(alpha));
        ltp2(2,2)=ltp(2,2)-(Dout*cosd(alpha));

        ltp2(4,:)=ltp2(1,:);

        plot(ltp2(:,1),(1200-ltp2(:,2)),'r')
        ltp2grid=polygrid(ltp2(:,1),1200-ltp2(:,2),(1/80));
        % plot(ltp2grid(:,1),ltp2grid(:,2),'*')

        %Dout cm area around the small square
        sqp2(1,:)=sqp(1,:)-Dout;
        sqp2(2,2)=sqp(2,2)-Dout;
        sqp2(2,1)=sqp(2,1)+Dout;
        sqp2(3,1)=sqp2(2,1);
        sqp2(3,2)=sqp(3,2)+Dout;
        sqp2(4,1)=sqp2(1,1);
        sqp2(4,2)=sqp2(3,2);
        sqp2(5,:)=sqp2(1,:);
        plot(sqp2(:,1),(1200-sqp2(:,2)),'r')
        sqp2grid=polygrid(sqp2(:,1),1200-sqp2(:,2),(1/80));
        %plot(sqp2grid(:,1),sqp2grid(:,2),'*')

        %Dout cm radius around the circles
        sc2(1,:)=sc(1,:)+Dout*cos(ang);
        sc2(2,:)=sc(2,:)+Dout*sin(ang);
        plot(sc2(1,:),1200-sc2(2,:),'r')
        sc2grid=polygrid(sc2(1,:),1200-sc2(2,:),(1/80));
        %plot(sc2grid(:,1),sc2grid(:,2),'*')

        lc2(1,:)=lc(1,:)+Dout*cos(ang);
        lc2(2,:)=lc(2,:)+Dout*sin(ang);
        plot(lc2(1,:),1200-lc2(2,:),'r')
        lc2grid=polygrid(lc2(1,:),1200-lc2(2,:),(1/80));
        %plot(lc2grid(:,1),lc2grid(:,2),'*')

        %Dout cm radius around the filter staying within the tank
        filter2(1,1)=filter(1,1)-Dout;
        filter2(1,2)=filter(1,2)-Dout;
        filter2(2,1)=filter(2,1)+Dout;
        filter2(2,2)=filter(2,2)-Dout;
        filter2(3,1)=filter(3,1)+Dout;
        filter2(3,2)=filter(3,2);
        filter2(4,1)=filter(4,1)-Dout;
        filter2(4,2)=filter(4,2);
        filter2(5,:)=filter2(1,:);

        plot(filter2(:,1),(1200-filter2(:,2)),'r')
        filter2grid=polygrid(filter2(:,1),1200-filter2(:,2),(1/80));
   
    end
    a=length(nuraw_count{tn});
    for j=1:a %for each unit, find the position of the animal when there was a spike. For each unit then make a vector indicating if the spike occurred near or away from landmarks
       % nutimes_out{j,tn}=zeros(size(nutimes{j,tn}));
        y_temp=interp1(xvalues{tn},hd_crct{tn}(:,2),nutimes{j,tn},'spline');
        x_temp=interp1(xvalues{tn},hd_crct{tn}(:,1),nutimes{j,tn},'spline');
        index=1:length(x_temp);

        out_tank=inpolygon(x_temp,y_temp,tank_circle2(1,:),1200-tank_circle2(2,:));
        in_home=inpolygon(x_temp,y_temp,hc2(:,1),1200-hc2(:,2));
        in_filter=inpolygon(x_temp,y_temp,filter2(:,1),1200-filter2(:,2));
        in_ltp=inpolygon(x_temp,y_temp,ltp2(:,1),1200-ltp2(:,2));
        in_stp=inpolygon(x_temp,y_temp,stp2(:,1),1200-stp2(:,2));
        in_sqp=inpolygon(x_temp,y_temp,sqp2(:,1),1200-sqp2(:,2));
        in_sc=inpolygon(x_temp,y_temp,sc2(1,:),1200-sc2(2,:));
        in_lc=inpolygon(x_temp,y_temp,lc2(1,:),1200-lc2(2,:));
        %index_out=find(out_tank==1);

        if filename{tn}(11)=='1' 
            index_out=find(in_home==0 & out_tank==1 );
        elseif filename{tn}(11)=='3' 
            index_out=find(in_filter==0 & in_ltp==0 & in_stp==0 & in_sqp==0 & in_sc==0 & in_lc==0 & out_tank==1);

        elseif filename{tn}(11)=='4'

            index_out=find(in_filter==0 & in_stp==0 & in_sqp==0 & in_sc==0 & in_lc==0& out_tank==1);

        elseif filename{tn}(11)== '5' || filename{tn}(11)== '6' || filename{tn}(11)== '7'

            index_out=find(in_filter==0 & in_ltp==0 & in_stp==0 & in_sqp==0 & in_lc==0& out_tank==1);

        else
            index_out=find(in_filter==0 & in_ltp==0 & in_stp==0& out_tank==1);

        end
        figure(1)
    plot(x_temp(index_out),y_temp(index_out),'k.');
    hold on
    
    %plot(x_temp(~ismember(index,index_out)),y_temp(~ismember(index,index_out)),'r.')
    nutimes_out{j,tn}=nutimes{j,tn}(index_out);
        spkcnt_in_out{j,tn}=[length(~ismember(index,index_out)),length(index_out)]
    %nutimes_in{j,tn}=nutimes{j,tn}((~ismember(index,index_out)));
    figure(2)
    plot(length(~ismember(index,index_out)),length(index_out),'o')
    hold on
    

   % nutimes_in{j,tn}=nutimes{j,tn}((~ismember(index,index_out)));
    end
    

end
clear index
clear index_out
    figure(2)
    plot(1:350,1:350,'k')
    hold on
save(strcat(filename{1}(1:8),'spkcnt_in_out.mat'),'spkcnt_in_out')