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

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2014 Deltares
%       Kees den Heijer
%
%       Kees.denHeijer@deltares.nl
%
%       Deltares
%       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: 10 Mar 2014
% Created with Matlab version: 8.1.0.604 (R2013a)

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

%%
OPT = struct(...
    'D50',                  [],     ...
    'Ph',                   [],     ...
    'PHs',                  [],     ...
    'PTp',                  [],     ...
    'volumefluct',          0,      ...
    'fluctlevel',           0,      ...
    'StormDurationFactor',  0,      ...
    'ModelAccuracyFactor',  0,      ...
    'x',                    [],     ...
    'z',                    [],     ...
    'lambda',               0,      ...
    'station1',             '',     ...
    'station2',             '',     ...
    'duneerosion_model',    'duros',...
    'MinD50',               76e-6,  ...
    'critical_rsp',         0,      ...
    'sigmaHs',              .6,     ...
    'sigmaTp',              1,      ...
    'RDrsp',                []);

% return defaults (aka introspection)
if nargin==0;
    varargout = {OPT};
    return
end
% overwrite defaults with user arguments
OPT = setproperty(OPT, varargin);

if ~isempty(OPT.RDrsp)
    z = OPT.RDrsp - OPT.critical_rsp;
    return
end

%% code
% Note: Ph needs to be a probability, PHs and PTp need to be realisations
% of the variation in wave height and period respectively
[h, h1, h2, Station1, Station2, lambda]     = getWl_2Stations(OPT.Ph, OPT.lambda, OPT.station1, OPT.station2);
[Hs, Hs1, Hs2, Station1, Station2]          = getHs_2Stations(OPT.PHs, lambda, h1, h2, Station1, Station2);
Tp                                          = getTp_2Stations(OPT.PTp, lambda, Hs1, Hs2, Station1, Station2);

OPT.x = OPT.x(:);
OPT.z = OPT.z(:);

z = NaN(length(OPT.D50), length(OPT.critical_rsp));
if strcmpi('duros', OPT.duneerosion_model)
    for ii = 1:length(OPT.D50)
        if OPT.D50(ii) >= OPT.MinD50
            D50 = OPT.D50(ii);
        else
            D50 = OPT.MinD50;
            warning(['D50 (' num2str(OPT.D50(ii)) ') below minimum value! Changed to ' num2str(OPT.MinD50)])
        end
        
        if OPT.volumefluct == 0
            xProfile = OPT.x;
            zProfile = OPT.z;
        else
            [xProfile, zProfile] = DuneProfileVariation(...
                'xInitial', OPT.x,...
                'zInitial', OPT.z,...
                'VariationVolume', OPT.volumefluct(ii),...
                'VerticalIntersectionLevel', -5+ OPT.fluctlevel(ii));
        end
        
        DuneErosionSettings('set',...
            'AdditionalErosion', true,...
            'AdditionalVolume', [num2str(OPT.StormDurationFactor(ii) + OPT.ModelAccuracyFactor(ii)) '*Volume'], ...
            'BoundaryProfile', false);
        
        result = DUROS(xProfile, zProfile,...
            'D50', D50,...
            'WL_t', h(ii),...
            'Tp_t', Tp(ii),...
            'Hsig_t', Hs(ii));
        if numel(result) == 1 && ~isempty(result(1).VTVinfo.Xr)
            z(ii,:) = result(1).VTVinfo.Xr - OPT.critical_rsp;
        elseif numel(result) >= 3 && ~isempty(result(1).VTVinfo.Xr)
            if OPT.StormDurationFactor(ii) == 0 && OPT.ModelAccuracyFactor(ii) == 0
                z(ii,:) = result(1).VTVinfo.Xr - OPT.critical_rsp;
            elseif abs(OPT.StormDurationFactor(ii)) > 0 && abs(OPT.ModelAccuracyFactor(ii)) > 0 && ~isempty(result(3).VTVinfo.Xr)
                z(ii,:) = result(3).VTVinfo.Xr - OPT.critical_rsp;
            end
        elseif h(ii) > max(zProfile)
            z(ii,:) = min(xProfile) - OPT.critical_rsp;    
        else
            error('No R-point found!')
        end
    end
else
    error('specified duneerosion_model "%s" not found', OPT.duneerosion_model)
end