workingdir = 'n:\Projects\1207500\1207724\B. Measurements and calculations\Toestand_van_de_kust\Bayesian_modelling\4Kees\runs\suppleties2\';

[x, z] = readBOT(fullfile(workingdir, 'iniProf.bot'));

nfsn = 200; % number of foreshore nourishments
ub_rangefsn = {-5, -3.65}; % range of upper boundaries foreshore nourishments
slopefsn = 1/60;
nbn = 200; % number of beach nourishments
ub_rangebn = {2, 4}; % range of upper boundaries beach nourishments
slopebn = 1/30;
upperboundaryies = [unif_inv(rand(1,nfsn), ub_rangefsn{:})...
    unif_inv(rand(1,nbn), ub_rangebn{:});];
vol_range_fsn = {50, 800};
vol_range_bn = {50, 400};
volumes = [unif_inv(rand(1,nfsn), vol_range_fsn{:}) unif_inv(rand(1,nbn), vol_range_bn{:})];

% create boolean vectors indicating either beach (bn) or foreshore (fsn)
% nourishment
[isbn, isfsn] = deal(false(size(volumes)));
isfsn(1:nfsn) = true;
isbn(nfsn+1:end) = true;

for idx = 1:length(volumes)
    ub = upperboundaryies(idx);
    vol = volumes(idx);
    slp = slopefsn;
    if isbn(idx)
        slp = slopebn;
    end
    [Volume(idx), z2(:,idx), xrange(idx,:), zrange(idx,:)] = nourishment_cross_shore(x, z, vol,...
        'upper_boundary', ub,...
        'slope', slp);
    
    if isbn(idx) && min(zrange(idx,:)) < max(ub_rangefsn{:})
        % if a beach nourishment reaches below the maximum upper boundary
        % of the foreshore nourishment range, it is indicated as a
        % foreshore nourishment as well (meaning: combined)
        isfsn(idx) = true;
    end
    
    if isbn(idx) && isfsn(idx)
        nour_type = 'combined';
    elseif isbn(idx)
        nour_type = 'beach';
    elseif isfsn(idx)
        nour_type = 'foreshore';
    end
    fh = figure;
    plot(x,z, x,z2(:,idx))
    title(sprintf('volume: %.1f (%.1f); vertical range: %.2f to %.2f; nourishment type: %s',...
        Volume(idx), vol, zrange(idx,:), nour_type))
    saveas(fh, fullfile(workingdir, sprintf('run%03i.emf', idx)))
    close(fh)
    
    writeBOT(x, z2(:,idx), fullfile(workingdir, sprintf('run%03i.bot', idx)))
end

nour_type_idx = ones(size(Volume')); % initialize all nourishments as beach (1)
nour_type_idx(isfsn) = 2; % foreshore
nour_type_idx(~xor(isbn, isfsn)) = 3; % combined


rundata = [Volume' nour_type_idx];
dlmwrite(fullfile(workingdir, 'inputspecs.csv'), rundata)