%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;
alpha=30;
fs=500;%sampling rate for the position of the fish (interpolated)
Dout=75
Din=75
for tn=1:length(filename)
    
    load(strcat('landmark_coordinates_',filename{tn}(1:8),'.mat'));
    
    figure(1)
    plot(tank_circle(1,:),(1200-tank_circle(2,:)),'k')
    tank_circle2(1,:)=tank_circle(1,:)-Dout*cos(ang);
    tank_circle2(2,:)=tank_circle(2,:)-Dout*sin(ang);
    
    hold on
    plot(hc(:,1),(1200-hc(:,2)))
    plot(stp(:,1),(1200-stp(:,2)))
    plot(ltp(:,1),(1200-ltp(:,2)))
    plot(sqp(:,1),(1200-sqp(:,2)))
    plot(sc(1,:),(1200-sc(2,:)))
    plot(lc(1,:),(1200-lc(2,:)))
    plot(filter(:,1),(1200-filter(:,2)))
    
    %% plot a 10 cm area around the landmark
    
    %look at a Dout pixel (10 cm) distance around the home
    hc2(1,:)=hc(1,:)-Dout;
    hc2(2,2)=hc(2,2)-Dout;
    hc2(2,1)=hc(2,1)+Dout;
    hc2(3,1)=hc2(2,1);
    hc2(3,2)=hc(3,2)+Dout;
    hc2(4,1)=hc2(1,1);
    hc2(4,2)=hc2(3,2);
    hc2(5,:)=hc2(1,:);
    plot(hc2(:,1),(1200-hc2(:,2)))
    hc2grid=polygrid(hc2(:,1),1200-hc2(:,2),(1/80));
    %plot(hc2grid(:,1),hc2grid(:,2),'*')
    
    %10 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)))
    stp2grid=polygrid(stp2(:,1),1200-stp2(:,2),(1/80));
    %plot(stp2grid(:,1),stp2grid(:,2),'*')
    
    %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)))
    ltp2grid=polygrid(ltp2(:,1),1200-ltp2(:,2),(1/80));
    % plot(ltp2grid(:,1),ltp2grid(:,2),'*')
    
    %10 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)))
    sqp2grid=polygrid(sqp2(:,1),1200-sqp2(:,2),(1/80));
    %plot(sqp2grid(:,1),sqp2grid(:,2),'*')
    
    %10 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,:))
    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,:))
    lc2grid=polygrid(lc2(1,:),1200-lc2(2,:),(1/80));
    %plot(lc2grid(:,1),lc2grid(:,2),'*')
    
    %10 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)))
    filter2grid=polygrid(filter2(:,1),1200-filter2(:,2),(1/80));
    
    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);
        in_home=inpolygon(x_temp,y_temp,hc2(:,1),1200-hc2(:,2));
        out_tank=inpolygon(x_temp,y_temp,tank_circle2(1,:),1200-tank_circle2(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(in_home==0 & in_filter==0 );
        if tn==1
            index_out=find(in_home==0 & in_filter==0 & out_tank==1 );
        elseif tn<6
            index_out=find(in_home==0 & in_filter==0 & in_ltp==0 & in_stp==0 & in_sqp==0 & in_sc==0 & in_lc==0& out_tank==1 );
        else
            index_out=find(in_filter==0 & in_ltp==0 & in_stp==0 & in_sqp==0 & in_sc==0 & in_lc==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:150,1:150,'k')
    hold on
%save(strcat(filename{1}(1:8),'spkcnt_in_out.mat'),'spkcnt_in_out')