%% V Beach 2 Google Earth
%

% load Vlugtenburg scripts
% Loed Oetsettings


%
%%%

% %% Read XYZ
% rootnaam=('F:\check_outs\tudelft\vlugtenburg\');
% maplijst=dir([ rootnaam 'jetski/raw/20*' ] );  % lijst met alle jestki surveys
% mapnaam=maplijst(16,1).name;      
% filename=[rootnaam 'jetski/raw/' mapnaam '\Products\XYZ_jetski_RD_NAP.txt']
% 
% % Import the file
% newData1 = importdata(filename, ' ', 9); %DELIMITER = ' '; HEADERLINES = 8;
% assignin('base', 'XYZ', newData1.data);
% assignin('base', 'info', newData1.textdata);
% %clear newData1
% 
% Surveydate=info{1,1}([17:20 11:12 14:15]);

% %% make grid
%  gridsize=10;
%  [XI_rd,YI_rd] = meshgrid(min(XYZ(:,1)):gridsize:max(XYZ(:,1)), min(XYZ(:,2)):gridsize:max(XYZ(:,2)));
%  ZI = griddata(XYZ(:,1),XYZ(:,2),XYZ(:,3),XI_rd,YI_rd);
% 
%  figure 
% pcolor(XI_rd,YI_rd,ZI) 

    

% Load

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


load([Mpath 'AllVbeachsurveys.mat'])

i_date=length(Vsurvey)




    th=2; %Thinning otherwise the files get to big 
    x=Vsurvey(i_date).grids.Xi(1:th:end,1:th:end)   ;
    y=Vsurvey(i_date).grids.Yi(1:th:end,1:th:end)    ;
    z=Vsurvey(i_date).grids.Zi(1:th:end,1:th:end)    ;
    
    % 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;];
    
    A=[66969.7580645161,447029.905913978;68239.9193548387,448266.465053763;69251.3440860215,447352.486559140;68703.6290322581,446616.599462366;68354.1666666667,446327.620967742;68213.0376344086,446398.185483871;67977.8225806452,446159.610215054;];
    
    [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

caxis([-10 5])
load cmapVBmin10to5.mat
colormap(cmap);
colorbar

%% Convert to WGS 84
[lon,lat,log]=convertCoordinates(x,y,'CS1.code',28992,'CS2.code',4326);

%% Export to KML
KMLname=['VlugtenburgSurvey' Vsurvey(i_date).date([1:2 4:5 7:10])]
KMLpcolor(lat,lon,z.*good,'cLim',[-12 7],'fileName',[KMLname '.kml'],'kmlName',KMLname,'fillAlpha',1,'disp',0,'colorMap',cmap);
%%
KMLcname=['VlugtenburgContourline' Vsurvey(i_date).date([1:2 4:5 7:10])]
levels=[-9:+4];
colors=ones(length(levels),3);
KMLcontour(lat,lon,z.*good,'levels',levels,'fileName',[KMLcname '.kml'],'kmlName',KMLcname,'colorMap',colors,'writeLabels','True','colorbar',0);
