function varargout = desb_xbeach_artificial_jarkus_plot_profiles(varargin)
%DESB_XBEACH_ARTIFICIAL_JARKUS_PLOT_PROFILES  One line description goes here.
%
%   More detailed description goes here.
%
%   Syntax:
%   varargout = desb_xbeach_artificial_jarkus_plot_profiles(varargin)
%
%   Input:
%   varargin  =
%
%   Output:
%   varargout =
%
%   Example
%   desb_xbeach_artificial_jarkus_plot_profiles
%
%   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: 02 Apr 2012
% Created with Matlab version: 7.13.0.564 (R2011b)

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

%%
OPT = struct(...
    'maindir', fullfile(fileparts(which(mfilename)), '..', 'simulations'),...
    'regexp_select', '(?<id1>\d{8})_(?<id2>\d{8}).*?run02.*?xb(output)\.nc',...
    'save', false ...
    );

OPT = setproperty(OPT, varargin);

%%
[~, ~, files] = dirlisting(OPT.maindir);
% select relevant files
fid = ~cellfun(@isempty, regexp(files, OPT.regexp_select));
ncfiles = files(fid);

% filter id1 and id2
id = cell2mat(regexp(ncfiles, OPT.regexp_select, 'names'));
[id1 id2] = deal(str2double({id.id1}), str2double({id.id2}));
refids = id1 == id2;

% read data
xb = cellfun(@(ncfile) xb_read_output(ncfile, 'vars', {'globalx' 'zb' 'zb' 'H' 'zs'}), ncfiles, 'UniformOutput', false);
xb = cellfun(@(x) xs_join(x, xb_get_hydro(x)), xb, 'UniformOutput', false);
[x zb H_hf H_lf H_tot] = cellfun(@(x) xs_get(x, 'DIMS.x', 'zb', 'Hrms_hf', 'Hrms_lf', 'Hrms_t'), xb, 'UniformOutput', false);
[zb0 zb1] = cellfun(@(x) deal(squeeze(x(1,:,:)), squeeze(x(end,:,:))), zb, 'UniformOutput', false);

data0 = [x; zb0];
data1 = [x; zb1];
dataH_hf = [x; H_hf];
dataH_lf = [x; H_lf];
% dataH_tot = [x; H_tot];

ylim = fix(minmax(cell2mat(cellfun(@(x) minmax(x(:)'), zb, 'UniformOutput', false)))) + [-1 1];

% plot
figure
ph = plot(data0{:,refids});
set(gca, 'xdir', 'reverse')
legtxt = cellfun(@num2str, num2cell(id1(refids)), 'UniformOutput', false);
legend(legtxt{:})
xlabel('Cross-shore coordinate [m]')
ylabel('Altitude [m]')
axis tight
set(gca, 'ylim', ylim, 'box', 'off')

% reverse color ordering in order get similar colors to the R figures
set(ph, {'color'}, flipud(get(ph, 'color')))
set(gca, 'xticklabels', abs(get(gca, 'xtick')))

figureStyle(@styleThesis,...
    {'Figure' 'Position' [1 1 15 8]});
tightenfigure

%%
if OPT.save
    fpath = fileparts(which(mfilename));
    fname = mfilename;
    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

%%
parts = {'Landward part' 'Seaward part'};
for p = 1:length(parts)
    idx = str2double({id.(sprintf('id%i',p))});
    idxc = str2double({id.(sprintf('id%i',find(~ismember(1:length(parts), p))))});
    for i = unique(idx)
        figure
        % apply all style features first, to prevent rerendereing and
        % messing up the legend
        set(gca, 'xdir', 'reverse')
        titletxt = sprintf('%s %i', parts{p}, i);
        title(titletxt)
        xlabel('Cross-shore coordinate [m]')
        ylabel('Altitude resp. H_{rms} [m]')
        axis tight
        set(gca, 'ylim', ylim, 'xlim',  minmax(x{1}'))
        
        figureStyle(@styleThesis,...
            {'Figure' 'Position' [1 1 15 8]});
        tightenfigure
        set(gcf, 'defaultlinelinewidth', 2)
        hold on
        
        ph0 = plot(data0{:,i == idx});
        legtxt = cellfun(@num2str, num2cell(id1(refids)), 'UniformOutput', false);
        legend(legtxt{:})
        uistack(ph0(idxc(i == idx) == i), 'top')
        % reverse color ordering in order get similar colors to the R figures
        set(ph0, {'color'}, flipud(get(ph0, 'color')))
        ph1 = plot(dataH_hf{:,i == idx});
        set(ph1, {'displayname'}, cellfun(@(p) [p ' Hrms_hf'], legtxt', 'uniformoutput', false))
        uistack(ph1(idxc(i == idx) == i), 'top') % put the line of the 'other' part on top
        set(ph1, 'linestyle', ':', 'handlevisibility', 'off')
        % reverse color ordering in order get similar colors to the R figures
        set(ph1, {'color'}, flipud(get(ph1, 'color')))
        ph2 = plot(dataH_lf{:,i == idx});
        set(ph2, {'displayname'}, cellfun(@(p) [p ' Hrms_lf'], legtxt', 'uniformoutput', false))
        uistack(ph2(idxc(i == idx) == i), 'top')
        set(ph2, 'linestyle', '-.', 'handlevisibility', 'off')
        % reverse color ordering in order get similar colors to the R figures
        set(ph2, {'color'}, flipud(get(ph2, 'color')))
        Hminmax = minmax([dataH_lf{end,i == idx} dataH_hf{end,i == idx}]);
        xall = mean([x{:}],2);
        xm = mean(minmax(xall'));
        ym = interp1(xall, Hminmax, xm);
        text(xm, ym(1), 'low frequency H_{rms}', 'VerticalAlignment', 'top', 'HorizontalAlignment', 'center')
        text(xm, ym(2), 'high frequency H_{rms}', 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center')

        if OPT.save
            fpath = fileparts(which(mfilename));
            fname = [mfilename '_Hrms_' strrep(titletxt, ' ', '_')];
            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
        
    end
end