function h = tb_V1Bottom(dims,zb,dirs)
%tb_V1Bottom: Plot the mean bottom elevation zb at t = 90 min.
%
%   Plot the mean bottom elevation zb at t = 90 min. The mean is
%   calculated from the bottom elevation at the profile measurements in the
%   comparison stretches.
%
%   Syntax:
%   h = tb_V1Bottom(dims,zb)
%
%   Input:      XBeach structure with dimensions & bottom data
%   Output:     Figure of the mean bottom elevation zb (h)
%
%   Example
%   h = tb_V1Bottom(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);


%% ===Load statistics of measurements======================================
%---Load wheelprofiler measurements----------------------------------------
load(fullfile(dirs.data,'V1WHEELPROFILER_BOTTOM_STATISTICS.mat'));


%% ===Calculate statistics of XBeach output================================
%---Interpolate XBeach output to grid with constant size-------------------
x=17.5:0.02:22.93;
for i=1:size(zb,2);
    Intx_XBz(i,:) = interp1(dims.x(1,:),squeeze(zb(end,i,:)),x);
end

%---Interpolate XBeach output to measurement y-locations-------------------
yin = [2.53 4.03 4.78 5.03 5.28 19.53 19.78 20.03 20.78 22.28];
for j=1:length(yin)
    for i=1:272
        Inty_XBz(j,i) = interp1(dims.y(:,1),Intx_XBz(:,i),yin(j));
    end
end
xb.D1.zb = Inty_XBz(1:5,:);
xb.D2.zb = Inty_XBz(6:10,:);
xb.D1.zb(:,end-1:end)=[];
xb.D2.zb(:,end-1:end)=[];

%---Calculate statistics of XBeach output----------------------------------
for i=1:size(xb.D1.zb,2)
    xb.mean(i)=mean([xb.D1.zb(:,i); xb.D2.zb(:,i)]);
end


%% ===Find Still Water Level intersection==================================
xcr = findCrossings(x,db.min,[min(x) max(x)],[0.45 0.45]);


%% ===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]); hold(gca,'on');

    plot(x,db.mean,'Color','k');
    plot(x,db.max,'Color',[0.6 0.6 0.6],'LineStyle','--');
    plot(x,db.min,'Color',[0.6 0.6 0.6],'LineStyle','--');
        
    plot(x(1:end-2),xb.mean,'r');
    
    plot(x,db.init,'-.k');
    plot(dims.x(1,:),squeeze(zb(1,1,:)),'-.r');
    
    plot([min(x) xcr],[0.45 0.45],'--b');
            
    ylabel('$\overline{\textrm{z}_{\textrm{b}}}$ [m]','Interpreter','latex'); xlabel('x [m]');
    xlim([17.5 22.93]); ylim([0.25 0.65]); box on;    
    set(haxes,'XTick',[17.5 18 19 20 21 22 22.93],...
        'XTickLabel',[17.5 18 19 20 21 22 22.93],'YTick',[0.3 0.4 0.5 0.6]);
    
xb_write_plot(gcf,dirs.output,'V1MeanBottom')