function h = tb_V4DeltaRMean(dims,zb,dirs)
%tb_V4DeltaRMean: Plot the development of the mean DeltaR* in time.
%
%   Plot the development of the mean DeltaR* in time. The mean is
%   calculated from the DeltaR* in the comparison stretches.
%
%   Syntax:
%   h = tb_V4DeltaRMean(dims,zb)
%
%   Input:      XBeach structure with dimensions & bottom data
%   Output:     Figure of time development of mean DeltaR* (h)
%
%   Example
%   h = tb_V4DeltaRMean(dims,xb.data(1,2).value)
%
%   See also

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2011 <COMPANY>
%       Bram de Vries
%
%       <EMAIL>	
%
%       <ADDRESS>
%
%   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: 13 May 2011
% Created with Matlab version: 7.9.1.705 (R2009b) Service Pack 1

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


%% ===Settings=============================================================
OPT = struct(...
    'savedir',nan,...
    'width',13.8,...
    'height',6.5);

Name = {'D11'};
Time = [15 30 90];


%% ===Load statistics of measurements======================================
load(fullfile(dirs.data,'V4ARGUS_DELTAR.mat'));
Stats_Argus = load(fullfile(dirs.data,'V4ARGUS_DELTAR_STATISTICS.mat'));
load(fullfile(dirs.data,'V4WHEELPROFILER_DELTAR_STATISTICS.mat'));

%---Merge statistics into vectors------------------------------------------
VTime = [DBDR.Time.T015' DBDR.Time.T030' DBDR.Time.T090'];
Db_Mean = [Stats_Argus.db(1,1).mean Stats_Argus.db(1,2).mean Stats_Argus.db(1,3).mean];
Db_Std = [Stats_Argus.db(1,1).std Stats_Argus.db(1,2).std Stats_Argus.db(1,3).std];


%% ===Calculate statistics of XBeach output================================
%---Create structure with XBeach data of dune stretches D11----------------
xb.D11.Ind = [find(dims.y(:,1)==15.28) find(dims.y(:,1)==25.42)];
xb.D11.Y = dims.y(xb.D11.Ind(1):xb.D11.Ind(2),1);
xb.D11.zb = zb(:,xb.D11.Ind(1):xb.D11.Ind(2),:);

%---Calculate DeltaR* for dune stretches D11-------------------------------
for k=1:length(Name)
    xb.(Name{k}).DeltaR = tb_calc_DeltaR(dims,xb.(Name{k}).zb);
end

%---Equalize begin & end y-coordinates to comparison stretches-------------
%Dune stretch D11
ind = find(xb.D11.Y >= DBDR.Y.Dune(end), 1 );
xb.D11.Y(ind+1:end) = [];
xb.D11.DeltaR(:,ind+1:end)=[];

for t=1:size(zb,1)
    xb.D11.DeltaR(t,1) = interp1(xb.D11.Y,xb.D11.DeltaR(t,:),DBDR.Y.Dune(1));
    xb.D11.DeltaR(t,end) = interp1(xb.D11.Y,xb.D11.DeltaR(t,:),DBDR.Y.Dune(end));
end
xb.D11.Y(1) = DBDR.Y.Dune(1);
xb.D11.Y(end) = DBDR.Y.Dune(end);

%---Calculate mean & standard deviation of DeltaR*-------------------------
for t=1:size(zb,1)
    xb.mean(t) = mean(xb.D11.DeltaR(t,:));
end


%% ===Plot statistics======================================================
h = figure; set(h,'units','centimeters'); set(h,'position',[0 0 OPT.width OPT.height]);  
haxes = axes('Parent',h); set(haxes,'units','centimeters'); 
set(haxes,'position',[1.5 1 11.8 5]);

    plot(VTime,Db_Mean+Db_Std,'--b','LineWidth',0.5); hold on;
    plot(VTime,Db_Mean-Db_Std,'--b','LineWidth',0.5); hold on;
    plot(VTime,Db_Mean,'b','LineWidth',0.5); hold on;
    
    plot(Time,wp.mean,'k','Marker','o','MarkerSize',3,'LineStyle','none',...
        'MarkerFaceColor','k');
    errorbar(Time,wp.mean,wp.std,'k','LineStyle','none'); hold on;
            
    plot((dims.t-dims.t(1))/60,xb.mean,'r','LineWidth',0.5);

    xlim([0 93]); ylim([-0.005 0.45]); xlabel('Time [min]');
    ylabel('$\overline{\Delta \textrm{R}^{*}}$ [m]','Interpreter','latex');
    
xb_write_plot(gcf,dirs.output,'V4MeanDeltaR')    