function plotRt(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',     [],             ...
    'Years',        [],             ...
    'ReturnFreqs',  [],             ...
    'LGW',          [],             ...
    'plotBKL',      true,           ...
    'plotMKL',      true,           ...
    'plotTRDA2006', false,          ...
    'TRDAmarker',   'c^',     ...
    'FigName',      'R-t_diagram',  ...
    'MarkerSize',   50,             ...
    'PrintDir',     '',             ...
    'PrintEN',      false,          ...
    'Print',        false);

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

%% Initializing
Years       = OPT.Years;
ReturnFreqs = OPT.ReturnFreqs;
R           = NaN(numel(Years),numel(ReturnFreqs));
Converged   = false(numel(Years),numel(ReturnFreqs));
MKL         = NaN(numel(Years),1);
FreqColors  = ['c';'g';'m'];

%% Plotting
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};
                Converged(iYear,iFreq)  = abs(DuneErosionResult{iYear,iFreq,iCountDown}.Output.P_f-ReturnFreqs(iFreq))<0.1*ReturnFreqs(iFreq);
                break
            end
        end
        
        if isnan(MKL(iYear)) && OPT.plotMKL
            transect    = jarkus_transects('id',OPT.JarkusID,'year',OPT.Years(iYear));
            x           = transect.cross_shore(~isnan(squeeze(transect.altitude)))';
            z           = squeeze(transect.altitude(~isnan(transect.altitude)));
            MKL(iYear)  = jarkus_getMKL(x, z, 3, -3);
            clear x z transect
        end
    end
    
    % Plot R point for every return frequency
    if OPT.PrintEN
        RPointName = ['R point 1/' num2str(1/ReturnFreqs(iFreq))];
    else
        RPointName = ['R punt 1/' num2str(1/ReturnFreqs(iFreq))];
    end
    scatter(hAx,Years(Converged(:,iFreq)),R(Converged(:,iFreq),iFreq),OPT.MarkerSize,FreqColors(1+mod(iFreq,numel(FreqColors))),'Filled', ...
        'MarkerEdgeColor','k','DisplayName',RPointName)
    if sum(~Converged(:,iFreq))>0
        scatter(hAx,Years(~Converged(:,iFreq)),R(~Converged(:,iFreq),iFreq),OPT.MarkerSize,['h' FreqColors(1+mod(iFreq,numel(FreqColors)))],'Filled', ...
            'MarkerEdgeColor','k','DisplayName',[RPointName ' not converged'])
    end
end

% Include MKL point per year
if OPT.plotMKL
    scatter(hAx,Years,MKL,OPT.MarkerSize,'rs','Filled','MarkerEdgeColor','k','DisplayName','MKL')
end

xlim(hAx,[floor(min(get(hAx,'XLim'))-1) ceil(max(get(hAx,'XLim'))+1)])

% Plot critical retreat line
if ~isempty(OPT.LGW)
    if OPT.PrintEN
        LGWName = 'Landward boundary flood defense';
    else
        LGWName = 'Landwaartse grens waterkering';
    end
    line(get(hAx,'XLim'),[OPT.LGW OPT.LGW],'Color','b','LineStyle','--','LineWidth',2,'DisplayName',LGWName)
end

% Include BKL line
if OPT.plotBKL
    BKL = jarkus_getBKL(OPT.JarkusID);
    line(get(hAx,'XLim'),[BKL BKL],'Color','b','LineStyle','-','LineWidth',2,'DisplayName','BKL')
end

% Include TRDA2006 results
if OPT.plotTRDA2006
    [R06, y06] = calc_Rt_trda2006('id',OPT.JarkusID,'AdditionalErosion',true);
    if OPT.PrintEN
        LGWName = 'TRDA 2006 results';
    else
        LGWName = 'TRDA 2006 resultaten';
    end
    scatter(y06, R06, OPT.TRDAmarker, 'filled', 'MarkerEdgeColor', 'k','DisplayName',LGWName)
end

hold off

if OPT.PrintEN
    xlabel('Year')
    ylabel('Horizontal distance w.r.t. RSP [m]')
    title(['R-t diagram for transect ' num2str(OPT.JarkusID)])
else
    xlabel('Jaar')
    ylabel('Horizontale afstand t.o.v. RSP [m]')
    title(['R-t diagram voor raai ' num2str(OPT.JarkusID)])
end

legend(hAx,'show','location','West')

if OPT.Print
    print('-dpng','-r600',fullfile(OPT.PrintDir,OPT.FigName))
    print('-dpdf',fullfile(OPT.PrintDir,OPT.FigName))
end