%% survey fixer
close all
%clear all
%% load survey

Mpath='F:\check_outs\tudelft\vlugtenburg\';
%Mpath='C:\Madeschipper\check_out\OpenEarthRawData\tudelft\vlugtenburg\scripts\Matthieu'
load([Mpath '\AllVbeachsurveys.mat'])


idate=6;
xdata=Vsurvey(idate).paths.combinedpath_rot(:,1);
ydata=Vsurvey(idate).paths.combinedpath_rot(:,2);
zdata=Vsurvey(idate).paths.combinedpath_rot(:,3);



%% if present load removed points
if exist('Vsurveybadpoints.mat')
    load('Vsurveybadpoints.mat')
    ind_bad=Vsurveybadpoints(idate,1).indexes;
     disp([num2str(length(ind_bad)) ' bad points in deze survey'])
else
    ind_bad=[];
    Vsurveybadpoints=struct;
end

%% plot scatter plot
figure(123);

set(gcf,'WindowStyle','docked')

scatter(xdata,ydata,20,zdata,'filled')
hold on
bp_plot=plot(xdata(ind_bad),ydata(ind_bad),'xk');
title(['Vlugtenburg survey ' Vsurvey(idate).date])
colormap(hsv(10));
caxis([-10 0])
colorbar
grid on
ylim([200 1200])
%% what to do
%% Pick a bad point point

%reply = input('Wat wil je?  Niks (n) , Xtra punten verwijderen (r), Punten weer goedkeuren (g)?', 's');
figure(123)
reply='z';

while strcmp(reply,'n')+strcmp(reply,'r')+strcmp(reply,'g') == 0
    if ~strcmp(reply,'z') 
    disp ('alleen n r of g zijn toegestaan')
    end
    reply = input('Wat wil je?  zo laten(n) , (Xtra) punten verwijderen (r), Punten weer goedkeuren (g)?', 's');
    
    %%% New point to remove
    if strcmp(reply,'r')
        figure(123)
        disp ('.....hit a key als je goed gezoomed zit, dan mag je klikken')
        pause
xya=axis;
inview = inpolygon(xdata,ydata,xya(1:2),xya(3:4));
caxis([min(zdata(inview)) max(zdata(inview))])

 %       colorbar
        disp('.....klik punt')
        a=[];
        while isempty(a)
            a=ginput(1);
        end
        dist_sq=(xdata-a(1)).^2+(ydata-a(2)).^2;
        [mindistsq,ind_a]=min(dist_sq);
        if mindistsq < 20^2;
            figure(123)
            delete(bp_plot)
            if exist('bp_plot_new')
                delete(bp_plot_new)
            end
            bp_plot_new=plot(xdata(ind_a),ydata(ind_a),'ok');
            bp_plot=plot(xdata(ind_bad),ydata(ind_bad),'xk');
            ind_bad=[ind_bad ind_a];
            disp('.........punt verwijderd')
        else
            disp('.........muisklik te ver van survey punten af.. niks gedaan')
        end
        reply = 'z';
    end
    
    %%% Restore removed point
    if strcmp(reply,'g')
        figure(123)
        disp ('.....hit a key als je goed gezoomed zit, dan mag je klikken')
        pause
        caxis auto
        colorbar
        disp('.....klik punt')
        gpoint=[];
        while isempty(gpoint)
            gpoint=ginput(1);
        end
        dist_sq=(xdata(ind_bad)-gpoint(1)).^2+(ydata(ind_bad)-gpoint(2)).^2;
        [mindistsq,ind_bad_gpoint]=min(dist_sq);
        if mindistsq < 20^2;
            figure(123)
            delete(bp_plot)
            if exist('bp_plot_new')
                delete(bp_plot_new)
            end
            bp_plot_new=plot(xdata(ind_bad(ind_bad_gpoint)),ydata(ind_bad(ind_bad_gpoint)),'sqk');
            ind_bad(ind_bad_gpoint)=[];
            bp_plot=plot(xdata(ind_bad),ydata(ind_bad),'xk');
                        disp('.........punt weer toegevoegd')
        else
            disp('.........muisklik te ver van een verwijderd survey punt af.. niks gedaan')
        end
        reply = 'z';
    end
    
    
    if reply == 'n'
        disp('.....Relaxed.. dan houden we het zo')
        disp([num2str(length(ind_bad)) ' bad points in deze survey'])
        break
    end
    
    
end
%%

Vsurveybadpoints(idate,1).indexes=ind_bad;

% 
% save([Mpath 'Vsurveybadpoints.mat'],'Vsurveybadpoints')
% disp('Vsurveybadpoints saved')
