% Europlatform
nc1 = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/waterbase/sea_surface_wave_from_direction/id23-EURPFM.nc';
nc2 = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/waterbase/sea_surface_height/id54-EURPFM.nc';

% Noordwijk
% nc1 = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/waterbase/sea_surface_wave_from_direction/id23-IJMDMNTSPS.nc';
% nc2 = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/waterbase/sea_surface_height/id1-NOORDWMPT.nc';
% K13
% nc1 = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/waterbase/sea_surface_wave_from_direction/id23-K13APFM.nc';
% nc2 = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/waterbase/sea_surface_height/id54-K13APFM.nc';

t1 = nc_varget(nc1, 'time');
t2 = nc_varget(nc2, 'time');
d1 = nc_varget(nc1, 'sea_surface_wave_from_direction');
h2 = nc_varget(nc2, 'sea_surface_height'); 

[t ix1 ix2] = intersect(t1, t2);
d = d1(ix1);
d2 = d;
d2(d2<180) = d2(d2<180)+360;
h = h2(ix2);
%%
b = .25:.25:2.25;
bs =[];
x = 1:length(b);
for ix = x
    y = sort(d2(h>b(ix) & h<=b(ix)+.25));
    figure;
%     subplot(2,1,1);
    plot(y)
%     hold on
    dy = diff(y);
    dyid = dy>0;
    c = 1:length(dy);
    dyr = dy(dyid);
    cr = c(dyid);
    dcr = diff(cr);
    if ~isempty(dcr)
    s = dyr(2:end) ./ dcr / sum(dy)*length(dy);
%     plot([nan dy])
    maxdy = median(dy(dy>0))*4;
%     b1 = find(dy<maxdy, 1, 'first');
    b1 = cr(find(s<1, 1, 'first'));
    if isempty(b1)
        b1 = 1;
    end
%     b2 = find(dy<maxdy, 1, 'last');
    b2 = cr(find(s<1, 1, 'last'));
    if isempty(b2)
        b2 = length(y);
    end
    else
        b1 = 1;
        b2 = length(y);
    end
    hold on
    vline([b1,b2])
    fprintf('%5.2f\t%3i\t%3i\n', b(ix), y(b1), y(b2));
    bs = [bs; [y(b1), y(b2)]];
%     subplot(2,1,2);
%     plot(diff(y))
end

%%
ncfile = 'http://opendap.deltares.nl/thredds/dodsC/opendap/deltares/landboundaries/northsea.nc';
x = nc_varget(ncfile, 'x');
y = nc_varget(ncfile, 'y');


tr = jarkus_transects(...
    'id', 9011176,...
    'year', 2010,...
    'output', {'x' 'y' 'angle' 'rsp_x' 'rsp_y'});

a1 = tr.angle + 10;
a2 = tr.angle + 40;
figure;
plot(x,y, tr.x([1 end]), tr.y([1 end]),...
    tr.rsp_x, tr.rsp_y, 'o',...
    tr.rsp_x + [0 1.2e6*sind(a1)], tr.rsp_y + [0 1.2e6*cosd(a1)], ':k',...
    tr.rsp_x + [0 1.2e6*sind(a2)], tr.rsp_y + [0 1.2e6*cosd(a2)], ':k')
axis image
xlabel('x coordinate [m]')
ylabel('y coordinate [m]')

figureStyle(@styleThesis,...
    {'figure' 'Position' [1 1 15 10]});
[fpath fname] = fileparts(which(mfilename));
% fname = sprintf('%s_%s', fname, strrep(OPT.suffix, ' ', '_'));
figurepath = fpath;%fullfile(fpath, '..', 'figures');
set(gcf, 'tag', '$HeadURL$ $Revision$')
print(gcf, '-depsc2', fullfile(figurepath, [fname '.eps']))
if ispc
    saveas(gcf, fullfile(figurepath, [fname '.emf']))
end
saveas(gcf, fullfile(figurepath, [fname '.fig']))
