function varargout = decc_plot_profile(varargin)
%DECC_PLOT_PROFILE  One line description goes here.
%
%   More detailed description goes here.
%
%   Syntax:
%   varargout = decc_plot_profile(varargin)
%
%   Input:
%   varargin  =
%
%   Output:
%   varargout =
%
%   Example
%   decc_plot_profile
%
%   See also 

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2012 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: 08 Feb 2012
% Created with Matlab version: 7.7.0.471 (R2008b)

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

%%
OPT = struct(...
    'maindir', fullfile(fileparts(which(mfilename)), '..', 'simulations'),...
    'regexp_select', 'run(26).*?xbsum\.nc',...
    'regexp_legend', 'run\d{2}.*?radius\d{4,5}(.*?rev\d{4}\D?)?',...
    'strrep_legend', {{{'\' '; '} {'s0' 's '} {'00' '00 m'}}},...
    'save', false ...
    );

OPT = setproperty(OPT, varargin);

%%
[dirs dircont files] = dirlisting(OPT.maindir);

fid = ~cellfun(@isempty, (cellfun(@(x) regexp(x, OPT.regexp_select), files, 'uniformoutput', false)));

ncfiles = files(fid);

for iradii = 1:length(ncfiles)
    ncfile = ncfiles{iradii};
    
    result(iradii) = decc_analyse(ncfile,...
    't0', false,...
    'zb', true,...
    's', true,...
    'Ero', false,...
    'Sed', false,...
    'Bal', false,...
    'Svsg_cs', false,...
    'dSvsg_dn', false,...
    'ccg_cs', false,...
    'zs_mean', false,...
    'u_mean', false,...
    'v_mean', false);
end

legtxt = cellfun(@(x) regexp(x, OPT.regexp_legend, 'match'), ncfiles);
for ilegrep = 1:length(OPT.strrep_legend)
    legtxt = cellfun(@(x) strrep(x, OPT.strrep_legend{ilegrep}{:}), legtxt,...
        'UniformOutput', false);
end

%%
plot_engine({result.phi}, {result.zb}, 'Bed elevation', 'm w.r.t. NAP',...
    'figure', figure,...
    'axes', subplot(3,1,1),...
    'axis', 'tight',...
    'xticklabel', false,...
    'title', 'Bathymetry',...
    'legend', legtxt)


%%
function plot_engine(x, y, varname, varunit, varargin)

OPT = struct(...
    'legend', false,...
    'figure', true,...
    'axes', true,...
    'axis', false,...
    'plotfunction', @plot,...
    'title', true,...
    'save', false,...
    'xlim', true,...
    'xtick', true,...
    'xticklabel', true,...
    'xlabel', false,...
    'ytick', true,...
    'ylabel', sprintf('%s [%s]', varname, varunit),...
    'suffix', strrep(varname, ' ', '_'),...
    'colors', 'rgbkmc');

OPT = setproperty(OPT, varargin);

%%
if islogical(OPT.figure) && OPT.figure
    fh = figure;
elseif ishandle(OPT.figure)
    fh = OPT.figure;
else
    fh = gcf;
end

if islogical(OPT.axes) && OPT.axes
    ah = axes('parent', fh);
elseif ishandle(OPT.axes)
    ah = OPT.axes;
else
    ah = gca;
end

for i = 1:length(x)
    ph(i) = feval(OPT.plotfunction, x{i}, y{i}, OPT.colors(i),...
        'parent', ah);
    
    if ischar(OPT.ylabel)
        ylabel(OPT.ylabel)
    end
    
    if ischar(OPT.xlabel)
        xlabel(OPT.xlabel)
    end
    
    if i == 1 && length(x) > 1
        hold on
    end
end

if isnumeric(OPT.xlim)
    set(ah, 'xlim', OPT.xlim)
elseif islogical(OPT.xlim) && ~OPT.xlim
    set(ah, 'xlim', [])
end

if isnumeric(OPT.xtick)
    set(ah, 'xtick', OPT.xtick)
elseif islogical(OPT.xtick) && ~OPT.xtick
    set(ah, 'xtick', [])
end

if isnumeric(OPT.xticklabel)
    set(ah, 'xticklabel', OPT.xticklabel)
elseif islogical(OPT.xticklabel) && ~OPT.xticklabel
    set(ah, 'xticklabel', [])
end

if isnumeric(OPT.ytick)
    set(ah, 'ytick', OPT.ytick)
elseif islogical(OPT.ytick) && ~OPT.ytick
    set(ah, 'ytick', [])
end

if islogical(OPT.legend) && OPT.legend
    legend show
elseif ~islogical(OPT.legend)
    legend(OPT.legend)
end

if ischar(OPT.title)
    title(OPT.title)
end

if OPT.axis
    axis(OPT.axis);
end

if OPT.save
    figureStyle(@stylePresentation,...
        {'figure', 'Units', 'Points'});
    [fpath fname] = fileparts(which(mfilename));
    fname = sprintf('%s_%s', fname, strrep(OPT.suffix, ' ', '_'));
    figurepath = fullfile(fpath, '..', 'figures');
    set(gcf, 'tag', '$HeadURL$ $Revision$')
    print(gcf, '-depsc2', fullfile(figurepath, [fname '.eps']))
    if ispc
        saveas(gcf, fullfile(figurepath, [fname '.emf']))
    end
    saveas(gcf, fullfile(figurepath, [fname '.fig']))
end