function [z_prof2, x2, locs, loc_land] = version1(yy, testing_profile, x1, z_mean_high, height_of_peaks)
%% Area of interest
% It finds the peaks at the profile, which will be considered as dune peaks.
% Then it calculates the position of the seaward and landward boundary based
% on the peaks and the mean high water level respectively.       


%   --------------------------------------------------------------------
% input:
%
% yy                    =   year of measurement
% testing_profile       =   altitude values 
% x1                    =   cross shore values
% z_mean_high           =   vertical value for mean high water level 
% height_of_peaks       =   height of peaks which will be considered as
%                           dune peaks
%
% output:
%
% z_prof2               =   vertical values for the area of interest 
% x2                    =   cross shore values for the area of interest
% locs                  =   location of the peaks
% loc_land              =   location of the landward boundary


%   --------------------------------------------------------------------
% $Id: $
% $Date: $
% $Author: $
% $Revision: $
% $HeadURL: $
% $Keywords: $
%%

%Find peaks

[pks,locs,~,~]  =   findpeaks(testing_profile, 'MinPeakProminence', height_of_peaks, 'Annotate', 'extents'); 
pks             =   pks (pks >= height_of_peaks + z_mean_high);
locs            =   locs(pks >= height_of_peaks + z_mean_high);

%Landward boundary 

if ~isempty(pks)
    x_1         =   x1(locs(end) : end);
    z_prof      =   testing_profile(locs(end) : end);
else
    x_1         =   x1;
    z_prof      =   testing_profile; 
end

if max(z_prof) > 6  
    loc_land    =   find   (z_prof > 6.0, 1, 'last');
    z_prof      =   z_prof (loc_land:end );
    x_1         =   x_1    (loc_land:end );
else
    loc_land    =   0;
end

if isempty(z_prof)
    z_prof2     =   [];
    x2          =   [];
    return
end

% Seaward boundary -- Crossing mean high water

diff_high       =   abs(z_prof - z_mean_high);
mat_high        =   diff_high == (min(diff_high) * ones(size(diff_high)));
if sum(mat_high(:)) == 0
    z_prof2     =   [];
    x2          =   [];
    return
else
    id_high(yy) =   find(mat_high(:), 1, 'last'); 
end


lower_boundary  =   id_high(yy); 
x2              =   x_1   (1 : lower_boundary);
z_prof2         =   z_prof(1 : lower_boundary);


if length(z_prof2) < 5 % exclude invalid transects
    z_prof2     =   [];
    x2          =   [];
    return
end


    