function plotRtProfiles(DuneErosionResult, varargin)
%PLOTRT  One line description goes here.
%
%   More detailed description goes here.
%
%   Syntax:
%   varargout = plotRt(varargin)
%
%   Input: For <keyword,value> pairs call plotRt() without arguments.
%   varargin  =
%
%   Output:
%   varargout =
%
%   Example
%   plotRt
%
%   See also

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2014 Deltares
%       Joost den Bieman
%
%       joost.denbieman@deltares.nl
%
%       P.O. Box 177
%       2600 MH Delft
%       The Netherlands
%
%   This library is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as published by
%   the Free Software Foundation, either version 3 of the License, or
%   (at your option) any later version.
%
%   This library is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public License
%   along with this library.  If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tool is part of <a href="http://www.OpenEarth.eu">OpenEarthTools</a>.
% OpenEarthTools is an online collaboration to share and manage data and
% programming tools in an open source, version controlled environment.
% Sign up to recieve regular updates of this function, and to contribute
% your own tools.

%% Version <http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html>
% Created: 19 Mar 2014
% Created with Matlab version: 8.2.0.701 (R2013b)

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

%% Settings
OPT = struct(...
    'JarkusID',     [],             ...
    'JarkusURL',    jarkus_url,     ...
    'station1',     [],             ...
    'station2',     [],             ...
    'Years',        [],             ...
    'ReturnFreqs',  [],             ...
    'LGW',          [],             ...
    'plotBKL',      true,           ...
    'plotMKL',      true,           ...
    'FigName',      'RpointProfile',  ...
    'MarkerSize',   50,             ...
    'PrintDir',     '',             ...
    'Print',        false);

OPT = setproperty(OPT, varargin{:});

%% Initializing
Years       = OPT.Years;
ReturnFreqs = OPT.ReturnFreqs;
R           = NaN(numel(Years),numel(ReturnFreqs));
DPU         = NaN(numel(Years),numel(ReturnFreqs),numel(DuneErosionResult{1,1,3}.Input));
DPX         = NaN(numel(Years),numel(ReturnFreqs),numel(DuneErosionResult{1,1,3}.Input));
MKL         = NaN(numel(Years),1);
FreqColors  = ['c';'g';'m'];

%% Plotting
DuneErosionSettings('set',...
    'AdditionalErosion', false,...
    'BoundaryProfile', false);

hFig    = figure;
hAx     = gca;
hold on

for iFreq = 1:numel(ReturnFreqs)
    for iYear = 1:numel(Years)
        for iCountDown = numel(DuneErosionResult(iYear,iFreq,:)):-1:1
            if ~isempty(DuneErosionResult{iYear,iFreq,iCountDown})
                R(iYear,iFreq)      = DuneErosionResult{iYear,iFreq,iCountDown}.settings.x2zVariables{12};
                DPU(iYear,iFreq,:)  = DuneErosionResult{iYear,iFreq,iCountDown}.Output.designpoint.finalU;
                DPX(iYear,iFreq,1:4) = [DuneErosionResult{iYear,iFreq,iCountDown}.Output.designpoint.D50, ...
                    DuneErosionResult{iYear,iFreq,iCountDown}.Output.designpoint.Ph, DuneErosionResult{iYear,iFreq,iCountDown}.Output.designpoint.PHs, ...
                    DuneErosionResult{iYear,iFreq,iCountDown}.Output.designpoint.PTp];
                DPX(iYear,iFreq,7:8)    = [DuneErosionResult{iYear,iFreq,iCountDown}.Output.designpoint.StormDurationFactor, DuneErosionResult{iYear,iFreq,iCountDown}.Output.designpoint.ModelAccuracyFactor];
                if numel(DuneErosionResult{1,1,3}.Input) == 8
                    DPX(iYear,iFreq,5:6)    = [DuneErosionResult{iYear,iFreq,iCountDown}.Output.designpoint.volumefluct, DuneErosionResult{iYear,iFreq,iCountDown}.Output.designpoint.fluctlevel];
                end
                break
            end
        end
        
        transect    = jarkus_transects('id',OPT.JarkusID,'year',OPT.Years(iYear),'url',OPT.JarkusURL);
        x           = transect.cross_shore(~isnan(squeeze(transect.altitude)))';
        z           = squeeze(transect.altitude(~isnan(transect.altitude)));
        if numel(DuneErosionResult{1,1,3}.Input) == 8
            [xProfile, zProfile] = DuneProfileVariation(...
                'xInitial', x,...
                'zInitial', z,...
                'VariationVolume', DPX(iYear,iFreq,5),...
                'VerticalIntersectionLevel', -5+ DPX(iYear,iFreq,6));
        else 
            xProfile    = x;
            zProfile    = z;
        end
        
        lambda                                      = getLambda_2Stations('Station1', OPT.station1, 'Station2', OPT.station2,'JarkusId', OPT.JarkusID);
        [h, h1, h2, Station1, Station2, lambda]     = getWl_2Stations(DPX(iYear,iFreq,2), lambda, OPT.station1, OPT.station2);
        [Hs, Hs1, Hs2, Station1, Station2]          = getHs_2Stations(DPX(iYear,iFreq,3), lambda, h1, h2, Station1, Station2);
        Tp                                          = getTp_2Stations(DPX(iYear,iFreq,4), lambda, Hs1, Hs2, Station1, Station2);
            
        DuneErosionSettings('set',...
            'AdditionalErosion', true,...
            'AdditionalVolume', [num2str(DPX(iYear,iFreq,7) + DPX(iYear,iFreq,8)) '*Volume'], ...
            'BoundaryProfile', false);
        
        ErosionResult   = DUROS(xProfile, zProfile,...
                'D50',      DPX(iYear,iFreq,1), ...
                'WL_t',     h, ...
                'Hsig_t',   Hs, ...
                'Tp_t',     Tp);
        if ~isempty(ErosionResult(1).z2Active)
            plotDuneErosion(ErosionResult,'xdir','normal')
        end
        
        if numel(DuneErosionResult{1,1,3}.Input) == 8
            hold on
            plot(x,z,'k--','DisplayName','Jarkus profile')
            hold off
        end
        
        xlim([min(xProfile) max(xProfile)])
        ylim([min(zProfile) max(zProfile)+5])
        title([num2str(Years(iYear)) ' freq ' num2str(ReturnFreqs(iFreq))])
        if OPT.Print
            print('-dpdf',fullfile(OPT.PrintDir,[OPT.FigName '_' num2str(Years(iYear)) '_' num2str(ReturnFreqs(iFreq))  '.pdf']))
            print('-dpng','-r600',fullfile(OPT.PrintDir,[OPT.FigName '_' num2str(Years(iYear)) '_' num2str(ReturnFreqs(iFreq))  '.png']))
        end
        close(hFig)
    end
end