function ph = desb_plot_corr_all(varargin)
%DESB_PLOT_CORR_ALL  One line description goes here.
%
%   More detailed description goes here.
%
%   Syntax:
%   varargout = desb_plot_corr_all(varargin)
%
%   Input:
%   varargin  =
%
%   Output:
%   varargout =
%
%   Example
%   desb_plot_corr_all
%
%   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 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: 23 Sep 2011
% Created with Matlab version: 7.7.0.471 (R2008b)

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

%%
OPT = struct(...
    'irun', 0,...
    'model', 'xbeach',...
    'zone', 3,...
    'measure', 'erosion_volume');

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

%%
ncfile = fullfile(fileparts(which(mfilename)), '..', 'data', 'jarkus_volumes.nc');
volume = nc_varget(ncfile, 'volume_sediment');
volume = volume(:,:,OPT.zone);
csb = nc_varget(ncfile, 'cross_shore_boundary');
zonewidth = diff(csb,1,2);
zonewidth = zonewidth(:,OPT.zone);
id = nc_varget(ncfile, 'id');

zonenames = {'landward zone' 'erosion zone' 'sedimentation zone' 'seaward zone'};
zonenames = zonenames(OPT.zone);
for iz = 1:length(zonenames)
    zonenames{iz} = sprintf('%s (%i)', zonenames{iz}, OPT.zone(iz));
end

[R L U] = deal(NaN(size(zonewidth)));

for i = 1:length(id)
    for j = 1:size(zonewidth,2)
        ncfile = fullfile(fileparts(which(mfilename)), '..', 'simulations',...
            sprintf('%08i', id(i)), OPT.model, sprintf('run%02i', OPT.irun), sprintf('%08i_pp.nc', id(i)));
        measure(:,i,j) = nc_varget(ncfile, OPT.measure);
        [r,p,rlo,rup] = corrcoef(volume(:,i,j), measure(:,i,j));
%         if p(2) < .05
            [R(i,j) L(i,j) U(i,j)] = deal(r(2), r(2)-rlo(2),rup(2)-r(2));
%         else
%             [R(i,j) L(i,j) U(i,j)] = deal(r(2), -1, 1);
%         end
    end
end
ph = errorbar(repmat((1:length(id))',1,size(zonewidth,2)), R, L, U, 'o');

if isscalar(zonenames)
    title(zonenames{:})
else
    legend(zonenames{:});
end

set(gca, 'XTick', 1:length(id),...
    'XLim', [1 length(id)] + [-1 1],...
    'YLim', [-1 1],...
    'XTickLabel', '')

xticklabels = cellfun(@strtrim, cellstr(num2str(id)),...
    'UniformOutput', false);
for i = 1:length(id)
    text(i, min(get(gca, 'YLim'))-.1, xticklabels{i},...
        'HorizontalAlignment', 'right',...
        'VerticalAlignment', 'middle',...
        'Rotation', 60,...
        'tag', 'XTickLabel');
end