%% Load visual observations

% clear;
% close all;
% clc;

%% Get data from jarkus

url             = jarkus_url;
id              = nc_varget(url,'id');
areacode        = nc_varget(url,'areacode');
areaname        = nc_varget(url,'areaname');
lat             = nc_varget(url,'lat',[0,0],[-1,-1]);
lon             = nc_varget(url,'lon',[0,0],[-1,-1]);
xRSP            = nc_varget(url,'cross_shore');

%% Retrieve data dunefoot

url_df = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/strandlijnen/strandlijnen.nc';

year        = round(1970 + nc_varget(url_df,'time')/365);
yrs         = 1965;
start_year  = find(year == yrs)-1;
yrs2        = year(end);
n_year      = yrs2-yrs+1;

id_DF   = nc_varget(url_df,'id');
DF      = nc_varget(url_df,'DF',[0 start_year],[-1 n_year]);
DF_x    = nc_varget(url_df,'DF_x',[0 start_year],[-1 n_year]);
DF_y    = nc_varget(url_df,'DF_y',[0 start_year],[-1 n_year]);
DF_lat  = nc_varget(url_df,'DF_lon',[0 start_year],[-1 n_year]);
DF_lon  = nc_varget(url_df,'DF_lat',[0 start_year],[-1 n_year]);

[idID_STa, idID_STb] = ismember(id,id_DF);
idID_STb(idID_STb == 0) = [];               

DF_filt(:,:)     = DF(idID_STb,:);
%DF_filt_x(:,:)   = DF_x(idID_STb,:);
%DF_filt_y(:,:)   = DF_y(idID_STb,:);
DF_filt_lat(:,:) = DF_lat(idID_STb,:);
%DF_filt_lon(:,:) = DF_lon(idID_STb,:);

%% Calculate X_rsp positions for the strandlijnen

[~, n] = size(DF);

DF_cross = zeros(size(DF_filt));

latdum = lat(idID_STa,:);
dd=[];
for i = 1:n;
    for j = 1:length(idID_STb)
        if ~isnan(DF_filt_lat(j,i))
            dummy    = abs(latdum(j,:) - DF_filt_lat(j,i));
            dd=[dd; min(dummy)];
            id_cross = find(dummy == min(dummy));
            DF_cross(j,i) = xRSP(id_cross);
        else
            DF_cross(j,i) = NaN;
        end
    end
end
