function [z result] = x2z_duros(varargin)
%X2Z_DUROS  limit state function for dune erosion calculation with duros+
%
%   More detailed description goes here.
%
%   Syntax:
%   varargout = x2z_duros(varargin)
%
%   Input:
%   varargin  =
%
%   Output:
%   varargout =
%
%   Example
%   x2z_duros
%
%   See also 

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2011 Delft University of Technology
%       Kees den Heijer
%
%       C.denHeijer@TUDelft.nl	
%
%       Faculty of Civil Engineering and Geosciences
%       P.O. Box 5048
%       2600 GA Delft
%       The Netherlands
%
%   This library is free software: you can redistribute it and/or
%   modify it under the terms of the GNU Lesser General Public
%   License as published by the Free Software Foundation, either
%   version 2.1 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
%   Lesser General Public License for more details.
%
%   You should have received a copy of the GNU Lesser General Public
%   License along with this library. If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tool is part of <a href="http://OpenEarth.nl">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: 30 Mar 2011
% Created with Matlab version: 7.7.0.471 (R2008b)

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

%%
OPT = struct(...
    'WL_t', [5],...
    'Hsig_t', [9],...
    'Tp_t', [12],...
    'D50', [225e-6],...
    'Accuracy', [0],...
    'Duration', [0],...
    'transectid', [7003700],...
    'xInitial', [-250 -24.375 5.625 55.725 230.625 2780.63]',...
    'zInitial', [15 15 3 0 -3 -20]',...
    'xRef', 5,...
    'database', '',...
    'table', '');

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

writeerrors = ~isempty(OPT.database);

global zVOL
global zERO

%% cross-shore profile
xInitial = OPT.xInitial;
zInitial = OPT.zInitial;

%% samples
samples.WL_t = OPT.WL_t;
samples.Hsig_t = OPT.Hsig_t;
samples.Tp_t = OPT.Tp_t;
samples.D50 = OPT.D50;
samples.Accuracy = OPT.Accuracy;
samples.Duration = OPT.Duration;

%% DUROS settings
AdditionalErosion = any([samples.Accuracy(:); samples.Duration(:)] ~= 0);
AdditionalErosion = 0;

DuneErosionSettings('set',...
    'AdditionalErosion', AdditionalErosion,... 
    'BoundaryProfile', false,...
    'DuneBreachCalculation', false);

%% DUROS calculation
z = ones(size(samples.D50,1),1); % pre-allocate z
zVOL = ones(size(samples.D50,1),1); % pre-allocate zVOL
zERO = ones(size(samples.D50,1),1); % pre-allocate zERO

for i = 1:size(samples.D50,1)
    if samples.WL_t(i) < max(zInitial)
        if AdditionalErosion
            DuneErosionSettings('set',...
                'AdditionalVolume', [num2str(sum([samples.Duration(i) samples.Accuracy(i)])) '*Volume']); % string voor het bepalen van het toeslagvolume gedurende de berekening (afslagvolume is negatief)
        end
        %% carry out DUROS+ computation
        [result messages] = DUROS(...
            'xInitial', xInitial,...
            'zInitial', zInitial,...
            'D50', samples.D50(i),...
            'WL_t', samples.WL_t(i),...
            'Hsig_t', samples.Hsig_t(i),...
            'Tp_t', samples.Tp_t(i));
        if ~isscalar(result)
            samples.Tp_t(i) = result(1).info.input.Tp_t;
        end
                     
        if writeerrors
            mksqlite('open', OPT.database);
            mksqlite('begin');
            nmessage = size(messages,1);
            for imessage = 1:nmessage
                mksqlite(['insert into ' OPT.table ' (profile, sample, errorcode, message) values (' num2str(OPT.transectid) ', ' num2str(i) ', ' num2str(messages{imessage,1}) ', ''' messages{imessage,2} ''')']);
            end
            mksqlite('commit');
            mksqlite('close', OPT.database);
        end
        
        %% derive z-value
        if ~isscalar(result)
            if AdditionalErosion
                z(i,:) = OPT.xRef - result(3).xActive(1);
            else
                z(i,:) = OPT.xRef - result(1).xActive(1);
                zVOL(i,:) = result(1,1).Volumes.Volume ;
                zERO(i,:) = result(1,1).Volumes.Erosion ;
            end
        end
   end
end