OPT = struct(...
    'id', 0,...
    'irun', 0,...
    'maindir', fullfile(fileparts(which(mfilename)), '..', 'simulations'),...
    'WL', 5,...
    'Hsig', 9,...
    'Tp', 12,...
    'D50', 225e-6,...
    'duration', 5*3600);
morfac = 5;
slopetoe = 100:25:200;

[x0 z0] = SimpleProfile('zmin', -4);
ztoe = min(floor([min(OPT.WL(:)) - max(OPT.Tp(:))/.55 -20])) * ones(2,1);
xtoe = max(x0) + diff([ztoe(1) min(z0)]) * slopetoe;
xtoe = [xtoe; max(xtoe)+repmat(100,size(xtoe))];

% combine all x resp. z for plotting purpose
xall = [repmat(x0,1,5); xtoe];
zall= repmat([z0; ztoe],1,5);
ph = plot(xall,zall);
set(gca,...
    'XLim', minmax(xall(:)'),...
    'YLim', minmax(zall(:)'))
set(ph, {'DisplayName'}, strread(sprintf('1:%i ', slopetoe), '%s'))
legend show
uistack(ph(3),'top'); % put reference case on top
% replace z in upper part of profile by NaN, to prevent multiple slope
% texts on top of each other
zall(1:end-3,1:end) = NaN;
addSlopeText(reshape(xall,numel(xall),1), reshape(zall,numel(zall),1),...
    'HorizontalAlignment', 'left')
xlabel('cross-shore coordinate [m]')
ylabel('altitude [m]')

%%
figureStyle(@styleThesis,...
    {'figure', 'Units', 'Points', 'Position', [200 200 350 250]});
tightenfigure
[fpath fname] = fileparts(which(mfilename));
figurepath = 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']))

%%
xb_setpref('grid_finalise', {'zmin', min(ztoe)})

for islope = fliplr(1:length(slopetoe))
    [x z] = deal([x0; xtoe(:,islope)], [z0; ztoe]);
    [x ix] = sort(-x);
    z = z(ix);
    if islope == length(slopetoe)
    xb(islope) = xb_generate_model(...
        'bathy', {'x', x, 'z', z, 'xgrid', {'dxmin', 2}, 'finalise', {'seaward_extend'}, 'superfast', true},...
        'tide', {'front' OPT.WL},...
        'waves', {...
        'type', 'jonswap',...
        'Hm0', OPT.Hsig,...
        'Tp', OPT.Tp,...
        'duration', 1800*morfac},...
        'wavegrid', {'nbins' 1},...
        'settings', {...
        'morfac', morfac,...
        'posdwn', -1,...
        'tintg', 100,...
        'tstop', ceil(OPT.duration/morfac)*morfac,...
        'D50', OPT.D50,...
        'D90', OPT.D50*1.5,...
        'random' 0,...
        'outputformat', 'netcdf',...
        'meanvars', {'H' 'zs'},...
        });
    % remove swtable from structure
    xb(islope) = xb_del(xb(islope), 'swtable');
    
    
    xori = xb_get(xb(islope), 'xori');
    xfile = xb_get(xb(islope), 'xfile.xfile');
    depfile = xb_get(xb(islope), 'depfile.depfile');
    else
        xb(islope) = xb(end);
        depfilea = interp1(x-xori, z, xfile);
        depfilea(isnan(depfilea)) = min(depfilea);
        
        xb(islope) = xb_set(xb(islope), 'depfile.depfile', depfilea);
    end

    basepath = fullfile(OPT.maindir, sprintf('%08i', OPT.id(1)), 'xbeach', sprintf('run%02i', OPT.irun));
    casename = sprintf('slope%03i', slopetoe(islope));
    calcdir = fullfile(basepath, casename);
    mkpath(calcdir)
    xb_write_input(calcdir, xb(islope));
end