function [Rvaksas,Heakon,success] = getVakken(indeling,selection,varargin)
%GETVAKKEN  get the compartments to be used.
%
%   For the given combination of selection and the information in the varargin 
%   parameter, all compartments to be used are select from the "indeling" sheet.
%   Both the Rvaksas and the Heakon numbers are returned to be able to
%   compose correct sheetnames.
%
%   Syntax:
%   [Rvaksas,Heakon] = getVakken(indeling,selection,varargin)
%
%   Input:
%   indeling  = the sheet with the combinations of compartments, macrocells
%               and tide information.
%   selection = what main selection of the classification is given
%               (macrocell, Rvaksas or Heakon)
%   varargin  = the information from this parameter depends from selection
%               selection='macrocell': trenchtype and set of macrocells
%               selection='Heakon' or 'Rvaksas': set of compartments
%
%   Output:
%   Rvaksas, Heakon = the compartment numbers for the selection and set
%                     combination
%   success         = the get is or is not successfull 
%
%   Example
%
%   See also 

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2010 <COMPANY>
%       Lou Verhage
%
%       <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: 07 Sep 2010
% Created with Matlab version: 7.8.0.347 (R2009a)

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

%% initialize
success = true;
Rvaksas = [];
Heakon  = [];
%% get the compartment numbers

records = ones(length(indeling)-2,1)*false;

if strcmpi(selection,'macrocel')
    if nargin < 2
        errordlg(sprintf('For selection "macrocel" two varargin parameters are needed.'));
        success = false;
    else
        trench = varargin{1};
        set    = varargin{2};
        for n = 1:length(set)
            for m = 3:length(indeling(:,1))
                a=indeling{m,4};
                if iscell(set(n))
                    s = cell2num(set(n));
                else
                    s = set(n);
                end
                if s == a
                    switch trench
                        case 'flood'
                            if indeling{m,5} == 'v' 
                                records(m-2) = true;
                            end
                        case 'ebb'
                            if indeling {m,5} == 'e' 
                                records(m-2) = true;
                            end
                        case 'trenches'
                            if indeling{m,5} == 'e' || indeling{m,5} == 'v'
                                records(m-2) = true;
                            end
                        case 'all'
                            records(m-2) = true;
                        otherwise
                            if isnan(indeling{m,5})
                                records(m-2) = true;
                            end
                    end
                    
                end
            end
        end
    end
else
    if strcmpi(selection,'heakon')
        index = 3;
    else
        index = 2;
    end
    set    = varargin{1};
    for n = 1:length(set)
        for m = 3:length(indeling)
            if set(n) == indeling{m,index}
                records(m-2) = true;
            end
        end
    end
end

teller = 0;
for n=1:length(records)
    if records(n)
        teller = teller + 1;
        Heakon(teller)  = indeling{n+2,3};
        Rvaksas(teller) = indeling{n+2,2};
    end
end
