%% Suggested Adjustments for the survey data

% Problem: Gaps in the data due to beach houses near dunefence.
% NaNs at the border difficult for volume calculations.
% also we see that the data on the seaward edge sometimes yoyo's due to bas
% sound speed estimation.

% Manual fix:
% add data from previosus survey at the boundary to have a good volume
% calculation
%
% and, adjust the SOS by hand.
%
% Use the new AllVbeachsurveys_corr with care. Obviously these adjustments
% make it difficult to observe montly changes in deep water!


close all
clear all

%% Options
FixSoS = 1  % 1 uses a manual adjustment of the soundspeed of 0.5 %  so the lower shoeface profile fits better

%% load data


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

load([Mpath '\AllVbeachsurveys.mat'])
load([Mpath '\datesandpairs.mat'])

%% Fix Jetski data
disp('--- precheck')
% find spots where there is no data at or before the x=0 point
for is=1:length(Vsurvey);
    disp(['survey  ' num2str(is)])
    for it=1:22
        if isnan(Vsurvey(is,1).transects.elevation2(301:311,it))
            disp(['error.. no data near -9 m NAP, transect' num2str(it)])
        end
        if isnan(Vsurvey(is,1).transects.elevation2(51:61,it))
            disp(['error.. no data near dune fence, transect' num2str(it)])
        end
        
    end
end

% Fill data with surveys from before/after

%%% Example how we fixing this?
figure(22)
plot(Vsurvey(8,1).transects.cr_sh,Vsurvey(8,1).transects.elevation2(:,21),Vsurvey(8,1).transects.cr_sh(61),Vsurvey(7,1).transects.elevation2(61,21),'xr'); grid on; %pause
% Fix
Vsurvey(8,1).transects.elevation2(61,21)=Vsurvey(7,1).transects.elevation2(61,21);

%%% Example how we fixing this?
figure(22)
plot(Vsurvey(8,1).transects.cr_sh,Vsurvey(8,1).transects.elevation2(:,22),Vsurvey(8,1).transects.cr_sh(61),Vsurvey(7,1).transects.elevation2(61,22),'xr'); grid on; %pause
% Fix
Vsurvey(8,1).transects.elevation2(61,22)=Vsurvey(7,1).transects.elevation2(61,22);

Vsurvey(1,1).transects.elevation2(301:311,1:2)=Vsurvey(2,1).transects.elevation2(301:311,1:2);
Vsurvey(1,1).transects.elevation2(301:311,1:2)=Vsurvey(2,1).transects.elevation2(301:311,1:2);
Vsurvey(4,1).transects.elevation2(301:311,1:4)=Vsurvey(3,1).transects.elevation2(301:311,1:4);
Vsurvey(4,1).transects.elevation2(301:311,1:4)=Vsurvey(3,1).transects.elevation2(301:311,1:4);
Vsurvey(9,1).transects.elevation2(51:61,22)=Vsurvey(8,1).transects.elevation2(51:61,22);
Vsurvey(10,1).transects.elevation2(301:311,1)=Vsurvey(9,1).transects.elevation2(301:311,1);
Vsurvey(11,1).transects.elevation2(51:61,22)=Vsurvey(10,1).transects.elevation2(51:61,22);
Vsurvey(12,1).transects.elevation2(51:61,22)=Vsurvey(13,1).transects.elevation2(51:61,22);
Vsurvey(14,1).transects.elevation2(51:61,22)=Vsurvey(13,1).transects.elevation2(51:61,22);
Vsurvey(15,1).transects.elevation2(301:311,1:2)=Vsurvey(14,1).transects.elevation2(301:311,1:2);
Vsurvey(19,1).transects.elevation2(301:311,1:2)=Vsurvey(20,1).transects.elevation2(301:311,1:2);
Vsurvey(18,1).transects.elevation2(301:311,1:8)=Vsurvey(19,1).transects.elevation2(301:311,1:8);
Vsurvey(17,1).transects.elevation2(301:311,1:5)=Vsurvey(18,1).transects.elevation2(301:311,1:5);
Vsurvey(22,1).transects.elevation2(51:61,22)=Vsurvey(21,1).transects.elevation2(51:61,22);
Vsurvey(25,1).transects.elevation2(51:61,22)=Vsurvey(24,1).transects.elevation2(51:61,22);
Vsurvey(28,1).transects.elevation2(51:61,22)=Vsurvey(27,1).transects.elevation2(51:61,22);


%% why are we fixing this?
figure(22)
plot(Vsurvey(31,1).transects.cr_sh,Vsurvey(31,1).transects.elevation2(:,22),Vsurvey(31,1).transects.cr_sh(51:61),Vsurvey(30,1).transects.elevation2(51:61,22),'xr');
grid on; pause
%%% Image shows a large jump in elevation due to the beach houses at
%%% the southern end.

%%% Fixed by copying the last months data near the dune
Vsurvey(31,1).transects.elevation2(51:61,22)=Vsurvey(30,1).transects.elevation2(51:61,22);

%% Idem ..
figure(22)
plot(Vsurvey(35,1).transects.cr_sh,Vsurvey(35,1).transects.elevation2(:,22),Vsurvey(35,1).transects.cr_sh(51:61),Vsurvey(34,1).transects.elevation2(51:61,22),'xr');
grid on; %pause
Vsurvey(35,1).transects.elevation2(51:61,22)=Vsurvey(34,1).transects.elevation2(51:61,22);



%%
disp('--- check after!')
ie=0;
for is=1:length(Vsurvey);
    %disp(['survey  ' num2str(is)])
    for it=1:22
        if isnan(Vsurvey(is,1).transects.elevation2(301:311,it))
            disp(['error.. no data near -9 m NAP ' num2str(it)])
            ie=ie+1
        end
        if isnan(Vsurvey(is,1).transects.elevation2(51:61,it))
            disp(['error.. no data near dune fence ' num2str(it)])
            ie=ie+1
        end
    end
end
if ie == 0
    disp(['No gaps in the data anymore.. ready for volume balance!'])
end
%%%  No error should be left here in the workspace

%% fix SoS (adjust 0.5 % by hand)
if FixSoS ==1
    disp(['fixing SoS!'])
    Vsurvey(7,1).transects.elevation2(Vsurvey(7,1).transects.elevation2 <0)=Vsurvey(7,1).transects.elevation2(Vsurvey(7,1).transects.elevation2 <0)*0.995;
    Vsurvey(10,1).transects.elevation2(Vsurvey(10,1).transects.elevation2 <0)=Vsurvey(10,1).transects.elevation2(Vsurvey(10,1).transects.elevation2 <0)*1.005;
    Vsurvey(12,1).transects.elevation2(Vsurvey(12,1).transects.elevation2 <0)=Vsurvey(12,1).transects.elevation2(Vsurvey(12,1).transects.elevation2 <0)*0.995;
    Vsurvey(13,1).transects.elevation2(Vsurvey(13,1).transects.elevation2 <0)=Vsurvey(13,1).transects.elevation2(Vsurvey(13,1).transects.elevation2 <0)*1.005;
    Vsurvey(14,1).transects.elevation2(Vsurvey(14,1).transects.elevation2 <0)=Vsurvey(14,1).transects.elevation2(Vsurvey(14,1).transects.elevation2 <0)*0.995;
    Vsurvey(16,1).transects.elevation2(Vsurvey(16,1).transects.elevation2 <0)=Vsurvey(16,1).transects.elevation2(Vsurvey(16,1).transects.elevation2 <0)*1.005;
    Vsurvey(17,1).transects.elevation2(Vsurvey(17,1).transects.elevation2 <0)=Vsurvey(17,1).transects.elevation2(Vsurvey(17,1).transects.elevation2 <0)*0.995;
    Vsurvey(18,1).transects.elevation2(Vsurvey(18,1).transects.elevation2 <0)=Vsurvey(18,1).transects.elevation2(Vsurvey(18,1).transects.elevation2 <0)*0.995;
    Vsurvey(19,1).transects.elevation2(Vsurvey(19,1).transects.elevation2 <0)=Vsurvey(19,1).transects.elevation2(Vsurvey(19,1).transects.elevation2 <0)*0.995;
    % Vsurvey(29,1).transects.elevation2(Vsurvey(29,1).transects.elevation2 <0)=Vsurvey(29,1).transects.elevation2(Vsurvey(29,1).transects.elevation2 <0)*0.995;
    Vsurvey(34,1).transects.elevation2(Vsurvey(34,1).transects.elevation2 <0)=Vsurvey(34,1).transects.elevation2(Vsurvey(34,1).transects.elevation2 <0)*1.005;
    Vsurvey(35,1).transects.elevation2(Vsurvey(35,1).transects.elevation2 <0)=Vsurvey(35,1).transects.elevation2(Vsurvey(35,1).transects.elevation2 <0)*0.995;
    Vsurvey(36,1).transects.elevation2(Vsurvey(36,1).transects.elevation2 <0)=Vsurvey(36,1).transects.elevation2(Vsurvey(36,1).transects.elevation2 <0)*0.995;
end

%%
disp(['saving!'])
save([Mpath '\AllVbeachsurveys_corr.mat'],'Vsurvey')
disp(['done'])