%This is a program that calculates the relation between Spike triggered
%landmark peak, direction preference and location of spike triggered EOD
%peak
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,'_Stage8_output.mat'));
load(strcat(exp_date,'_Stage9_output_v6.mat'));
load(strcat(exp_date,'_Stage8_output2.mat'));

[a,b]=size(uname);
w1=-0.5;% w1 second delay before each spike and w2 s delay after
w2=0.5;
res2=0.01;
interval=w1:res2:w2;
angles=[0,180];%bin centers for the swim dir histogram
for tn=1:length(filename)

    for i=1:a
        if isempty(uname{i,tn})
            uname{i,tn}='NO';
        end
    end
end

k=1;
for tn=1:length(filename)
    for i=1:length(nuraw_count{tn})
        unitnames(k,:)=uname{i,tn};
        unitcounts(k)=nuraw_count{tn}(i);
        k=k+1;
    end
end
goodcells=unique(unitnames(:,2));
for w=1:length(goodcells)
    cellname=strcat('U',goodcells(w))
    stlmUname(w)
    index=strmatch(cellname,uname);
    [i,j]=ind2sub(size(uname),index);%i is trial number, j is the cell position in the matrix
    %for each cell, go through all the trials
    z=1;
    Theta_interp_pool{w}=[];
    Theta_crct_pool{w}=[];
    
    for k=1:length(index)
        Theta_interp_pool{w}=[Theta_interp_pool{w};Theta_interp{i(k),j(k)}];
        Theta_crct_pool{w}=[Theta_crct_pool{w};Theta_crct{j(k)}];
    end
    [a1,e]=histcounts(Theta_interp_pool{w},linspace(0,180,3),'Normalization','count');
    [a2,e]=histcounts(Theta_crct_pool{w},linspace(0,180,3),'Normalization','count');
    FR_Theta_pool_norm{w}=a1./(a2/15);%this is rate per second
%     figure
%     plot(interval(3:end-2),smooth(spk_trig_ave_EOD_rate{w}(3:end-2)),'k-');   
     [m,idx]=max(FR_Theta_pool_norm{w});
%     plot(angles(index),ThetaLM(w),'*')
%     hold on
    dir_index(w)=(1-dir_ratio(w))./(1+dir_ratio(w));

    DirThetaLM(w,:)=[angles(idx),ThetaLM(w)];
    ud_pref(w)=index_side_pref(w,1);
    lr_pref(w)=index_side_pref(w,2);

end
%%save(strcat(exp_date,'_Stage12_output_v6.mat'),'ud_pref','lr_pref','dir_index','FR_Theta_pool_norm','ThetaLM','spk_trig_ave_EOD_rate','goodcells','stlmUname','DirThetaLM');

