% Pernille Tønnesen
% 2020 Jan 2

clear

% Define plot position
f=figure(26),clf;
scrsz = get(0,'ScreenSize');
set(f,'Position',[scrsz(3)*0.01 scrsz(4)*0.05 scrsz(3)*0.92 scrsz(4)*0.9]);
prop0 = 2;
propx = 22;
propy = 25;
set(gcf, 'Units', 'Centimeters', 'Position', [prop0, prop0-2, propx, propy], 'PaperUnits', 'Inches', 'PaperSize', [propx, propy])

x1 = 0.13;
x2 = 0.88;
x3 = 0.96;
y1 = 0.55;
y2 = 0.4;
y3 = 0.1;

AX1 = axes('Position',[x1 y1  x3-x1 0.95-y1]);
AX2 = axes('Position',[x1 y2  x2-x1 y1-y2]);
AX3 = axes('Position',[x1 y3  x2-x1 y2-y3]);
AX4 = axes('Position',[0.43 0.21  0.233 0.18]);
AX6 = axes('Position',[0.67 0.25  0.21 0.11]);
AX7 = axes('Position',[0 0 1 1]);


% ADJUSTMENTS
type = 2;           % 1 = echoes per min, 2 = 0-1 of max prey dens
daynight = 1;       % 1 = all dives seperate, 2 = day and night separate
gray = 2;           % 1 = adds grey color in areas deeper than sampled
stip = 1;           % 1 = adds black line for tag movement
botpres = 1;        % 1 = adds brown line for bottom
tagblow = 1;        % 1 = adds zoomed in pics of tag next to blow hole

% DIVE PROFILE
axes(AX1)
load('C:\MATLAB_pt\SW_AcousticScene\Dive profiles\Scripts\Make prh\Data\PRH\PRH_sw17_196a.mat')
tagon = 10.365;  % in hours 10:21:54 UTC = Azores
y1 = 700;
y2 = 0;
ms1 = 15;
ms2 = 25;
cc = 0.66;

% Prey dens
load('C:\MATLAB_pt\SW_AcousticScene\Data\sw17_196a_dive.mat');
pas = 'C:\MATLAB_pt\SW_AcousticScene\Submission\Figures\';
load([pas 'frac.mat'])
load([pas 'fracmax.mat'])
load([pas 'xd_echo.mat'])

if daynight == 1
    for jdive = 1:27
        if jdive ~= 17 & jdive ~= 21 & jdive ~= 23
            y(:,1) = xd_echo;
            y(:,2) = xd_echo;
            
            if jdive == 1
                x(:,1) = zeros(size(y,1),1)+10.2;
            elseif jdive == 18 || jdive == 22 || jdive == 24
                x(:,1) = zeros(size(y,1),1)+tagon+(diving(jdive-2).dive_end+(diving(jdive).dive_start-diving(jdive-2).dive_end)/2)/3600;
            else
                x(:,1) = zeros(size(y,1),1)+tagon+(diving(jdive-1).dive_end+(diving(jdive).dive_start-diving(jdive-1).dive_end)/2)/3600;
            end
            
            if jdive == 27
                x(:,2) = zeros(size(y,1),1)+tagon+30;
            elseif jdive == 16 || jdive == 20 || jdive == 22
                x(:,2) = zeros(size(y,1),1)+tagon+(diving(jdive).dive_end+(diving(jdive+2).dive_start-diving(jdive).dive_end)/2)/3600;
            else
                x(:,2) = zeros(size(y,1),1)+tagon+(diving(jdive).dive_end+(diving(jdive+1).dive_start-diving(jdive).dive_end)/2)/3600;
            end
            
            z = zeros(size(y));
            if type == 1
                col = frac(:,jdive);
            elseif type == 2
                col = frac(:,jdive)/max(frac(:,jdive));
            end
            aval = 0.5;
            figure(f), hold on
            s1 = surf(x,y,z,col,'FaceAlpha',aval)
            view(2)
            s1.EdgeColor = 'none';
            
            if gray == 1
                xx = [x(1,1) x(1,1) x(1,2) x(1,2)];
                yy = [1000 xd_echo(find(frac(:,jdive)>0,1,'last')+1) xd_echo(find(frac(:,jdive)>0,1,'last')+1) 1000];
                cg = 0.8; cc = [cg cg cg];
                patch(xx,yy,cc,'edgecolor','none')
            end
        end
    end
    
elseif daynight == 2
    sunset = 19.067;
    sunrise = 7.85;
    pa = 'C:\MATLAB_pt\SW_AcousticScene\Echogram\Data\';
    fi = {'frac_d','frac_n','xd_echo'};
    for j = 1:length(fi)
        load([pa fi{j} '.mat']);
    end
    y(:,1) = xd_echo;
    y(:,2) = xd_echo;
    x = zeros(size(y));
    x(:,2) = x(:,2)+sunset;
    z = zeros(size(y));
    
    if type == 1
        col = frac_d';
    elseif type == 2
        col = (frac_d/max(frac_d))';
    end
    
    aval = 0.5;
    s1 = surf(x,y,z,col,'FaceAlpha',aval);
    view(2)
    s1.EdgeColor = 'none';
    ylim([y2 y1])
    %     set(gca, 'YDir','reverse')
    hold on
    xn(:,1) = x(:,2);
    xn(:,2) = x(:,1)+24+sunrise;
    
    if type == 1
        coln = frac_n';
    elseif type == 2
        coln = (frac_n/max(frac_n))';
    end
    
    s2 = surf(xn,y,z,coln,'FaceAlpha',aval);
    view(2)
    s2.EdgeColor = 'none';
    
    if gray == 1
        xx = [10 10 31.8496 31.8496]
        yy = [1000 700 700 1000]
        cg = 0.8; cc = [cg cg cg],
        patch(xx,yy,cc,'edgecolor','none')
    end
    
end


colormap(winter)
c = colorbar('eastoutside');
c.Ticks = linspace(0, 1, 2) ; %Create 8 ticks from zero to 1
c.TickLabels = {'0','Max'} ;
c.Label.String = 'Echoes/min';
pos = get(c,'Position');
% c.Position = [0.91    0.5498    0.02    0.3997];
c.Label.Position = [1.3 pos(2)]; % to change its position
ylim([y2 y1])
set(gca, 'YDir','reverse')


% dive profile
fs = prh.FS_all_sensors;
P = prh.DepthData;
time=(1:length(P))/fs/60/60; % time in hours
P=P(1:length(time));
h(1) = plot(tagon+time,P,'color',(0.5*[1 1 1]),'LineWidth',3);

% echolocation
tagg = {'sw196a';'sw204a';'sw205a'};
pa = 'C:\MATLAB_pt\SW_AcousticScene\Clickdetection\Data';
load([pa '\' tagg{1} '\sw17_' tagg{1}(end-3:end) '_click.mat'])     % Load clicktimes

for j = 1:length(whale)
    plot(tagon+round(whale(j).CLclean)/3600,P(round(whale(j).CLclean)*fs),'k.','markersize',ms1);
end
h(2) = plot([0 5],[800 900],'k','linewidth',4);

% buzz
for j = 1:length(whale)
    if j ~= 16
        for jbuzz = 1:length(whale(j).CLbuzz)
            h(3) = plot(tagon+round((whale(j).CLbuzz{1,jbuzz}(1,end)+whale(j).CLbuzz{1,jbuzz}(1,1))/2)/3600,P(round((whale(j).CLbuzz{1,jbuzz}(1,end)+whale(j).CLbuzz{1,jbuzz}(1,1))/2)*fs),'r.','markersize',ms2);
        end
    end
end

% Bottom
if botpres == 1
    load('C:\MATLAB_pt\SW_AcousticScene\Data\sw17_196a_data.mat')
    for jdive = 1:size(whale,2)
        if jdive ~= 17 & jdive ~= 18
            h(4) = plot(whale(jdive).CLclean/3600+tagon, P(round(whale(jdive).CLclean)*fs)+whale(jdive).bottom_dist'*750,'color',[0.6 0.3 0.1],'LineWidth',2);
        end
    end
end

% make nice
grid off
xlim([tagon 33])
ylim([0 750])
xt=[11:1:34];
set(gca,'xtick',xt);
set(gca,'xticklabel',[]);
set(gca,'ytick',0:100:600);
ylabel('Depth (m)')
box on
lgd = legend(h,{'Dive profile','Echolocation','Buzzes','Sea floor'},'location','northeast','FontSize',10,'Location',[0.26 0.96 0.5 0.03],'Orientation','horizontal')

% fix colorbar
pos = get(c,'position');
AX5 = axes('position',pos);
set(AX5,'color','none')
set(gca,'xtick',[]);
set(gca,'ytick',[]);
x = [0 1 1 0];
y = [0 0 1 1];
patch(x,y,'w','facealpha',1-aval)

% PREY DENS
axes(AX2)
for jdive = 1:27
    if jdive ~= 17 & jdive ~= 21 & jdive ~= 23
        plot(tagon+(diving(jdive).dive_start+(diving(jdive).dive_end-diving(jdive).dive_start)/2)/3600,fracmax(jdive),'b.','markersize',20), hold on
    end
end
ylim([0 60])
xlim([tagon 33])
xt=[11:1:34];
set(gca,'xtick',xt,'xticklabel',[])
ylabel({'Maximum','Echoes/min'})
set(gca,'ytick',0:20:60);
set(gca, 'YGrid', 'on', 'XGrid', 'off')

if stip == 1
    plot(13.8957*[1 1],ylim,'k--','linewidth',1)
    plot(18.0498*[1 1],ylim,'k--','linewidth',1)
end

% PREY DENS AND BUZZ
axes(AX3)
load('C:\MATLAB_pt\SW_AcousticScene\Dive profiles\Data\sw196a\sw17_196a_dive.mat')

yyaxis left
for jdive = 1:size(whale,2)
    tt(jdive) = diving(jdive).dive_start+(diving(jdive).dive_end-diving(jdive).dive_start)/2;
    ttt(jdive) = tagon + tt(jdive)/3600;
    bar(ttt(jdive),length(whale(jdive).echo_all),0.2,'facecolor',[30/255 144/255 1],'edgecolor',[0 0 0]),hold on
end
ylabel('Echoes per dive')
ylim([0 900])

yyaxis right
for jdive = 1:size(whale,2)
    tt(jdive) = diving(jdive).dive_start+(diving(jdive).dive_end-diving(jdive).dive_start)/2;
    ttt(jdive) = tagon + tt(jdive)/3600;
%     bar(ttt(jdive)+0.2,whale(jdive).buzz_no,0.2,'facecolor',[1 69/255 0],'edgecolor','none'),hold on
    hp = bar(ttt(jdive)+0.2,whale(jdive).buzz_no,0.2,'facecolor',[152 251 152]/255,'edgecolor',[0 0 0]),hold on
    if ~isnan(hp.YData)
        hatchfill2(hp,'single','HatchAngle',0);
    end
end

ylim([0 20])
ylabel('Buzzes per dive')
xlim([tagon 33])
xt=[11:1:34];
set(gca,'xtick',xt,'xticklabel',mod(xt,24))
yt=[0:5:10];
set(gca,'ytick',yt,'yticklabel',{'0','5','10'})
xlabel('Local time (hours)')
set(AX3,'XColor','k','YColor',[112 175 112]/255)

if stip == 1
    plot(13.8957*[1 1],ylim,'k--','linewidth',1)
    plot(18.0498*[1 1],ylim,'k--','linewidth',1)
end

% TAG PLACEMENT
if tagblow == 1
    axes(AX4)
elseif tagblow == 2
    axes(AX6)
end

cd('C:\MATLAB_pt\SW_AcousticScene\Dive profiles\Scripts')
imshow('tag_placement_big.png')

if tagblow == 1
    axes(AX6)
    cd('C:\MATLAB_pt\SW_AcousticScene\Dive profiles\Scripts')
    imshow('tag_placement_small.png')
end


% MAKE NICE
set(gcf,'color','w')

% figure labels
axes(AX7)
x1 = 0.13;
x2 = 0.895;
x3 = 0.96;
y1 = 0.55;
y2 = 0.4;
y3 = 0.1;
plot([0.48 0.6933],[0.3476 0.28],'color',[1 69/255 0],'linewidth',1.5)
ylim([0 1])
xlim([0 1])
set(AX7,'color','none')
set(AX7,'XColor','w','YColor','w')
set(gca,'xtick',[]);
set(gca,'ytick',[]);
text(x1-0.12,0.95-0.01,'\it (a)','fontsize',12)
text(x1-0.12,y1-0.01,'\it (b)','fontsize',12)
text(x1-0.12,y2-0.01,'\it (c)','fontsize',12)
text(0.41,0.38,'\it (d)','fontsize',12)
text(0.6721,0.37,'\it (e)','fontsize',12)

% prop0 = 1;
% propx = 16.5;
% propy = 15.5;
% set(gcf, 'Units', 'Centimeters', 'Position', [prop0, prop0, propx, propy], 'PaperUnits', 'Inches', 'PaperSize', [propx, propy])
% set(findall(gcf,'-property','FontSize'),'FontSize',10)
% set(findall(gcf,'-property','FontName'),'FontName','Arial')

% 
% % SAVE
% 
% saveas(f,[pas 'FIG1.fig'])
% 
% 
% pas = 'C:\B_PHD\A PhD-afhandling\Figurer\High Res\';
% exportgraphics(f,[pas 'SWfig1AZ.png'],'Resolution',300)




















% % histogram data
% clear
% 
% load('C:\MATLAB_pt\SW_AcousticScene\Data\sw17_196a_data.mat')
% load('C:\MATLAB_pt\SW_AcousticScene\Data\sw17_196a_dive.mat')
% 
% bincen=[-25:50:1000];
% for jdive = 1:27
%     if jdive ~= 17 & jdive ~= 21 & jdive ~= 23
%     ech = [whale(jdive).echo_depth];
%     tim = [diving(jdive).depth_data];
%     figure(100)
%     [nd_echo, xd_echo] = histf(ech,bincen,'facecolor',[0 1 0.5],'facealpha',.5,'edgecolor','none');
%     [nd_tim, xd_tim] = histf(tim,bincen,'facecolor',[0.4 0.6 1],'facealpha',.5,'edgecolor','none');
%     fs = diving(jdive).depth_fs;
%     frac(:,jdive) = nd_echo./((nd_tim/fs)/60); % echoes per minute
%     frac(isnan(frac)) = 0;
%     frac(isinf(frac)) = 0;
%     fracmax(jdive) = max(frac(:,jdive));
%     clear ech tim
%     end
% end
% pas = 'C:\MATLAB_pt\SW_AcousticScene\Submission\Figures\'
% save([pas 'frac.mat'],'frac')
% save([pas 'fracmax.mat'],'fracmax')
% save([pas 'xd_echo.mat'],'xd_echo')


