%% make plots of the monthly profiles and profile changes for each survey
Mpath='F:\check_outs\tudelft\vlugtenburg'

% figures save in folders per survey, and per profile

savefigs=1; %Toggle to save the images

%% load data
load([Mpath '\AllVbeachsurveys.mat'])

load 'barspecs.mat'
%%
if 0
    %%
    for i_transect=22%:22;
        i_transect
        for i_date=1:length(Vsurvey)
            timestack(i_date,:)=Vsurvey(i_date).transects.elevation2(:,i_transect)';
            x=Vsurvey(i_date).transects.cr_sh(:)    ;
            date(i_date)=Vsurvey(i_date).matlabdate;
        end
        
        % Timestack profile
        for i_date=1:length(Vsurvey);
            
            zp=timestack(i_date,:);
            figure(13)
            close(13)
            figure(13)
            set(gcf,'WindowStyle','docked')
            plot(x,zp,'k')
            grid on
            xlim([180 500])
            ylim([-4 -1])
            hold all
            if Barposition(i_date,i_transect) > 50
                plot(Barposition(i_date,i_transect),Barheight(i_date,i_transect),'+r')
            end
            title(num2str(i_date))
            xlabel('Cross-shore distance [m]')
            ylabel('Elevation [m]')
            reply = 'z';
            while strcmp(reply,'o')+strcmp(reply,'p')== 0
                if ~strcmp(reply,'z')
                    disp ('alleen o of p zijn toegestaan')
                end
                reply = input('Wat wil je?  OK(o) , Nieuw punt(p)', 's');
                
                %%% New point to remove
                if strcmp(reply,'p')
                    figure(13)
                    hold on
                    plot(x,zp,'k')
                    
                    if Barposition(i_date,i_transect) > 50
                        plot(Barposition(i_date,i_transect),Barheight(i_date,i_transect),'+r')
                    end
                    disp('.....klik punt')
                    a=[];
                    while isempty(a)
                        a=ginput(1);
                    end
                    ind=find (abs(x-a(1) )<=20)
                    [e,r]=max(zp(ind))
                    Barposition(i_date,i_transect)=x(ind(r));
                    Barheight(i_date,i_transect)=zp(ind(r));
                    plot(x(ind(r)),zp(ind(r)),'ok')
                    hold off
                    reply = 'z';
                end
                
                
                if reply == 'o'
                    disp('.....Relaxed.. dan houden we het zo')
                    
                end
                
            end
            
            
        end
    end
    %%
end

%% Are u sure?

%save ('barspecs.mat','Barposition' ,'Barheight' )

%%
for i_date=1:1:length(Vsurvey)
    
    for i_transect=1:22
        Bar(i_date,1).barheight(i_transect,1)=Barheight(i_date,i_transect);
        Bar(i_date,1).barposition(i_transect,1)=Barposition(i_date,i_transect);
    end
    
    datesmatlab(i_date)=Vsurvey(i_date).matlabdate;
end


for i_date=1:22
    Bar(4,1).barposition(:,1)=NaN;
end
Bar(1,1).barposition(3,1)=NaN;
Bar(3,1).barposition(3,1)=NaN;

Bar(6,1).barposition(4,1)=NaN;
Bar(3,1).barposition(5,1)=NaN;
Bar(6,1).barposition(5,1)=NaN;
Bar(3,1).barposition(6,1)=NaN;
Bar(6,1).barposition(6,1)=NaN;
Bar(1,1).barposition(8,1)=NaN;
Bar(2,1).barposition(8,1)=NaN;
Bar(3,1).barposition(8,1)=NaN;
Bar(6,1).barposition(8,1)=NaN;
Bar(3,1).barposition(9,1)=NaN;
Bar(5,1).barposition(9,1)=NaN;
Bar(7,1).barposition(9,1)=NaN;
Bar(1,1).barposition(10,1)=NaN;
Bar(2,1).barposition(10,1)=NaN;
Bar(7,1).barposition(10,1)=NaN;

Bar(1,1).barposition(11,1)=NaN;
Bar(7,1).barposition(11,1)=NaN;
Bar(8,1).barposition(11,1)=NaN;

Bar(1,1).barposition(12,1)=NaN;
Bar(7,1).barposition(12,1)=NaN;
Bar(9,1).barposition(13,1)=NaN;
Bar(10,1).barposition(13,1)=NaN;

Bar(1,1).barposition(14,1)=NaN;
Bar(3,1).barposition(14,1)=NaN;
Bar(1,1).barposition(15,1)=NaN;

Bar(6,1).barposition(16,1)=NaN;
Bar(11,1).barposition(16,1)=NaN;

Bar(1,1).barposition(17,1)=NaN;
Bar(5,1).barposition(17,1)=NaN;

Bar(1,1).barposition(18,1)=NaN;
Bar(6,1).barposition(18,1)=NaN;

Bar(1,1).barposition(19,1)=NaN;
Bar(2,1).barposition(19,1)=NaN;
Bar(7,1).barposition(19,1)=NaN;


Bar(11,1).barposition(20,1)=NaN;
Bar(35,1).barposition(20,1)=NaN;
Bar(36,1).barposition(20,1)=NaN;
Bar(37,1).barposition(20,1)=NaN;


Bar(37,1).barposition(21,1)=NaN;

Bar(1,1).barposition(22,1)=NaN;

for i_date=1:1:length(Vsurvey)
    
    for i_transect=1:22
        
        Bar(i_date,1).barheight(i_transect,1)=NaN;
        if isnan(Bar(i_date,1).barposition(i_transect,1))
            Bar(i_date,1).barheight(i_transect,1)=NaN;
        end
    end
    
    datesmatlab(i_date)=Vsurvey(i_date).matlabdate;
end


monthv=[];
for i_mm=7:12
    iv=datenum([num2str(i_mm) '/01/2009'],'mm/dd/yyyy');
    monthv=[ monthv iv];
end
for i_mm=1:12
    iv=datenum([num2str(i_mm) '/01/2010'],'mm/dd/yyyy');
    monthv=[ monthv iv];
end
for i_mm=1:12
    iv=datenum([num2str(i_mm) '/01/2011'],'mm/dd/yyyy');
    monthv=[ monthv iv];
end
for i_mm=1:11
    iv=datenum([num2str(i_mm) '/01/2012'],'mm/dd/yyyy');
    monthv=[ monthv iv];
end

xtickstring=datestr(monthv,'mmm/yy')
xtickstring(1:2:end,:)=NaN;



colors=colormap(winter(22));
figure
set(gcf,'position',[  50         203        1310         378])
set(gcf,'PaperPosition',[0 0  35 12])

hold on
for i_transect=1:22
    for i_date=1:1:length(Vsurvey)
        plot(datesmatlab(i_date),Bar(i_date,1).barposition(i_transect,1),'.','markersize',14,'color',colors(i_transect,:))
        % plot(i_date,Bar(i_date,1).barposition2(i_transect,1),'.r')
        
    end
end
colormap(colors)
h=colorbar

h2=annotation('textarrow',[0.92 0.7],[0.13 0.8],'string',{'Transect nr. , 1 (North) to 22 (South)'}, ...
    'HeadStyle','none','LineStyle', 'none', 'TextRotation',-90);

for i_date=1:1:length(Vsurvey)
    barsurvtemp=[]
    for i_transect=1:22
        barsurvtemp(i_transect)=Bar(i_date,1).barposition(i_transect,1)
        % plot(i_date,Bar(i_date,1).barposition2(i_transect,1),'.r')
        
    end
    MedianBarPos(i_date)=median(barsurvtemp(~isnan(barsurvtemp)))
end

plot(datesmatlab,MedianBarPos,'color',[0.5 0 0])

xlim([datenum(['06/01/2009'],'mm/dd/yyyy') datenum(['11/1/2012'],'mm/dd/yyyy')])

xlabel('Date')
ylabel({'Cross shore position [m]'})


holdax=axis

x = [datenum(['01/01/2010'],'mm/dd/yyyy'); datenum(['12/31/2010'],'mm/dd/yyyy');datenum(['12/31/2010'],'mm/dd/yyyy') ; datenum(['01/01/2010'],'mm/dd/yyyy')];
y = [ 600; 600;0; 0];
tcolor(1,1,1:3) = 0.7*[1 1 1];
patch(x,y,tcolor,'facealpha',0.3,'EdgeColor','none')

x = [datenum(['01/01/2012'],'mm/dd/yyyy'); datenum(['12/31/2012'],'mm/dd/yyyy');datenum(['12/31/2012'],'mm/dd/yyyy') ; datenum(['01/01/2012'],'mm/dd/yyyy')];
y = [ 600; 600;0; 0];
tcolor(1,1,1:3) = 0.7*[1 1 1];
patch(x,y,tcolor,'facealpha',0.3,'EdgeColor','none')

axis(holdax)
set(gca,'Xtick',monthv)
set(gca,'Xticklabel',xtickstring)


% for i_transect=11:13
%     for i_date=1:1:length(Vsurvey)
% plot(i_date,Bar(i_date,1).barposition(i_transect,1),'.r')
% % plot(i_date,Bar(i_date,1).barposition2(i_transect,1),'.r')
%
%     end
% end
ylim([50 500])

set(gca,'ytick',[50:50:500])
grid on
nourishmentposition11=[490 475 465 450 470 445 440 420 410 405 ];
nourishmentposition12=[480 465 470 465 460 445 435 430 405 395 ];
nourishmentposition13=[490 480 485 450 480 480 445 430 465 450 435];

plot(datesmatlab(1:10),nourishmentposition11,'or')
plot(datesmatlab(1:10),nourishmentposition12,'or')
%plot(datesmatlab(1:11),nourishmentposition13,'or')

inshorebar=nan(22,37);
inshorebar(12,34:36)=[245 230 240];
inshorebar(13,35:36)=[250 240];
inshorebar(5,[32 35:36])=[250 230 225];
inshorebar(6,35:36)=[220 220];
inshorebar(7,35)=[215 ];
inshorebar(10,35:36)=[230 225];
% for i_transect=1:22
% plot(datesmatlab,inshorebar(i_transect,:),'.b')
% end

load('F:\check_outs\tudelft\vlugtenburg\scripts\Matthieu\VolumeAnalysis\xMCL.mat')

for i_transect=1:22
    for i_date=1:1:length(Vsurvey)
        plot(datesmatlab(i_date),xMCL(i_date,i_transect),'x','color',colors(i_transect,:))
        % plot(i_date,Bar(i_date,1).barposition2(i_transect,1),'.r')
        
    end
end

plot(datesmatlab, nanmean(xMCL,2))
pause(1)
% set(gcf,'renderer','zbuffer')
set (gcf,'color','w')
%drawnow
print ([ 'BarPosition.png'],'-dpng')