%% Simple example to plot a topview of the data
clear
close all
savefigs=1; %Toggle

savemovie=1 ;  % save as movie (1) or not (0)
clear M

% Load

Mpath='F:\check_outs\tudelft\vlugtenburg\'
%Mpath='C:\Madeschipper\check_out\OpenEarthRawData\tudelft\vlugtenburg\'


load([Mpath 'AllVbeachsurveys.mat'])
load('cmapVBmin10to5.mat')

%%% images
% 
% 1. Oblique gridded from paths 
% 2. rectified Elevation gridded from paths
% 3. rectified Elevation gridded from transect
% 4. rctified elevation on transects (with white bands in between)
% 5. Show interpolation surveys points to transect data

%% plot gridded (from paths) data oblque!
if 0
    i_date= length(Vsurvey)-2
    
    x=Vsurvey(i_date).grids.Xi    ;
    y=Vsurvey(i_date).grids.Yi    ;
    z=Vsurvey(i_date).grids.Zi    ;
    
    % Cut off part of survey outside northern and southern surveylines
    %A=ginput(4)
    A=[66926.1327253133,447066.868279570;68119.0090693993,446005.040322581;69392.5305747756,447238.239247312;68031.6434780015,448457.997311828;];
    [in] = inpolygon(x,y,A(:,1),A(:,2));
    good=+in;
    ind=find (good == 0);
    good(ind)=NaN;
    
    % Topview in RD (scheef)
    figure
    pcolor(x,y,z.*good)
    shading interp
    axis equal
    hold on
    [C,h] = contour(x,y,z.*good,[-9:1:4],'--k');
    clabel(C,h,'LabelSpacing',72,'FontSize',10,'Color','k','Rotation',0) ;
    
    ylabel('Northing in m')
    xlabel('Easting in m')
    zlabel('NAP m')
    
    shading interp
    
    grid on
    
        caxis([-10 5])
        colorbar
         colormap(cmap)
    % in RD with survey lines
    figure
    
    pcolor(x,y,z.*good)
    shading interp
    axis equal
    hold on
    
    [C,h] = contour(x,y,z.*good,[-9:1:5],'--k');
    clabel(C,h,'LabelSpacing',72,'FontSize',10,'Color','k','Rotation',0) ;
    
    add_surveygrid
    
    plot(Vsurvey(i_date).paths.combinedpath(:,1),Vsurvey(i_date).paths.combinedpath(:,2),'.k','MarkerSize',5)
    
    
    ylabel('Northing in m')
    xlabel('Easting in m')
    zlabel('NAP m')
    
    grid on
    %
    shading interp
    
    caxis([-10 4])
    colorbar
end


%% plot gridded (from transects) data
if 1
    for i_date=1:length(Vsurvey)
        figure(223)
        set(gcf,'PaperPosition',[0  0  40 20])
        set(gcf,'Position',[9          86        1641         881])
        set(gcf,'Color',[1,1,1])
     
        pk=pcolor(Vsurvey(i_date).transects.y,Vsurvey(i_date).transects.cr_sh,Vsurvey(i_date).transects.elevation2)
        hold all
        
        [C,h] = contour(Vsurvey(i_date).transects.y,Vsurvey(i_date).transects.cr_sh,Vsurvey(i_date).transects.elevation2,[-9:1:-5],'--k');
        clabel(C,h,'LabelSpacing',72,'FontSize',10,'Color','k','Rotation',0) ;
        
        [C,h] = contour(Vsurvey(i_date).transects.y,Vsurvey(i_date).transects.cr_sh,Vsurvey(i_date).transects.elevation2,[-4:1:4],'--k');
        clabel(C,h,'LabelSpacing',72,'FontSize',10,'Color','k','Rotation',0) ;
        
        
        shading flat
        ylim([0 1200])
        caxis([-1 1])
        colorbar
        hold all
        
        set(gca,'Xtick',[flipud(Vsurvey(i_date).transects.y)])
        set(gca,'Xticklabel',num2str([flipud(Vsurvey(i_date).transects.y)],'%4.0f'))
        
        xlim([0 1750])
        xlabel('Alongshore distance [m]')
        
        ylim([0 1200])
        ylabel('Cross shore distance [m]')
        
        caxis([-10 5])
        colorbar
         colormap(cmap)
        set(gca,'position',[0.1300    0.1100    0.7750    0.8150])
        legend([pk  h], 'Elevation [m]',[ 'contour ' Vsurvey(i_date).date([4:6 1:3 7:10])] )
        title([ 'Topography survey  ' Vsurvey(i_date).date([1:3 7:10]) ', gridded (based on transects)'])
        
        
        x0=30;           y0=1050;  theta0 =-131.65534968173412+90;
        h=100; w=100 ;
        [x_ds3,y_ds3]=northarrow(h,w,x0,y0,theta0);
        
        set(gcf,'renderer','zbuffer')
        hold off
        
        if savefigs==1
              title([ 'Topography survey  ' Vsurvey(i_date).date([1:3 7:10]) ''],'Fontsize',20)
              filename=[ 'Topography ' Vsurvey(i_date).date([7:10]) '_' Vsurvey(i_date).date([1:2]) '_' Vsurvey(i_date).date([4:5])]
            print ([ Mpath '\scripts\Matthieu\MakeTopo\images\tgrid' filename '.jpg'],'-djpeg')
        end
               if savemovie==1
     
        legend off
        pause(0.2)
        M(i_date)=getframe(gcf);
               end

    end
end
if savemovie==1
M(1,length(M)+1)=M(1,length(M))
M(1,length(M)).cdata=M(1,length(M)).cdata*0+255
    cd images
    make_movie_gif(M,1,'MovieTopo.gif',10)
    cd ..
end


%% Elevation gridded from paths
if 0
    
    for i_date=32%1:length(Vsurvey)
        
        figure(22)
        set(gcf,'PaperPosition',[0  0  40 20])
        set(gcf,'Position',[9          86        1641         881])
        set(gcf,'Color',[1,1,1])
     
        
        pk=pcolor(Vsurvey(i_date).grids.Xi_r,Vsurvey(i_date).grids.Yi_r,Vsurvey(i_date).grids.Zi_r)
        hold all
        
        [C,h] = contour(Vsurvey(i_date).grids.Xi_r,Vsurvey(i_date).grids.Yi_r,Vsurvey(i_date).grids.Zi_r,[-9:1:4],'--k');
        clabel(C,h,'LabelSpacing',72,'FontSize',10,'Color','k','Rotation',0) ;
        
        shading flat
        ylim([0 1200])
        caxis([-1 1])
        colorbar
        hold all
        
        set(gca,'Xtick',[flipud(Vsurvey(i_date).transects.y)])
        set(gca,'Xticklabel',num2str([flipud(Vsurvey(i_date).transects.y)],'%4.0f'))
        
        xlim([0 1750])
        xlabel('Alongshore distance [m]')
        
        ylim([0 1200])
        ylabel('Cross shore distance [m]')
        
    
        caxis([-10 5])
        colorbar
         colormap(cmap)
        
        set(gca,'position',[0.1300    0.1100    0.7750    0.8150])
        legend([pk  h], 'Elevation [m]',[ 'contour ' Vsurvey(i_date).date([4:6 1:3 7:10])] )
%        title([ 'Topography survey  ' Vsurvey(i_date).date([1:3 7:10]) ', gridded (based on path)'])
              title([ 'Topography survey  ' Vsurvey(i_date).date([1:3 7:10]) ''],'Fontsize',20)
        
        
        x0=30;           y0=1050;  theta0 =-131.65534968173412+90;
        h=100; w=100 ;
        [x_ds3,y_ds3]=northarrow(h,w,x0,y0,theta0);
        
        set(gcf,'renderer','zbuffer')
        hold off
        
        if savefigs==1
                 legend off
            filename=[ 'Topography ' Vsurvey(i_date).date([7:10]) '_' Vsurvey(i_date).date([1:2]) '_' Vsurvey(i_date).date([4:5])]
            print ([ Mpath '\scripts\Matthieu\MakeTopo\images\pgrid' filename '.jpg'],'-djpeg')
        end
 
        
        
    end
    
end


%%   Show how surveys points are interpolated! in local coordinates (rectangular), yaxis alongshore m
if 1
    for i_date=1:length(Vsurvey)
        
        figure(123)
        set(gcf,'PaperPosition',[0  0  40 20])
        set(gcf,'Position',[1          31        1680         944])
        
        bw=30;
        hold all
        for i_t=1:22
            x=Vsurvey(i_date).transects.cr_sh    ;
            y=[Vsurvey(i_date).transects.y(i_t)-bw Vsurvey(i_date).transects.y(i_t)+bw]  ;
            z=[Vsurvey(i_date).transects.elevation2(:,i_t) Vsurvey(i_date).transects.elevation2(:,i_t)];
            pcolor(y,x,z)
            
            % [C,h] = contour(y,x,z,[-9 -9],'--k');
            % if i_t == 1 | i_t == 5 | i_t == 9 | i_t == 13 | i_t == 17 | i_t == 22
            % clabel(C,h,'FontSize',10,'Color','k','Rotation',0) ;
            % end
            
            % survey line
            plot([Vsurvey(i_date).transects.y(i_t) Vsurvey(i_date).transects.y(i_t)] ,[0  1200],'k')
            
        end
        shading interp
        %axis equal
        hold on
        
        
        set(gca,'Xtick',[flipud(Vsurvey(i_date).transects.y)])
        set(gca,'Xticklabel',num2str([flipud(Vsurvey(i_date).transects.y)],'%4.0f'))
        
        
        x0=30;           y0=1050;
        theta0 =-131.65534968173412+90;
        h=100
        w=100
        [x_ds3,y_ds3]=northarrow(h,w,x0,y0,theta0);
        %text(har,'String','N','Fontsize',8)
        
        grid on
        %set(gca, 'XDir', 'reverse')
        xlim([-50 1800])
        xlabel('Alongshore distance [m]')
        
        ylim([0 1200])
        ylabel('Cross shore distance [m]')
        shading interp
        
        caxis([-10 5])
        colorbar
        
        title([ 'Survey data of ' Vsurvey(i_date).date([1:3 7:10]) ' interpolated on transects'])
        
        hold off
        pause(0.1)
        if savefigs==1
            filename=[ 'Topography ' Vsurvey(i_date).date([7:10]) '_' Vsurvey(i_date).date([1:2]) '_' Vsurvey(i_date).date([4:5]) ]
            print ([ Mpath '\scripts\Matthieu\MakeTopo\images\' filename '.jpg'],'-djpeg')
        end
        
    end
end



%%   Show how surveys points are interpolated! in local coordinates (rectangular), yaxis alongshore m
if 1
    for i_date=1:length(Vsurvey)
        
        figure(12)
        set(gcf,'PaperPosition',[0  0  40 20])
        set(gcf,'Position',[1          31        1680         944])
        
        bw=30;
        
        plot(Vsurvey(i_date).paths.combinedpath_rot(:,1),Vsurvey(i_date).paths.combinedpath_rot(:,2),'.w','MarkerSize',5)
        hold all
        for i_t=1:22
            x=Vsurvey(i_date).transects.cr_sh    ;
            y=[Vsurvey(i_date).transects.y(i_t)-bw Vsurvey(i_date).transects.y(i_t)+bw]  ;
            z=[Vsurvey(i_date).transects.elevation2(:,i_t) Vsurvey(i_date).transects.elevation2(:,i_t)];
            pcolor(y,x,z)
            
            % [C,h] = contour(y,x,z,[-9 -9],'--k');
            % if i_t == 1 | i_t == 5 | i_t == 9 | i_t == 13 | i_t == 17 | i_t == 22
            % clabel(C,h,'FontSize',10,'Color','k','Rotation',0) ;
            % end
            
            % survey line
            plot([Vsurvey(i_date).transects.y(i_t) Vsurvey(i_date).transects.y(i_t)] ,[0  1200],'k')
            
        end
        shading interp
        %axis equal
        hold on
        
        
        set(gca,'Xtick',[flipud(Vsurvey(i_date).transects.y)])
        set(gca,'Xticklabel',num2str([flipud(Vsurvey(i_date).transects.y)],'%4.0f'))
        
        
        x0=30;           y0=1050;
        theta0 =-131.65534968173412+90;
        h=100
        w=100
        %[x_ds3,y_ds3]=northarrow(h,w,x0,y0,theta0);
        %text(har,'String','N','Fontsize',8)
        
        grid on
        %set(gca, 'XDir', 'reverse')
        xlim([-50 1800])
        xlabel('Alongshore distance [m]','fontsize',12)
        
        ylim([0 1200])
        ylabel('Cross shore distance [m]','fontsize',12)
        shading interp
        colormap(cmap)
        caxis([-10 5])
        colorbar
        
        title([ 'Survey data of ' Vsurvey(i_date).date([1:3 7:10]) ' interpolated on transects'],'fontsize',12)
        
         set(gca,'fontsize',12)
        hold off
        pause(0.1)
        if savefigs==1
            filename=[ 'InterpolationTransects ' Vsurvey(i_date).date([7:10]) '_' Vsurvey(i_date).date([1:2]) '_' Vsurvey(i_date).date([4:5]) ]
            print ([ Mpath '\scripts\Matthieu\MakeTopo\images\' filename '.jpg'],'-djpeg')
        end
        
    end
end



%% in local coordinates (rectangular), yaxis transect nr <<-- unhandy
if 0
    x=Vsurvey(i_date).transects.cr_sh    ;
    y=Vsurvey(i_date).transects.tr_nr  ;
    z=Vsurvey(i_date).transects.elevation2;
    
    figure
    pcolor(y,x,z)
    shading interp
    %axis equal
    hold on
    grid on
    [C,h] = contour(y,x,z,[-9:1:5],'--k');
    clabel(C,h,'LabelSpacing',72,'FontSize',10,'Color','k','Rotation',0) ;
    
    %add_surveygrid
    set(gca,'Xtick',[1:22])
    set(gca, 'XDir', 'reverse')
    
    xlabel('Transect nr')
    ylabel('Cross shore distance [m]')
    
    shading interp
    
    caxis([-10 5])
    colorbar
end



