function varargout = dewa_plot_concentration_time(varargin)
%DEWA_PLOT_CONCENTRATION_TIME  One line description goes here.
%
%   More detailed description goes here.
%
%   Syntax:
%   varargout = dewa_plot_concentration_time(varargin)
%
%   Input:
%   varargin  =
%
%   Output:
%   varargout =
%
%   Example
%   dewa_plot_concentration_time
%
%   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: 03 Sep 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', 'run(09).*xb_ccg.nc',...
    'regexp_legend', 'run\d{2}.*?angle\d{3}(.*?rev\d{4}\D?)?',...
    'strrep_legend', {{{'\' '; '} {'s0' 's '} {'00' '00 m'}}},...
    'offset', 0,...
    'n_contours', 5,...
    'save', false,...
    'style', @styleThesis ...
    );

OPT = setproperty(OPT, varargin);

%%
[~, ~, files] = dirlisting(OPT.maindir);

ncfiles = files(~cellfun(@isempty, regexp(files, OPT.regexp_select)));

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
%%
if isscalar(ncfiles)
    fprintf('%s\n', ncfiles{:});
else
    fparts = cellfun(@(x) textscan(x, '%s', 'delimiter', repmat(filesep, 1, ispc+1)), ncfiles, 'UniformOutput', false);
    fparts = [fparts{:}];
    for i = 1:min(cellfun(@length, fparts))-1
        if ~isscalar(unique(cellfun(@(x) x{i}, fparts, 'UniformOutput', false)))
            fprintf('%s\n', fullfile(fparts{1}{1:i-1}));
            for j = 1:length(ncfiles)
                fprintf('\t%s\n', fullfile(' ', fparts{j}{i:end}));
            end
            break
        end
    end
end

%%
[x y t] = cellfun(@(ncfile) deal(nc_varget(ncfile, 'globalx'), nc_varget(ncfile, 'globaly'), nc_varget(ncfile, 'meantime')), ncfiles, 'uniformoutput', false); 
g = cellfun(@xb_stagger, x, y);
ymax = cellfun(@(y) max(y(:)), y);

ccg = cellfun(@(ncfile) nc_varget(ncfile, 'ccg_mean') * 2650, ncfiles, 'uniformoutput', false);
id = cellfun(@(y) round(interp1(y(:,1)/y(end,1), 1:size(y,1), [.3 .7])), y, 'uniformoutput', false);
ccg = cellfun(@(ccg, id) squeeze(mean(ccg(:,id(1):id(2),:), 2)), ccg, id, 'uniformoutput', false);
% ccgdiff = cellfun(@(ccg0, ccg1) ccg0-ccg1, ccg(1:end-1), ccg(2:end), 'uniformoutput', false);

X = cellfun(@(x,t) x(1:length(t),:), x, t, 'uniformoutput', false);
T = cellfun(@(x,t) repmat(t,[1 size(x,2)]), x, t, 'uniformoutput', false);

ymax = max(ymax);
max_ccg = cellfun(@(ccg) max(ccg(:)), ccg);
max_ccg = 30;
min_ccg = cellfun(@(ccg) min(ccg(:)), ccg);

fh = figure;
nangles = length(ncfiles);
nrows = min(nangles,2);
ncols = ceil(nangles/nrows);
for iangle = 1:nangles
    subplot(nrows,ncols,iangle)
    pcolor(X{iangle},T{iangle},ccg{iangle})
%     set(gca, 'ylim', [0 max(ymax)])
    title(legtxt{iangle})
    shading interp
    c = colorbar;
    caxis([min(min_ccg) max(max_ccg)])
end
linkaxes(findobj(fh, 'type', 'axes', '-and', '-not', 'tag', 'legend', '-and', '-not', 'tag', 'Colorbar'), 'x')
