clear all; close all;
dirstr={...
'Antares_066440.21',...
'Antares_066442.21',...
'Antares_066444.21',...
'Antares_066447.21',...
'Antares_066449.21',...
'Antares_066451.21',...
'Antares_066453.21',...
'Antares_066455.21',...
'Antares_066458.21',...
'Antares_066460.21',...
'Antares_066463.21',...
'Antares_066465.21',...
'Antares_066467.21',...
'Antares_066469.21',...
'Antares_066471.21',...
'Antares_066473.21',...
'Antares_066475.21',...
'Antares_066477.21',...
'Antares_066479.21',...
'Antares_066481.21',...
'Antares_066483.21',...
'Antares_066485.21',...
'Antares_066492.21',...
'Antares_066494.21',...
'Antares_066496.21',...
'Antares_066498.21',...
'Antares_066500.21',...
'Antares_066503.21',...
'Antares_066505.21',...
'Antares_066507.21',...
'Antares_066509.21',...
'Antares_066511.21',...
'Antares_066513.21',...
'Antares_066516.21',...
'Antares_066518.21',...
'Antares_066520.21',...
'Antares_066521.21',...
'Antares_066523.21',...
'Antares_066525.21',...
'Antares_066527.21',...
'Antares_066529.21',...
'Antares_066531.21',...
'Antares_066533.21',...
'Antares_066535.21',...
'Antares_066537.21',...
'Antares_066539.21',...
'Antares_066542.21',...
'Antares_066545.21',...
'Antares_066547.21',...
'Antares_066549.21',...
'Antares_066551.21',...
'Antares_066553.21',...
'Antares_066556.21',...
'Antares_066559.21',...
'Antares_066561.21',...
'Antares_066566.21',...
'Antares_066567.21',...
'Antares_066570.21',...
'Antares_066572.21',...
'Antares_066574.21',...
'Antares_066576.21',...
'Antares_066579.21',...
'Antares_066582.21',...
'Antares_066584.21',...
'Antares_066586.21',...
'Antares_066588.21',...
'Antares_066591.21',...
'Antares_066593.21',...
'Antares_066595.21',...
'Antares_066597.21',...
'Antares_066600.21',...
'Antares_066601.21',...
'Antares_066603.21',...
'Antares_066605.21',...
'Antares_066607.21',...
'Antares_066609.21',...
'Antares_066615.21',...
'Antares_066618.21',...
'Antares_066620.21',...
'Antares_066623.21',...
'Antares_066625.21',...
'Antares_066627.21',...
'Antares_066629.21',...
'Antares_066631.21',...
'Antares_066633.21',...
'Antares_066635.21',...
'Antares_066637.21',...
'Antares_066639.21',...
'Antares_066641.21',...
'Antares_066644.21',...
'Antares_066646.21',...
'Antares_066648.21',...
'Antares_066650.21',...
'Antares_066652.21',...
'Antares_066655.21',...
'Antares_066657.21',...
'Antares_066659.21',...
'Antares_066661.21',...
'Antares_066663.21',...
'Antares_066666.21',...
'Antares_066668.21',...
'Antares_066670.21',...
'Antares_066672.21',...
'Antares_066674.21',...
'Antares_066676.21',...
'Antares_066679.21',...
'Antares_066681.21',...
'Antares_066683.21',...
'Antares_066685.21',...
'Antares_066688.21',...
'Antares_066690.21',...
'Antares_066692.21',...
'Antares_066695.21',...
'Antares_066697.21',...
'Antares_066699.21',...
'Antares_066702.21',...
'Antares_066704.21',...
'Antares_066706.21',...
'Antares_066708.21',...
'Antares_066710.21',...
'Antares_066712.21',...
'Antares_066715.21',...
'Antares_066717.21',...
'Antares_066719.21',...
'Antares_066721.21',...
'Antares_066723.21',...
'Antares_066725.21',...
'Antares_066727.21',...
'Antares_066729.21',...
'Antares_066731.21',...
'Antares_066734.21',...
'Antares_066736.21',...
'Antares_066738.21',...
'Antares_066744.21',...
'Antares_066746.21',...
'Antares_066748.21',...
'Antares_066750.21',...
'Antares_066753.21',...
'Antares_066755.21',...
'Antares_066757.21',...
'Antares_066759.21',...
'Antares_066761.21',...
'Antares_066764.21',...
'Antares_066766.21',...
'Antares_066769.21',...
'Antares_066771.21',...
'Antares_066773.21',...
'Antares_066775.21',...
'Antares_066777.21',...
'Antares_066780.21',...
'Antares_066782.21',...
'Antares_066784.21',...
'Antares_066787.21',...
'Antares_066789.21',...
'Antares_066791.21',...
'Antares_066794.21',...
'Antares_066796.21',...
'Antares_066802.21',...
'Antares_066803.21',...
'Antares_066806.21',...
'Antares_066808.21',...
'Antares_066810.21',...
'Antares_066813.21',...
'Antares_066815.21',...
'Antares_066817.21',...
'Antares_066819.21',...
'Antares_066820.21',...
'Antares_066822.21',...
'Antares_066824.21',...
'Antares_066826.21',...
'Antares_066828.21',...
'Antares_066830.21',...
'Antares_066833.21',...
'Antares_066835.21',...
'Antares_066837.21',...
'Antares_066840.21',...
'Antares_066842.21',...
'Antares_066845.21',...
'Antares_066848.21',...
'Antares_066850.21',...
'Antares_066852.21',...
'Antares_066854.21',...
'Antares_066856.21',...
'Antares_066858.21',...
'Antares_066860.21',...
'Antares_066863.21',...
'Antares_066865.21',...
'Antares_066868.21',...
'Antares_066872.21',...
'Antares_066876.21',...
'Antares_066879.21',...
'Antares_066885.21',...
'Antares_066887.21',...
'Antares_066889.21',...
'Antares_066891.21',...
'Antares_066893.21',...
'Antares_066895.21',...
'Antares_066897.21',...
'Antares_066900.21',...
'Antares_066902.21',...
'Antares_067155.21',...
'Antares_067161.21',...
'Antares_067176.21',...
'Antares_067186.21',...
'Antares_067191.21',...
'Antares_067193.21',...
'Antares_067195.21',...
'Antares_067199.21',...
'Antares_067201.21',...
'Antares_067205.21',...
'Antares_067207.21',...
'Antares_067209.21',...
'Antares_067211.21',...
'Antares_067214.21',...
'Antares_067216.21',...
'Antares_067218.21',...
'Antares_067221.21',...
'Antares_067223.21',...
'Antares_067227.21',...
'Antares_067230.21',...
'Antares_067232.21',...
'Antares_067235.21',...
'Antares_067238.21',...
'Antares_067240.21',...
'Antares_067243.21',...
'Antares_067246.21',...
'Antares_067249.21',...
'Antares_067251.21',...
'Antares_067254.21',...
'Antares_067256.21',...
'Antares_067258.21',...
'Antares_067260.21',...
'Antares_067264.21',...
'Antares_067265.21',...
'Antares_067268.21',...
'Antares_067271.21',...
'Antares_067273.21',...
'Antares_067275.21',...
'Antares_067278.21',...
'Antares_067281.21',...
'Antares_067283.21',...
'Antares_067285.21',...
'Antares_067287.21',...
'Antares_067290.21',...
'Antares_067293.21',...
'Antares_067296.21',...
'Antares_067299.21',...
'Antares_067302.21',...
'Antares_067305.21',...
'Antares_067308.21',...
'Antares_067309.21',...
'Antares_067311.21',...
'Antares_067314.21',...
'Antares_067316.21',...
'Antares_067318.21',...
'Antares_067324.21',...
'Antares_067326.21',...
'Antares_067329.21',...
'Antares_067332.21',...
'Antares_067334.21',...
'Antares_067337.21',...
'Antares_067340.21',...
'Antares_067344.21',...
'Antares_067346.21',...
'Antares_067349.21',...
'Antares_067351.21',...
'Antares_067354.21',...
'Antares_067356.21',...
'Antares_067358.21',...
'Antares_067361.21',...
'Antares_067363.21',...
'Antares_067365.21',...
'Antares_067368.21',...
'Antares_067370.21',...
'Antares_067373.21',...
'Antares_067376.21',...
'Antares_067378.21',...
'Antares_067381.21',...
'Antares_067383.21',...
'Antares_067386.21',...
'Antares_067388.21',...
'Antares_067390.21',...
'Antares_067393.21',...
'Antares_067395.21',...
'Antares_067398.21',...
'Antares_067400.21',...
'Antares_067402.21',...
'Antares_067408.21',...
'Antares_067411.21',...
'Antares_067415.21',...
'Antares_067419.21',...
'Antares_067422.21',...
'Antares_067424.21',...
'Antares_067426.21',...
'Antares_067427.21',...
'Antares_067430.21',...
'Antares_067432.21',...
'Antares_067435.21',...
'Antares_067437.21',...
'Antares_067439.21',...
'Antares_067441.21',...
'Antares_067443.21',...
'Antares_067445.21',...
'Antares_067447.21',...
'Antares_067449.21',...
'Antares_067451.21',...
'Antares_067453.21',...
'Antares_067460.21',...
'Antares_067462.21',...
'Antares_067464.21',...
'Antares_067470.21',...
'Antares_067474.21',...
'Antares_067476.21',...
'Antares_067479.21',...
'Antares_067481.21',...
'Antares_067483.21',...
'Antares_067485.21',...
'Antares_067489.21',...
'Antares_067491.21',...
'Antares_067494.21',...
'Antares_067496.21',...
'Antares_067498.21',...
'Antares_067500.21',...
'Antares_067503.21',...
'Antares_067505.21',...
'Antares_067506.21',...
'Antares_067508.21',...
'Antares_067511.21',...
'Antares_067513.21',...
'Antares_067515.21',...
'Antares_067517.21',...
'Antares_067519.21',...
'Antares_067521.21',...
'Antares_067524.21',...
'Antares_067526.21',...
'Antares_067528.21',...
'Antares_067530.21',...
'Antares_067532.21',...
'Antares_067534.21',...
'Antares_067536.21',...
'Antares_067538.21',...
'Antares_067541.21',...
'Antares_067543.21',...
'Antares_067546.21',...
'Antares_067548.21',...
'Antares_067550.21',...
'Antares_067552.21',...
'Antares_067555.21',...
'Antares_067557.21',...
'Antares_067560.21',...
'Antares_067563.21',...
'Antares_067565.21',...
'Antares_067567.21',...
'Antares_067570.21',...
'Antares_067572.21',...
'Antares_067574.21',...
'Antares_067577.21',...
'Antares_067579.21',...
'Antares_067581.21',...
'Antares_067584.21',...
'Antares_067587.21',...
'Antares_067589.21',...
'Antares_067591.21',...
'Antares_067593.21',...
'Antares_067595.21',...
'Antares_067598.21',...
'Antares_067600.21',...
'Antares_067606.21',...
'Antares_067608.21',...
'Antares_067610.21',...
'Antares_067612.21',...
'Antares_067614.21',...
'Antares_067616.21',...
'Antares_067618.21',...
'Antares_067620.21',...
'Antares_067624.21',...
'Antares_067626.21',...
'Antares_067629.21',...
'Antares_067631.21',...
'Antares_067633.21',...
};


mooncalendar2009{8}=0.5*ones(1,200);
mooncalendar2009{9}=0.5*ones(1,200);
mooncalendar2009{10}=0.5*ones(1,200);
mooncalendar2009{11}=0.5*ones(1,200);
mooncalendar2009{8}(100+ [20:27])=linspace(0,0.5, length( 20:27) );
mooncalendar2009{8}(100+ [27:35])=linspace(0.5,1, length( 27:35) );
mooncalendar2009{9}(100+ [-3:4])=linspace(0.5,1, length( -3:4) );
mooncalendar2009{9}(100+ [4:12])=linspace(1,0.5, length( 4:12) );
mooncalendar2009{9}(100+ [12:18])=linspace(0.5,0, length( 12:18) );
mooncalendar2009{9}(100+ [18:26])=linspace(0,0.5, length( 18:26) );
mooncalendar2009{9}(100+ [26:34])=linspace(0.5,1, length( 26:34) );
mooncalendar2009{10}(100+ [-4:4])=linspace(0.5,1, length( -4:4) );
mooncalendar2009{10}(100+ [4:11])=linspace(1,0.5, length( 4:11) );
mooncalendar2009{10}(100+ [11:18])=linspace(0.5,0, length( 11:18) );
mooncalendar2009{10}(100+ [18:26])=linspace(0,0.5, length( 18:26) );
mooncalendar2009{10}(100+ [26:33])=linspace(0.5,1, length( 26:33) );
mooncalendar2009{11}(100+ [-5:2])=linspace(0.5,1, length( -5:2) );
mooncalendar2009{11}(100+ [2:9])=linspace(1,0.5, length( 2:9) );
mooncalendar2009{11}(100+ [9:16])=linspace(0.5,0, length( 9:16) );
mooncalendar2009{11}(100+ [16:24])=linspace(0,0.5, length( 16:24) );
%mooncalendar2009{}(100+])=linspace(1,0.5, length );
%mooncalendar2009{}(100+])=linspace(0.5,0, length );
%mooncalendar2009{}(100+])=linspace(0,0.5, length );
%mooncalendar2009{}(100+])=linspace(0.5,1, length );

for Ctdoaecho=[]
c=1484;
mMicroToSurf1ToBottom2=[2300 400]; SeaDepth=sum(mMicroToSurf1ToBottom2);
step=10;maxWhaleDepth=SeaDepth; maxMicroDist=2000;
surf1bottom2str={'surface','bottom', 'all'};
for surf1bottom2all3=1:3
if surf1bottom2all3<=2   
mMicroToSurf=mMicroToSurf1ToBottom2(surf1bottom2all3);
ALLhoriz=1:maxMicroDist;            
if surf1bottom2all3==1
ALLvert=1:maxWhaleDepth;
else
ALLvert=SeaDepth:-1:(SeaDepth-maxWhaleDepth);    
end;
ALLstephoriz=downsample(ALLhoriz,step);   
ALLstepvert=downsample(ALLvert,step);

tdoa{surf1bottom2all3}=zeros(length(ALLstepvert),length(ALLstephoriz));

for indmEpikitToEpimicro=1:length(ALLstephoriz)
for indmKitToSurf=1:length(ALLstepvert)
mEpikitToEpimicro=ALLstephoriz(indmEpikitToEpimicro);
if surf1bottom2all3==1
mKitToSurf=ALLstepvert(indmKitToSurf);   
else
mKitToSurf=SeaDepth-ALLstepvert(indmKitToSurf); 
end;
mEpimicroEchosurf=(mEpikitToEpimicro*mMicroToSurf)/(mMicroToSurf+mKitToSurf);
mKitMicro=sqrt(mEpikitToEpimicro^2 + (mKitToSurf-mMicroToSurf)^2);
mKitMicroECHO=sqrt(mKitToSurf^2 + (mEpimicroEchosurf-mEpikitToEpimicro)^2) + sqrt(mEpimicroEchosurf^2+mMicroToSurf^2);  
tdoa{surf1bottom2all3}(indmKitToSurf,indmEpikitToEpimicro)=(mKitMicroECHO-mKitMicro)/c;
end;
end;
[X,Y] = meshgrid(ALLstepvert,ALLstephoriz);
[XI,YI] = meshgrid(ALLvert,ALLhoriz);
tdoa{surf1bottom2all3}= interp2(X,Y,tdoa{surf1bottom2all3}',XI,YI);
tdoa{surf1bottom2all3}=tdoa{surf1bottom2all3}'; if surf1bottom2all3==2, tdoa{surf1bottom2all3}=flipud(tdoa{surf1bottom2all3});end;
else
s1=size(tdoa{1});  s2=size(tdoa{2}); s12=min([s1;s2]);  
tdoa{surf1bottom2all3}=(tdoa{1}(1:s12(1),1:s12(2))+tdoa{2}(1:s12(1),1:s12(2)))/2;   
end;

imagesc(tdoa{surf1bottom2all3});text(1850,2300,'Hydro','BackgroundColor',[1 1 1]); colormap('Gray');colorbar;%('location','West');
                        xticklabels =linspace(maxMicroDist,0,8);
                        xticklabels = round(xticklabels*1)/1;
                        xticks = linspace(1, size(tdoa{surf1bottom2all3}, 2), 8);
                        set(gca, 'XTick', xticks, 'XTickLabel', xticklabels);
                        xlabel(['horizontal distance between sound source and hydrophone, m']); ylabel('depth of the sound source, m');
                        title(['TDOA,sec between direct signal and Echo from ' surf1bottom2str{surf1bottom2all3} ' for all source locations'],'FontSize',12);
saveas(gcf,['TDOAEchofrom ' surf1bottom2str{surf1bottom2all3} '.jpg']);
saveas(gcf,['TDOAEchofrom ' surf1bottom2str{surf1bottom2all3} '.eps'],'psc2');
close all;

binBorders{Nbins}=linspace(0.2,0.9,200); 
shallow=reshape(tdoa{surf1bottom2all3}(1:300,:),[],1);
middle=reshape(tdoa{surf1bottom2all3}(300:600,:),[],1);
deep=reshape(tdoa{surf1bottom2all3}(600:900,:),[],1);
minL=min(length(shallow),length(deep));
[Ntdoa] = hist([deep(1:minL), middle(1:minL), shallow(1:minL)],binBorders{Nbins});
figure;bar(binBorders{Nbins},Ntdoa,'');colormap summer;
legend(['deep whale location(600m-900m, mostly hunt); meanTDOAecho=' num2str(nanmean(deep))],...
    ['middle whale location(300m-600m, dive and hunt); meanTDOAecho=' num2str(nanmean(middle))],...
    ['shallow whale location (0-300m, mostly dive); meanTDOAecho=' num2str(nanmean(shallow))],...    
    'Location','NorthOutside');
                        xlabel(['TDOA,sec ']); ylabel('Number of TDOA');
                        title(['Theoretical distribution of TDOA,echo from ' surf1bottom2str{surf1bottom2all3} ' over all whale locations'],'FontSize',14);
saveas(gcf,['TDOAhistEchofrom ' surf1bottom2str{surf1bottom2all3} '.jpg']);
saveas(gcf,['TDOAhistEchofrom ' surf1bottom2str{surf1bottom2all3} '.eps'],'psc2');
close all;
end;


end;


allNbins=[10 15 20]; %allNbins=[5 10 15 20 25 100 200];
maxdiff = 1.2; mindiff = 0.1; 
coefNbins=allNbins(end)./allNbins;
for indNbins=1:length(allNbins)
Nbins=allNbins(indNbins);  
Nmoonperc=Nbins;  %Nmoonperc=100;  
ALLperc=0:Nmoonperc;    
 
meanNUMici{Nbins}=zeros(1,Nbins);
binBorders{Nbins}=linspace(mindiff,maxdiff,Nbins);    
allNUMiciBYday=zeros(length(dirstr),Nbins);
allNUMiciBYmoonperc{Nbins}=zeros(Nmoonperc+1,Nbins);
allMOONpercBYday=zeros(1,length(dirstr));
nNUMiciBYmoonperc=zeros(Nmoonperc+1,1);

for CALCiciBYmoonperc=[1]
for istr=1:length(dirstr)
s=textread([dirstr{istr} '/README.txt'],'%s');
day=s{6}(18:19); day(day=='_')=[]; day=str2num(day);
month=s{6}(20:22); month(month=='_')=[]; month=str2num(month);

dirp=dir([dirstr{istr} '/DETECTION_SPERM_part*.txt']);
p=[];
for idirp=1:length(dirp)
curp=load([dirstr{istr} '/' dirp(idirp).name]);
p=[p;curp(:,3)];
end;
p(p<=0)=[];
p=0.000512*p;
diffp=diff(p);
diffp(diffp<=mindiff)=[];
diffp(diffp>=maxdiff)=[];
[NUMici{Nbins}] = hist(diffp,binBorders{Nbins});

%allMOONpercBYday(istr)=round(mooncalendar2009{month}(100+day)*100);
allMOONpercBYday(istr)=round(mooncalendar2009{month}(100+day)*Nbins);
allNUMiciBYday(istr,:)=NUMici{Nbins};


allMOONpercBYday(istr)



allNUMiciBYmoonperc{Nbins}(1+allMOONpercBYday(istr),:)=allNUMiciBYmoonperc{Nbins}(1+allMOONpercBYday(istr),:)+NUMici{Nbins};
nNUMiciBYmoonperc(1+allMOONpercBYday(istr))=nNUMiciBYmoonperc(1+allMOONpercBYday(istr))+1;

meanNUMici{Nbins}=meanNUMici{Nbins}+NUMici{Nbins};
end;

meanNUMici{Nbins}=meanNUMici{Nbins}/length(dirstr);
for ibin=1:size(allNUMiciBYmoonperc{Nbins},2)
allNUMiciBYmoonperc{Nbins}(:,ibin)=allNUMiciBYmoonperc{Nbins}(:,ibin)./nNUMiciBYmoonperc;
end;

for jphase=1:size(allNUMiciBYmoonperc{Nbins},1)
allNUMiciBYmoonperc{Nbins}(jphase,:)=allNUMiciBYmoonperc{Nbins}(jphase,:)/sum(allNUMiciBYmoonperc{Nbins}(jphase,:));    
end;

existPhases=unique(sort(ALLperc(allMOONpercBYday+1)+1));
[X,Y] = meshgrid(existPhases,1:size(allNUMiciBYmoonperc{Nbins},2));
[XI,YI] = meshgrid(1:size(allNUMiciBYmoonperc{Nbins},1),1:size(allNUMiciBYmoonperc{Nbins},2));
allNUMiciBYmoonpercINTERP{Nbins} = interp2(X,Y,allNUMiciBYmoonperc{Nbins}(existPhases,:)',XI,YI);

save(['iciBYmoonpercjul9.mat'],'allNUMiciBYmoonpercINTERP','allNUMiciBYmoonperc','meanNUMici','binBorders','NUMici');
end;

load(['iciBYmoonpercjul9.mat']); %load(['iciBYmoonperc.mat']); %
for maxSmooth=5 %wrev([4:7])
allNUMiciBYmoonpercINTERPsmooth{Nbins}= allNUMiciBYmoonpercINTERP{Nbins};
for raz=3:maxSmooth
for i=1:size(allNUMiciBYmoonpercINTERPsmooth{Nbins},2)
allNUMiciBYmoonpercINTERPsmooth{Nbins}(:,i)=smooth(allNUMiciBYmoonpercINTERPsmooth{Nbins}(:,i));
end;    
for i=1:size(allNUMiciBYmoonpercINTERPsmooth{Nbins},1)
allNUMiciBYmoonpercINTERPsmooth{Nbins}(i,:)=smooth(allNUMiciBYmoonpercINTERPsmooth{Nbins}(i,:));
end;
% for i=1:size(allNUMiciBYmoonpercINTERPsmooth{Nbins},2)
% allNUMiciBYmoonpercINTERPsmooth{Nbins}(:,i)=smooth(allNUMiciBYmoonpercINTERPsmooth{Nbins}(:,i));
% end;
end;

% PeakLinesICImoonperc{Nbins}=zeros(Nmoonperc+1,allNbins(end));
% for iperc=1:size(allNUMiciBYmoonpercINTERPsmooth{Nbins},2)
% [PeakICImoonperc,LocPeakICImoonperc] = findpeaks(allNUMiciBYmoonpercINTERPsmooth{Nbins}(:,iperc),'MINPEAKDISTANCE',allNbins(end)/10); %min(10,allNbins(end)/10));
% PeakLinesICImoonperc{Nbins}(iperc,coefNbins(indNbins)*LocPeakICImoonperc)=1;%=PeakICImoonperc;
% end;
ydive=100*mean(allNUMiciBYmoonpercINTERPsmooth{Nbins}(:,unique(round([65:90]*Nbins/100))),2);
xdive=linspace(0,100,length(ydive));
figure;plot(xdive,ydive,'lineWidth',2);
                        ylabel(['P(ICI)-P(ICI(full moon)), %']); xlabel('lunar phase, % from full');
                        title(['The probapility of diving-ICI (the diving-ICI at full moon is taken as a baseline)'],'FontSize',13); 
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'divingICIsjul9.jpg']);
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'divingICIsjul9.eps'],'psc2');
close all;

yShallowEcho=mean(allNUMiciBYmoonpercINTERPsmooth{Nbins}(:,unique(round([24:44]*Nbins/100))),2); %(:,34),2); %
yMiddleEcho=mean(allNUMiciBYmoonpercINTERPsmooth{Nbins}(:,unique(round([40:60]*Nbins/100))),2); %(:,50),2); %
yDeepEcho=mean(allNUMiciBYmoonpercINTERPsmooth{Nbins}(:,unique(round([59:79]*Nbins/100))),2); %(:,69),2); %
figure;plot(xdive,yShallowEcho,xdive,yMiddleEcho,xdive,yDeepEcho,'lineWidth',2);
legend('Shallow-dive echo','Middle-dive echo','Deep-dive echo');
                        ylabel(['Shallow, Middle and Deep echo']); xlabel('lunar phase, % from full');
                        title(['The probapility of Shallow, Middle and Deep echo-ICI'],'FontSize',12); 
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'DMSechoICIsjul9.jpg']);
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'DMSechoICIsjul9.eps'],'psc2');
close all;
figure;plot(xdive(1:end-1),100*(yShallowEcho(1:end-1)-yDeepEcho(1:end-1))./yDeepEcho(1:end-1),'lineWidth',2);
                        ylabel(['Shallow-Deep echo, %']); xlabel('lunar phase, % from full');
                        title(['The probapility difference Shallow-Deep echoes as % from Deep echoes'],'FontSize',12); 
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'DSechoICIsjul9.jpg']);
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'DSechoICIsjul9.eps'],'psc2');
close all;
yAverEcho=sum([yShallowEcho,yDeepEcho],2);
figure;hold on;
plot(xdive(1:end-1),100*yShallowEcho(1:end-1)./yAverEcho(1:end-1),'lineWidth',2,'LineStyle','--');
plot(xdive(1:end-1),100*yDeepEcho(1:end-1)./yAverEcho(1:end-1),'lineWidth',2,'LineStyle','-');
legend('Shallow-dive echo, %','Deep-dive echo, %','Location','NorthWest');
                        ylabel(['Shallow and Deep echo, % from total Shallow+Deep echoes']); xlabel('lunar phase, % from full');
                        title(['The Deep or Shallow echoes dependance on lunar phase'],'FontSize',12); 
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'DandSechoICIsjul9.jpg']);
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'DandSechoICIsjul9.eps'],'psc2');
close all;



% figure;imagesc(PeakLinesICImoonperc{Nbins});colormap(1-colormap('Gray'));
%                         xticklabels =linspace(mindiff,maxdiff,8);
%                         xticklabels = round(xticklabels*10)/10;
%                         xticks = linspace(1, size(PeakLinesICImoonperc{Nbins}, 2), 8);
%                         set(gca, 'XTick', xticks, 'XTickLabel', xticklabels);
%                         xlabel(['ICI,sec ']); ylabel('lunar phase, % from full');
%                         title(['Peaks of ICI in dependance on lunar phase'],'FontSize',14);   
% saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'peaksjul9.jpg']);
% saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'peaksjul9.eps'],'psc2');
% close all;
figure;bar(binBorders{Nbins},meanNUMici{Nbins});
                        xlabel(['ICI,sec ']); ylabel('Average number of ICI');
                        title(['Accumulative histogram: ICI over all lunar phases'],'FontSize',14);
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'bar1jul9.jpg']);
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'bar1jul9.eps'],'psc2');
close all;
figure;bar3(allNUMiciBYmoonperc{Nbins});view(130,35);
                        xticklabels =linspace(mindiff,maxdiff,8);
                        xticklabels = round(xticklabels*10)/10;
                        xticks = linspace(1, size(allNUMiciBYmoonpercINTERP{Nbins}', 2), 8);
                        set(gca, 'XTick', xticks, 'XTickLabel', xticklabels);
                        xlabel(['ICI,sec ']); 
                        yticklabels =linspace(0,100,8);
                        yticklabels = round(yticklabels/10)*10;
                        yticks = linspace(1, size(allNUMiciBYmoonpercINTERP{Nbins}', 1), 8);
                        set(gca, 'YTick', yticks, 'YTickLabel', yticklabels);
                        ylabel('lunar phase, % from full');zlabel('Average number of ICI');
                        title(['Distribution of ICI in dependance on lunar phase'],'FontSize',14);
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'bar3jul9.jpg']);
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'bar3jul9.eps'],'psc2');
close all;
% figure;imagesc(allNUMiciBYmoonperc{Nbins});colormap(1-colormap('Gray'));colorbar;
%                         xticklabels =linspace(mindiff,maxdiff,8);
%                         xticklabels = round(xticklabels*10)/10;
%                         xticks = linspace(1, size(allNUMiciBYmoonperc{Nbins}, 2), 8);
%                         set(gca, 'XTick', xticks, 'XTickLabel', xticklabels);
%                         xlabel(['ICI,sec ']); ylabel('lunar phase, % from full');
%                         title(['Distribution of ICI in dependance on lunar phase'],'FontSize',14);
% saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'image.jpg']);
% saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'image.eps'],'psc2');
% close all;
figure;imagesc(allNUMiciBYmoonpercINTERP{Nbins}');colormap(1-colormap('Gray'));colorbar;
                        xticklabels =linspace(mindiff,maxdiff,8);
                        xticklabels = round(xticklabels*10)/10;
                        xticks = linspace(1, size(allNUMiciBYmoonpercINTERP{Nbins}', 2), 8);
                        set(gca, 'XTick', xticks, 'XTickLabel', xticklabels);
                        xlabel(['ICI,sec ']); 
                        yticklabels =linspace(0,100,8);
                        yticklabels = round(yticklabels/10)*10;
                        yticks = linspace(1, size(allNUMiciBYmoonpercINTERP{Nbins}', 1), 8);
                        set(gca, 'YTick', yticks, 'YTickLabel', yticklabels);
                        ylabel('lunar phase, % from full');
                        title(['Distribution of ICI in dependance on lunar phase'],'FontSize',14);
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'imageINTERPjul9.jpg']);
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'imageINTERPjul9.eps'],'psc2');
close all;
figure;imagesc(allNUMiciBYmoonpercINTERPsmooth{Nbins}');colormap(1-colormap('Gray'));colorbar;
                        xticklabels =linspace(mindiff,maxdiff,8);
                        xticklabels = round(xticklabels*10)/10;
                        xticks = linspace(1, size(allNUMiciBYmoonpercINTERPsmooth{Nbins}', 2), 8);
                        set(gca, 'XTick', xticks, 'XTickLabel', xticklabels);
                        xlabel(['ICI,sec ']); 
                        yticklabels =linspace(0,100,8);
                        yticklabels = round(yticklabels/10)*10;
                        yticks = linspace(1, size(allNUMiciBYmoonpercINTERP{Nbins}', 1), 8);
                        set(gca, 'YTick', yticks, 'YTickLabel', yticklabels);
                        ylabel('lunar phase, % from full');
                        title(['Distribution of ICI in dependance on lunar phase'],'FontSize',14);
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'imageINTERPsmoothjul9.jpg']);
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'imageINTERPsmoothjul9.eps'],'psc2');
close all;

allNUMiciBYmoonpercINTERPsmoothTHRESH{Nbins}=zeros(size(allNUMiciBYmoonpercINTERPsmooth{Nbins}));
ALLthreshold=[0.05 0.075 0.095 0.1 0.18] 
for indthreshold=1:length(ALLthreshold) 
threshold=ALLthreshold(indthreshold);     
allNUMiciBYmoonpercINTERPsmoothTHRESH{Nbins}(allNUMiciBYmoonpercINTERPsmooth{Nbins}>threshold)=indthreshold;
end;
figure;imagesc(allNUMiciBYmoonpercINTERPsmoothTHRESH{Nbins}');colormap(1-colormap('Gray')); %colorbar;
                        xticklabels =linspace(mindiff,maxdiff,8);
                        xticklabels = round(xticklabels*10)/10;
                        xticks = linspace(1, size(allNUMiciBYmoonpercINTERPsmoothTHRESH{Nbins}', 2), 8);
                        set(gca, 'XTick', xticks, 'XTickLabel', xticklabels);
                        xlabel(['ICI,sec ']); 
                        yticklabels =linspace(0,100,8);
                        yticklabels = round(yticklabels/10)*10;
                        yticks = linspace(1, size(allNUMiciBYmoonpercINTERP{Nbins}', 1), 8);
                        set(gca, 'YTick', yticks, 'YTickLabel', yticklabels);
                        ylabel('lunar phase, % from full');
                        title(['Distribution of ICI in dependance on lunar phase'],'FontSize',14);
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'imageINTERPsmoothTHRESHjul9.jpg']);
saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'imageINTERPsmoothTHRESHjul9.eps'],'psc2');
close all;
% figure;bar3(allNUMiciBYday);saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'bar3.jpg']);close all;
% figure;imagesc(allNUMiciBYday);saveas(gcf,['Nbins=' num2str(Nbins) 'Smooth=' num2str(maxSmooth) 'bar3.jpg']);close all;    
end;
end;
