% Program to plot suspended transport
% Stef Boersen
% stef.boersen@rhdhv.com 
% 21-11-2017

varname = 'bedload';
axlims = [0 30 -5e-4 5e-4]
%axlims = [0 30 -Inf +Inf]
legpos = 'southeast';
%bedload_1D2D = Quasi2D
%% adjust the data
id_1D2D = find(y_cor_sed_2D>1); % find which results below to the 1D2D grid (here y=1 is used)
bedload_1D2D =  bedload_2D(id_1D2D,:,:);
x_cor1D2D = x_cor_sed_2D(id_1D2D,:);
y_cor1D2D = y_cor_sed_2D(id_1D2D,:);

id_2D = find(y_cor_sed_2D<1); % find which results below to the 1D2D grid (here y=1 is used)
bedload_2D_full =  bedload_2D(id_2D,:,:);
x_cor2D_full = x_cor_sed_2D(id_2D,:);
y_cor2D_full = y_cor_sed_2D(id_2D,:);

%% calculate difference
% A = 1D -1D2D
% B = 1D -2D
% C = 1D2D-2D
for i=1:size(x_cor_sed_1D)
[id_diff_A(i),val_diff_A(i)]=neighbour(x_cor_sed_1D(i),x_cor1D2D);
end
%plot(val_diff_A)

x_corQuasi2D_axis = x_cor1D2D(id_diff_A);
% find corresponding indices for full2D grid
for i=1:size(x_corQuasi2D_axis)
[id_diff_C(i),val_diff_C(i)]=neighbour(x_corQuasi2D_axis(i),x_cor2D_full);
end
x_corFull2D_axis = x_cor2D_full(id_diff_C);

% bepaal suspensieve transporten op posities overeenkomstig
% met 1D-rooster
bedload_1D2D_voor_diff   = bedload_1D2D   (id_diff_A,:,:);
bedload_full2D_voor_diff = bedload_2D_full(id_diff_C,:,:);
if id_time == -1;
    % find last time on mapfile
    id_time = size(bedload_1D,3)
end

%% make figure
figure
subplot(3,1,1)
hold on; grid on; box on;
plot(x_cor_sed_1D,bedload_1D(:,1,id_time),'.')
plot(x_cor1D2D,bedload_1D2D(:,1,id_time),'x')
title([{[tit_text 'bedload']},' 1D comparison '],'fontsize',10)
xlabel('x (m)','fontsize',10)
ylabel([{'transport'} ;{'(m^3 s^{-1} m^{-1})'}],'fontsize',10)
legend('1D','quasi2D','location',legpos)
box on
grid on
axis(axlims)

subplot(3,1,2)
hold on; grid on; box on;
plot(x_cor1D2D,bedload_1D2D(:,1,id_time),'.')
plot(x_cor2D_full,bedload_2D_full(:,1,id_time),'x')
title([{[tit_text 'bedload']},' 2D comparison '],'fontsize',10)
%title(['Quasi-2D'],'fontsize',10)
xlabel('x (m)','fontsize',10)
ylabel([{'transport'} ;{'(m^3 s^{-1} m^{-1})'}],'fontsize',10)
legend('quasi2D','2D','location',legpos)
axis(axlims)

subplot(3,1,3)
hold on; grid on; box on;
%size(x_cor1D2D(id_diff_A))
%size(x_cor_sed_1D)
plot(x_cor_sed_1D, bedload_full2D_voor_diff(:,1,id_time) - bedload_1D2D_voor_diff(:,1,id_time),'.')
plot(x_cor_sed_1D, bedload_1D(:,1,id_time)               - bedload_1D2D_voor_diff(:,1,id_time),'.')
%plot(x_cor_sed_1D,bedload_2D_full(:,1,id_time),'.')
title([{[tit_text 'bedload']},' differences '],'fontsize',10)
xlabel('x (m)','fontsize',10)
ylabel([{'transport'} ;{'(m^3 s^{-1} m^{-1})'}],'fontsize',10)
xlim(axlims(1:2))
legend('2D minus Quasi2D','1D minus Quasi2D','location','southwest')
%axis(axlims)

% subplot(3,1,3)
% plot(x_cor2D_full,bedload_2D_full(:,1,id_time),'.')
% title([' Full 2D model'],'fontsize',10)
% xlabel('x (m)','fontsize',10)
% ylabel([{'transport'} ;{'(m^3 s^{-1} m^{-1})'}],'fontsize',10)
% box on
% grid on
% axis(axlims)

% overal title 

 % store figure
x0=11;
y0=0;
if f_sav ==1;
    width=16.5;
    height=24.7;
    tit_2=tit_text(find(~isspace(tit_text))); %remove whitespaces from name 
    set(gcf,'units','centimeters','position',[x0,y0,width,height]);
    set(gcf,'PaperPositionMode','Auto','PaperUnits','centimeters','PaperSize',[width, height]);  
    figname = [varname ,'_map', num2str(id_time)];
    figname = strrep(figname,' ','');
    figname = strrep(figname,'.','');
    print([fig_dir,figname,'.pdf'],'-dpdf','-r300')
    print([fig_dir,figname,'.png'],'-dpng','-r300')
%     width=16.5;
%         height=24.7;
%     tit_2=tit_text(find(~isspace(tit_text))); %remove whitespaces from name 
%     set(gcf,'units','centimeters','position',[x0,y0,width,height]);
%     set(gcf,'PaperPositionMode','Auto','PaperUnits','centimeters','PaperSize',[width, height]);  
%     print([fig_dir,'bedload_','_',num2str(id_time),'.pdf'],'-dpdf','-r300')
end

%% opruimen
if f_debug ~=1
clearvars id_1D2D bedload_1D2D x_cor1D2D y_cor1D2D id_2D bedload_2D_full ...
            x_cor2D_full y_cor2D_full
end
